From 2c03ec66d07f10e4a3b5873cce560fb7b5b0a920 Mon Sep 17 00:00:00 2001 From: Liu Date: Sat, 3 Feb 2018 19:47:58 +0800 Subject: [PATCH] feat: add "--config" option (#261) --- .../cli/fixtures/specify-config-file/commitlint.config.js | 5 +++++ .../specify-config-file/config/commitlint.config.js | 5 +++++ @commitlint/cli/src/cli.js | 8 ++++++-- @commitlint/cli/src/cli.test.js | 8 ++++++++ .../fixtures/specify-config-file/commitlint.config.js | 6 ++++++ .../specify-config-file/config/commitlint.config.js | 5 +++++ @commitlint/core/src/load.js | 7 ++++--- @commitlint/core/src/load.test.js | 7 +++++++ 8 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 @commitlint/cli/fixtures/specify-config-file/commitlint.config.js create mode 100644 @commitlint/cli/fixtures/specify-config-file/config/commitlint.config.js create mode 100644 @commitlint/core/fixtures/specify-config-file/commitlint.config.js create mode 100644 @commitlint/core/fixtures/specify-config-file/config/commitlint.config.js diff --git a/@commitlint/cli/fixtures/specify-config-file/commitlint.config.js b/@commitlint/cli/fixtures/specify-config-file/commitlint.config.js new file mode 100644 index 0000000000..5f9c52dd7d --- /dev/null +++ b/@commitlint/cli/fixtures/specify-config-file/commitlint.config.js @@ -0,0 +1,5 @@ +module.exports = { + rules: { + 'header-max-length': [2, 'always', 4] + } +}; diff --git a/@commitlint/cli/fixtures/specify-config-file/config/commitlint.config.js b/@commitlint/cli/fixtures/specify-config-file/config/commitlint.config.js new file mode 100644 index 0000000000..b6775fef5b --- /dev/null +++ b/@commitlint/cli/fixtures/specify-config-file/config/commitlint.config.js @@ -0,0 +1,5 @@ +module.exports = { + rules: { + 'type-enum': [2, 'never', ['foo']] + } +}; diff --git a/@commitlint/cli/src/cli.js b/@commitlint/cli/src/cli.js index 2b0b9d8c10..1e5e75761b 100755 --- a/@commitlint/cli/src/cli.js +++ b/@commitlint/cli/src/cli.js @@ -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', @@ -22,6 +22,7 @@ const configuration = { t: 'to', q: 'quiet', h: 'help', + g: 'config', v: 'version', x: 'extends', p: 'parser-preset' @@ -29,6 +30,7 @@ const configuration = { 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', @@ -41,6 +43,7 @@ const configuration = { default: { color: true, cwd: process.cwd(), + config: null, edit: false, from: null, to: null, @@ -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: {}}; diff --git a/@commitlint/cli/src/cli.test.js b/@commitlint/cli/src/cli.test.js index 9ca1b98ae2..4f54be168b 100644 --- a/@commitlint/cli/src/cli.test.js +++ b/@commitlint/cli/src/cli.test.js @@ -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'); diff --git a/@commitlint/core/fixtures/specify-config-file/commitlint.config.js b/@commitlint/core/fixtures/specify-config-file/commitlint.config.js new file mode 100644 index 0000000000..5ff20b8d45 --- /dev/null +++ b/@commitlint/core/fixtures/specify-config-file/commitlint.config.js @@ -0,0 +1,6 @@ +module.exports = { + rules: { + 'foo': 'hello', + 'bar': 'world' + } +}; diff --git a/@commitlint/core/fixtures/specify-config-file/config/commitlint.config.js b/@commitlint/core/fixtures/specify-config-file/config/commitlint.config.js new file mode 100644 index 0000000000..cc32dc37ab --- /dev/null +++ b/@commitlint/core/fixtures/specify-config-file/config/commitlint.config.js @@ -0,0 +1,5 @@ +module.exports = { + rules: { + 'foo': 'bar' + } +}; diff --git a/@commitlint/core/src/load.js b/@commitlint/core/src/load.js index 0750b2e8f3..c552902edb 100644 --- a/@commitlint/core/src/load.js +++ b/@commitlint/core/src/load.js @@ -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 @@ -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); diff --git a/@commitlint/core/src/load.test.js b/@commitlint/core/src/load.test.js index 3d9569aa09..8c33429721 100644 --- a/@commitlint/core/src/load.test.js +++ b/@commitlint/core/src/load.test.js @@ -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(