Skip to content

Commit

Permalink
Merge pull request #9 from rwjblue/update-launch-editor
Browse files Browse the repository at this point in the history
Use execa.command instead of this.exect for `launchEditor`.
  • Loading branch information
rwjblue committed Mar 10, 2020
2 parents 4e220f7 + 00ec2d6 commit 4c2a0fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs');
const { Plugin } = require('release-it');
const { format } = require('release-it/lib/util');
const tmp = require('tmp');
const execa = require('execa');

module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
get lernaPath() {
Expand Down Expand Up @@ -76,13 +77,15 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
throw error;
}

// `${file}` is actually interpolated by `this.exec`
// `${file}` is interpolated just below
editorCommand = EDITOR + ' ${file}';
} else {
editorCommand = this.options.launchEditor;
}

await this.exec(editorCommand, { context: { file: tmpFile } });
editorCommand = editorCommand.replace('${file}', tmpFile);

await execa.command(editorCommand, { stdio: 'inherit' });
}

async reviewChangelog(changelog) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -25,6 +25,7 @@
"serial": true
},
"dependencies": {
"execa": "^4.0.0",
"lerna-changelog": "^1.0.1",
"release-it": "^13.0.2",
"tmp": "^0.1.0"
Expand Down
36 changes: 24 additions & 12 deletions test.js
Expand Up @@ -17,6 +17,22 @@ function resetEDITOR() {
}
}

async function buildEditorCommand() {
let editor = tmp.fileSync().name;
let output = tmp.fileSync().name;

let fakeCommand = `#!${process.execPath}
'use strict';
const fs = require('fs');
fs.writeFileSync('${output}', \`args: \${process.argv.slice(2).join(' ')}\`, 'utf-8');
`;

await fs.writeFileSync(editor, fakeCommand, { encoding: 'utf-8' });

return { editor: `${process.execPath} ${editor}`, output };
}

class TestPlugin extends Plugin {
constructor() {
super(...arguments);
Expand Down Expand Up @@ -105,31 +121,27 @@ test('prepends the changelog to the existing file', async t => {
test('uses launchEditor command', async t => {
let infile = tmp.fileSync().name;

let plugin = buildPlugin({ infile, launchEditor: 'foo-editor -w ${file}' });
let { editor, output } = await buildEditorCommand();

let plugin = buildPlugin({ infile, launchEditor: `${editor} \${file}` });

await runTasks(plugin);

t.deepEqual(plugin.commands, [
[`git show-ref --tags --quiet --verify -- "refs/tags/1.0.0"`, { write: false }],
[`${plugin.lernaPath} --next-version=1.0.1 --from=1.0.0`, { write: false }],
[`foo-editor -w ${plugin.launchedTmpFile}`, {}],
]);
t.is(fs.readFileSync(output, 'utf-8'), `args: ${plugin.launchedTmpFile}`);
});

test('detects default editor if launchEditor is `true`', async t => {
let infile = tmp.fileSync().name;

let { editor, output } = await buildEditorCommand();

let plugin = buildPlugin({ infile, launchEditor: true });

try {
process.env.EDITOR = 'foo-editor -w';
process.env.EDITOR = editor;
await runTasks(plugin);

t.deepEqual(plugin.commands, [
[`git show-ref --tags --quiet --verify -- "refs/tags/1.0.0"`, { write: false }],
[`${plugin.lernaPath} --next-version=1.0.1 --from=1.0.0`, { write: false }],
[`foo-editor -w ${plugin.launchedTmpFile}`, {}],
]);
t.is(fs.readFileSync(output, 'utf-8'), `args: ${plugin.launchedTmpFile}`);
} finally {
resetEDITOR();
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -1281,7 +1281,7 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

execa@4.0.0:
execa@4.0.0, execa@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-4.0.0.tgz#7f37d6ec17f09e6b8fc53288611695b6d12b9daf"
integrity sha512-JbDUxwV3BoT5ZVXQrSVbAiaXhXUkIwvbhPIwZ0N13kX+5yCzOhUNdocxB/UQRuYOHRYYwAxKYwJYc0T4D12pDA==
Expand Down

0 comments on commit 4c2a0fd

Please sign in to comment.