Skip to content

Commit 92ea938

Browse files
sheerunrandycoulman
authored andcommitted
Add support for non-interactive terminal (facebook#1032)
1 parent 5b27ff8 commit 92ea938

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

packages/react-scripts/scripts/start.js

+40-15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var paths = require('../config/paths');
3535

3636
var useYarn = pathExists.sync(paths.yarnLockFile);
3737
var cli = useYarn ? 'yarn' : 'npm';
38+
var isInteractive = process.stdout.isTTY;
3839

3940
// Warn and crash if required files are missing
4041
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
@@ -71,21 +72,33 @@ function setupCompiler(host, port, protocol) {
7172
// bundle, so if you refresh, it'll wait instead of serving the old one.
7273
// "invalid" is short for "bundle invalidated", it doesn't imply any errors.
7374
compiler.plugin('invalid', function() {
74-
clearConsole();
75+
if (isInteractive) {
76+
clearConsole();
77+
}
7578
console.log('Compiling...');
7679
});
7780

81+
var isFirstCompile = true;
82+
7883
// "done" event fires when Webpack has finished recompiling the bundle.
7984
// Whether or not you have warnings or errors, you will get this event.
8085
compiler.plugin('done', function(stats) {
81-
clearConsole();
86+
if (isInteractive) {
87+
clearConsole();
88+
}
8289

8390
// We have switched off the default Webpack output in WebpackDevServer
8491
// options so we are going to "massage" the warnings and errors and present
8592
// them in a readable focused way.
8693
var messages = formatWebpackMessages(stats.toJson({}, true));
87-
if (!messages.errors.length && !messages.warnings.length) {
94+
var isSuccessful = !messages.errors.length && !messages.warnings.length;
95+
var showInstructions = isSuccessful && (isInteractive || isFirstCompile);
96+
97+
if (isSuccessful) {
8898
console.log(chalk.green('Compiled successfully!'));
99+
}
100+
101+
if (showInstructions) {
89102
console.log();
90103
console.log('The app is running at:');
91104
console.log();
@@ -94,6 +107,7 @@ function setupCompiler(host, port, protocol) {
94107
console.log('Note that the development build is not optimized.');
95108
console.log('To create a production build, use ' + chalk.cyan(cli + ' run build') + '.');
96109
console.log();
110+
isFirstCompile = false;
97111
}
98112

99113
// If errors exist, only show errors.
@@ -262,8 +276,15 @@ function runDevServer(host, port, protocol) {
262276
return console.log(err);
263277
}
264278

265-
clearConsole();
279+
if (isInteractive) {
280+
clearConsole();
281+
}
266282
console.log(chalk.cyan('Starting the development server...'));
283+
console.log();
284+
285+
if (isInteractive) {
286+
openBrowser(protocol + '://' + host + ':' + port + '/');
287+
}
267288
});
268289
}
269290

@@ -286,16 +307,20 @@ detect(DEFAULT_PORT).then(port => {
286307
return;
287308
}
288309

289-
clearConsole();
290-
var existingProcess = getProcessForPort(DEFAULT_PORT);
291-
var question =
292-
chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.' +
293-
((existingProcess) ? ' Probably:\n ' + existingProcess : '')) +
294-
'\n\nWould you like to run the app on another port instead?';
310+
if (isInteractive) {
311+
clearConsole();
312+
var existingProcess = getProcessForPort(DEFAULT_PORT);
313+
var question =
314+
chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.' +
315+
((existingProcess) ? ' Probably:\n ' + existingProcess : '')) +
316+
'\n\nWould you like to run the app on another port instead?';
295317

296-
prompt(question, true).then(shouldChangePort => {
297-
if (shouldChangePort) {
298-
run(port);
299-
}
300-
});
318+
prompt(question, true).then(shouldChangePort => {
319+
if (shouldChangePort) {
320+
run(port);
321+
}
322+
});
323+
} else {
324+
console.log(chalk.red('Something is already running on port ' + DEFAULT_PORT + '.'));
325+
}
301326
});

0 commit comments

Comments
 (0)