Skip to content

Commit

Permalink
feat: add "--config" option (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft authored and marionebl committed Feb 3, 2018
1 parent 73fd0fd commit 2c03ec6
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'header-max-length': [2, 'always', 4]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'type-enum': [2, 'never', ['foo']]
}
};
8 changes: 6 additions & 2 deletions @commitlint/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const pkg = require('../package');
const help = require('./help');

const configuration = {
string: ['cwd', 'from', 'to', 'edit', 'extends', 'parser-preset'],
string: ['cwd', 'from', 'to', 'edit', 'extends', 'parser-preset', 'config'],
boolean: ['help', 'version', 'quiet', 'color'],
alias: {
c: 'color',
Expand All @@ -22,13 +22,15 @@ const configuration = {
t: 'to',
q: 'quiet',
h: 'help',
g: 'config',
v: 'version',
x: 'extends',
p: 'parser-preset'
},
description: {
color: 'toggle colored output',
cwd: 'directory to execute in',
config: 'path to the config file',
edit:
'read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG',
extends: 'array of shareable configurations to extend',
Expand All @@ -41,6 +43,7 @@ const configuration = {
default: {
color: true,
cwd: process.cwd(),
config: null,
edit: false,
from: null,
to: null,
Expand Down Expand Up @@ -96,7 +99,8 @@ async function main(options) {
throw err;
}

const loaded = await core.load(getSeed(flags), {cwd: flags.cwd});
const loadOpts = {cwd: flags.cwd, file: flags.config};
const loaded = await core.load(getSeed(flags), loadOpts);
const parserOpts = selectParserOpts(loaded.parserPreset);
const opts = parserOpts ? {parserOpts} : {parserOpts: {}};

Expand Down
8 changes: 8 additions & 0 deletions @commitlint/cli/src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ test('should fail for input from stdin with rule from rc', async t => {
t.is(actual.code, 1);
});

test('should work with --config option', async t => {
const file = 'config/commitlint.config.js';
const cwd = await git.bootstrap('fixtures/specify-config-file');
const actual = await cli(['--config', file], {cwd})('foo: bar');
t.true(actual.stdout.includes('type must not be one of [foo]'));
t.is(actual.code, 1);
});

test('should fail for input from stdin with rule from js', async t => {
const cwd = await git.bootstrap('fixtures/extends-root');
const actual = await cli(['--extends', './extended'], {cwd})('foo: bar');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
rules: {
'foo': 'hello',
'bar': 'world'
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'foo': 'bar'
}
};
7 changes: 4 additions & 3 deletions @commitlint/core/src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const w = (a, b) => (Array.isArray(b) ? b : undefined);
const valid = input => pick(input, 'extends', 'rules', 'parserPreset');

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

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

async function loadConfig(cwd) {
async function loadConfig(cwd, configPath) {
const explorer = cosmiconfig('commitlint', {
rcExtensions: true
rcExtensions: true,
configPath: configPath ? path.resolve(cwd, configPath) : null
});

const local = await explorer.load(cwd);
Expand Down
7 changes: 7 additions & 0 deletions @commitlint/core/src/load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ test('uses seed as configured', async t => {
t.is(actual.rules.foo, 'bar');
});

test('rules should be loaded from specify config file', async t => {
const file = 'config/commitlint.config.js';
const cwd = await git.bootstrap('fixtures/specify-config-file');
const actual = await load({}, {cwd, file});
t.is(actual.rules.foo, 'bar');
});

test('uses seed with parserPreset', async t => {
const cwd = await git.bootstrap('fixtures/parser-preset');
const {parserPreset: actual} = await load(
Expand Down

0 comments on commit 2c03ec6

Please sign in to comment.