diff --git a/@commitlint/cli/src/cli.js b/@commitlint/cli/src/cli.js index a82724f2b3..a0b31a47b5 100755 --- a/@commitlint/cli/src/cli.js +++ b/@commitlint/cli/src/cli.js @@ -224,20 +224,28 @@ function getEditValue(flags) { if (typeof edit === 'boolean') { return edit; } - // The recommended method to specify -e with husky was `commitlint -e $GIT_PARAMS` + // The recommended method to specify -e with husky was `commitlint -e $HUSKY_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 // This has been superceded by the `-E GIT_PARAMS` / `-E HUSKY_GIT_PARAMS` - if (edit === '$GIT_PARAMS' || edit === '%GIT_PARAMS%') { + const isGitParams = edit === '$GIT_PARAMS' || edit === '%GIT_PARAMS%'; + const isHuskyParams = + edit === '$HUSKY_GIT_PARAMS' || edit === '%HUSKY_GIT_PARAMS%'; + + if (isGitParams || isHuskyParams) { console.warn(`Using environment variable syntax (${edit}) in -e |\ ---edit is deprecated. Use '{-E|--env} GIT_PARAMS instead'`); - if (!('GIT_PARAMS' in process.env)) { - throw new Error( - `Received ${edit} as value for -e | --edit, but GIT_PARAMS is not available globally.` - ); +--edit is deprecated. Use '{-E|--env} HUSKY_GIT_PARAMS instead'`); + + if (isGitParams && 'GIT_PARAMS' in process.env) { + return process.env.GIT_PARAMS; + } + if ('HUSKY_GIT_PARAMS' in process.env) { + return process.env.HUSKY_GIT_PARAMS; } - return process.env.GIT_PARAMS; + throw new Error( + `Received ${edit} as value for -e | --edit, but GIT_PARAMS or HUSKY_GIT_PARAMS are not available globally.` + ); } return edit; } diff --git a/@commitlint/cli/src/cli.test.js b/@commitlint/cli/src/cli.test.js index 4a5bd64ec2..2462ca56c0 100644 --- a/@commitlint/cli/src/cli.test.js +++ b/@commitlint/cli/src/cli.test.js @@ -127,6 +127,30 @@ test('should work with husky via commitlint -e %GIT_PARAMS%', async () => { await execa('git', ['commit', '-m', '"test: this should work"'], {cwd}); }); +test('should work with husky via commitlint -e $HUSKY_GIT_PARAMS', async () => { + const cwd = await git.bootstrap('fixtures/husky/integration'); + await writePkg( + {scripts: {commitmsg: `'${bin}' -e $HUSKY_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 %HUSKY_GIT_PARAMS%', async () => { + const cwd = await git.bootstrap('fixtures/husky/integration'); + await writePkg( + {scripts: {commitmsg: `'${bin}' -e %HUSKY_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 allow reading of environment variables for edit file, succeeding if valid', async t => { const cwd = await git.bootstrap(); await sander.writeFile(cwd, 'commit-msg-file', 'foo');