Skip to content

Commit 2ec981e

Browse files
authored
Merge pull request #91 from rwjblue/avoid-using-launch-editor-if-not-present
2 parents 8c366c6 + 5f138a8 commit 2ec981e

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

__tests__/bin-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const execa = require('execa');
66

77
const BIN_PATH = require.resolve('../bin/rwjblue-release-it-setup');
88
const ROOT = process.cwd();
9+
const EDITOR = 'EDITOR' in process.env ? process.env.EDITOR : null;
10+
const PATH = process.env.PATH;
911

1012
function exec(args) {
1113
return execa(process.execPath, ['--unhandled-rejections=strict', BIN_PATH, ...args]);
@@ -18,10 +20,22 @@ describe('main binary', function () {
1820
project = new Project('some-thing-cool', '0.1.0');
1921
project.writeSync();
2022
process.chdir(path.join(project.root, project.name));
23+
24+
// ensure an EDITOR is present
25+
process.env.EDITOR = '/bin/whatever';
2126
});
2227

2328
afterEach(function () {
2429
process.chdir(ROOT);
30+
31+
// reset process.env.EDITOR to initial state
32+
if (EDITOR === null) {
33+
delete process.env.EDITOR;
34+
} else {
35+
process.env.EDITOR = EDITOR;
36+
}
37+
38+
process.env.PATH = PATH;
2539
});
2640

2741
it('adds CHANGELOG.md file', async function () {
@@ -178,6 +192,20 @@ describe('main binary', function () {
178192
`);
179193
});
180194

195+
it('does not add launchEditor if no $EDITOR is found', async function () {
196+
delete process.env.EDITOR;
197+
198+
// have to reset $PATH in order to ensure `launchEditor` is false on Ubuntu systems
199+
// since they always have a `editor` command
200+
process.env.PATH = '';
201+
202+
await exec(['--no-install', '--no-label-updates']);
203+
204+
let pkg = JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' }));
205+
206+
expect(pkg['release-it'].plugins['release-it-lerna-changelog'].launchEditor).toBeFalsy();
207+
});
208+
181209
it('adds release-it configuration for monorepos to package.json', async function () {
182210
project.pkg.workspaces = ['packages/*'];
183211
project.writeSync();

bin/rwjblue-release-it-setup.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ const util = require('util');
66
const execa = require('execa');
77
const sortPackageJson = require('sort-package-json');
88
const getRepoInfoFromURL = require('hosted-git-info').fromUrl;
9+
const gitconfig = util.promisify(require('gitconfiglocal'));
910
const semver = require('semver');
11+
const which = require('which');
12+
1013
const skipInstall = process.argv.includes('--no-install');
1114
const skipLabels = process.argv.includes('--no-label-updates');
1215
const labelsOnly = process.argv.includes('--labels-only');
1316
const update = process.argv.includes('--update');
14-
const gitconfig = util.promisify(require('gitconfiglocal'));
1517

1618
const RELEASE_IT_VERSION = (() => {
1719
let pkg = require('../package');
@@ -33,6 +35,16 @@ const RELEASE_IT_YARN_WORKSPACES_VERSION = (() => {
3335

3436
const DETECT_TRAILING_WHITESPACE = /\s+$/;
3537

38+
function hasEditor() {
39+
let EDITOR = process.env.EDITOR;
40+
41+
if (!EDITOR) {
42+
EDITOR = which.sync('editor', { nothrow: true });
43+
}
44+
45+
return !!EDITOR;
46+
}
47+
3648
function getDependencyRange(theirs, ours) {
3749
if (theirs) {
3850
let ourRange = new semver.Range(ours);
@@ -113,7 +125,7 @@ async function updatePackageJSON() {
113125
releaseItConfig.plugins['release-it-lerna-changelog'] = Object.assign(
114126
{
115127
infile: 'CHANGELOG.md',
116-
launchEditor: true,
128+
launchEditor: hasEditor(),
117129
},
118130
releaseItConfig.plugins['release-it-lerna-changelog']
119131
);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"github-label-sync": "^1.6.0",
2626
"hosted-git-info": "^3.0.4",
2727
"semver": "^7.3.2",
28-
"sort-package-json": "^1.42.2"
28+
"sort-package-json": "^1.42.2",
29+
"which": "^2.0.2"
2930
},
3031
"devDependencies": {
3132
"eslint": "^7.0.0",

0 commit comments

Comments
 (0)