Skip to content

Commit 2c03ec6

Browse files
lc-softmarionebl
authored andcommitted
feat: add "--config" option (#261)
1 parent 73fd0fd commit 2c03ec6

File tree

8 files changed

+46
-5
lines changed

8 files changed

+46
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'header-max-length': [2, 'always', 4]
4+
}
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'type-enum': [2, 'never', ['foo']]
4+
}
5+
};

@commitlint/cli/src/cli.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const pkg = require('../package');
1212
const help = require('./help');
1313

1414
const configuration = {
15-
string: ['cwd', 'from', 'to', 'edit', 'extends', 'parser-preset'],
15+
string: ['cwd', 'from', 'to', 'edit', 'extends', 'parser-preset', 'config'],
1616
boolean: ['help', 'version', 'quiet', 'color'],
1717
alias: {
1818
c: 'color',
@@ -22,13 +22,15 @@ const configuration = {
2222
t: 'to',
2323
q: 'quiet',
2424
h: 'help',
25+
g: 'config',
2526
v: 'version',
2627
x: 'extends',
2728
p: 'parser-preset'
2829
},
2930
description: {
3031
color: 'toggle colored output',
3132
cwd: 'directory to execute in',
33+
config: 'path to the config file',
3234
edit:
3335
'read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG',
3436
extends: 'array of shareable configurations to extend',
@@ -41,6 +43,7 @@ const configuration = {
4143
default: {
4244
color: true,
4345
cwd: process.cwd(),
46+
config: null,
4447
edit: false,
4548
from: null,
4649
to: null,
@@ -96,7 +99,8 @@ async function main(options) {
9699
throw err;
97100
}
98101

99-
const loaded = await core.load(getSeed(flags), {cwd: flags.cwd});
102+
const loadOpts = {cwd: flags.cwd, file: flags.config};
103+
const loaded = await core.load(getSeed(flags), loadOpts);
100104
const parserOpts = selectParserOpts(loaded.parserPreset);
101105
const opts = parserOpts ? {parserOpts} : {parserOpts: {}};
102106

@commitlint/cli/src/cli.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ test('should fail for input from stdin with rule from rc', async t => {
5959
t.is(actual.code, 1);
6060
});
6161

62+
test('should work with --config option', async t => {
63+
const file = 'config/commitlint.config.js';
64+
const cwd = await git.bootstrap('fixtures/specify-config-file');
65+
const actual = await cli(['--config', file], {cwd})('foo: bar');
66+
t.true(actual.stdout.includes('type must not be one of [foo]'));
67+
t.is(actual.code, 1);
68+
});
69+
6270
test('should fail for input from stdin with rule from js', async t => {
6371
const cwd = await git.bootstrap('fixtures/extends-root');
6472
const actual = await cli(['--extends', './extended'], {cwd})('foo: bar');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
rules: {
3+
'foo': 'hello',
4+
'bar': 'world'
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'foo': 'bar'
4+
}
5+
};

@commitlint/core/src/load.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const w = (a, b) => (Array.isArray(b) ? b : undefined);
1212
const valid = input => pick(input, 'extends', 'rules', 'parserPreset');
1313

1414
export default async (seed = {}, options = {cwd: process.cwd()}) => {
15-
const loaded = await loadConfig(options.cwd);
15+
const loaded = await loadConfig(options.cwd, options.file);
1616
const base = loaded.filepath ? path.dirname(loaded.filepath) : options.cwd;
1717

1818
// Merge passed config with file based options
@@ -78,9 +78,10 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
7878
}, preset);
7979
};
8080

81-
async function loadConfig(cwd) {
81+
async function loadConfig(cwd, configPath) {
8282
const explorer = cosmiconfig('commitlint', {
83-
rcExtensions: true
83+
rcExtensions: true,
84+
configPath: configPath ? path.resolve(cwd, configPath) : null
8485
});
8586

8687
const local = await explorer.load(cwd);

@commitlint/core/src/load.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ test('uses seed as configured', async t => {
1616
t.is(actual.rules.foo, 'bar');
1717
});
1818

19+
test('rules should be loaded from specify config file', async t => {
20+
const file = 'config/commitlint.config.js';
21+
const cwd = await git.bootstrap('fixtures/specify-config-file');
22+
const actual = await load({}, {cwd, file});
23+
t.is(actual.rules.foo, 'bar');
24+
});
25+
1926
test('uses seed with parserPreset', async t => {
2027
const cwd = await git.bootstrap('fixtures/parser-preset');
2128
const {parserPreset: actual} = await load(

0 commit comments

Comments
 (0)