Skip to content

feat: edit flag now accepts the path to the commit file #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 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,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);
Expand Down Expand Up @@ -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');
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
9 changes: 8 additions & 1 deletion docs/reference-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>
Expand All @@ -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']
Expand Down
28 changes: 11 additions & 17 deletions docs/reference-cli.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
# CLI

## Installation

```bash
npm install --save-dev @commitlint/cli
```
❯ npx commitlint --help

## Usage

```bash
❯ commitlint --help
commitlint - Lint commit messages
[email protected] - 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
```