Skip to content
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

feat: check stage before entering prompt #495

Merged
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
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