Skip to content

Commit c62bd41

Browse files
kumarharshmarionebl
authored andcommitted
fix(cli): add support for GIT_PARAMS on windows
* fix(cli): add support for GIT_PARAMS on windows * test(cli): add more husky integration cases Closes #103 (#175)
1 parent 3853e0d commit c62bd41

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

@commitlint/cli/src/cli.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,29 @@ function checkFromHistory(flags) {
139139
function normalizeFlags(flags) {
140140
// The `edit` flag is either a boolean or a string but we are only allowed
141141
// to specify one of them in minimist
142-
if (flags.edit === '') {
143-
return merge({}, flags, {edit: true, e: true});
144-
}
142+
const edit = flags.edit === '' ? true : normalizeEdit(flags.edit);
143+
return merge({}, flags, {edit, e: edit});
144+
}
145145

146-
return flags;
146+
function normalizeEdit(edit) {
147+
if (typeof edit === 'boolean') {
148+
return edit;
149+
}
150+
// The recommended method to specify -e with husky is commitlint -e $GIT_PARAMS
151+
// This does not work properly with win32 systems, where env variable declarations
152+
// use a different syntax
153+
// See https://github.com/marionebl/commitlint/issues/103 for details
154+
if (edit === '$GIT_PARAMS' || edit === '%GIT_PARAMS%') {
155+
if (!('GIT_PARAMS' in process.env)) {
156+
throw new Error(
157+
`Received ${
158+
edit
159+
} as value for -e | --edit, but GIT_PARAMS is not available globally.`
160+
);
161+
}
162+
return process.env.GIT_PARAMS;
163+
}
164+
return edit;
147165
}
148166

149167
function getSeed(seed) {

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

+18
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ test('should work with husky commitmsg hook in sub packages', async () => {
101101
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
102102
});
103103

104+
test('should work with husky via commitlint -e $GIT_PARAMS', async () => {
105+
const cwd = await git.bootstrap('fixtures/husky/integration');
106+
await writePkg({scripts: {commitmsg: `${bin} -e $GIT_PARAMS`}}, {cwd});
107+
108+
await execa('npm', ['install'], {cwd});
109+
await execa('git', ['add', 'package.json'], {cwd});
110+
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
111+
});
112+
113+
test('should work with husky via commitlint -e %GIT_PARAMS%', async () => {
114+
const cwd = await git.bootstrap('fixtures/husky/integration');
115+
await writePkg({scripts: {commitmsg: `${bin} -e %GIT_PARAMS%`}}, {cwd});
116+
117+
await execa('npm', ['install'], {cwd});
118+
await execa('git', ['add', 'package.json'], {cwd});
119+
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
120+
});
121+
104122
test('should pick up parser preset and fail accordingly', async t => {
105123
const cwd = await git.bootstrap('fixtures/parser-preset');
106124
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(

0 commit comments

Comments
 (0)