Skip to content

Commit

Permalink
feat: edit flag now accepts the path to the commit file
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Oct 13, 2017
1 parent 97ccf2b commit ddba66f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
21 changes: 17 additions & 4 deletions @commitlint/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -74,10 +75,11 @@ 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);

const range = pick(flags, 'edit', 'from', 'to');
const input = fromStdin ? stdin() : core.read(range, {cwd: flags.cwd});
const fmt = new chalk.constructor({enabled: flags.color});
Expand Down Expand Up @@ -113,6 +115,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');
Expand Down
12 changes: 8 additions & 4 deletions @commitlint/core/src/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -57,15 +57,19 @@ async function isShallow(cwd) {
}

// Get recently edited commit message
// (cwd: string) => Promise<Array<String>>
async function getEditCommit(cwd) {
// (cwd: string, edit: any) => Promise<Array<String>>
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`];
}
10 changes: 10 additions & 0 deletions @commitlint/core/src/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit ddba66f

Please sign in to comment.