Skip to content

Commit

Permalink
Add support for non-interactive terminal (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun authored and fson committed Nov 23, 2016
1 parent ce91819 commit dc6edce
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var paths = require('../config/paths');

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

// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
Expand Down Expand Up @@ -69,21 +70,33 @@ function setupCompiler(host, port, protocol) {
// bundle, so if you refresh, it'll wait instead of serving the old one.
// "invalid" is short for "bundle invalidated", it doesn't imply any errors.
compiler.plugin('invalid', function() {
clearConsole();
if (isInteractive) {
clearConsole();
}
console.log('Compiling...');
});

var isFirstCompile = true;

// "done" event fires when Webpack has finished recompiling the bundle.
// Whether or not you have warnings or errors, you will get this event.
compiler.plugin('done', function(stats) {
clearConsole();
if (isInteractive) {
clearConsole();
}

// We have switched off the default Webpack output in WebpackDevServer
// options so we are going to "massage" the warnings and errors and present
// them in a readable focused way.
var messages = formatWebpackMessages(stats.toJson({}, true));
if (!messages.errors.length && !messages.warnings.length) {
var isSuccessful = !messages.errors.length && !messages.warnings.length;
var showInstructions = isSuccessful && (isInteractive || isFirstCompile);

if (isSuccessful) {
console.log(chalk.green('Compiled successfully!'));
}

if (showInstructions) {
console.log();
console.log('The app is running at:');
console.log();
Expand All @@ -92,6 +105,7 @@ function setupCompiler(host, port, protocol) {
console.log('Note that the development build is not optimized.');
console.log('To create a production build, use ' + chalk.cyan(cli + ' run build') + '.');
console.log();
isFirstCompile = false;
}

// If errors exist, only show errors.
Expand Down Expand Up @@ -258,10 +272,15 @@ function runDevServer(host, port, protocol) {
return console.log(err);
}

clearConsole();
if (isInteractive) {
clearConsole();
}
console.log(chalk.cyan('Starting the development server...'));
console.log();
openBrowser(protocol + '://' + host + ':' + port + '/');

if (isInteractive) {

This comment has been minimized.

Copy link
@CaryLandholt

CaryLandholt Dec 13, 2016

Contributor

@sheerun I'm running a CRA app with an express backend, much like https://github.com/fullstackreact/food-lookup-demo. However, the introduction of isInteractive prevents the browser from automatically opening when started.

We use the concurrently package to execute the front-end and backend concurrently in development. I suspect this is what is setting process.stdout.isTTY to false.

FYI - I commented out the condition check surrounding openBrowser, and the browser does open on start.

Any ideas on how to resolve? Thanks much!

ref: fullstackreact/food-lookup-demo#14

This comment has been minimized.

Copy link
@gaearon

gaearon Dec 13, 2016

Contributor

Could you file a new issue please so we can discuss?

This comment has been minimized.

Copy link
@gaearon

gaearon Dec 13, 2016

Contributor

I think we should still attempt to open the browser (but not clear the console).
Since #1247 was merged users will be able to opt out of opening the browser anyway if they want to.

openBrowser(protocol + '://' + host + ':' + port + '/');
}
});
}

Expand All @@ -280,16 +299,20 @@ detect(DEFAULT_PORT).then(port => {
return;
}

clearConsole();
var existingProcess = getProcessForPort(DEFAULT_PORT);
var question =
chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.' +
((existingProcess) ? ' Probably:\n ' + existingProcess : '')) +
'\n\nWould you like to run the app on another port instead?';
if (isInteractive) {
clearConsole();
var existingProcess = getProcessForPort(DEFAULT_PORT);
var question =
chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.' +
((existingProcess) ? ' Probably:\n ' + existingProcess : '')) +
'\n\nWould you like to run the app on another port instead?';

prompt(question, true).then(shouldChangePort => {
if (shouldChangePort) {
run(port);
}
});
prompt(question, true).then(shouldChangePort => {
if (shouldChangePort) {
run(port);
}
});
} else {
console.log(chalk.red('Something is already running on port ' + DEFAULT_PORT + '.'));
}
});

0 comments on commit dc6edce

Please sign in to comment.