Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): print-config now can be configured to print a json in stdout #3863

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'} */
escapedcat marked this conversation as resolved.
Show resolved Hide resolved
'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