Skip to content

Commit 6381a2d

Browse files
authored
feat(cli): print-config now can be configured to print a json in stdout (#3863)
fixes #3819
1 parent e3d2091 commit 6381a2d

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

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

+45-9
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ test('should print help', async () => {
513513
Options:
514514
-c, --color toggle colored output [boolean] [default: true]
515515
-g, --config path to the config file [string]
516-
--print-config print resolved config [boolean] [default: false]
516+
--print-config print resolved config
517+
[string] [choices: "", "text", "json"]
517518
-d, --cwd directory to execute in
518519
[string] [default: (Working Directory)]
519520
-e, --edit read last commit message from the specified file or
@@ -547,14 +548,16 @@ test('should print version', async () => {
547548
expect(actual.stdout).toMatch('@commitlint/cli@');
548549
});
549550

550-
test('should print config', async () => {
551-
const cwd = await gitBootstrap('fixtures/default');
552-
const actual = await cli(['--print-config', '--no-color'], {cwd})();
553-
const stdout = actual.stdout
554-
.replace(/^{[^\n]/g, '{\n ')
555-
.replace(/[^\n]}$/g, '\n}')
556-
.replace(/(helpUrl:)\n[ ]+/, '$1 ');
557-
expect(stdout).toMatchInlineSnapshot(`
551+
describe('should print config', () => {
552+
test('should print config when flag is present but without value', async () => {
553+
const cwd = await gitBootstrap('fixtures/default');
554+
const actual = await cli(['--print-config', 'text', '--no-color'], {cwd})();
555+
556+
const stdout = actual.stdout
557+
.replace(/^{[^\n]/g, '{\n ')
558+
.replace(/[^\n]}$/g, '\n}')
559+
.replace(/(helpUrl:)\n[ ]+/, '$1 ');
560+
expect(stdout).toMatchInlineSnapshot(`
558561
"{
559562
extends: [],
560563
formatter: '@commitlint/format',
@@ -567,6 +570,39 @@ test('should print config', async () => {
567570
prompt: {}
568571
}"
569572
`);
573+
});
574+
575+
test('should print config when flag has `text` value', async () => {
576+
const cwd = await gitBootstrap('fixtures/default');
577+
const actual = await cli(['--print-config=text', '--no-color'], {cwd})();
578+
579+
const stdout = actual.stdout
580+
.replace(/^{[^\n]/g, '{\n ')
581+
.replace(/[^\n]}$/g, '\n}')
582+
.replace(/(helpUrl:)\n[ ]+/, '$1 ');
583+
expect(stdout).toMatchInlineSnapshot(`
584+
"{
585+
extends: [],
586+
formatter: '@commitlint/format',
587+
parserPreset: undefined,
588+
ignores: undefined,
589+
defaultIgnores: undefined,
590+
plugins: {},
591+
rules: { 'type-enum': [ 2, 'never', [ 'foo' ] ] },
592+
helpUrl: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
593+
prompt: {}
594+
}"
595+
`);
596+
});
597+
598+
test('should print config when flag has `json` value', async () => {
599+
const cwd = await gitBootstrap('fixtures/default');
600+
const actual = await cli(['--print-config=json', '--no-color'], {cwd})();
601+
602+
expect(actual.stdout).toMatchInlineSnapshot(
603+
`"{"extends":[],"formatter":"@commitlint/format","plugins":{},"rules":{"type-enum":[2,"never",["foo"]]},"helpUrl":"https://github.com/conventional-changelog/commitlint/#what-is-commitlint\","prompt":{}}"`
604+
);
605+
});
570606
});
571607

572608
async function writePkg(payload: unknown, options: TestOptions) {

@commitlint/cli/src/cli.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ const cli = yargs
3838
type: 'string',
3939
},
4040
'print-config': {
41-
type: 'boolean',
42-
default: false,
41+
choices: ['', 'text', 'json'],
4342
description: 'print resolved config',
43+
type: 'string',
4444
},
4545
cwd: {
4646
alias: 'd',
@@ -175,13 +175,22 @@ async function main(args: MainArgs): Promise<void> {
175175
const raw = options._;
176176
const flags = normalizeFlags(options);
177177

178-
if (flags['print-config']) {
178+
if (typeof options['print-config'] === 'string') {
179179
const loaded = await load(getSeed(flags), {
180180
cwd: flags.cwd,
181181
file: flags.config,
182182
});
183-
console.log(util.inspect(loaded, false, null, options.color));
184-
return;
183+
184+
switch (options['print-config']) {
185+
case 'json':
186+
console.log(JSON.stringify(loaded));
187+
return;
188+
189+
case 'text':
190+
default:
191+
console.log(util.inspect(loaded, false, null, options.color));
192+
return;
193+
}
185194
}
186195

187196
const fromStdin = checkFromStdin(raw, flags);

@commitlint/cli/src/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export interface CliFlags {
1515
to?: string;
1616
version?: boolean;
1717
verbose?: boolean;
18-
'print-config'?: boolean;
18+
/** @type {'' | 'text' | 'json'} */
19+
'print-config'?: string;
1920
strict?: boolean;
2021
_: (string | number)[];
2122
$0: string;

docs/reference-cli.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
Options:
1111
-c, --color toggle colored output [boolean] [default: true]
1212
-g, --config path to the config file [string]
13-
--print-config print resolved config [boolean] [default: false]
13+
--print-config print resolved config
14+
[string] [choices: "", "text", "json"]
1415
-d, --cwd directory to execute in
1516
[string] [default: (Working Directory)]
1617
-e, --edit read last commit message from the specified file or

0 commit comments

Comments
 (0)