From fbfc52c832600d1dd35ea38cd7e1248575c1e119 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Fri, 6 Jan 2017 06:02:37 -0800 Subject: [PATCH] packager: enable @flow in react-packager/index.js Reviewed By: cpojer Differential Revision: D4377411 fbshipit-source-id: 300d239d8e2818f0488549feafc98fd3451e452d --- react-packager/index.js | 71 ++----------------------- react-packager/react-packager.js | 91 ++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 67 deletions(-) create mode 100644 react-packager/react-packager.js diff --git a/react-packager/index.js b/react-packager/index.js index 4d43921a..3406f681 100644 --- a/react-packager/index.js +++ b/react-packager/index.js @@ -5,74 +5,11 @@ * 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. + * + * @flow */ + 'use strict'; require('../../setupBabel')(); - -const debug = require('debug'); -const Logger = require('./src/Logger'); - -exports.createServer = createServer; -exports.Logger = Logger; - -exports.buildBundle = function(options, bundleOptions) { - var server = createNonPersistentServer(options); - return server.buildBundle(bundleOptions) - .then(p => { - server.end(); - return p; - }); -}; - -exports.getOrderedDependencyPaths = function(options, bundleOptions) { - var server = createNonPersistentServer(options); - return server.getOrderedDependencyPaths(bundleOptions) - .then(function(paths) { - server.end(); - return paths; - }); -}; - -function enableDebug() { - // react-packager logs debug messages using the 'debug' npm package, and uses - // the following prefix throughout. - // To enable debugging, we need to set our pattern or append it to any - // existing pre-configured pattern to avoid disabling logging for - // other packages - var debugPattern = 'RNP:*'; - var existingPattern = debug.load(); - if (existingPattern) { - debugPattern += ',' + existingPattern; - } - debug.enable(debugPattern); -} - -function createServer(options) { - // the debug module is configured globally, we need to enable debugging - // *before* requiring any packages that use `debug` for logging - if (options.verbose) { - enableDebug(); - } - - options = Object.assign({}, options); - delete options.verbose; - if (options.reporter == null) { - // It's unsound to set-up the reporter here, but this allows backward - // compatibility. - var TerminalReporter = require('./src/lib/TerminalReporter'); - options.reporter = new TerminalReporter(); - } - var Server = require('./src/Server'); - return new Server(options); -} - -function createNonPersistentServer(options) { - if (options.reporter == null) { - // It's unsound to set-up the reporter here, but this allows backward - // compatibility. - options.reporter = require('./src/lib/reporting').nullReporter; - } - options.watch = !options.nonPersistent; - return createServer(options); -} +module.exports = require('./react-packager'); diff --git a/react-packager/react-packager.js b/react-packager/react-packager.js new file mode 100644 index 00000000..680c69fb --- /dev/null +++ b/react-packager/react-packager.js @@ -0,0 +1,91 @@ +/** + * 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. + * + * @flow + */ + +'use strict'; + +const Logger = require('./src/Logger'); + +const debug = require('debug'); + +import type {Reporter} from './src/lib/reporting'; + +exports.createServer = createServer; +exports.Logger = Logger; + +type Options = { + reporter?: Reporter, + watch?: boolean, + nonPersistent: boolean, +}; + +exports.buildBundle = function(options: Options, bundleOptions: {}) { + var server = createNonPersistentServer(options); + return server.buildBundle(bundleOptions) + .then(p => { + server.end(); + return p; + }); +}; + +exports.getOrderedDependencyPaths = function(options: Options, bundleOptions: {}) { + var server = createNonPersistentServer(options); + return server.getOrderedDependencyPaths(bundleOptions) + .then(function(paths) { + server.end(); + return paths; + }); +}; + +function enableDebug() { + // react-packager logs debug messages using the 'debug' npm package, and uses + // the following prefix throughout. + // To enable debugging, we need to set our pattern or append it to any + // existing pre-configured pattern to avoid disabling logging for + // other packages + var debugPattern = 'RNP:*'; + var existingPattern = debug.load(); + if (existingPattern) { + debugPattern += ',' + existingPattern; + } + debug.enable(debugPattern); +} + +function createServer(options: Options) { + // the debug module is configured globally, we need to enable debugging + // *before* requiring any packages that use `debug` for logging + if (options.verbose) { + enableDebug(); + } + + const serverOptions = Object.assign({}, options); + delete serverOptions.verbose; + if (serverOptions.reporter == null) { + // It's unsound to set-up the reporter here, but this allows backward + // compatibility. + var TerminalReporter = require('./src/lib/TerminalReporter'); + serverOptions.reporter = new TerminalReporter(); + } + var Server = require('./src/Server'); + return new Server(serverOptions); +} + +function createNonPersistentServer(options: Options) { + const serverOptions = { + // It's unsound to set-up the reporter here, + // but this allows backward compatibility. + reporter: options.reporter == null + ? require('./src/lib/reporting').nullReporter + : options.reporter, + ...options, + watch: !options.nonPersistent, + }; + return createServer(serverOptions); +}