Skip to content

Commit 458f24c

Browse files
DJMcNabmarionebl
authored andcommitted
fix(cli): add environment variable cli flag (#343)
fixes #319. additionally fixes some minor typos in docs
1 parent 9d4264b commit 458f24c

File tree

7 files changed

+90
-35
lines changed

7 files changed

+90
-35
lines changed

@commitlint/cli/src/cli.js

+38-10
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,22 @@ const flags = {
4040
'read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG',
4141
type: 'string'
4242
},
43+
env: {
44+
alias: 'E',
45+
default: null,
46+
description:
47+
'check message in the file at path given by environment variable value',
48+
type: 'string'
49+
},
4350
extends: {
4451
alias: 'x',
4552
description: 'array of shareable configurations to extend',
4653
type: 'string'
4754
},
4855
help: {
4956
alias: 'h',
50-
type: 'boolean'
57+
type: 'boolean',
58+
description: 'display this help message'
5159
},
5260
from: {
5361
alias: 'f',
@@ -74,14 +82,15 @@ const flags = {
7482
},
7583
version: {
7684
alias: 'v',
77-
type: 'boolean'
85+
type: 'boolean',
86+
description: 'display version information'
7887
}
7988
};
8089

8190
const cli = meow({
8291
description: `${pkg.name}@${pkg.version} - ${pkg.description}`,
8392
flags,
84-
help: `[input] reads from stdin if --edit, --from and --to are omitted\n${help(
93+
help: `[input] reads from stdin if --edit, --env, --from and --to are omitted\n${help(
8594
flags
8695
)}`,
8796
unknown(arg) {
@@ -114,7 +123,7 @@ async function main(options) {
114123

115124
if (messages.length === 0 && !checkFromRepository(flags)) {
116125
const err = new Error(
117-
'[input] is required: supply via stdin, or --edit or --from and --to'
126+
'[input] is required: supply via stdin, or --env or --edit or --from and --to'
118127
);
119128
err.type = pkg.name;
120129
console.log(`${cli.help}\n`);
@@ -165,29 +174,48 @@ function checkFromRepository(flags) {
165174
}
166175

167176
function checkFromEdit(flags) {
168-
return Boolean(flags.edit);
177+
return Boolean(flags.edit) || flags.env;
169178
}
170179

171180
function checkFromHistory(flags) {
172181
return typeof flags.from === 'string' || typeof flags.to === 'string';
173182
}
174183

175184
function normalizeFlags(flags) {
176-
// The `edit` flag is either a boolean or a string but we are only allowed
177-
// to specify one of them in minimist
178-
const edit = flags.edit === '' ? true : normalizeEdit(flags.edit);
185+
const edit = getEditValue(flags);
179186
return merge({}, flags, {edit, e: edit});
180187
}
181188

182-
function normalizeEdit(edit) {
189+
function getEditValue(flags) {
190+
if (flags.env) {
191+
if (!(flags.env in process.env)) {
192+
throw new Error(
193+
`Recieved '${
194+
flags.env
195+
}' as value for -E | --env, but environment variable '${
196+
flags.env
197+
}' is not available globally`
198+
);
199+
}
200+
return process.env[flags.env];
201+
}
202+
const edit = flags.edit;
203+
// If the edit flag is set but empty (i.e '-e') we default
204+
// to .git/COMMIT_EDITMSG
205+
if (edit === '') {
206+
return true;
207+
}
183208
if (typeof edit === 'boolean') {
184209
return edit;
185210
}
186-
// The recommended method to specify -e with husky is commitlint -e $GIT_PARAMS
211+
// The recommended method to specify -e with husky was `commitlint -e $GIT_PARAMS`
187212
// This does not work properly with win32 systems, where env variable declarations
188213
// use a different syntax
189214
// See https://github.com/marionebl/commitlint/issues/103 for details
215+
// This has been superceded by the `-E GIT_PARAMS` / `-E HUSKY_GIT_PARAMS`
190216
if (edit === '$GIT_PARAMS' || edit === '%GIT_PARAMS%') {
217+
console.warn(`Using environment variable syntax (${edit}) in -e |\
218+
--edit is deprecated. Use '{-E|--env} GIT_PARAMS instead'`);
191219
if (!('GIT_PARAMS' in process.env)) {
192220
throw new Error(
193221
`Received ${edit} as value for -e | --edit, but GIT_PARAMS is not available globally.`

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

+29-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ test('should produce no error output with -q flag', async t => {
9292

9393
test('should work with husky commitmsg hook and git commit', async () => {
9494
const cwd = await git.bootstrap('fixtures/husky/integration');
95-
await writePkg({scripts: {commitmsg: `${bin} -e`}}, {cwd});
95+
await writePkg({scripts: {commitmsg: `'${bin}' -e`}}, {cwd});
9696

9797
await execa('npm', ['install'], {cwd});
9898
await execa('git', ['add', 'package.json'], {cwd});
@@ -102,7 +102,7 @@ test('should work with husky commitmsg hook and git commit', async () => {
102102
test('should work with husky commitmsg hook in sub packages', async () => {
103103
const upper = await git.bootstrap('fixtures/husky');
104104
const cwd = path.join(upper, 'integration');
105-
await writePkg({scripts: {commitmsg: `${bin} -e`}}, {cwd: upper});
105+
await writePkg({scripts: {commitmsg: `'${bin}' -e`}}, {cwd: upper});
106106

107107
await execa('npm', ['install'], {cwd});
108108
await execa('git', ['add', 'package.json'], {cwd});
@@ -111,7 +111,7 @@ test('should work with husky commitmsg hook in sub packages', async () => {
111111

112112
test('should work with husky via commitlint -e $GIT_PARAMS', async () => {
113113
const cwd = await git.bootstrap('fixtures/husky/integration');
114-
await writePkg({scripts: {commitmsg: `${bin} -e $GIT_PARAMS`}}, {cwd});
114+
await writePkg({scripts: {commitmsg: `'${bin}' -e $GIT_PARAMS`}}, {cwd});
115115

116116
await execa('npm', ['install'], {cwd});
117117
await execa('git', ['add', 'package.json'], {cwd});
@@ -120,13 +120,37 @@ test('should work with husky via commitlint -e $GIT_PARAMS', async () => {
120120

121121
test('should work with husky via commitlint -e %GIT_PARAMS%', async () => {
122122
const cwd = await git.bootstrap('fixtures/husky/integration');
123-
await writePkg({scripts: {commitmsg: `${bin} -e %GIT_PARAMS%`}}, {cwd});
123+
await writePkg({scripts: {commitmsg: `'${bin}' -e %GIT_PARAMS%`}}, {cwd});
124124

125125
await execa('npm', ['install'], {cwd});
126126
await execa('git', ['add', 'package.json'], {cwd});
127127
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
128128
});
129129

130+
test('should allow reading of environment variables for edit file, succeeding if valid', async t => {
131+
const cwd = await git.bootstrap();
132+
await sander.writeFile(cwd, 'commit-msg-file', 'foo');
133+
const actual = await cli(['--env', 'variable'], {
134+
cwd,
135+
env: {variable: 'commit-msg-file'}
136+
})();
137+
t.is(actual.code, 0);
138+
});
139+
140+
test('should allow reading of environment variables for edit file, failing if invalid', async t => {
141+
const cwd = await git.bootstrap('fixtures/simple');
142+
await sander.writeFile(
143+
cwd,
144+
'commit-msg-file',
145+
'foo: bar\n\nFoo bar bizz buzz.\n\nCloses #123.'
146+
);
147+
const actual = await cli(['--env', 'variable'], {
148+
cwd,
149+
env: {variable: 'commit-msg-file'}
150+
})();
151+
t.is(actual.code, 1);
152+
});
153+
130154
test('should pick up parser preset and fail accordingly', async t => {
131155
const cwd = await git.bootstrap('fixtures/parser-preset');
132156
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(
@@ -178,7 +202,7 @@ test('should pick up config from inside git repo with precedence and fail accord
178202

179203
test('should handle --amend with signoff', async () => {
180204
const cwd = await git.bootstrap('fixtures/signoff');
181-
await writePkg({scripts: {commitmsg: `${bin} -e`}}, {cwd});
205+
await writePkg({scripts: {commitmsg: `'${bin}' -e`}}, {cwd});
182206

183207
await execa('npm', ['install'], {cwd});
184208
await execa('git', ['add', 'package.json'], {cwd});

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ To lint commits before they are created you can use the 'commitmsg' hook as desc
5353
```json
5454
{
5555
"scripts": {
56-
"commitmsg": "commitlint -e $GIT_PARAMS"
56+
"commitmsg": "commitlint -E GIT_PARAMS"
5757
}
5858
}
5959
```
@@ -92,7 +92,7 @@ A number of shared configurations are available to install and use with `commitl
9292

9393
## API
9494

95-
* Alternative, programatic way to interact with `commitlint`
95+
* Alternative, programmatic way to interact with `commitlint`
9696
* Packages:
9797
* [format](./@commitlint/format) - Format commitlint reports
9898
* [lint](./@commitlint/lint) - Lint a string against commitlint rules
@@ -111,7 +111,7 @@ A number of shared configurations are available to install and use with `commitl
111111
112112
`commitlint` is considered stable and is used in various projects as development tool.
113113

114-
We indentify **ease of adoption** and **developer experience** as fields where there
114+
We identify **ease of adoption** and **developer experience** as fields where there
115115
is room and need for improvement. The items on the roadmap should enhance `commitlint` regarding those aspects.
116116

117117
* [x] **Adoption**: Provide reusable Travis CI integration: `@commitlint/travis-cli` (https://github.com/marionebl/commitlint/releases/tag/v5.1.0)

docs/guides-local-setup.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ This allows us to add [git hooks](https://github.com/typicode/husky/blob/master/
3333
```json
3434
{
3535
"scripts": {
36-
"commitmsg": "commitlint -e $GIT_PARAMS"
36+
"commitmsg": "commitlint -E GIT_PARAMS"
3737
}
3838
}
3939
```
4040

41-
Using `commitmsg` gives us exactly what we want: It is executed everytime a new commit is created. Passing husky's `$GIT_PARAMS` to `commitlint` via the `-e|--edit` flag directs it to the relevant edit file. `-e` defaults to `.git/COMMIT_EDITMSG`.
41+
Using `commitmsg` gives us exactly what we want: It is executed whenever a new commit is created. Passing husky's `GIT_PARAMS` to `commitlint` via the `-E|--env` flag directs it to the relevant edit file. `-e` would default to `.git/COMMIT_EDITMSG`.
4242

4343
## Test
4444

45-
You can test the hook by simple commiting. You should see something like this if everything works.
45+
You can test the hook by simply committing. You should see something like this if everything works.
4646

4747
```bash
4848
git commit -m "foo: this will fail"

docs/guides-upgrade.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ npm install --save-dev @commitlint/cli @commitlint/config-conventional
2020
```
2121
{
2222
"scripts": {
23-
"commitmsg": "commitlint -x @commitlint/config-conventional -e $GIT_PARAMS"
23+
"commitmsg": "commitlint -x @commitlint/config-conventional -E GIT_PARAMS"
2424
}
2525
}
2626
```
@@ -47,7 +47,7 @@ npm install --save-dev @commitlint/cli @commitint/config-conventional
4747
```
4848
{
4949
"scripts": {
50-
"commitmsg": "commitlint -e $GIT_PARAMS"
50+
"commitmsg": "commitlint -E GIT_PARAMS"
5151
}
5252
}
5353
```

docs/reference-cli.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
```bash
44
❯ npx commitlint --help
55

6-
commitlint@4.2.0 - Lint your commit messages
6+
@commitlint@6.2.0 - Lint your commit messages
77

8-
[input] reads from stdin if --edit, --from and --to are omitted
9-
--color, -c toggle colored output, defaults to: true
10-
--cwd, -d directory to execute in, defaults to: /Users/marneb/Documents/oss/commitlint
11-
--edit, -e read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG
12-
--extends, -x array of shareable configurations to extend
13-
--config, -g path to a custom configuration
14-
--from, -f lower end of the commit range to lint; applies if edit=false
15-
--to, -t upper end of the commit range to lint; applies if edit=false
16-
--quiet, -q toggle console output
17-
--parser-preset, -p configuration preset to use for conventional-commits-parser
8+
[input] reads from stdin if --edit, --env, --from and --to are omitted
9+
--color, -c toggle colored output, defaults to: true
10+
--config, -g path to the config file
11+
--cwd, -d directory to execute in, defaults to: $CD
12+
--edit, -e read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG
13+
--env, -E check message in the file at path given by environment variable value
14+
--extends, -x array of shareable configurations to extend
15+
--help, -h display this help message
16+
--from, -f lower end of the commit range to lint; applies if edit=false
17+
--parser-preset, -p configuration preset to use for conventional-commits-parser
18+
--quiet, -q toggle console output
19+
--to, -t upper end of the commit range to lint; applies if edit=false
20+
--version, -v display version information
1821
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "lerna run build --stream --parallel --include-filtered-dependencies",
99
"clean": "npx lerna clean --yes && npx lerna run clean --stream --parallel --include-filtered-dependencies",
1010
"commit": "node @commitlint/prompt-cli/cli.js",
11-
"commitmsg": "node @commitlint/cli/lib/cli.js -e $GIT_PARAMS",
11+
"commitmsg": "node @commitlint/cli/lib/cli.js -E GIT_PARAMS",
1212
"deps": "lerna run deps",
1313
"pkg": "lerna run pkg",
1414
"docs": "docsify serve docs",

0 commit comments

Comments
 (0)