Skip to content

Commit 8626883

Browse files
authored
feat(cli): implement print-config cli flag (#2391)
* feat(cli): implement print-config cli flag * test(cli): normalize difference between node console.log
1 parent 1479cad commit 8626883

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

@commitlint/cli/src/cli.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ test('should print help', async () => {
446446
Options:
447447
-c, --color toggle colored output [boolean] [default: true]
448448
-g, --config path to the config file [string]
449+
--print-config print resolved config [boolean] [default: false]
449450
-d, --cwd directory to execute in
450451
[string] [default: (Working Directory)]
451452
-e, --edit read last commit message from the specified file or
@@ -475,6 +476,27 @@ test('should print version', async () => {
475476
expect(actual.stdout).toMatch('@commitlint/cli@');
476477
});
477478

479+
test('should print config', async () => {
480+
const cwd = await gitBootstrap('fixtures/default');
481+
const actual = await cli(['--print-config', '--no-color'], {cwd})();
482+
const stdout = actual.stdout
483+
.replace(/^{[^\n]/g, '{\n ')
484+
.replace(/[^\n]}$/g, '\n}')
485+
.replace(/(helpUrl:)\n[ ]+/, '$1 ');
486+
expect(stdout).toMatchInlineSnapshot(`
487+
"{
488+
extends: [],
489+
formatter: '@commitlint/format',
490+
parserPreset: undefined,
491+
ignores: undefined,
492+
defaultIgnores: undefined,
493+
plugins: {},
494+
rules: { 'type-enum': [ 2, 'never', [ 'foo' ] ] },
495+
helpUrl: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint'
496+
}"
497+
`);
498+
});
499+
478500
async function writePkg(payload: unknown, options: TestOptions) {
479501
const pkgPath = path.join(options.cwd, 'package.json');
480502
const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));

@commitlint/cli/src/cli.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import stdin from 'get-stdin';
66
import resolveFrom from 'resolve-from';
77
import resolveGlobal from 'resolve-global';
88
import yargs from 'yargs';
9+
import util from 'util';
910

1011
import {CliFlags, Seed} from './types';
1112
import {
@@ -33,6 +34,11 @@ const cli = yargs
3334
description: 'path to the config file',
3435
type: 'string',
3536
},
37+
'print-config': {
38+
type: 'boolean',
39+
default: false,
40+
description: 'print resolved config',
41+
},
3642
cwd: {
3743
alias: 'd',
3844
default: process.cwd(),
@@ -123,6 +129,16 @@ main({edit: false, ...cli.argv}).catch((err) => {
123129
async function main(options: CliFlags) {
124130
const raw = options._;
125131
const flags = normalizeFlags(options);
132+
133+
if (flags['print-config']) {
134+
const loaded = await load(getSeed(flags), {
135+
cwd: flags.cwd,
136+
file: flags.config,
137+
});
138+
console.log(util.inspect(loaded, false, null, options.color));
139+
return;
140+
}
141+
126142
const fromStdin = checkFromStdin(raw, flags);
127143

128144
const input = await (fromStdin
@@ -149,8 +165,10 @@ async function main(options: CliFlags) {
149165
throw err;
150166
}
151167

152-
const loadOpts = {cwd: flags.cwd, file: flags.config};
153-
const loaded = await load(getSeed(flags), loadOpts);
168+
const loaded = await load(getSeed(flags), {
169+
cwd: flags.cwd,
170+
file: flags.config,
171+
});
154172
const parserOpts = selectParserOpts(loaded.parserPreset);
155173
const opts: LintOptions & {parserOpts: ParserOptions} = {
156174
parserOpts: {},

@commitlint/cli/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface CliFlags {
1414
to?: string;
1515
version?: boolean;
1616
verbose?: boolean;
17+
'print-config'?: boolean;
1718
_: string[];
1819
$0: string;
1920
}

docs/reference-cli.md

+18-17
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,32 @@
33
```bash
44
❯ npx commitlint --help
55

6-
@commitlint/cli@8.3.5 - Lint your commit messages
6+
@commitlint/cli@11.0.0 - Lint your commit messages
77

88
[input] reads from stdin if --edit, --env, --from and --to are omitted
99

1010
Options:
11-
--color, -c toggle colored output [boolean] [default: true]
12-
--config, -g path to the config file [string]
13-
--cwd, -d directory to execute in [string] [default: cwd]
14-
--edit, -e read last commit message from the specified file or
15-
fallbacks to ./.git/COMMIT_EDITMSG
16-
[string] [default: false]
17-
--env, -E check message in the file at path given by environment
11+
-c, --color toggle colored output [boolean] [default: true]
12+
-g, --config path to the config file [string]
13+
--print-config print resolved config [boolean] [default: false]
14+
-d, --cwd directory to execute in
15+
[string] [default: (Working Directory)]
16+
-e, --edit read last commit message from the specified file or
17+
fallbacks to ./.git/COMMIT_EDITMSG [string]
18+
-E, --env check message in the file at path given by environment
1819
variable value [string]
19-
--extends, -x array of shareable configurations to extend [array]
20-
--help-url, -H helpurl in error message [string]
21-
--from, -f lower end of the commit range to lint; applies if
20+
-x, --extends array of shareable configurations to extend [array]
21+
-H, --help-url help url in error message [string]
22+
-f, --from lower end of the commit range to lint; applies if
2223
edit=false [string]
23-
--format, -o output format of the results [string]
24-
--parser-preset, -p configuration preset to use for
24+
-o, --format output format of the results [string]
25+
-p, --parser-preset configuration preset to use for
2526
conventional-commits-parser [string]
26-
--quiet, -q toggle console output [boolean] [default: false]
27-
--to, -t upper end of the commit range to lint; applies if
27+
-q, --quiet toggle console output [boolean] [default: false]
28+
-t, --to upper end of the commit range to lint; applies if
2829
edit=false [string]
29-
--verbose, -V enable verbose output for reports without problems
30+
-V, --verbose enable verbose output for reports without problems
3031
[boolean]
3132
-v, --version display version information [boolean]
32-
-h, --help Show help [boolean]
33+
-h, --help Show help [boolean]"
3334
```

0 commit comments

Comments
 (0)