diff --git a/@commitlint/cli/src/cli.js b/@commitlint/cli/src/cli.js index 56c164b155..e9908aa6f2 100755 --- a/@commitlint/cli/src/cli.js +++ b/@commitlint/cli/src/cli.js @@ -139,11 +139,29 @@ function checkFromHistory(flags) { function normalizeFlags(flags) { // The `edit` flag is either a boolean or a string but we are only allowed // to specify one of them in minimist - if (flags.edit === '') { - return merge({}, flags, {edit: true, e: true}); - } + const edit = flags.edit === '' ? true : normalizeEdit(flags.edit); + return merge({}, flags, {edit, e: edit}); +} - return flags; +function normalizeEdit(edit) { + if (typeof edit === 'boolean') { + return edit; + } + // The recommended method to specify -e with husky is commitlint -e $GIT_PARAMS + // This does not work properly with win32 systems, where env variable declarations + // use a different syntax + // See https://github.com/marionebl/commitlint/issues/103 for details + if (edit === '$GIT_PARAMS' || edit === '%GIT_PARAMS%') { + if (!('GIT_PARAMS' in process.env)) { + throw new Error( + `Received ${ + edit + } as value for -e | --edit, but GIT_PARAMS is not available globally.` + ); + } + return process.env.GIT_PARAMS; + } + return edit; } function getSeed(seed) { diff --git a/@commitlint/cli/src/cli.test.js b/@commitlint/cli/src/cli.test.js index c65d18324f..09f5ec324b 100644 --- a/@commitlint/cli/src/cli.test.js +++ b/@commitlint/cli/src/cli.test.js @@ -101,6 +101,24 @@ test('should work with husky commitmsg hook in sub packages', async () => { await execa('git', ['commit', '-m', '"test: this should work"'], {cwd}); }); +test('should work with husky via commitlint -e $GIT_PARAMS', async () => { + const cwd = await git.bootstrap('fixtures/husky/integration'); + await writePkg({scripts: {commitmsg: `${bin} -e $GIT_PARAMS`}}, {cwd}); + + await execa('npm', ['install'], {cwd}); + await execa('git', ['add', 'package.json'], {cwd}); + await execa('git', ['commit', '-m', '"test: this should work"'], {cwd}); +}); + +test('should work with husky via commitlint -e %GIT_PARAMS%', async () => { + const cwd = await git.bootstrap('fixtures/husky/integration'); + await writePkg({scripts: {commitmsg: `${bin} -e %GIT_PARAMS%`}}, {cwd}); + + await execa('npm', ['install'], {cwd}); + await execa('git', ['add', 'package.json'], {cwd}); + await execa('git', ['commit', '-m', '"test: this should work"'], {cwd}); +}); + test('should pick up parser preset and fail accordingly', async t => { const cwd = await git.bootstrap('fixtures/parser-preset'); const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(