Skip to content

Commit f7a4915

Browse files
Tapppistevemao
authored andcommitted
fix(err): don't fail on stderr output, but print the output to stderr (#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
1 parent d5d5e9e commit f7a4915

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ function outputChangelog (argv, cb) {
108108
})
109109
}
110110

111+
function handleExecError (err, stderr) {
112+
// If exec returns content in stderr, but no error, print it as a warning
113+
// If exec returns an error, print it and exit with return code 1
114+
if (err) {
115+
console.error(chalk.red(stderr || err.message))
116+
process.exit(1)
117+
} else if (stderr) {
118+
console.warn(chalk.yellow(stderr))
119+
}
120+
}
121+
111122
function commit (argv, newVersion, cb) {
112123
var msg = 'committing %s'
113124
var args = [argv.infile]
@@ -118,14 +129,6 @@ function commit (argv, newVersion, cb) {
118129
}
119130
checkpoint(msg, args)
120131

121-
function handleExecError (err, stderr) {
122-
// If exec returns an error or content in stderr, log it and exit with return code 1
123-
var errMessage = stderr || (err && err.message)
124-
if (errMessage) {
125-
console.log(chalk.red(errMessage))
126-
process.exit(1)
127-
}
128-
}
129132
exec('git add package.json ' + argv.infile, function (err, stdout, stderr) {
130133
handleExecError(err, stderr)
131134
exec('git commit ' + verify + (argv.sign ? '-S ' : '') + 'package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
@@ -148,17 +151,11 @@ function tag (newVersion, argv) {
148151
}
149152
checkpoint('tagging release %s', [newVersion])
150153
exec('git tag ' + tagOption + 'v' + newVersion + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
154+
handleExecError(err, stderr)
151155
var message = 'git push --follow-tags origin master'
152-
var errMessage = null
153-
if (err) errMessage = err.message
154-
if (stderr) errMessage = stderr
155156
if (pkg.private !== true) message += '; npm publish'
156-
if (errMessage) {
157-
console.log(chalk.red(errMessage))
158-
process.exit(1)
159-
} else {
160-
checkpoint('Run `%s` to publish', [message], chalk.blue(figures.info))
161-
}
157+
158+
checkpoint('Run `%s` to publish', [message], chalk.blue(figures.info))
162159
})
163160
}
164161

test.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('cli', function () {
117117

118118
var result = execCli()
119119
result.code.should.equal(1)
120-
result.stdout.should.match(/commit yourself/)
120+
result.stderr.should.match(/commit yourself/)
121121

122122
unmock()
123123
})
@@ -131,7 +131,7 @@ describe('cli', function () {
131131

132132
var result = execCli()
133133
result.code.should.equal(1)
134-
result.stdout.should.match(/addition is hard/)
134+
result.stderr.should.match(/addition is hard/)
135135

136136
unmock()
137137
})
@@ -145,7 +145,21 @@ describe('cli', function () {
145145

146146
var result = execCli()
147147
result.code.should.equal(1)
148-
result.stdout.should.match(/tag, you're it/)
148+
result.stderr.should.match(/tag, you're it/)
149+
150+
unmock()
151+
})
152+
})
153+
154+
it('doesn\'t fail fast on stderr output from git', function () {
155+
// mock git by throwing on attempt to commit
156+
return mockGit('console.error("haha, kidding, this is just a warning"); process.exit(0);', 'add')
157+
.then(function (unmock) {
158+
writePackageJson('1.0.0')
159+
160+
var result = execCli()
161+
result.code.should.equal(0)
162+
result.stderr.should.match(/haha, kidding, this is just a warning/)
149163

150164
unmock()
151165
})

0 commit comments

Comments
 (0)