Skip to content

Commit

Permalink
feat(cli): implement print-config cli flag (#2391)
Browse files Browse the repository at this point in the history
* feat(cli): implement print-config cli flag

* test(cli): normalize difference between node console.log
  • Loading branch information
armano2 committed Jan 11, 2021
1 parent 1479cad commit 8626883
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
22 changes: 22 additions & 0 deletions @commitlint/cli/src/cli.test.ts
Expand Up @@ -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
Expand Down Expand Up @@ -475,6 +476,27 @@ 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(`
"{
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'));
Expand Down
22 changes: 20 additions & 2 deletions @commitlint/cli/src/cli.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -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: {},
Expand Down
1 change: 1 addition & 0 deletions @commitlint/cli/src/types.ts
Expand Up @@ -14,6 +14,7 @@ export interface CliFlags {
to?: string;
version?: boolean;
verbose?: boolean;
'print-config'?: boolean;
_: string[];
$0: string;
}
Expand Down
35 changes: 18 additions & 17 deletions docs/reference-cli.md
Expand Up @@ -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]"
```

0 comments on commit 8626883

Please sign in to comment.