diff --git a/@commitlint/cli/cli.js b/@commitlint/cli/cli.js index 27edcdbeeb..7478e6ce16 100755 --- a/@commitlint/cli/cli.js +++ b/@commitlint/cli/cli.js @@ -23,8 +23,8 @@ const rules = { }; const configuration = { - string: ['cwd', 'from', 'to', 'extends', 'parser-preset'], - boolean: ['edit', 'help', 'version', 'quiet', 'color'], + string: ['cwd', 'from', 'to', 'edit', 'extends', 'parser-preset'], + boolean: ['help', 'version', 'quiet', 'color'], alias: { c: 'color', d: 'cwd', @@ -40,7 +40,8 @@ const configuration = { description: { color: 'toggle colored output', cwd: 'directory to execute in', - edit: 'read last commit message found in ./.git/COMMIT_EDITMSG', + edit: + 'read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG', extends: 'array of shareable configurations to extend', from: 'lower end of the commit range to lint; applies if edit=false', to: 'upper end of the commit range to lint; applies if edit=false', @@ -74,6 +75,8 @@ const cli = meow( const load = (seed, opts) => core.load(seed, opts); function main(options) { + normalizeOptions(options); + const raw = options.input; const flags = options.flags; const fromStdin = rules.fromStdin(raw, flags); @@ -113,6 +116,17 @@ function main(options) { ); } +function normalizeOptions(options) { + const flags = options.flags; + + // The `edit` flag is either a boolean or a string but we are only allowed + // to specify one of them in minimist + if (flags.edit === '') { + flags.edit = true; + flags.e = true; + } +} + function getSeed(seed) { const e = Array.isArray(seed.extends) ? seed.extends : [seed.extends]; const n = e.filter(i => typeof i === 'string'); diff --git a/@commitlint/core/src/read.js b/@commitlint/core/src/read.js index 7f11878000..642da86e3f 100644 --- a/@commitlint/core/src/read.js +++ b/@commitlint/core/src/read.js @@ -19,7 +19,7 @@ async function getCommitMessages(settings) { const {cwd, from, to, edit} = settings; if (edit) { - return getEditCommit(cwd); + return getEditCommit(cwd, edit); } if (await isShallow(cwd)) { @@ -57,15 +57,19 @@ async function isShallow(cwd) { } // Get recently edited commit message -// (cwd: string) => Promise> -async function getEditCommit(cwd) { +// (cwd: string, edit: any) => Promise> +async function getEditCommit(cwd, edit) { const top = await toplevel(cwd); if (typeof top !== 'string') { throw new TypeError(`Could not find git root from ${cwd}`); } - const editFilePath = path.join(top, '.git/COMMIT_EDITMSG'); + const editFilePath = + typeof edit === 'string' + ? path.resolve(top, edit) + : path.join(top, '.git/COMMIT_EDITMSG'); + const editFile = await sander.readFile(editFilePath); return [`${editFile.toString('utf-8')}\n`]; } diff --git a/@commitlint/core/src/read.test.js b/@commitlint/core/src/read.test.js index 958074d735..fcc802baae 100644 --- a/@commitlint/core/src/read.test.js +++ b/@commitlint/core/src/read.test.js @@ -6,6 +6,16 @@ import * as sander from '@marionebl/sander'; import pkg from '../package'; import read from './read'; +test('get edit commit message specified by the `edit` flag', async t => { + const cwd = await git.bootstrap(); + + await sander.writeFile(cwd, 'commit-msg-file', 'foo'); + + const expected = ['foo\n']; + const actual = await read({edit: 'commit-msg-file', cwd}); + t.deepEqual(actual, expected); +}); + test('get edit commit message from git root', async t => { const cwd = await git.bootstrap();