From 40993cad9350e624fd85e38e67ffdbc87914f1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Fri, 13 Oct 2017 20:31:36 +0100 Subject: [PATCH 1/2] feat: edit flag now accepts the path to the commit file Closes #40. --- @commitlint/cli/cli.js | 20 +++++++++++++++++--- @commitlint/core/src/read.js | 12 ++++++++---- @commitlint/core/src/read.test.js | 10 ++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) 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(); From 5a94e47e08d28d964a53cc371e97ed2d701c54cd Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Sat, 14 Oct 2017 09:53:59 +0200 Subject: [PATCH 2/2] docs: add string variant of .edit --- docs/reference-api.md | 9 ++++++++- docs/reference-cli.md | 28 +++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/docs/reference-api.md b/docs/reference-api.md index 478720f4fa..1ba2ec1728 100644 --- a/docs/reference-api.md +++ b/docs/reference-api.md @@ -213,9 +213,12 @@ load({parserPreset: './parser-preset.js'}) ```ts type Range = { + /* Lower end of the commit range to read */ from: string; + /* Upper end of the commit range to read */ to: string; - edit?: boolean; + /* Wether (boolean) to read from ./.git/COMMIT_EDITMSG or where to read from (string) */ + edit?: boolean | string; }; read(range: Range) => Promise @@ -231,6 +234,10 @@ read({edit: true}) .then(messages => console.log(messages)); // => ['I did something\n\n'] +read({edit: './git/GITGUI_EDITMESSAGE'}) + .then(messages => console.log(messages)); +// => ['I did something via git gui\n\n'] + read({from: 'HEAD~2'}) .then(messages => console.log(messages)); // => ['I did something\n\n', 'Initial commit\n\n'] diff --git a/docs/reference-cli.md b/docs/reference-cli.md index b75870c1e0..f3697d8e51 100644 --- a/docs/reference-cli.md +++ b/docs/reference-cli.md @@ -1,23 +1,17 @@ # CLI -## Installation - ```bash -npm install --save-dev @commitlint/cli -``` +❯ npx commitlint --help -## Usage - -```bash -❯ commitlint --help - commitlint - Lint commit messages +commitlint@4.2.0 - Lint your commit messages - [input] reads from stdin if --edit, --from, --to are omitted - --color, -c toggle formatted output, defaults to: true - --edit, -e read last commit message found in ./.git/COMMIT_EDITMSG - --extends, -x array of resolvable ids pointing to shareable configurations to extend - --from, -f lower end of the commit range to lint; applies if edit=false - --to, -t upper end of the commit range to lint; applies if edit=false - --quiet, -q toggle console output - --parser-preset, -p resolvable id to load and pass to conventional-commits-parser + [input] reads from stdin if --edit, --from and --to are omitted + --color, -c toggle colored output, defaults to: true + --cwd, -d directory to execute in, defaults to: /Users/marneb/Documents/oss/commitlint + --edit, -e read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG + --extends, -x array of shareable configurations to extend + --from, -f lower end of the commit range to lint; applies if edit=false + --to, -t upper end of the commit range to lint; applies if edit=false + --quiet, -q toggle console output + --parser-preset, -p configuration preset to use for conventional-commits-parser ```