Skip to content

Commit

Permalink
feat: check stage before entering prompt (#495)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
marionebl authored Nov 25, 2018
1 parent f27e7ac commit 3b3667a
Show file tree
Hide file tree
Showing 7 changed files with 876 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function (report) {
module.exports = function(_report) {
return 'custom-formatter-ok';
}
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function (report) {
module.exports = function(_report) {
return 'ok';
}
};
3 changes: 2 additions & 1 deletion @commitlint/load/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {

// Resolve config-relative formatter module
if (typeof config.formatter === 'string') {
preset.formatter = resolveFrom.silent(base, config.formatter) || config.formatter;
preset.formatter =
resolveFrom.silent(base, config.formatter) || config.formatter;
}

// Execute rule config functions if needed
Expand Down
17 changes: 15 additions & 2 deletions @commitlint/prompt-cli/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
const execa = require('execa');
const meow = require('meow');
const prompter = require('@commitlint/prompt').prompter;
const {prompter} = require('@commitlint/prompt');

const HELP = `
Usage
Expand All @@ -18,7 +18,20 @@ main(meow(HELP)).catch(err => {
});

function main() {
return prompt();
return isStageEmpty()
.then(empty => {
if (empty) {
console.log(
`Nothing to commit. Stage your changes via "git add" execute "commit" again`
);
process.exit(1);
}
})
.then(() => prompt());
}

function isStageEmpty() {
return execa('git', ['diff', '--cached']).then(r => r.stdout === '');
}

function commit(message) {
Expand Down
25 changes: 25 additions & 0 deletions @commitlint/prompt-cli/cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import path from 'path';
import {git} from '@commitlint/test';
import test from 'ava';
import execa from 'execa';
import stream from 'string-to-stream';

const bin = path.join(__dirname, './cli.js');

const cli = (args, options) => {
return (input = '') => {
const c = execa(bin, args, {
capture: ['stdout'],
cwd: options.cwd,
env: options.env
});
stream(input).pipe(c.stdin);
return c.catch(err => err);
};
};

test('should print warning if stage is empty', async t => {
const cwd = await git.bootstrap();
const actual = await cli([], {cwd})('foo: bar');
t.true(actual.stdout.includes('Nothing to commit.'));
});
8 changes: 6 additions & 2 deletions @commitlint/prompt-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"commit": "$npm_package_bin_commit",
"deps": "dep-check",
"pkg": "pkg-check --skip-main",
"lint": "xo"
"lint": "xo",
"test": "ava -c 4 --verbose"
},
"xo": false,
"repository": {
Expand All @@ -31,12 +32,15 @@
},
"homepage": "https://github.com/marionebl/commitlint#readme",
"devDependencies": {
"@commitlint/test": "^7.1.2",
"@commitlint/utils": "^7.1.2",
"ava": "^0.25.0",
"xo": "0.20.3"
},
"dependencies": {
"@commitlint/prompt": "^7.2.1",
"execa": "0.9.0",
"meow": "3.7.0"
"meow": "3.7.0",
"string-to-stream": "^1.1.1"
}
}
Loading

0 comments on commit 3b3667a

Please sign in to comment.