6
6
*/
7
7
8
8
import Metro from 'metro' ;
9
+ import type { Reporter , ReportableEvent } from 'metro' ;
9
10
import type Server from 'metro/src/Server' ;
10
11
import type { Middleware } from 'metro-config' ;
11
12
import { Terminal } from 'metro-core' ;
@@ -40,19 +41,6 @@ export type Args = {
40
41
} ;
41
42
42
43
async function runServer ( _argv : Array < string > , ctx : Config , args : Args ) {
43
- let reportEvent : ( ( event : any ) => void ) | undefined ;
44
- const terminal = new Terminal ( process . stdout ) ;
45
- const ReporterImpl = getReporterImpl ( args . customLogReporterPath ) ;
46
- const terminalReporter = new ReporterImpl ( terminal ) ;
47
- const reporter = {
48
- update ( event : any ) {
49
- terminalReporter . update ( event ) ;
50
- if ( reportEvent ) {
51
- reportEvent ( event ) ;
52
- }
53
- } ,
54
- } ;
55
-
56
44
const metroConfig = await loadMetroConfig ( ctx , {
57
45
config : args . config ,
58
46
maxWorkers : args . maxWorkers ,
@@ -61,8 +49,14 @@ async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
61
49
watchFolders : args . watchFolders ,
62
50
projectRoot : args . projectRoot ,
63
51
sourceExts : args . sourceExts ,
64
- reporter,
65
52
} ) ;
53
+ // if customLogReporterPath is provided, use the custom reporter, otherwise use the default one
54
+ let reporter : Reporter = metroConfig . reporter ;
55
+ if ( args . customLogReporterPath ) {
56
+ const terminal = new Terminal ( process . stdout ) ;
57
+ const ReporterImpl = getReporterImpl ( args . customLogReporterPath ) ;
58
+ reporter = new ReporterImpl ( terminal ) ;
59
+ }
66
60
67
61
if ( args . assetPlugins ) {
68
62
// @ts -ignore - assigning to readonly property
@@ -95,16 +89,26 @@ async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
95
89
return middleware . use ( metroMiddleware ) ;
96
90
} ;
97
91
98
- const serverInstance = await Metro . runServer ( metroConfig , {
99
- host : args . host ,
100
- secure : args . https ,
101
- secureCert : args . cert ,
102
- secureKey : args . key ,
103
- // @ts -ignore - ws.Server types are incompatible
104
- websocketEndpoints,
105
- } ) ;
106
-
107
- reportEvent = eventsSocketEndpoint . reportEvent ;
92
+ const serverInstance = await Metro . runServer (
93
+ {
94
+ ...metroConfig ,
95
+ reporter : {
96
+ update ( event : ReportableEvent ) {
97
+ reporter . update ( event ) ;
98
+ // Add reportEvent to the reporter update method.
99
+ eventsSocketEndpoint . reportEvent ( event ) ;
100
+ } ,
101
+ } ,
102
+ } ,
103
+ {
104
+ host : args . host ,
105
+ secure : args . https ,
106
+ secureCert : args . cert ,
107
+ secureKey : args . key ,
108
+ // @ts -ignore - ws.Server types are incompatible
109
+ websocketEndpoints,
110
+ } ,
111
+ ) ;
108
112
109
113
if ( args . interactive ) {
110
114
enableWatchMode ( messageSocketEndpoint , ctx ) ;
@@ -125,10 +129,7 @@ async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
125
129
await version . logIfUpdateAvailable ( ctx . root ) ;
126
130
}
127
131
128
- function getReporterImpl ( customLogReporterPath : string | undefined ) {
129
- if ( customLogReporterPath === undefined ) {
130
- return require ( 'metro/src/lib/TerminalReporter' ) ;
131
- }
132
+ function getReporterImpl ( customLogReporterPath : string ) {
132
133
try {
133
134
// First we let require resolve it, so we can require packages in node_modules
134
135
// as expected. eg: require('my-package/reporter');
0 commit comments