Skip to content

Commit

Permalink
refactor(bin): updated the binary file to esm
Browse files Browse the repository at this point in the history
for #2543
  • Loading branch information
travi committed Oct 8, 2022
1 parent 1e424a0 commit 0a14f4f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 46 deletions.
28 changes: 15 additions & 13 deletions 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.`
);
Expand All @@ -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);
}
Expand All @@ -36,7 +38,7 @@ execa('git', ['--version'])
});

// Node 10+ from this point on
require('../cli')()
cli()
.then((exitCode) => {
process.exitCode = exitCode;
})
Expand Down
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
);
Expand Down
4 changes: 3 additions & 1 deletion 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';
Expand All @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Expand Up @@ -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
Expand Down
61 changes: 33 additions & 28 deletions 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"}] */

Expand All @@ -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()]);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)}`));
Expand Down

0 comments on commit 0a14f4f

Please sign in to comment.