diff --git a/packager/react-packager/src/lib/__mocks__/BatchProcessor.js b/packager/react-packager/src/lib/__mocks__/BatchProcessor.js new file mode 100644 index 0000000000..1d54393e59 --- /dev/null +++ b/packager/react-packager/src/lib/__mocks__/BatchProcessor.js @@ -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; diff --git a/packager/react-packager/src/lib/reporting.js b/packager/react-packager/src/lib/reporting.js index 77cb46ff49..7baccec6f3 100644 --- a/packager/react-packager/src/lib/reporting.js +++ b/packager/react-packager/src/lib/reporting.js @@ -84,6 +84,14 @@ function logWarning(terminal: Terminal, format: string, ...args: Array): 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): 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, };