Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[local-cli][packager] Move packager initialization events to reporter #13209

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions local-cli/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -31,10 +30,12 @@ const systraceProfileMiddleware = require('./middleware/systraceProfileMiddlewar
const unless = require('./middleware/unless');
const webSocketProxy = require('./util/webSocketProxy.js');

function runServer(args, config, readyCallback) {
function runServer(args, config, startedCallback, readyCallback) {
var wsProxy = null;
var ms = null;
const packagerServer = getPackagerServer(args, config);
startedCallback(packagerServer._reporter);

const inspectorProxy = new InspectorProxy();
const app = connect()
.use(loadRawBodyMiddleware)
Expand Down Expand Up @@ -68,7 +69,7 @@ function runServer(args, config, readyCallback) {
wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
ms = messageSocket.attachToServer(serverInstance, '/message');
inspectorProxy.attachToServer(serverInstance, '/inspector');
readyCallback();
readyCallback(packagerServer._reporter);
}
);
// Disable any kind of automatic timeout behavior for incoming
Expand All @@ -86,6 +87,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(),
Expand All @@ -96,7 +112,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,
Expand Down
68 changes: 25 additions & 43 deletions local-cli/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
'use strict';

const chalk = require('chalk');
const formatBanner = require('./formatBanner');
const path = require('path');
const runServer = require('./runServer');

Expand All @@ -19,50 +17,31 @@ const runServer = require('./runServer');
function server(argv, config, args) {
args.projectRoots = args.projectRoots.concat(args.root);

console.log(formatBanner(
'Running packager on port ' + args.port + '.\n\n' +
'Keep this packager running while developing on any JS projects. ' +
'Feel free to close this tab and run your own packager instance if you ' +
'prefer.\n\n' +
'https://github.com/facebook/react-native', {
marginLeft: 1,
marginRight: 1,
paddingBottom: 1,
})
);
const startedCallback = logReporter => {
logReporter.update({
type: 'initialize_packager_started',
port: args.port,
projectRoots: args.projectRoots,
});

console.log(
'Looking for JS files in\n ',
chalk.dim(args.projectRoots.join('\n ')),
'\n'
);
process.on('uncaughtException', error => {
logReporter.update({
type: 'initialize_packager_failed',
port: args.port,
error,
});

process.on('uncaughtException', error => {
if (error.code === 'EADDRINUSE') {
console.log(
chalk.bgRed.bold(' ERROR '),
chalk.red('Packager can\'t listen on port', chalk.bold(args.port))
);
console.log('Most likely another process is already using this port');
console.log('Run the following command to find out which process:');
console.log('\n ', chalk.bold('lsof -i :' + args.port), '\n');
console.log('Then, you can either shut down the other process:');
console.log('\n ', chalk.bold('kill -9 <PID>'), '\n');
console.log('or run packager on different port.');
} else {
console.log(chalk.bgRed.bold(' ERROR '), chalk.red(error.message));
const errorAttributes = JSON.stringify(error);
if (errorAttributes !== '{}') {
console.error(chalk.red(errorAttributes));
}
console.error(chalk.red(error.stack));
}
console.log('\nSee', chalk.underline('http://facebook.github.io/react-native/docs/troubleshooting.html'));
console.log('for common problems and solutions.');
process.exit(11);
});
process.exit(11);
});
};

const readyCallback = logReporter => {
logReporter.update({
type: 'initialize_packager_done',
});
};

runServer(args, config, () => console.log('\nReact packager ready.\n'));
runServer(args, config, startedCallback, readyCallback);
}

module.exports = {
Expand Down Expand Up @@ -118,6 +97,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',
Expand Down
60 changes: 60 additions & 0 deletions packager/src/lib/TerminalReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

'use strict';

const chalk = require('chalk');
const path = require('path');
const reporting = require('./reporting');
const terminal = require('./terminal');
const throttle = require('lodash/throttle');
const util = require('util');
const formatBanner = require('../../../local-cli/server/formatBanner');

import type {ReportableEvent, GlobalCacheDisabledReason} from './reporting';

Expand Down Expand Up @@ -133,12 +135,70 @@ class TerminalReporter {
}
}


_logPackagerInitializing(port: number, projectRoots: Array<string>) {
terminal.log(
formatBanner(
'Running packager on port ' +
port +
'.\n\n' +
'Keep this packager running while developing on any JS projects. ' +
'Feel free to close this tab and run your own packager instance if you ' +
'prefer.\n\n' +
'https://github.com/facebook/react-native',
{
marginLeft: 1,
marginRight: 1,
paddingBottom: 1,
}
)
);

terminal.log(
'Looking for JS files in\n ',
chalk.dim(projectRoots.join('\n ')),
'\n'
);
}

_logPackagerInitializingFailed(port: number, error: Error) {
if (error.code === 'EADDRINUSE') {
terminal.log(
chalk.bgRed.bold(' ERROR '),
chalk.red("Packager can't listen on port", chalk.bold(port))
);
terminal.log('Most likely another process is already using this port');
terminal.log('Run the following command to find out which process:');
terminal.log('\n ', chalk.bold('lsof -i :' + port), '\n');
terminal.log('Then, you can either shut down the other process:');
terminal.log('\n ', chalk.bold('kill -9 <PID>'), '\n');
terminal.log('or run packager on different port.');
} else {
terminal.log(chalk.bgRed.bold(' ERROR '), chalk.red(error.message));
const errorAttributes = JSON.stringify(error);
if (errorAttributes !== '{}') {
terminal.log(chalk.red(errorAttributes));
}
terminal.log(chalk.red(error.stack));
}
}


/**
* This function is only concerned with logging and should not do state
* or terminal status updates.
*/
_log(event: TerminalReportableEvent): void {
switch (event.type) {
case 'initialize_packager_started':
this._logPackagerInitializing(event.port, event.projectRoots);
break;
case 'initialize_packager_done':
terminal.log('\nReact packager ready.\n');
break;
case 'initialize_packager_failed':
this._logPackagerInitializingFailed(event.port, event.error);
break;
case 'bundle_build_done':
this._logBundleBuildDone(event.entryFilePath);
break;
Expand Down
10 changes: 10 additions & 0 deletions packager/src/lib/reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ export type GlobalCacheDisabledReason = 'too_many_errors' | 'too_many_misses';
* report to the tool user.
*/
export type ReportableEvent = {
port: number,
projectRoots: Array<string>,
type: 'initialize_packager_started',
} | {
type: 'initialize_packager_done',
} | {
type: 'initialize_packager_failed',
port: number,
error: Error,
} | {
entryFilePath: string,
type: 'bundle_build_done',
} | {
Expand Down