Skip to content

Commit

Permalink
fix(err): don't fail on stderr output, but print the output to stderr (
Browse files Browse the repository at this point in the history
…#110)

* test: add test for not exiting on stderr content and match stderr output

* feat: don't fail fast on stderr content, and print stderr, closes #91

* fix: print warnings in yellow instead of red

* style: simplify handleExecError if logic
  • Loading branch information
Tapppi authored and stevemao committed Sep 25, 2016
1 parent d5d5e9e commit f7a4915
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
31 changes: 14 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ function outputChangelog (argv, cb) {
})
}

function handleExecError (err, stderr) {
// If exec returns content in stderr, but no error, print it as a warning
// If exec returns an error, print it and exit with return code 1
if (err) {
console.error(chalk.red(stderr || err.message))
process.exit(1)
} else if (stderr) {
console.warn(chalk.yellow(stderr))
}
}

function commit (argv, newVersion, cb) {
var msg = 'committing %s'
var args = [argv.infile]
Expand All @@ -118,14 +129,6 @@ function commit (argv, newVersion, cb) {
}
checkpoint(msg, args)

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)
}
}
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) {
Expand All @@ -148,17 +151,11 @@ function tag (newVersion, argv) {
}
checkpoint('tagging release %s', [newVersion])
exec('git tag ' + tagOption + 'v' + newVersion + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
handleExecError(err, stderr)
var message = 'git push --follow-tags origin master'
var errMessage = null
if (err) errMessage = err.message
if (stderr) errMessage = stderr
if (pkg.private !== true) message += '; npm publish'
if (errMessage) {
console.log(chalk.red(errMessage))
process.exit(1)
} else {
checkpoint('Run `%s` to publish', [message], chalk.blue(figures.info))
}

checkpoint('Run `%s` to publish', [message], chalk.blue(figures.info))
})
}

Expand Down
20 changes: 17 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('cli', function () {

var result = execCli()
result.code.should.equal(1)
result.stdout.should.match(/commit yourself/)
result.stderr.should.match(/commit yourself/)

unmock()
})
Expand All @@ -131,7 +131,7 @@ describe('cli', function () {

var result = execCli()
result.code.should.equal(1)
result.stdout.should.match(/addition is hard/)
result.stderr.should.match(/addition is hard/)

unmock()
})
Expand All @@ -145,7 +145,21 @@ describe('cli', function () {

var result = execCli()
result.code.should.equal(1)
result.stdout.should.match(/tag, you're it/)
result.stderr.should.match(/tag, you're it/)

unmock()
})
})

it('doesn\'t fail fast on stderr output from git', function () {
// mock git by throwing on attempt to commit
return mockGit('console.error("haha, kidding, this is just a warning"); process.exit(0);', 'add')
.then(function (unmock) {
writePackageJson('1.0.0')

var result = execCli()
result.code.should.equal(0)
result.stderr.should.match(/haha, kidding, this is just a warning/)

unmock()
})
Expand Down

0 comments on commit f7a4915

Please sign in to comment.