Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): add fallback with husky git params to deprecation handling #498

Merged
merged 2 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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