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): implement print-config cli flag #2391

Merged
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
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 ');
armano2 marked this conversation as resolved.
Show resolved Hide resolved
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,
});
Comment on lines +134 to +137
Copy link
Contributor Author

@armano2 armano2 Jan 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure if this is best way to get resolved config, should we include here configuration that comes from cli arguments?

i think load should not return extends

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we include here configuration that comes from cli arguments?

I think that would be nice to always reflect the actual used configuration.

i think load should not return extends

@byCedric ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be nice to always reflect the actual used configuration.

ok, than its correct

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
escapedcat marked this conversation as resolved.
Show resolved Hide resolved
❯ 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]"
```