Skip to content

Commit 5a34b8c

Browse files
byCedricmarionebl
authored andcommitted
fix: add fallback with husky git params to deprecation handling (#498)
* refactor(cli): rename old husky git params to husky prefix * fix(cli): add old git params as alias to husky params
1 parent ca74d90 commit 5a34b8c

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

@commitlint/cli/src/cli.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,28 @@ function getEditValue(flags) {
224224
if (typeof edit === 'boolean') {
225225
return edit;
226226
}
227-
// The recommended method to specify -e with husky was `commitlint -e $GIT_PARAMS`
227+
// The recommended method to specify -e with husky was `commitlint -e $HUSKY_GIT_PARAMS`
228228
// This does not work properly with win32 systems, where env variable declarations
229229
// use a different syntax
230230
// See https://github.com/marionebl/commitlint/issues/103 for details
231231
// This has been superceded by the `-E GIT_PARAMS` / `-E HUSKY_GIT_PARAMS`
232-
if (edit === '$GIT_PARAMS' || edit === '%GIT_PARAMS%') {
232+
const isGitParams = edit === '$GIT_PARAMS' || edit === '%GIT_PARAMS%';
233+
const isHuskyParams =
234+
edit === '$HUSKY_GIT_PARAMS' || edit === '%HUSKY_GIT_PARAMS%';
235+
236+
if (isGitParams || isHuskyParams) {
233237
console.warn(`Using environment variable syntax (${edit}) in -e |\
234-
--edit is deprecated. Use '{-E|--env} GIT_PARAMS instead'`);
235-
if (!('GIT_PARAMS' in process.env)) {
236-
throw new Error(
237-
`Received ${edit} as value for -e | --edit, but GIT_PARAMS is not available globally.`
238-
);
238+
--edit is deprecated. Use '{-E|--env} HUSKY_GIT_PARAMS instead'`);
239+
240+
if (isGitParams && 'GIT_PARAMS' in process.env) {
241+
return process.env.GIT_PARAMS;
242+
}
243+
if ('HUSKY_GIT_PARAMS' in process.env) {
244+
return process.env.HUSKY_GIT_PARAMS;
239245
}
240-
return process.env.GIT_PARAMS;
246+
throw new Error(
247+
`Received ${edit} as value for -e | --edit, but GIT_PARAMS or HUSKY_GIT_PARAMS are not available globally.`
248+
);
241249
}
242250
return edit;
243251
}

@commitlint/cli/src/cli.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@ test('should work with husky via commitlint -e %GIT_PARAMS%', async () => {
127127
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
128128
});
129129

130+
test('should work with husky via commitlint -e $HUSKY_GIT_PARAMS', async () => {
131+
const cwd = await git.bootstrap('fixtures/husky/integration');
132+
await writePkg(
133+
{scripts: {commitmsg: `'${bin}' -e $HUSKY_GIT_PARAMS`}},
134+
{cwd}
135+
);
136+
137+
await execa('npm', ['install'], {cwd});
138+
await execa('git', ['add', 'package.json'], {cwd});
139+
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
140+
});
141+
142+
test('should work with husky via commitlint -e %HUSKY_GIT_PARAMS%', async () => {
143+
const cwd = await git.bootstrap('fixtures/husky/integration');
144+
await writePkg(
145+
{scripts: {commitmsg: `'${bin}' -e %HUSKY_GIT_PARAMS%`}},
146+
{cwd}
147+
);
148+
149+
await execa('npm', ['install'], {cwd});
150+
await execa('git', ['add', 'package.json'], {cwd});
151+
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
152+
});
153+
130154
test('should allow reading of environment variables for edit file, succeeding if valid', async t => {
131155
const cwd = await git.bootstrap();
132156
await sander.writeFile(cwd, 'commit-msg-file', 'foo');

0 commit comments

Comments
 (0)