Skip to content

feat(prompt-cli): check stage before prompt #51 #461

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

Closed
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
16 changes: 14 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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is due to linting


const HELP = `
Usage
Expand All @@ -17,10 +17,22 @@ main(meow(HELP)).catch(err => {
});
});

function main() {
async function main() {
if (await isStageEmpty()) {
console.log(
`Nothing to commit. Stage your changes via "git add" execute "commit" again`
);
process.exit(1);
}

return prompt();
}

async function isStageEmpty() {
const result = await execa('git', ['diff', '--cached']);
return result.stdout === '';
}

function commit(message) {
const c = execa('git', ['commit', '-m', message]);
c.stdout.pipe(process.stdout);
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.'));
});
6 changes: 4 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 @@ -37,6 +38,7 @@
"dependencies": {
"@commitlint/prompt": "^7.2.1",
"execa": "0.9.0",
"meow": "3.7.0"
"meow": "3.7.0",
"string-to-stream": "^1.1.1"
}
}