From 6381a2daa0d5d89ab2195998d63a9690a533d3f2 Mon Sep 17 00:00:00 2001 From: Marco Pasqualetti <24919330+marcalexiei@users.noreply.github.com> Date: Mon, 22 Jan 2024 10:40:31 +0100 Subject: [PATCH] feat(cli): print-config now can be configured to print a json in stdout (#3863) fixes #3819 --- @commitlint/cli/src/cli.test.ts | 54 +++++++++++++++++++++++++++------ @commitlint/cli/src/cli.ts | 19 +++++++++--- @commitlint/cli/src/types.ts | 3 +- docs/reference-cli.md | 3 +- 4 files changed, 63 insertions(+), 16 deletions(-) diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index e0e1bb3e3d..a26d09cf39 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -513,7 +513,8 @@ test('should print help', async () => { Options: -c, --color toggle colored output [boolean] [default: true] -g, --config path to the config file [string] - --print-config print resolved config [boolean] [default: false] + --print-config print resolved config + [string] [choices: "", "text", "json"] -d, --cwd directory to execute in [string] [default: (Working Directory)] -e, --edit read last commit message from the specified file or @@ -547,14 +548,16 @@ test('should print version', async () => { expect(actual.stdout).toMatch('@commitlint/cli@'); }); -test('should print config', async () => { - const cwd = await gitBootstrap('fixtures/default'); - const actual = await cli(['--print-config', '--no-color'], {cwd})(); - const stdout = actual.stdout - .replace(/^{[^\n]/g, '{\n ') - .replace(/[^\n]}$/g, '\n}') - .replace(/(helpUrl:)\n[ ]+/, '$1 '); - expect(stdout).toMatchInlineSnapshot(` +describe('should print config', () => { + test('should print config when flag is present but without value', async () => { + const cwd = await gitBootstrap('fixtures/default'); + const actual = await cli(['--print-config', 'text', '--no-color'], {cwd})(); + + const stdout = actual.stdout + .replace(/^{[^\n]/g, '{\n ') + .replace(/[^\n]}$/g, '\n}') + .replace(/(helpUrl:)\n[ ]+/, '$1 '); + expect(stdout).toMatchInlineSnapshot(` "{ extends: [], formatter: '@commitlint/format', @@ -567,6 +570,39 @@ test('should print config', async () => { prompt: {} }" `); + }); + + test('should print config when flag has `text` value', async () => { + const cwd = await gitBootstrap('fixtures/default'); + const actual = await cli(['--print-config=text', '--no-color'], {cwd})(); + + const stdout = actual.stdout + .replace(/^{[^\n]/g, '{\n ') + .replace(/[^\n]}$/g, '\n}') + .replace(/(helpUrl:)\n[ ]+/, '$1 '); + expect(stdout).toMatchInlineSnapshot(` + "{ + extends: [], + formatter: '@commitlint/format', + parserPreset: undefined, + ignores: undefined, + defaultIgnores: undefined, + plugins: {}, + rules: { 'type-enum': [ 2, 'never', [ 'foo' ] ] }, + helpUrl: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint', + prompt: {} + }" + `); + }); + + test('should print config when flag has `json` value', async () => { + const cwd = await gitBootstrap('fixtures/default'); + const actual = await cli(['--print-config=json', '--no-color'], {cwd})(); + + expect(actual.stdout).toMatchInlineSnapshot( + `"{"extends":[],"formatter":"@commitlint/format","plugins":{},"rules":{"type-enum":[2,"never",["foo"]]},"helpUrl":"https://github.com/conventional-changelog/commitlint/#what-is-commitlint\","prompt":{}}"` + ); + }); }); async function writePkg(payload: unknown, options: TestOptions) { diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 8d7c202121..0820831a5e 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -38,9 +38,9 @@ const cli = yargs type: 'string', }, 'print-config': { - type: 'boolean', - default: false, + choices: ['', 'text', 'json'], description: 'print resolved config', + type: 'string', }, cwd: { alias: 'd', @@ -175,13 +175,22 @@ async function main(args: MainArgs): Promise { const raw = options._; const flags = normalizeFlags(options); - if (flags['print-config']) { + if (typeof options['print-config'] === 'string') { const loaded = await load(getSeed(flags), { cwd: flags.cwd, file: flags.config, }); - console.log(util.inspect(loaded, false, null, options.color)); - return; + + switch (options['print-config']) { + case 'json': + console.log(JSON.stringify(loaded)); + return; + + case 'text': + default: + console.log(util.inspect(loaded, false, null, options.color)); + return; + } } const fromStdin = checkFromStdin(raw, flags); diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts index a951180e7a..a622cb05fe 100644 --- a/@commitlint/cli/src/types.ts +++ b/@commitlint/cli/src/types.ts @@ -15,7 +15,8 @@ export interface CliFlags { to?: string; version?: boolean; verbose?: boolean; - 'print-config'?: boolean; + /** @type {'' | 'text' | 'json'} */ + 'print-config'?: string; strict?: boolean; _: (string | number)[]; $0: string; diff --git a/docs/reference-cli.md b/docs/reference-cli.md index e95ac8b10c..387b8048db 100644 --- a/docs/reference-cli.md +++ b/docs/reference-cli.md @@ -10,7 +10,8 @@ Options: -c, --color toggle colored output [boolean] [default: true] -g, --config path to the config file [string] - --print-config print resolved config [boolean] [default: false] + --print-config print resolved config + [string] [choices: "", "text", "json"] -d, --cwd directory to execute in [string] [default: (Working Directory)] -e, --edit read last commit message from the specified file or