From 0a14f4fd5d4aa632f7e69cd9f3173640ab9829ee Mon Sep 17 00:00:00 2001 From: Matt Travi Date: Fri, 7 Oct 2022 23:11:14 -0500 Subject: [PATCH] refactor(bin): updated the binary file to esm for #2543 --- bin/semantic-release.js | 28 +++++++++-------- index.js | 6 ++-- test/helpers/npm-registry.js | 4 ++- test/index.test.js | 2 +- test/integration.test.js | 61 +++++++++++++++++++----------------- 5 files changed, 55 insertions(+), 46 deletions(-) diff --git a/bin/semantic-release.js b/bin/semantic-release.js index c7d928d2bb..e9da11ca4c 100755 --- a/bin/semantic-release.js +++ b/bin/semantic-release.js @@ -1,20 +1,22 @@ #!/usr/bin/env node -// Bad news: We have to write plain ES5 in this file -// Good news: It's the only file of the entire project - /* eslint-disable no-var */ -var semver = require('semver'); -var execa = require('execa'); -var findVersions = require('find-versions'); -var pkg = require('../package.json'); +import semver from 'semver'; +import { execa } from 'execa'; +import findVersions from 'find-versions'; +import cli from '../cli.js'; +import {createRequire} from 'node:module'; + +const require = createRequire(import.meta.url); +const { engines } = require('../package.json'); +const { satisfies, lt } = semver; -var MIN_GIT_VERSION = '2.7.1'; +const MIN_GIT_VERSION = '2.7.1'; -if (!semver.satisfies(process.version, pkg.engines.node)) { +if (!satisfies(process.version, engines.node)) { console.error( - `[semantic-release]: node version ${pkg.engines.node} is required. Found ${process.version}. + `[semantic-release]: node version ${engines.node} is required. Found ${process.version}. See https://github.com/semantic-release/semantic-release/blob/master/docs/support/node-version.md for more details and solutions.` ); @@ -23,8 +25,8 @@ See https://github.com/semantic-release/semantic-release/blob/master/docs/suppor execa('git', ['--version']) .then(({stdout}) => { - var gitVersion = findVersions(stdout)[0]; - if (semver.lt(gitVersion, MIN_GIT_VERSION)) { + const gitVersion = findVersions(stdout)[0]; + if (lt(gitVersion, MIN_GIT_VERSION)) { console.error(`[semantic-release]: Git version ${MIN_GIT_VERSION} is required. Found ${gitVersion}.`); process.exit(1); } @@ -36,7 +38,7 @@ execa('git', ['--version']) }); // Node 10+ from this point on -require('../cli')() +cli() .then((exitCode) => { process.exitCode = exitCode; }) diff --git a/index.js b/index.js index d37b7fd0d6..e6718d640c 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ import {createRequire} from 'node:module'; import {pick} from 'lodash-es'; import * as marked from 'marked'; import envCi from 'env-ci'; -import hookStd from 'hook-std'; +import {hookStdout} from 'hook-std'; import semver from 'semver'; import AggregateError from 'aggregate-error'; import hideSensitive from './lib/hide-sensitive.js'; @@ -44,7 +44,7 @@ async function run(context, plugins) { logger.warn('This run was not triggered in a known CI environment, running in dry-run mode.'); options.dryRun = true; } else { - // When running on CI, set the commits author and commiter info and prevent the `git` CLI to prompt for username/password. See #703. + // When running on CI, set the commits author and committer info and prevent the `git` CLI to prompt for username/password. See #703. Object.assign(env, { GIT_AUTHOR_NAME: COMMIT_NAME, GIT_AUTHOR_EMAIL: COMMIT_EMAIL, @@ -251,7 +251,7 @@ async function callFail(context, plugins, err) { } export default async (cliOptions = {}, {cwd = process.cwd(), env = process.env, stdout, stderr} = {}) => { - const {unhook} = hookStd( + const {unhook} = hookStdout( {silent: false, streams: [process.stdout, process.stderr, stdout, stderr].filter(Boolean)}, hideSensitive(env) ); diff --git a/test/helpers/npm-registry.js b/test/helpers/npm-registry.js index 459779cdd3..fff502a25c 100644 --- a/test/helpers/npm-registry.js +++ b/test/helpers/npm-registry.js @@ -1,4 +1,5 @@ -import path from 'node:path'; +import path, {dirname} from 'node:path'; +import {fileURLToPath} from 'node:url'; import Docker from 'dockerode'; import getStream from 'get-stream'; import got from 'got'; @@ -12,6 +13,7 @@ const NPM_USERNAME = 'integration'; const NPM_PASSWORD = 'suchsecure'; const NPM_EMAIL = 'integration@test.com'; const docker = new Docker(); +const __dirname = dirname(fileURLToPath(import.meta.url)); let container; /** diff --git a/test/index.test.js b/test/index.test.js index 6edacb3ffd..a7b1e5acb0 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -22,7 +22,7 @@ import { mergeFf, rebase } from './helpers/git-utils.js'; -import pluginNoop from './fixtures/plugin-noop.js'; +import pluginNoop from './fixtures/plugin-noop.cjs'; test.beforeEach((t) => { // Stub the logger functions diff --git a/test/integration.test.js b/test/integration.test.js index 2adc5fe78a..d7f06f83df 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -1,28 +1,31 @@ -const path = require('path'); -const test = require('ava'); -const td = require('testdouble'); -const {escapeRegExp} = require('lodash'); -const {writeJson, readJson} = require('fs-extra'); -const {execa} = require('execa'); -const {WritableStreamBuffer} = require('stream-buffers'); -const delay = require('delay'); -const getAuthUrl = require('../lib/get-git-auth-url'); -const {SECRET_REPLACEMENT} = require('../lib/definitions/constants'); -const { - gitHead, - gitTagHead, - gitRepo, - gitCommits, - gitRemoteTagHead, - gitPush, +import path from 'path'; +import test from 'ava'; +import * as td from 'testdouble'; +import {escapeRegExp} from 'lodash-es'; +import fsExtra from 'fs-extra'; +import {execa} from 'execa'; +import {WritableStreamBuffer} from 'stream-buffers'; +import delay from 'delay'; + +import getAuthUrl from '../lib/get-git-auth-url.js'; +import {SECRET_REPLACEMENT} from '../lib/definitions/constants.js'; +import { gitCheckout, - merge, + gitCommits, gitGetNote, -} = require('./helpers/git-utils'); -const {npmView} = require('./helpers/npm-utils'); -const gitbox = require('./helpers/gitbox'); -const mockServer = require('./helpers/mockserver'); -const npmRegistry = require('./helpers/npm-registry'); + gitHead, + gitPush, + gitRemoteTagHead, + gitRepo, + gitTagHead, + merge +} from './helpers/git-utils.js'; +import {npmView} from './helpers/npm-utils.js'; +import * as gitbox from './helpers/gitbox.js'; +import * as mockServer from './helpers/mockserver.js'; +import * as npmRegistry from './helpers/npm-registry.js'; + +const {readJson, writeJson} = fsExtra; /* eslint camelcase: ["error", {properties: "never"}] */ @@ -47,10 +50,10 @@ const npmTestEnv = { LEGACY_TOKEN: Buffer.from(`${env.NPM_USERNAME}:${env.NPM_PASSWORD}`, 'utf8').toString('base64'), }; -const cli = require.resolve('../bin/semantic-release'); -const pluginError = require.resolve('./fixtures/plugin-error'); -const pluginInheritedError = require.resolve('./fixtures/plugin-error-inherited'); -const pluginLogEnv = require.resolve('./fixtures/plugin-log-env'); +const cli = path.resolve('./bin/semantic-release.js'); +const pluginError = path.resolve('./test/fixtures/plugin-error'); +const pluginInheritedError = path.resolve('./test/fixtures/plugin-error-inherited'); +const pluginLogEnv = path.resolve('./test/fixtures/plugin-log-env'); test.before(async () => { await Promise.all([gitbox.start(), npmRegistry.start(), mockServer.start()]); @@ -509,7 +512,7 @@ test('Pass options via CLI arguments', async (t) => { test('Run via JS API', async (t) => { td.replace('../lib/logger', {log: () => {}, error: () => {}, stdout: () => {}}); td.replace('env-ci', () => ({isCi: true, branch: 'master', isPr: false})); - const semanticRelease = require('..'); + const semanticRelease = (await import('../index.js')).default; const packageName = 'test-js-api'; const owner = 'git'; // Create a git repository, set the current working directory at the root of the repo @@ -656,6 +659,8 @@ test('Hide sensitive environment variable values from the logs', async (t) => { extendEnv: false, }); + console.log({stderr}) + t.regex(stdout, new RegExp(`Console: Exposing token ${escapeRegExp(SECRET_REPLACEMENT)}`)); t.regex(stdout, new RegExp(`Log: Exposing token ${escapeRegExp(SECRET_REPLACEMENT)}`)); t.regex(stderr, new RegExp(`Error: Console token ${escapeRegExp(SECRET_REPLACEMENT)}`));