Skip to content

Commit 963fd60

Browse files
committed
Add support for custom log reporter cli option for local server
Summary: In Expo tools such as XDE, exp we listen listen to stdout from the packager process and print it in our own buffer. In the case of XDE, an electron app, our log pane is DOM-based, and before printing each log chunk we need to remove special tty characters and sometimes parse it to get information that we need (eg: progress bar). By using a custom reporter, we can take the raw events and pass them along in a format that is easy to consume by XDE and exp. This same motivation applies to create-react-native-app, where we currently don't show a progress bar in the terminal, but we can with this change. Create `LogReporter.js` in the root of a project with the CLI changes included in this PR. ``` class LogReporter { update(event) { console.log(JSON.stringify(event)); } } module.exports = LogReporter; ``` Now, run `react-native start --customLogReporterPath=LogReporter.js` -- all of the raw events will be output as JSON (while the logs Closes #13172 Differential Revision: D4795760 Pulled By: hramos fbshipit-source-id: 80164b2f30e33a3f9965f4865a8404f8640a52c1
1 parent adc0963 commit 963fd60

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

local-cli/server/runServer.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
const InspectorProxy = require('./util/inspectorProxy.js');
1212
const ReactPackager = require('../../packager/react-packager');
13-
const TerminalReporter = require('../../packager/src/lib/TerminalReporter');
1413

1514
const attachHMRServer = require('./util/attachHMRServer');
1615
const connect = require('connect');
@@ -88,6 +87,21 @@ function getPackagerServer(args, config) {
8887
const providesModuleNodeModules =
8988
args.providesModuleNodeModules || defaultProvidesModuleNodeModules;
9089

90+
let LogReporter;
91+
if (args.customLogReporterPath) {
92+
try {
93+
// First we let require resolve it, so we can require packages in node_modules
94+
// as expected. eg: require('my-package/reporter');
95+
LogReporter = require(args.customLogReporterPath);
96+
} catch(e) {
97+
// If that doesn't work, then we next try relative to the cwd, eg:
98+
// require('./reporter');
99+
LogReporter = require(path.resolve(args.customLogReporterPath));
100+
}
101+
} else {
102+
LogReporter = require('../../packager/src/lib/TerminalReporter');
103+
}
104+
91105
return ReactPackager.createServer({
92106
assetExts: defaultAssetExts.concat(args.assetExts),
93107
blacklistRE: config.getBlacklistRE(),
@@ -98,7 +112,7 @@ function getPackagerServer(args, config) {
98112
platforms: defaultPlatforms.concat(args.platforms),
99113
projectRoots: args.projectRoots,
100114
providesModuleNodeModules: providesModuleNodeModules,
101-
reporter: new TerminalReporter(),
115+
reporter: new LogReporter(),
102116
resetCache: args.resetCache,
103117
transformModulePath: transformModulePath,
104118
verbose: args.verbose,

local-cli/server/server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ module.exports = {
118118
}, {
119119
command: '--reset-cache, --resetCache',
120120
description: 'Removes cached files',
121+
}, {
122+
command: '--custom-log-reporter-path, --customLogReporterPath [string]',
123+
description: 'Path to a JavaScript file that exports a log reporter as a replacement for TerminalReporter',
121124
}, {
122125
command: '--verbose',
123126
description: 'Enables logging',

0 commit comments

Comments
 (0)