Makes js1 run use the builtin Metro CLI

Reviewed By: rafeca

Differential Revision: D6510003

fbshipit-source-id: c0593c71a4b5c236b3e2e91c31f8a7c87bd83e42
This commit is contained in:
Maël Nison 2018-01-12 07:26:11 -08:00 committed by Facebook Github Bot
parent da2fdba240
commit cfe3670a07
4 changed files with 44 additions and 35 deletions

View File

@ -78,20 +78,11 @@ exports.handler = makeAsyncCommand(async (argv: any) => {
server = await MetroApi.runServer({ server = await MetroApi.runServer({
...argv, ...argv,
config, config,
onReady,
}); });
restarting = false; 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); const metroConfigLocation = await findMetroConfig(argv.config);
if (metroConfigLocation) { if (metroConfigLocation) {

View File

@ -38,6 +38,7 @@ export type {ConfigT} from './Config';
type PublicMetroOptions = {| type PublicMetroOptions = {|
config?: ConfigT, config?: ConfigT,
maxWorkers?: number, maxWorkers?: number,
port?: ?number,
projectRoots: Array<string>, projectRoots: Array<string>,
// deprecated // deprecated
resetCache?: boolean, resetCache?: boolean,
@ -67,9 +68,13 @@ async function runMetro({
config, config,
resetCache = false, resetCache = false,
maxWorkers = 1, maxWorkers = 1,
// $FlowFixMe TODO t0 https://github.com/facebook/flow/issues/183
port = null,
projectRoots = [], projectRoots = [],
watch = false, watch = false,
}: PrivateMetroOptions): Promise<MetroServer> { }: PrivateMetroOptions): Promise<MetroServer> {
const reporter = new TerminalReporter(new Terminal(process.stdout));
const normalizedConfig = config ? Config.normalize(config) : Config.DEFAULT; const normalizedConfig = config ? Config.normalize(config) : Config.DEFAULT;
const assetExts = defaults.assetExts.concat( const assetExts = defaults.assetExts.concat(
@ -90,6 +95,19 @@ async function runMetro({
? normalizedConfig.getProvidesModuleNodeModules() ? normalizedConfig.getProvidesModuleNodeModules()
: defaults.providesModuleNodeModules; : 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 = { const serverOptions: ServerOptions = {
assetExts: normalizedConfig.assetTransforms ? [] : assetExts, assetExts: normalizedConfig.assetTransforms ? [] : assetExts,
assetRegistryPath: normalizedConfig.assetRegistryPath, assetRegistryPath: normalizedConfig.assetRegistryPath,
@ -109,7 +127,7 @@ async function runMetro({
postProcessBundleSourcemap: normalizedConfig.postProcessBundleSourcemap, postProcessBundleSourcemap: normalizedConfig.postProcessBundleSourcemap,
providesModuleNodeModules, providesModuleNodeModules,
resetCache, resetCache,
reporter: new TerminalReporter(new Terminal(process.stdout)), reporter,
sourceExts: normalizedConfig.assetTransforms sourceExts: normalizedConfig.assetTransforms
? sourceExts.concat(assetExts) ? sourceExts.concat(assetExts)
: sourceExts, : sourceExts,
@ -118,12 +136,7 @@ async function runMetro({
watch, watch,
workerPath: workerPath:
normalizedConfig.getWorkerPath && normalizedConfig.getWorkerPath(), normalizedConfig.getWorkerPath && normalizedConfig.getWorkerPath(),
projectRoots: await Promise.all( projectRoots: finalProjectRoots,
normalizedConfig
.getProjectRoots()
.concat(projectRoots)
.map(path => asyncRealpath(path)),
),
}; };
return new MetroServer(serverOptions); return new MetroServer(serverOptions);
@ -139,6 +152,7 @@ exports.createConnectMiddleware = async function(
const metroServer = await runMetro({ const metroServer = await runMetro({
config: options.config, config: options.config,
maxWorkers: options.maxWorkers, maxWorkers: options.maxWorkers,
port: options.port,
projectRoots: options.projectRoots, projectRoots: options.projectRoots,
resetCache: options.resetCache, resetCache: options.resetCache,
watch: true, watch: true,
@ -167,6 +181,8 @@ type RunServerOptions = {|
|}; |};
exports.runServer = async (options: RunServerOptions) => { exports.runServer = async (options: RunServerOptions) => {
const port = options.port || 8080;
// Lazy require // Lazy require
const connect = require('connect'); const connect = require('connect');
@ -175,6 +191,7 @@ exports.runServer = async (options: RunServerOptions) => {
const {middleware, end} = await exports.createConnectMiddleware({ const {middleware, end} = await exports.createConnectMiddleware({
config: options.config, config: options.config,
maxWorkers: options.maxWorkers, maxWorkers: options.maxWorkers,
port,
projectRoots: options.projectRoots, projectRoots: options.projectRoots,
resetCache: options.resetCache, resetCache: options.resetCache,
}); });
@ -195,7 +212,7 @@ exports.runServer = async (options: RunServerOptions) => {
httpServer = Http.createServer(serverApp); httpServer = Http.createServer(serverApp);
} }
httpServer.listen(options.port, options.host, () => { httpServer.listen(port, options.host, () => {
options.onReady && options.onReady(httpServer); options.onReady && options.onReady(httpServer);
}); });

View File

@ -172,23 +172,24 @@ class TerminalReporter {
} }
} }
_logInitializing(port: number, projectRoots: $ReadOnlyArray<string>) { _logInitializing(port: ?number, projectRoots: $ReadOnlyArray<string>) {
this.terminal.log( if (port) {
formatBanner( this.terminal.log(
'Running Metro Bundler on port ' + formatBanner(
port + 'Running Metro Bundler on port ' +
'.\n\n' + port +
'Keep Metro Bundler running while developing on any JS projects. ' + '.\n\n' +
'Feel free to close this tab and run your own Metro Bundler ' + 'Keep Metro running while developing on any JS projects. Feel ' +
' instance if you prefer.\n\n' + 'free to close this tab and run your own Metro instance ' +
'https://github.com/facebook/react-native', 'if you prefer.\n\n' +
{ 'https://github.com/facebook/react-native',
marginLeft: 1, {
marginRight: 1, paddingTop: 1,
paddingBottom: 1, paddingBottom: 1,
}, },
), ) + '\n',
); );
}
this.terminal.log( this.terminal.log(
'Looking for JS files in\n ', 'Looking for JS files in\n ',

View File

@ -33,7 +33,7 @@ export type BundleDetails = {
*/ */
export type ReportableEvent = export type ReportableEvent =
| { | {
port: number, port: ?number,
projectRoots: $ReadOnlyArray<string>, projectRoots: $ReadOnlyArray<string>,
type: 'initialize_started', type: 'initialize_started',
} }