From 9035e99c28a0959032143b4bb1a1bf797995db5f Mon Sep 17 00:00:00 2001 From: Armano Date: Tue, 5 Jan 2021 20:20:30 +0100 Subject: [PATCH 1/2] feat(cli): implement print-config cli flag --- @commitlint/cli/src/cli.test.ts | 18 +++++++++++++++++ @commitlint/cli/src/cli.ts | 22 +++++++++++++++++++-- @commitlint/cli/src/types.ts | 1 + docs/reference-cli.md | 35 +++++++++++++++++---------------- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index 4cc3a304ea..f8a1e5e049 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -446,6 +446,7 @@ 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] -d, --cwd directory to execute in [string] [default: (Working Directory)] -e, --edit read last commit message from the specified file or @@ -475,6 +476,23 @@ 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})(); + expect(actual.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' + }" + `); +}); + async function writePkg(payload: unknown, options: TestOptions) { const pkgPath = path.join(options.cwd, 'package.json'); const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8')); diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index b8d4b127a2..0e0603ba67 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -6,6 +6,7 @@ import stdin from 'get-stdin'; import resolveFrom from 'resolve-from'; import resolveGlobal from 'resolve-global'; import yargs from 'yargs'; +import util from 'util'; import {CliFlags, Seed} from './types'; import { @@ -33,6 +34,11 @@ const cli = yargs description: 'path to the config file', type: 'string', }, + 'print-config': { + type: 'boolean', + default: false, + description: 'print resolved config', + }, cwd: { alias: 'd', default: process.cwd(), @@ -123,6 +129,16 @@ main({edit: false, ...cli.argv}).catch((err) => { async function main(options: CliFlags) { const raw = options._; const flags = normalizeFlags(options); + + if (flags['print-config']) { + const loaded = await load(getSeed(flags), { + cwd: flags.cwd, + file: flags.config, + }); + console.log(util.inspect(loaded, false, null, options.color)); + return; + } + const fromStdin = checkFromStdin(raw, flags); const input = await (fromStdin @@ -149,8 +165,10 @@ async function main(options: CliFlags) { throw err; } - const loadOpts = {cwd: flags.cwd, file: flags.config}; - const loaded = await load(getSeed(flags), loadOpts); + const loaded = await load(getSeed(flags), { + cwd: flags.cwd, + file: flags.config, + }); const parserOpts = selectParserOpts(loaded.parserPreset); const opts: LintOptions & {parserOpts: ParserOptions} = { parserOpts: {}, diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts index 67dcc606a1..fd76023cc3 100644 --- a/@commitlint/cli/src/types.ts +++ b/@commitlint/cli/src/types.ts @@ -14,6 +14,7 @@ export interface CliFlags { to?: string; version?: boolean; verbose?: boolean; + 'print-config'?: boolean; _: string[]; $0: string; } diff --git a/docs/reference-cli.md b/docs/reference-cli.md index 3f0b66a6c1..3d045a1e73 100644 --- a/docs/reference-cli.md +++ b/docs/reference-cli.md @@ -3,31 +3,32 @@ ```bash ❯ npx commitlint --help -@commitlint/cli@8.3.5 - Lint your commit messages +@commitlint/cli@11.0.0 - Lint your commit messages [input] reads from stdin if --edit, --env, --from and --to are omitted Options: - --color, -c toggle colored output [boolean] [default: true] - --config, -g path to the config file [string] - --cwd, -d directory to execute in [string] [default: cwd] - --edit, -e read last commit message from the specified file or - fallbacks to ./.git/COMMIT_EDITMSG - [string] [default: false] - --env, -E check message in the file at path given by environment + -c, --color toggle colored output [boolean] [default: true] + -g, --config path to the config file [string] + --print-config print resolved config [boolean] [default: false] + -d, --cwd directory to execute in + [string] [default: (Working Directory)] + -e, --edit read last commit message from the specified file or + fallbacks to ./.git/COMMIT_EDITMSG [string] + -E, --env check message in the file at path given by environment variable value [string] - --extends, -x array of shareable configurations to extend [array] - --help-url, -H helpurl in error message [string] - --from, -f lower end of the commit range to lint; applies if + -x, --extends array of shareable configurations to extend [array] + -H, --help-url help url in error message [string] + -f, --from lower end of the commit range to lint; applies if edit=false [string] - --format, -o output format of the results [string] - --parser-preset, -p configuration preset to use for + -o, --format output format of the results [string] + -p, --parser-preset configuration preset to use for conventional-commits-parser [string] - --quiet, -q toggle console output [boolean] [default: false] - --to, -t upper end of the commit range to lint; applies if + -q, --quiet toggle console output [boolean] [default: false] + -t, --to upper end of the commit range to lint; applies if edit=false [string] - --verbose, -V enable verbose output for reports without problems + -V, --verbose enable verbose output for reports without problems [boolean] -v, --version display version information [boolean] - -h, --help Show help [boolean] + -h, --help Show help [boolean]" ``` From e61fc3552586c1f132cc4c6febacb60ab02f04ff Mon Sep 17 00:00:00 2001 From: Armano Date: Wed, 6 Jan 2021 06:07:08 +0100 Subject: [PATCH 2/2] test(cli): normalize difference between node console.log --- @commitlint/cli/src/cli.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index f8a1e5e049..7dc80db634 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -479,7 +479,11 @@ test('should print version', async () => { test('should print config', async () => { const cwd = await gitBootstrap('fixtures/default'); const actual = await cli(['--print-config', '--no-color'], {cwd})(); - expect(actual.stdout).toMatchInlineSnapshot(` + const stdout = actual.stdout + .replace(/^{[^\n]/g, '{\n ') + .replace(/[^\n]}$/g, '\n}') + .replace(/(helpUrl:)\n[ ]+/, '$1 '); + expect(stdout).toMatchInlineSnapshot(` "{ extends: [], formatter: '@commitlint/format',