From 5eb8bcf43b64591fc6c2920bfae7cb7d1b8829c0 Mon Sep 17 00:00:00 2001 From: escapedcat Date: Fri, 5 Oct 2018 16:11:17 +0200 Subject: [PATCH 1/3] feat(prompt-cli): check stage before prompt #51 --- @commitlint/prompt-cli/cli.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/@commitlint/prompt-cli/cli.js b/@commitlint/prompt-cli/cli.js index 3cbcef009b..15b5c66dc2 100755 --- a/@commitlint/prompt-cli/cli.js +++ b/@commitlint/prompt-cli/cli.js @@ -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 @@ -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); From 72fb3e491c4bda83ea3a1622081ae2ad47246691 Mon Sep 17 00:00:00 2001 From: escapedcat Date: Thu, 8 Nov 2018 16:31:20 +0100 Subject: [PATCH 2/3] feat(prompt-cli): add test #51 --- @commitlint/prompt-cli/cli.test.js | 25 +++++++++++++++++++++++++ @commitlint/prompt-cli/package.json | 8 ++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 @commitlint/prompt-cli/cli.test.js diff --git a/@commitlint/prompt-cli/cli.test.js b/@commitlint/prompt-cli/cli.test.js new file mode 100644 index 0000000000..34c27b3e4e --- /dev/null +++ b/@commitlint/prompt-cli/cli.test.js @@ -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.')); +}); diff --git a/@commitlint/prompt-cli/package.json b/@commitlint/prompt-cli/package.json index ac5fe5fd12..272d3ff6cc 100644 --- a/@commitlint/prompt-cli/package.json +++ b/@commitlint/prompt-cli/package.json @@ -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": { @@ -36,7 +37,10 @@ }, "dependencies": { "@commitlint/prompt": "^7.2.0", + "@commitlint/test": "^7.1.2", + "ava": "^0.25.0", "execa": "0.9.0", - "meow": "3.7.0" + "meow": "3.7.0", + "string-to-stream": "^1.1.1" } } From ccc157069d7bd18e57b8186cb4139d5910d53e1a Mon Sep 17 00:00:00 2001 From: escapedcat Date: Sun, 18 Nov 2018 14:32:15 +0100 Subject: [PATCH 3/3] chore: linting --- .../cli/fixtures/custom-formatter/formatters/custom.js | 4 ++-- .../load/fixtures/formatter-local-module/formatters/custom.js | 4 ++-- @commitlint/load/src/index.js | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/@commitlint/cli/fixtures/custom-formatter/formatters/custom.js b/@commitlint/cli/fixtures/custom-formatter/formatters/custom.js index 96b56d55e4..8f56d1384f 100644 --- a/@commitlint/cli/fixtures/custom-formatter/formatters/custom.js +++ b/@commitlint/cli/fixtures/custom-formatter/formatters/custom.js @@ -1,3 +1,3 @@ -module.exports = function (report) { +module.exports = function(_report) { return 'custom-formatter-ok'; -} +}; diff --git a/@commitlint/load/fixtures/formatter-local-module/formatters/custom.js b/@commitlint/load/fixtures/formatter-local-module/formatters/custom.js index 7fb04a7d0c..0b5f961820 100644 --- a/@commitlint/load/fixtures/formatter-local-module/formatters/custom.js +++ b/@commitlint/load/fixtures/formatter-local-module/formatters/custom.js @@ -1,3 +1,3 @@ -module.exports = function (report) { +module.exports = function(_report) { return 'ok'; -} +}; diff --git a/@commitlint/load/src/index.js b/@commitlint/load/src/index.js index f7b900f9ca..2ef93829bc 100644 --- a/@commitlint/load/src/index.js +++ b/@commitlint/load/src/index.js @@ -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