Skip to content

Commit c881433

Browse files
satazormarionebl
authored andcommitted
feat: edit flag now accepts the path to the commit file
Closes #40.
1 parent 97ccf2b commit c881433

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

@commitlint/cli/cli.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const rules = {
2323
};
2424

2525
const configuration = {
26-
string: ['cwd', 'from', 'to', 'extends', 'parser-preset'],
27-
boolean: ['edit', 'help', 'version', 'quiet', 'color'],
26+
string: ['cwd', 'from', 'to', 'edit', 'extends', 'parser-preset'],
27+
boolean: ['help', 'version', 'quiet', 'color'],
2828
alias: {
2929
c: 'color',
3030
d: 'cwd',
@@ -40,7 +40,8 @@ const configuration = {
4040
description: {
4141
color: 'toggle colored output',
4242
cwd: 'directory to execute in',
43-
edit: 'read last commit message found in ./.git/COMMIT_EDITMSG',
43+
edit:
44+
'read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG',
4445
extends: 'array of shareable configurations to extend',
4546
from: 'lower end of the commit range to lint; applies if edit=false',
4647
to: 'upper end of the commit range to lint; applies if edit=false',
@@ -74,6 +75,8 @@ const cli = meow(
7475
const load = (seed, opts) => core.load(seed, opts);
7576

7677
function main(options) {
78+
normalizeOptions(options);
79+
7780
const raw = options.input;
7881
const flags = options.flags;
7982
const fromStdin = rules.fromStdin(raw, flags);
@@ -113,6 +116,17 @@ function main(options) {
113116
);
114117
}
115118

119+
function normalizeOptions(options) {
120+
const flags = options.flags;
121+
122+
// The `edit` flag is either a boolean or a string but we are only allowed
123+
// to specify one of them in minimist
124+
if (flags.edit === '') {
125+
flags.edit = true;
126+
flags.e = true;
127+
}
128+
}
129+
116130
function getSeed(seed) {
117131
const e = Array.isArray(seed.extends) ? seed.extends : [seed.extends];
118132
const n = e.filter(i => typeof i === 'string');

@commitlint/core/src/read.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function getCommitMessages(settings) {
1919
const {cwd, from, to, edit} = settings;
2020

2121
if (edit) {
22-
return getEditCommit(cwd);
22+
return getEditCommit(cwd, edit);
2323
}
2424

2525
if (await isShallow(cwd)) {
@@ -57,15 +57,19 @@ async function isShallow(cwd) {
5757
}
5858

5959
// Get recently edited commit message
60-
// (cwd: string) => Promise<Array<String>>
61-
async function getEditCommit(cwd) {
60+
// (cwd: string, edit: any) => Promise<Array<String>>
61+
async function getEditCommit(cwd, edit) {
6262
const top = await toplevel(cwd);
6363

6464
if (typeof top !== 'string') {
6565
throw new TypeError(`Could not find git root from ${cwd}`);
6666
}
6767

68-
const editFilePath = path.join(top, '.git/COMMIT_EDITMSG');
68+
const editFilePath =
69+
typeof edit === 'string'
70+
? path.resolve(top, edit)
71+
: path.join(top, '.git/COMMIT_EDITMSG');
72+
6973
const editFile = await sander.readFile(editFilePath);
7074
return [`${editFile.toString('utf-8')}\n`];
7175
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import * as sander from '@marionebl/sander';
66
import pkg from '../package';
77
import read from './read';
88

9+
test('get edit commit message specified by the `edit` flag', async t => {
10+
const cwd = await git.bootstrap();
11+
12+
await sander.writeFile(cwd, 'commit-msg-file', 'foo');
13+
14+
const expected = ['foo\n'];
15+
const actual = await read({edit: 'commit-msg-file', cwd});
16+
t.deepEqual(actual, expected);
17+
});
18+
919
test('get edit commit message from git root', async t => {
1020
const cwd = await git.bootstrap();
1121

0 commit comments

Comments
 (0)