Skip to content

Commit 0ec23e3

Browse files
committed
[local-cli][packager] Use the given log reporter for packager startup log messages too
1 parent b25ebc3 commit 0ec23e3

File tree

4 files changed

+120
-45
lines changed

4 files changed

+120
-45
lines changed

local-cli/server/runServer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ const systraceProfileMiddleware = require('./middleware/systraceProfileMiddlewar
3030
const unless = require('./middleware/unless');
3131
const webSocketProxy = require('./util/webSocketProxy.js');
3232

33-
function runServer(args, config, readyCallback) {
33+
function runServer(args, config, startedCallback, readyCallback) {
3434
var wsProxy = null;
3535
var ms = null;
3636
const packagerServer = getPackagerServer(args, config);
37+
startedCallback(packagerServer._reporter);
38+
3739
const inspectorProxy = new InspectorProxy();
3840
const app = connect()
3941
.use(loadRawBodyMiddleware)
@@ -67,7 +69,7 @@ function runServer(args, config, readyCallback) {
6769
wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
6870
ms = messageSocket.attachToServer(serverInstance, '/message');
6971
inspectorProxy.attachToServer(serverInstance, '/inspector');
70-
readyCallback();
72+
readyCallback(packagerServer._reporter);
7173
}
7274
);
7375
// Disable any kind of automatic timeout behavior for incoming

local-cli/server/server.js

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
*/
99
'use strict';
1010

11-
const chalk = require('chalk');
12-
const formatBanner = require('./formatBanner');
1311
const path = require('path');
1412
const runServer = require('./runServer');
1513

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

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

34-
console.log(
35-
'Looking for JS files in\n ',
36-
chalk.dim(args.projectRoots.join('\n ')),
37-
'\n'
38-
);
27+
process.on('uncaughtException', error => {
28+
logReporter.update({
29+
type: 'initialize_packager_failed',
30+
port: args.port,
31+
error,
32+
});
3933

40-
process.on('uncaughtException', error => {
41-
if (error.code === 'EADDRINUSE') {
42-
console.log(
43-
chalk.bgRed.bold(' ERROR '),
44-
chalk.red('Packager can\'t listen on port', chalk.bold(args.port))
45-
);
46-
console.log('Most likely another process is already using this port');
47-
console.log('Run the following command to find out which process:');
48-
console.log('\n ', chalk.bold('lsof -i :' + args.port), '\n');
49-
console.log('Then, you can either shut down the other process:');
50-
console.log('\n ', chalk.bold('kill -9 <PID>'), '\n');
51-
console.log('or run packager on different port.');
52-
} else {
53-
console.log(chalk.bgRed.bold(' ERROR '), chalk.red(error.message));
54-
const errorAttributes = JSON.stringify(error);
55-
if (errorAttributes !== '{}') {
56-
console.error(chalk.red(errorAttributes));
57-
}
58-
console.error(chalk.red(error.stack));
59-
}
60-
console.log('\nSee', chalk.underline('http://facebook.github.io/react-native/docs/troubleshooting.html'));
61-
console.log('for common problems and solutions.');
62-
process.exit(11);
63-
});
34+
process.exit(11);
35+
});
36+
};
37+
38+
const readyCallback = logReporter => {
39+
logReporter.update({
40+
type: 'initialize_packager_done',
41+
});
42+
};
6443

65-
runServer(args, config, () => console.log('\nReact packager ready.\n'));
44+
runServer(args, config, startedCallback, readyCallback);
6645
}
6746

6847
module.exports = {

packager/src/lib/TerminalReporter.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
'use strict';
1313

14+
const chalk = require('chalk');
1415
const path = require('path');
1516
const reporting = require('./reporting');
1617
const terminal = require('./terminal');
1718
const throttle = require('lodash/throttle');
1819
const util = require('util');
20+
const formatBanner = require('../../../local-cli/server/formatBanner');
1921

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

@@ -133,12 +135,70 @@ class TerminalReporter {
133135
}
134136
}
135137

