/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import Metro from 'metro' ; import { Terminal } from 'metro-core' ; import path from 'path' ; import { createDevServerMiddleware , indexPageMiddleware , } from '@react-native-community/cli-server-api' ; import { Config } from '@react-native-community/cli-types' ; import loadMetroConfig from '../../tools/loadMetroConfig' ; import { releaseChecker } from '@react-native-community/cli-tools' ; import enableWatchMode from './watchMode' ; export type Args = { assetPlugins ? : string [ ] ; cert ? : string ; customLogReporterPath ? : string ; host ? : string ; https ? : boolean ; maxWorkers ? : number ; key ? : string ; platforms ? : string [ ] ; port ? : number ; resetCache ? : boolean ; sourceExts ? : string [ ] ; transformer ? : string ; watchFolders ? : string [ ] ; config ? : string ; projectRoot ? : string ; interactive : boolean ; } ; async function runServer ( _argv , ctx , args ) { let reportEvent ; const terminal = new Terminal ( process . stdout ) ; const ReporterImpl = getReporterImpl ( args . customLogReporterPath ) ; const terminalReporter = new ReporterImpl ( terminal ) ; const reporter = { update ( event ) { terminalReporter . update ( event ) ; if ( reportEvent ) { reportEvent ( event ) ; } } , } ; const metroConfig = await loadMetroConfig ( ctx , { config : args . config , maxWorkers : args . maxWorkers , port : args . port , resetCache : args . resetCache , watchFolders : args . watchFolders , projectRoot : args . projectRoot , sourceExts : args . sourceExts , reporter , } ) ; if ( args . assetPlugins ) { metroConfig . transformer . assetPlugins = args . assetPlugins . map ( ( plugin ) => require . resolve ( plugin ) ) ; } const { middleware , attachToServer } = createDevServerMiddleware ( { host : args . host , port : metroConfig . server . port , watchFolders : metroConfig . watchFolders , } ) ; middleware . use ( indexPageMiddleware ) ; const customEnhanceMiddleware = metroConfig . server . enhanceMiddleware ; metroConfig . server . enhanceMiddleware = ( metroMiddleware , server ) => { if ( customEnhanceMiddleware ) { metroMiddleware = customEnhanceMiddleware ( metroMiddleware , server ) ; } return middleware . use ( metroMiddleware ) ; } ; const serverInstance = await Metro . runServer ( metroConfig , { host : args . host , secure : args . https , secureCert : args . cert , secureKey : args . key , hmrEnabled : true , } ) ; const { messageSocket , eventsSocket } = attachToServer ( serverInstance ) ; reportEvent = eventsSocket . reportEvent ; if ( args . interactive ) { enableWatchMode ( messageSocket ) ; } serverInstance . keepAliveTimeout = 30000 ; await releaseChecker ( ctx . root ) ; } function getReporterImpl ( customLogReporterPath ) { if ( customLogReporterPath === undefined ) { return require ( 'metro/src/lib/TerminalReporter' ) ; } try { return require ( customLogReporterPath ) ; } catch ( e ) { if ( ( < any > e ) . code !== 'MODULE_NOT_FOUND' ) { throw e ; } return require ( path . resolve ( customLogReporterPath ) ) ; } } export default runServer ;