Skip to content

Commit

Permalink
fix(cli): add support for GIT_PARAMS on windows
Browse files Browse the repository at this point in the history
* fix(cli): add support for GIT_PARAMS on windows
* test(cli): add more husky integration cases

Closes #103 (#175)
  • Loading branch information
kumarharsh authored and marionebl committed Dec 5, 2017
1 parent 3853e0d commit c62bd41
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
26 changes: 22 additions & 4 deletions @commitlint/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,29 @@ function checkFromHistory(flags) {
function normalizeFlags(flags) {
// The `edit` flag is either a boolean or a string but we are only allowed
// to specify one of them in minimist
if (flags.edit === '') {
return merge({}, flags, {edit: true, e: true});
}
const edit = flags.edit === '' ? true : normalizeEdit(flags.edit);
return merge({}, flags, {edit, e: edit});
}

return flags;
function normalizeEdit(edit) {
if (typeof edit === 'boolean') {
return edit;
}
// The recommended method to specify -e with husky is commitlint -e $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
if (edit === '$GIT_PARAMS' || edit === '%GIT_PARAMS%') {
if (!('GIT_PARAMS' in process.env)) {
throw new Error(
`Received ${
edit
} as value for -e | --edit, but GIT_PARAMS is not available globally.`
);
}
return process.env.GIT_PARAMS;
}
return edit;
}

function getSeed(seed) {
Expand Down
18 changes: 18 additions & 0 deletions @commitlint/cli/src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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 pick up parser preset and fail accordingly', async t => {
const cwd = await git.bootstrap('fixtures/parser-preset');
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(
Expand Down

0 comments on commit c62bd41

Please sign in to comment.