Skip to content

Commit 3b3667a

Browse files
authored
feat: check stage before entering prompt (#495)
* feat(prompt-cli): check stage before prompt #51 * feat(prompt-cli): add test #51 * chore: linting * fix: add missing dev dependencies * chore: ensure Node.js 6 compat
1 parent f27e7ac commit 3b3667a

File tree

7 files changed

+876
-145
lines changed

7 files changed

+876
-145
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module.exports = function (report) {
1+
module.exports = function(_report) {
22
return 'custom-formatter-ok';
3-
}
3+
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module.exports = function (report) {
1+
module.exports = function(_report) {
22
return 'ok';
3-
}
3+
};

@commitlint/load/src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
5454

5555
// Resolve config-relative formatter module
5656
if (typeof config.formatter === 'string') {
57-
preset.formatter = resolveFrom.silent(base, config.formatter) || config.formatter;
57+
preset.formatter =
58+
resolveFrom.silent(base, config.formatter) || config.formatter;
5859
}
5960

6061
// Execute rule config functions if needed

@commitlint/prompt-cli/cli.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
const execa = require('execa');
33
const meow = require('meow');
4-
const prompter = require('@commitlint/prompt').prompter;
4+
const {prompter} = require('@commitlint/prompt');
55

66
const HELP = `
77
Usage
@@ -18,7 +18,20 @@ main(meow(HELP)).catch(err => {
1818
});
1919

2020
function main() {
21-
return prompt();
21+
return isStageEmpty()
22+
.then(empty => {
23+
if (empty) {
24+
console.log(
25+
`Nothing to commit. Stage your changes via "git add" execute "commit" again`
26+
);
27+
process.exit(1);
28+
}
29+
})
30+
.then(() => prompt());
31+
}
32+
33+
function isStageEmpty() {
34+
return execa('git', ['diff', '--cached']).then(r => r.stdout === '');
2235
}
2336

2437
function commit(message) {

@commitlint/prompt-cli/cli.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import path from 'path';
2+
import {git} from '@commitlint/test';
3+
import test from 'ava';
4+
import execa from 'execa';
5+
import stream from 'string-to-stream';
6+
7+
const bin = path.join(__dirname, './cli.js');
8+
9+
const cli = (args, options) => {
10+
return (input = '') => {
11+
const c = execa(bin, args, {
12+
capture: ['stdout'],
13+
cwd: options.cwd,
14+
env: options.env
15+
});
16+
stream(input).pipe(c.stdin);
17+
return c.catch(err => err);
18+
};
19+
};
20+
21+
test('should print warning if stage is empty', async t => {
22+
const cwd = await git.bootstrap();
23+
const actual = await cli([], {cwd})('foo: bar');
24+
t.true(actual.stdout.includes('Nothing to commit.'));
25+
});

@commitlint/prompt-cli/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"commit": "$npm_package_bin_commit",
1414
"deps": "dep-check",
1515
"pkg": "pkg-check --skip-main",
16-
"lint": "xo"
16+
"lint": "xo",
17+
"test": "ava -c 4 --verbose"
1718
},
1819
"xo": false,
1920
"repository": {
@@ -31,12 +32,15 @@
3132
},
3233
"homepage": "https://github.com/marionebl/commitlint#readme",
3334
"devDependencies": {
35+
"@commitlint/test": "^7.1.2",
3436
"@commitlint/utils": "^7.1.2",
37+
"ava": "^0.25.0",
3538
"xo": "0.20.3"
3639
},
3740
"dependencies": {
3841
"@commitlint/prompt": "^7.2.1",
3942
"execa": "0.9.0",
40-
"meow": "3.7.0"
43+
"meow": "3.7.0",
44+
"string-to-stream": "^1.1.1"
4145
}
4246
}

0 commit comments

Comments
 (0)