packager: add utilities to log errors internally

Reviewed By: davidaurelio

Differential Revision: D4468471

fbshipit-source-id: 10deb5014d06e4d3bb9d3122eef362c09f74d044
This commit is contained in:
Jean Lauliac 2017-01-27 11:05:00 -08:00 committed by Facebook Github Bot
parent 94f1c6b34a
commit 00940db5d5
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,41 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const {EventEmitter} = require('events');
class BatchProcessorMock {
constructor(_, processBatch) {
this._processBatch = processBatch;
this._queue = [];
BatchProcessorMock.mocks.emit('new', this);
}
queue(item, callback) {
this._queue.push([item, callback]);
}
flush(callback) {
const {_queue} = this;
this._queue = [];
process.nextTick(() => {
this._processBatch(_queue.map(pair => pair[0]), (error, res) => {
_queue.forEach((pair, i) => pair[1](error, res && res[i]));
callback();
});
});
}
}
BatchProcessorMock.mocks = new EventEmitter();
module.exports = BatchProcessorMock;

View File

@ -84,6 +84,14 @@ function logWarning(terminal: Terminal, format: string, ...args: Array<mixed>):
terminal.log('%s: %s', chalk.yellow('warning'), str);
}
/**
* Similar to `logWarning`, but for messages that require the user to act.
*/
function logError(terminal: Terminal, format: string, ...args: Array<mixed>): void {
const str = util.format(format, ...args);
terminal.log('%s: %s', chalk.red('error'), str);
}
/**
* A reporter that does nothing. Errors and warnings will be swallowed, that
* is generally not what you want.
@ -92,5 +100,6 @@ const nullReporter = {update() {}};
module.exports = {
logWarning,
logError,
nullReporter,
};