138+
139+
_logPackagerInitializing(port: number, projectRoots: Array<string>) {
140+
terminal.log(
141+
formatBanner(
142+
'Running packager on port ' +
143+
port +
144+
'.\n\n' +
145+
'Keep this packager running while developing on any JS projects. ' +
146+
'Feel free to close this tab and run your own packager instance if you ' +
147+
'prefer.\n\n' +
148+
'https://github.com/facebook/react-native',
149+
{
150+
marginLeft: 1,
151+
marginRight: 1,
152+
paddingBottom: 1,
153+
}
154+
)
155+
);
156+
157+
terminal.log(
158+
'Looking for JS files in\n ',
159+
chalk.dim(projectRoots.join('\n ')),
160+
'\n'
161+
);
162+
}
163+
164+
_logPackagerInitializingFailed(port: number, error: Error) {
165+
if (error.code === 'EADDRINUSE') {
166+
terminal.log(
167+
chalk.bgRed.bold(' ERROR '),
168+
chalk.red("Packager can't listen on port", chalk.bold(port))
169+
);
170+
terminal.log('Most likely another process is already using this port');
171+
terminal.log('Run the following command to find out which process:');
172+
terminal.log('\n ', chalk.bold('lsof -i :' + port), '\n');
173+
terminal.log('Then, you can either shut down the other process:');
174+
terminal.log('\n ', chalk.bold('kill -9 <PID>'), '\n');
175+
terminal.log('or run packager on different port.');
176+
} else {
177+
terminal.log(chalk.bgRed.bold(' ERROR '), chalk.red(error.message));
178+
const errorAttributes = JSON.stringify(error);
179+
if (errorAttributes !== '{}') {
180+
terminal.log(chalk.red(errorAttributes));
181+
}
182+
terminal.log(chalk.red(error.stack));
183+
}
184+
}
185+
186+
136187
/**
137188
* This function is only concerned with logging and should not do state
138189
* or terminal status updates.
139190
*/
140191
_log(event: TerminalReportableEvent): void {
141192
switch (event.type) {
193+
case 'initialize_packager_started':
194+
this._logPackagerInitializing(event.port, event.projectRoots);
195+
break;
196+
case 'initialize_packager_done':
197+
terminal.log('\nReact packager ready.\n');
198+
break;
199+
case 'initialize_packager_failed':
200+
this._logPackagerInitializingFailed(event.port, event.error);
201+
break;
142202
case 'bundle_build_done':
143203
this._logBundleBuildDone(event.entryFilePath);
144204
break;

packager/src/lib/reporting.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,45 @@ import type {Terminal} from './terminal';
1818

1919
export type GlobalCacheDisabledReason = 'too_many_errors' | 'too_many_misses';
2020

21+
const startedCallback = logReporter => {
22+
logReporter.update({
23+
type: 'initialize_packager_started',
24+
port: args.port,
25+
projectRoots: args.projectRoots,
26+
});
27+
28+
process.on('uncaughtException', error => {
29+
logReporter.update({
30+
type: 'initialize_packager_failed',
31+
port: args.port,
32+
error,
33+
});
34+
35+
process.exit(11);
36+
});
37+
};
38+
39+
const readyCallback = logReporter => {
40+
logReporter.update({
41+
type: 'initialize_packager_done',
42+
});
43+
};
44+
2145
/**
2246
* A tagged union of all the actions that may happen and we may want to
2347
* report to the tool user.
2448
*/
2549
export type ReportableEvent = {
50+
port: number,
51+
projectRoots: Array<string>,
52+
type: 'initialize_packager_started',
53+
} | {
54+
type: 'initialize_packager_done',
55+
} | {
56+
type: 'initialize_packager_failed',
57+
port: number,
58+
error: Error,
59+
} | {
2660
entryFilePath: string,
2761
type: 'bundle_build_done',
2862
} | {

0 commit comments

Comments
 (0)