From 7fdf045d8ae3bdffcbeb0ed8d1cd253ab24f0eba Mon Sep 17 00:00:00 2001 From: Tapani Moilanen Date: Wed, 8 Jun 2016 01:48:21 +0300 Subject: [PATCH] fix(commit): fix windows commits by separating git add and commit execution 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 c041469ec..1beff92f1 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')