From cfe3670a07721948ae636eecf72c359c7789f7e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Fri, 12 Jan 2018 07:26:11 -0800 Subject: [PATCH] Makes js1 run use the builtin Metro CLI Reviewed By: rafeca Differential Revision: D6510003 fbshipit-source-id: c0593c71a4b5c236b3e2e91c31f8a7c87bd83e42 --- packages/metro/src/commands/serve.js | 9 ------ packages/metro/src/index.js | 33 +++++++++++++++----- packages/metro/src/lib/TerminalReporter.js | 35 +++++++++++----------- packages/metro/src/lib/reporting.js | 2 +- 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/packages/metro/src/commands/serve.js b/packages/metro/src/commands/serve.js index a9c3fd88..6d056796 100644 --- a/packages/metro/src/commands/serve.js +++ b/packages/metro/src/commands/serve.js @@ -78,20 +78,11 @@ exports.handler = makeAsyncCommand(async (argv: any) => { server = await MetroApi.runServer({ ...argv, config, - onReady, }); restarting = false; } - function onReady(server) { - console.log( - `The HTTP server is ready to accept requests on ${ - server.address().address - }:${server.address().port}`, - ); - } - const metroConfigLocation = await findMetroConfig(argv.config); if (metroConfigLocation) { diff --git a/packages/metro/src/index.js b/packages/metro/src/index.js index afbe00d8..6df0b7f2 100644 --- a/packages/metro/src/index.js +++ b/packages/metro/src/index.js @@ -38,6 +38,7 @@ export type {ConfigT} from './Config'; type PublicMetroOptions = {| config?: ConfigT, maxWorkers?: number, + port?: ?number, projectRoots: Array, // deprecated resetCache?: boolean, @@ -67,9 +68,13 @@ async function runMetro({ config, resetCache = false, maxWorkers = 1, + // $FlowFixMe TODO t0 https://github.com/facebook/flow/issues/183 + port = null, projectRoots = [], watch = false, }: PrivateMetroOptions): Promise { + const reporter = new TerminalReporter(new Terminal(process.stdout)); + const normalizedConfig = config ? Config.normalize(config) : Config.DEFAULT; const assetExts = defaults.assetExts.concat( @@ -90,6 +95,19 @@ async function runMetro({ ? normalizedConfig.getProvidesModuleNodeModules() : defaults.providesModuleNodeModules; + const finalProjectRoots = await Promise.all( + normalizedConfig + .getProjectRoots() + .concat(projectRoots) + .map(path => asyncRealpath(path)), + ); + + reporter.update({ + type: 'initialize_started', + port, + projectRoots: finalProjectRoots, + }); + const serverOptions: ServerOptions = { assetExts: normalizedConfig.assetTransforms ? [] : assetExts, assetRegistryPath: normalizedConfig.assetRegistryPath, @@ -109,7 +127,7 @@ async function runMetro({ postProcessBundleSourcemap: normalizedConfig.postProcessBundleSourcemap, providesModuleNodeModules, resetCache, - reporter: new TerminalReporter(new Terminal(process.stdout)), + reporter, sourceExts: normalizedConfig.assetTransforms ? sourceExts.concat(assetExts) : sourceExts, @@ -118,12 +136,7 @@ async function runMetro({ watch, workerPath: normalizedConfig.getWorkerPath && normalizedConfig.getWorkerPath(), - projectRoots: await Promise.all( - normalizedConfig - .getProjectRoots() - .concat(projectRoots) - .map(path => asyncRealpath(path)), - ), + projectRoots: finalProjectRoots, }; return new MetroServer(serverOptions); @@ -139,6 +152,7 @@ exports.createConnectMiddleware = async function( const metroServer = await runMetro({ config: options.config, maxWorkers: options.maxWorkers, + port: options.port, projectRoots: options.projectRoots, resetCache: options.resetCache, watch: true, @@ -167,6 +181,8 @@ type RunServerOptions = {| |}; exports.runServer = async (options: RunServerOptions) => { + const port = options.port || 8080; + // Lazy require const connect = require('connect'); @@ -175,6 +191,7 @@ exports.runServer = async (options: RunServerOptions) => { const {middleware, end} = await exports.createConnectMiddleware({ config: options.config, maxWorkers: options.maxWorkers, + port, projectRoots: options.projectRoots, resetCache: options.resetCache, }); @@ -195,7 +212,7 @@ exports.runServer = async (options: RunServerOptions) => { httpServer = Http.createServer(serverApp); } - httpServer.listen(options.port, options.host, () => { + httpServer.listen(port, options.host, () => { options.onReady && options.onReady(httpServer); }); diff --git a/packages/metro/src/lib/TerminalReporter.js b/packages/metro/src/lib/TerminalReporter.js index 50dec3cd..361550be 100644 --- a/packages/metro/src/lib/TerminalReporter.js +++ b/packages/metro/src/lib/TerminalReporter.js @@ -172,23 +172,24 @@ class TerminalReporter { } } - _logInitializing(port: number, projectRoots: $ReadOnlyArray) { - this.terminal.log( - formatBanner( - 'Running Metro Bundler on port ' + - port + - '.\n\n' + - 'Keep Metro Bundler running while developing on any JS projects. ' + - 'Feel free to close this tab and run your own Metro Bundler ' + - ' instance if you prefer.\n\n' + - 'https://github.com/facebook/react-native', - { - marginLeft: 1, - marginRight: 1, - paddingBottom: 1, - }, - ), - ); + _logInitializing(port: ?number, projectRoots: $ReadOnlyArray) { + if (port) { + this.terminal.log( + formatBanner( + 'Running Metro Bundler on port ' + + port + + '.\n\n' + + 'Keep Metro running while developing on any JS projects. Feel ' + + 'free to close this tab and run your own Metro instance ' + + 'if you prefer.\n\n' + + 'https://github.com/facebook/react-native', + { + paddingTop: 1, + paddingBottom: 1, + }, + ) + '\n', + ); + } this.terminal.log( 'Looking for JS files in\n ', diff --git a/packages/metro/src/lib/reporting.js b/packages/metro/src/lib/reporting.js index e0b898a9..dad0dfc9 100644 --- a/packages/metro/src/lib/reporting.js +++ b/packages/metro/src/lib/reporting.js @@ -33,7 +33,7 @@ export type BundleDetails = { */ export type ReportableEvent = | { - port: number, + port: ?number, projectRoots: $ReadOnlyArray, type: 'initialize_started', }