From f361c465f87078467b597804a5d5b1a937ccc3c3 Mon Sep 17 00:00:00 2001 From: Tapani Moilanen Date: Wed, 8 Jun 2016 19:02:54 +0300 Subject: [PATCH] fix(commit): fix windows by separating add and commit exec (#55) Windows cmd doesn't support separating commands with ';'. Fix by separating the execution of the 'git add' and 'git commit' commands. Added separate test for git add. Fixes #49 --- index.js | 16 +++++++++++----- test.js | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 5bec43ea6..3c593d857 100755 --- a/index.js +++ b/index.js @@ -121,15 +121,21 @@ function commit (argv, newVersion, cb) { args.unshift('package.json') } checkpoint(msg, args) - exec('git add package.json ' + argv.infile + ';git commit ' + verify + (argv.sign ? '-S ' : '') + 'package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) { - var errMessage = null - if (err) errMessage = err.message - if (stderr) errMessage = stderr + + function handleExecError (err, stderr) { + // If exec returns an error or content in stderr, log it and exit with return code 1 + var errMessage = stderr || (err && err.message) if (errMessage) { console.log(chalk.red(errMessage)) process.exit(1) } - return cb() + } + exec('git add package.json ' + argv.infile, function (err, stdout, stderr) { + handleExecError(err, stderr) + exec('git commit ' + verify + (argv.sign ? '-S ' : '') + 'package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) { + handleExecError(err, stderr) + return cb() + }) }) } diff --git a/test.js b/test.js index b0c41292e..929e11329 100644 --- a/test.js +++ b/test.js @@ -118,6 +118,20 @@ describe('cli', function () { }) }) + it('exits with error code if git add fails', function () { + // mock git by throwing on attempt to add + return mockGit('console.error("addition is hard"); process.exit(128);', 'add') + .then(function (unmock) { + writePackageJson('1.0.0') + + var result = shell.exec(cliPath) + result.code.should.equal(1) + result.stdout.should.match(/addition is hard/) + + unmock() + }) + }) + it('exits with error code if git tag fails', function () { // mock git by throwing on attempt to commit return mockGit('console.error("tag, you\'re it"); process.exit(128);', 'tag')