From 952caada6adb5e8ed78899bfde515abac0277092 Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Thu, 30 Mar 2017 18:14:28 -0700 Subject: [PATCH] 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 https://github.com/facebook/react-native/pull/13172 Differential Revision: D4795760 Pulled By: hramos fbshipit-source-id: 80164b2f30e33a3f9965f4865a8404f8640a52c1 --- local-cli/server/runServer.js | 18 ++++++++++++++++-- local-cli/server/server.js | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/local-cli/server/runServer.js b/local-cli/server/runServer.js index 5cc924a3bd4946..595ae927d9de62 100644 --- a/local-cli/server/runServer.js +++ b/local-cli/server/runServer.js @@ -10,7 +10,6 @@ const InspectorProxy = require('./util/inspectorProxy.js'); const ReactPackager = require('../../packager/react-packager'); -const TerminalReporter = require('../../packager/src/lib/TerminalReporter'); const attachHMRServer = require('./util/attachHMRServer'); const connect = require('connect'); @@ -86,6 +85,21 @@ function getPackagerServer(args, config) { const providesModuleNodeModules = args.providesModuleNodeModules || defaultProvidesModuleNodeModules; + let LogReporter; + if (args.customLogReporterPath) { + try { + // First we let require resolve it, so we can require packages in node_modules + // as expected. eg: require('my-package/reporter'); + LogReporter = require(args.customLogReporterPath); + } catch(e) { + // If that doesn't work, then we next try relative to the cwd, eg: + // require('./reporter'); + LogReporter = require(path.resolve(args.customLogReporterPath)); + } + } else { + LogReporter = require('../../packager/src/lib/TerminalReporter'); + } + return ReactPackager.createServer({ assetExts: defaultAssetExts.concat(args.assetExts), blacklistRE: config.getBlacklistRE(), @@ -96,7 +110,7 @@ function getPackagerServer(args, config) { platforms: defaultPlatforms.concat(args.platforms), projectRoots: args.projectRoots, providesModuleNodeModules: providesModuleNodeModules, - reporter: new TerminalReporter(), + reporter: new LogReporter(), resetCache: args.resetCache, transformModulePath: transformModulePath, verbose: args.verbose, diff --git a/local-cli/server/server.js b/local-cli/server/server.js index 3d72c8d771bf9c..c09bc7c0bb96cd 100644 --- a/local-cli/server/server.js +++ b/local-cli/server/server.js @@ -118,6 +118,9 @@ module.exports = { }, { command: '--reset-cache, --resetCache', description: 'Removes cached files', + }, { + command: '--custom-log-reporter-path, --customLogReporterPath [string]', + description: 'Path to a JavaScript file that exports a log reporter as a replacement for TerminalReporter', }, { command: '--verbose', description: 'Enables logging',