Skip to content

Commit

Permalink
fix: add fallback with husky git params to deprecation handling (#498)
Browse files Browse the repository at this point in the history
* refactor(cli): rename old husky git params to husky prefix

* fix(cli): add old git params as alias to husky params
  • Loading branch information
byCedric authored and marionebl committed Nov 30, 2018
1 parent ca74d90 commit 5a34b8c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
24 changes: 16 additions & 8 deletions @commitlint/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
24 changes: 24 additions & 0 deletions @commitlint/cli/src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 5a34b8c

Please sign in to comment.