Skip to content

Commit

Permalink
feat(cli): print-config now can be configured to print a json in stdo…
Browse files Browse the repository at this point in the history
…ut (#3863)

fixes #3819
  • Loading branch information
marcalexiei authored Jan 22, 2024
1 parent e3d2091 commit 6381a2d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
54 changes: 45 additions & 9 deletions @commitlint/cli/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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) {
Expand Down
19 changes: 14 additions & 5 deletions @commitlint/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -175,13 +175,22 @@ async function main(args: MainArgs): Promise<void> {
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);
Expand Down
3 changes: 2 additions & 1 deletion @commitlint/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion docs/reference-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6381a2d

Please sign in to comment.