Skip to content

Commit 8ab4d7c

Browse files
committed
Split out logOnly method from log
Previously log would always call .exit(), which is intended to be fatal. In order to support non-fatal logging, split out a logOnly method which outputs the formatted lines but does not exit. This preserves the current API completely. Also add a call to .slice() if the lines argument is not a string so that the argument Array is not modified. This codepath is not currently used, but it is no longer a waiting surprise for future callers. Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
1 parent 2ca2fb9 commit 8ab4d7c

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

index.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,16 @@ Hook.prototype.parse = function parse() {
112112
/**
113113
* Write messages to the terminal, for feedback purposes.
114114
*
115-
* @param {Array} lines The messages that need to be written.
116-
* @param {Number} exit Exit code for the process.exit.
115+
* @param {string|Array<string>} lines The messages that need to be written.
116+
* @param {?function(string)} dest Function to which lines will be written.
117+
* (default: console.error)
118+
* @returns {Array<string>} Lines written to output.
117119
* @api public
118120
*/
119-
Hook.prototype.log = function log(lines, exit) {
121+
Hook.prototype.logOnly = function logOnly(lines, dest) {
122+
dest = dest || console.error;
120123
if (!Array.isArray(lines)) lines = lines.split('\n');
121-
if ('number' !== typeof exit) exit = 1;
124+
else lines = lines.slice();
122125

123126
var prefix = this.colors
124127
? '\u001b[38;5;166mpre-commit:\u001b[39;49m '
@@ -132,11 +135,25 @@ Hook.prototype.log = function log(lines, exit) {
132135
});
133136

134137
if (!this.silent) lines.forEach(function output(line) {
135-
if (exit) console.error(line);
136-
else console.log(line);
138+
// Note: This wrapper function is necessary to avoid extra args to output.
139+
dest(line);
137140
});
138141

139-
this.exit(exit, lines);
142+
return lines;
143+
};
144+
145+
/**
146+
* Write messages to the terminal, for feedback purposes, then call exit.
147+
*
148+
* @param {string|Array<string>} lines The messages that need to be written.
149+
* @param {number} exit Exit code for the process.exit.
150+
* @api public
151+
*/
152+
Hook.prototype.log = function log(lines, exit) {
153+
if ('number' !== typeof exit) exit = 1;
154+
155+
var outputLines = this.logOnly(lines, exit ? console.error : console.log);
156+
this.exit(exit, outputLines);
140157
return exit === 0;
141158
};
142159

0 commit comments

Comments
 (0)