From d231b4a7e608c5e35b72169083aeda27c1bd687f Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Tue, 27 Nov 2018 23:01:37 +0100 Subject: [PATCH] fix(cli): add old git params as alias to husky params --- @commitlint/cli/src/cli.js | 22 +++++++++++++++------- @commitlint/cli/src/cli.test.js | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/@commitlint/cli/src/cli.js b/@commitlint/cli/src/cli.js index 4ca0739eef..a0b31a47b5 100755 --- a/@commitlint/cli/src/cli.js +++ b/@commitlint/cli/src/cli.js @@ -228,16 +228,24 @@ function getEditValue(flags) { // 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 HUSKY_GIT_PARAMS` / `-E HUSKY_GIT_PARAMS` - if (edit === '$HUSKY_GIT_PARAMS' || edit === '%HUSKY_GIT_PARAMS%') { + // This has been superceded by the `-E GIT_PARAMS` / `-E HUSKY_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} HUSKY_GIT_PARAMS instead'`); - if (!('HUSKY_GIT_PARAMS' in process.env)) { - throw new Error( - `Received ${edit} as value for -e | --edit, but HUSKY_GIT_PARAMS is not available globally.` - ); + + 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.HUSKY_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 34f1ebd72a..2462ca56c0 100644 --- a/@commitlint/cli/src/cli.test.js +++ b/@commitlint/cli/src/cli.test.js @@ -109,6 +109,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 work with husky via commitlint -e $HUSKY_GIT_PARAMS', async () => { const cwd = await git.bootstrap('fixtures/husky/integration'); await writePkg(