From 552b98871f227a6c8638945a27bf29ea80707230 Mon Sep 17 00:00:00 2001 From: Peter Indiola Date: Sat, 23 Jul 2016 05:08:19 +0800 Subject: [PATCH 1/9] WIP notify if port is used and ask to confirm to use another avaiable port --- package.json | 1 + scripts/start.js | 161 ++++++++++++++++++++++++++++------------------- 2 files changed, 98 insertions(+), 64 deletions(-) diff --git a/package.json b/package.json index aed0d9fb586..cfaca70b13f 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "chalk": "1.1.3", "cross-spawn": "4.0.0", "css-loader": "0.23.1", + "detect-port": "^0.1.4", "eslint": "3.1.1", "eslint-loader": "1.4.1", "eslint-plugin-import": "1.10.3", diff --git a/scripts/start.js b/scripts/start.js index 2032a526f38..35081562bbf 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -16,6 +16,10 @@ var WebpackDevServer = require('webpack-dev-server'); var config = require('../config/webpack.config.dev'); var execSync = require('child_process').execSync; var opn = require('opn'); +var detect = require('detect-port'); +var readline = require('readline'); +var port = 3000; +process.env.PORT = port; // TODO: hide this behind a flag and eliminate dead code on eject. // This shouldn't be exposed to the user. @@ -66,62 +70,89 @@ function clearConsole() { } var compiler = webpack(config, handleCompile); -compiler.plugin('invalid', function () { - clearConsole(); - console.log('Compiling...'); -}); -compiler.plugin('done', function (stats) { - clearConsole(); - var hasErrors = stats.hasErrors(); - var hasWarnings = stats.hasWarnings(); - if (!hasErrors && !hasWarnings) { - console.log(chalk.green('Compiled successfully!')); - console.log(); - console.log('The app is running at http://localhost:3000/'); - console.log(); - return; - } +detect(port, function(error, _port) { - var json = stats.toJson(); - var formattedErrors = json.errors.map(message => - 'Error in ' + formatMessage(message) - ); - var formattedWarnings = json.warnings.map(message => - 'Warning in ' + formatMessage(message) - ); + if (port !== _port) { + var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); - if (hasErrors) { - console.log(chalk.red('Failed to compile.')); - console.log(); - if (formattedErrors.some(isLikelyASyntaxError)) { - // If there are any syntax errors, show just them. - // This prevents a confusing ESLint parsing error - // preceding a much more useful Babel syntax error. - formattedErrors = formattedErrors.filter(isLikelyASyntaxError); - } - formattedErrors.forEach(message => { - console.log(message); - console.log(); + rl.question('Something is already running at http://localhost:' + port + '. Would you like to run the app at another port instead? [Y/n] ', function(answer){ + if(answer === 'Y') { + port = _port; + config.entry = config.entry.map(function(c){ return c.replace(/(\d+)/g, port) }); // Replace the port in webpack config + compiler = webpack(config, handleCompile); + setupCompiler(); + runDevServer(); + } + rl.close(); }); - // If errors exist, ignore warnings. - return; + } else { + runDevServer(); + setupCompiler(); } +}); - if (hasWarnings) { - console.log(chalk.yellow('Compiled with warnings.')); - console.log(); - formattedWarnings.forEach(message => { - console.log(message); +function setupCompiler() { + compiler.plugin('invalid', function () { + clearConsole(); + console.log('Compiling...'); + }); + + compiler.plugin('done', function (stats) { + clearConsole(); + var hasErrors = stats.hasErrors(); + var hasWarnings = stats.hasWarnings(); + if (!hasErrors && !hasWarnings) { + console.log(chalk.green('Compiled successfully!')); console.log(); - }); + console.log('The app is running at http://localhost:' + port + '/'); + console.log(); + return; + } - console.log('You may use special comments to disable some warnings.'); - console.log('Use ' + chalk.yellow('// eslint-disable-next-line') + ' to ignore the next line.'); - console.log('Use ' + chalk.yellow('/* eslint-disable */') + ' to ignore all warnings in a file.'); - } -}); + var json = stats.toJson(); + var formattedErrors = json.errors.map(message => + 'Error in ' + formatMessage(message) + ); + var formattedWarnings = json.warnings.map(message => + 'Warning in ' + formatMessage(message) + ); + + if (hasErrors) { + console.log(chalk.red('Failed to compile.')); + console.log(); + if (formattedErrors.some(isLikelyASyntaxError)) { + // If there are any syntax errors, show just them. + // This prevents a confusing ESLint parsing error + // preceding a much more useful Babel syntax error. + formattedErrors = formattedErrors.filter(isLikelyASyntaxError); + } + formattedErrors.forEach(message => { + console.log(message); + console.log(); + }); + // If errors exist, ignore warnings. + return; + } + + if (hasWarnings) { + console.log(chalk.yellow('Compiled with warnings.')); + console.log(); + formattedWarnings.forEach(message => { + console.log(message); + console.log(); + }); + + console.log('You may use special comments to disable some warnings.'); + console.log('Use ' + chalk.yellow('// eslint-disable-next-line') + ' to ignore the next line.'); + console.log('Use ' + chalk.yellow('/* eslint-disable */') + ' to ignore all warnings in a file.'); + } + }); +} -function openBrowser() { +function openBrowser(port) { if (process.platform === 'darwin') { try { // Try our best to reuse existing tab @@ -130,7 +161,7 @@ function openBrowser() { execSync( 'osascript ' + path.resolve(__dirname, './openChrome.applescript') + - ' http://localhost:3000/' + ' http://localhost:' + port + '/' ); return; } catch (err) { @@ -139,21 +170,23 @@ function openBrowser() { } // Fallback to opn // (It will always open new tab) - opn('http://localhost:3000/'); + opn('http://localhost:' + port + '/'); } -new WebpackDevServer(compiler, { - historyApiFallback: true, - hot: true, // Note: only CSS is currently hot reloaded - publicPath: config.output.publicPath, - quiet: true -}).listen(3000, 'localhost', function (err, result) { - if (err) { - return console.log(err); - } +function runDevServer() { + new WebpackDevServer(compiler, { + historyApiFallback: true, + hot: true, // Note: only CSS is currently hot reloaded + publicPath: config.output.publicPath, + quiet: true + }).listen(port, 'localhost', function (err, result) { + if (err) { + return console.log(err); + } - clearConsole(); - console.log(chalk.cyan('Starting the development server...')); - console.log(); - openBrowser(); -}); + clearConsole(); + console.log(chalk.cyan('Starting the development server...')); + console.log(); + openBrowser(port); + }); +} From 10c6adfced2b98862c36309a81343dcd1646610f Mon Sep 17 00:00:00 2001 From: Peter Indiola Date: Sat, 23 Jul 2016 05:10:56 +0800 Subject: [PATCH 2/9] remove env port declaration --- scripts/start.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/start.js b/scripts/start.js index 35081562bbf..44cd5810cfa 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -19,7 +19,6 @@ var opn = require('opn'); var detect = require('detect-port'); var readline = require('readline'); var port = 3000; -process.env.PORT = port; // TODO: hide this behind a flag and eliminate dead code on eject. // This shouldn't be exposed to the user. From ac6d9d2472365893837c4de382ea68f761feae99 Mon Sep 17 00:00:00 2001 From: Peter Indiola Date: Sun, 24 Jul 2016 17:00:29 +0800 Subject: [PATCH 3/9] fix function declarion styles --- scripts/start.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/start.js b/scripts/start.js index 003b860c8dc..70bbe583d19 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -27,7 +27,7 @@ var isSmokeTest = process.argv.some(arg => arg.indexOf('--smoke-test') > -1 ); if (isSmokeTest) { - handleCompile = function (err, stats) { + handleCompile = (err, stats) => { if (err || stats.hasErrors() || stats.hasWarnings()) { process.exit(1); } else { @@ -69,7 +69,7 @@ function clearConsole() { } var compiler = webpack(config, handleCompile); -detect(PORT, function(error, _port) { +detect(PORT, (error, _port) => { if (PORT !== _port) { var rl = readline.createInterface({ @@ -77,10 +77,11 @@ detect(PORT, function(error, _port) { output: process.stdout }); - rl.question('Something is already running at http://localhost:' + PORT + '. Would you like to run the app at another port instead? [Y/n] ', function(answer){ + rl.question('Something is already running at http://localhost:' + PORT + '. Would you like to run the app at another port instead? [Y/n] ', answer => { if(answer === 'Y') { PORT = _port; - config.entry = config.entry.map(function(c){ return c.replace(/(\d+)/g, PORT) }); // Replace the port in webpack config + // Replace the port in webpack config + config.entry = config.entry.map(c => c.replace(/(\d+)/g, PORT)); compiler = webpack(config, handleCompile); setupCompiler(); runDevServer(); @@ -94,12 +95,12 @@ detect(PORT, function(error, _port) { }); function setupCompiler() { - compiler.plugin('invalid', function () { + compiler.plugin('invalid', () => { clearConsole(); console.log('Compiling...'); }); - compiler.plugin('done', function (stats) { + compiler.plugin('done', stats => { clearConsole(); var hasErrors = stats.hasErrors(); var hasWarnings = stats.hasWarnings(); @@ -178,7 +179,7 @@ function runDevServer() { hot: true, // Note: only CSS is currently hot reloaded publicPath: config.output.publicPath, quiet: true - }).listen(PORT, 'localhost', function (err, result) { + }).listen(PORT, 'localhost', (err, result) => { if (err) { return console.log(err); } From aa40a965afe26665b7eb238670fdffb5f79e670a Mon Sep 17 00:00:00 2001 From: Peter Indiola Date: Sun, 24 Jul 2016 17:40:36 +0800 Subject: [PATCH 4/9] update for function declartion style --- scripts/start.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/scripts/start.js b/scripts/start.js index 70bbe583d19..37870b93140 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -23,11 +23,12 @@ var PORT = 3000; // TODO: hide this behind a flag and eliminate dead code on eject. // This shouldn't be exposed to the user. var handleCompile; -var isSmokeTest = process.argv.some(arg => +var isSmokeTest = process.argv.some(function(arg) { arg.indexOf('--smoke-test') > -1 -); +}); + if (isSmokeTest) { - handleCompile = (err, stats) => { + handleCompile = function(err, stats) { if (err || stats.hasErrors() || stats.hasWarnings()) { process.exit(1); } else { @@ -69,7 +70,7 @@ function clearConsole() { } var compiler = webpack(config, handleCompile); -detect(PORT, (error, _port) => { +detect(PORT, function(error, _port) { if (PORT !== _port) { var rl = readline.createInterface({ @@ -77,11 +78,13 @@ detect(PORT, (error, _port) => { output: process.stdout }); - rl.question('Something is already running at http://localhost:' + PORT + '. Would you like to run the app at another port instead? [Y/n] ', answer => { + rl.question('Something is already running at http://localhost:' + PORT + '. Would you like to run the app at another port instead? [Y/n] ', function(answer) { if(answer === 'Y') { PORT = _port; - // Replace the port in webpack config - config.entry = config.entry.map(c => c.replace(/(\d+)/g, PORT)); + // Replace the port in webpack config + config.entry = config.entry.map(function(c) { + return c.replace(/(\d+)/g, PORT) + }); compiler = webpack(config, handleCompile); setupCompiler(); runDevServer(); @@ -95,12 +98,12 @@ detect(PORT, (error, _port) => { }); function setupCompiler() { - compiler.plugin('invalid', () => { + compiler.plugin('invalid', function() { clearConsole(); console.log('Compiling...'); }); - compiler.plugin('done', stats => { + compiler.plugin('done', function(stats) { clearConsole(); var hasErrors = stats.hasErrors(); var hasWarnings = stats.hasWarnings(); @@ -113,12 +116,12 @@ function setupCompiler() { } var json = stats.toJson(); - var formattedErrors = json.errors.map(message => + var formattedErrors = json.errors.map(function(message) { 'Error in ' + formatMessage(message) - ); - var formattedWarnings = json.warnings.map(message => + }); + var formattedWarnings = json.warnings.map(function(message) { 'Warning in ' + formatMessage(message) - ); + }); if (hasErrors) { console.log(chalk.red('Failed to compile.')); @@ -129,7 +132,7 @@ function setupCompiler() { // preceding a much more useful Babel syntax error. formattedErrors = formattedErrors.filter(isLikelyASyntaxError); } - formattedErrors.forEach(message => { + formattedErrors.forEach(function(message) { console.log(message); console.log(); }); @@ -140,7 +143,7 @@ function setupCompiler() { if (hasWarnings) { console.log(chalk.yellow('Compiled with warnings.')); console.log(); - formattedWarnings.forEach(message => { + formattedWarnings.forEach(function(message) { console.log(message); console.log(); }); @@ -179,7 +182,7 @@ function runDevServer() { hot: true, // Note: only CSS is currently hot reloaded publicPath: config.output.publicPath, quiet: true - }).listen(PORT, 'localhost', (err, result) => { + }).listen(PORT, 'localhost', function(err, result) { if (err) { return console.log(err); } From ea71a78b4e3d27255493db5cf526069d15e1dccf Mon Sep 17 00:00:00 2001 From: Peter Indiola Date: Sun, 24 Jul 2016 18:24:31 +0800 Subject: [PATCH 5/9] fix eslint warnings --- scripts/start.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/start.js b/scripts/start.js index 37870b93140..2fda32131ef 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -24,7 +24,7 @@ var PORT = 3000; // This shouldn't be exposed to the user. var handleCompile; var isSmokeTest = process.argv.some(function(arg) { - arg.indexOf('--smoke-test') > -1 + return arg.indexOf('--smoke-test') > -1 }); if (isSmokeTest) { @@ -117,10 +117,10 @@ function setupCompiler() { var json = stats.toJson(); var formattedErrors = json.errors.map(function(message) { - 'Error in ' + formatMessage(message) + return 'Error in ' + formatMessage(message) }); var formattedWarnings = json.warnings.map(function(message) { - 'Warning in ' + formatMessage(message) + return 'Warning in ' + formatMessage(message) }); if (hasErrors) { From 63b42906b986db40d4dbeb12f3353df9cb8f2e26 Mon Sep 17 00:00:00 2001 From: Peter Indiola Date: Sun, 24 Jul 2016 19:19:59 +0800 Subject: [PATCH 6/9] revert back since arrow functions are supported --- scripts/start.js | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/scripts/start.js b/scripts/start.js index 2fda32131ef..fd34fdad97c 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -23,12 +23,11 @@ var PORT = 3000; // TODO: hide this behind a flag and eliminate dead code on eject. // This shouldn't be exposed to the user. var handleCompile; -var isSmokeTest = process.argv.some(function(arg) { - return arg.indexOf('--smoke-test') > -1 -}); - +var isSmokeTest = process.argv.some(arg => + arg.indexOf('--smoke-test') > -1 +); if (isSmokeTest) { - handleCompile = function(err, stats) { + handleCompile = function (err, stats) { if (err || stats.hasErrors() || stats.hasWarnings()) { process.exit(1); } else { @@ -70,7 +69,7 @@ function clearConsole() { } var compiler = webpack(config, handleCompile); -detect(PORT, function(error, _port) { +detect(PORT, (error, _port) => { if (PORT !== _port) { var rl = readline.createInterface({ @@ -78,13 +77,11 @@ detect(PORT, function(error, _port) { output: process.stdout }); - rl.question('Something is already running at http://localhost:' + PORT + '. Would you like to run the app at another port instead? [Y/n] ', function(answer) { + rl.question('Something is already running at http://localhost:' + PORT + '. Would you like to run the app at another port instead? [Y/n] ', (answer) => { if(answer === 'Y') { PORT = _port; // Replace the port in webpack config - config.entry = config.entry.map(function(c) { - return c.replace(/(\d+)/g, PORT) - }); + config.entry = config.entry.map(c => c.replace(/(\d+)/g, PORT)); compiler = webpack(config, handleCompile); setupCompiler(); runDevServer(); @@ -116,12 +113,12 @@ function setupCompiler() { } var json = stats.toJson(); - var formattedErrors = json.errors.map(function(message) { - return 'Error in ' + formatMessage(message) - }); - var formattedWarnings = json.warnings.map(function(message) { - return 'Warning in ' + formatMessage(message) - }); + var formattedErrors = json.errors.map(message => + 'Error in ' + formatMessage(message) + ); + var formattedWarnings = json.warnings.map(message => + 'Warning in ' + formatMessage(message) + ); if (hasErrors) { console.log(chalk.red('Failed to compile.')); @@ -132,7 +129,7 @@ function setupCompiler() { // preceding a much more useful Babel syntax error. formattedErrors = formattedErrors.filter(isLikelyASyntaxError); } - formattedErrors.forEach(function(message) { + formattedErrors.forEach(message => { console.log(message); console.log(); }); @@ -143,7 +140,7 @@ function setupCompiler() { if (hasWarnings) { console.log(chalk.yellow('Compiled with warnings.')); console.log(); - formattedWarnings.forEach(function(message) { + formattedWarnings.forEach(message => { console.log(message); console.log(); }); @@ -182,7 +179,7 @@ function runDevServer() { hot: true, // Note: only CSS is currently hot reloaded publicPath: config.output.publicPath, quiet: true - }).listen(PORT, 'localhost', function(err, result) { + }).listen(PORT, 'localhost', (err, result) => { if (err) { return console.log(err); } From c0f391a2835ef135d9fdb8c25c728306d4e5a174 Mon Sep 17 00:00:00 2001 From: Peter Indiola Date: Mon, 25 Jul 2016 11:33:57 +0800 Subject: [PATCH 7/9] Refactor and code cleanup --- scripts/start.js | 73 ++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/scripts/start.js b/scripts/start.js index fd34fdad97c..98bd886cbae 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -18,7 +18,7 @@ var execSync = require('child_process').execSync; var opn = require('opn'); var detect = require('detect-port'); var readline = require('readline'); -var PORT = 3000; +var DEFAULT_PORT = 3000; // TODO: hide this behind a flag and eliminate dead code on eject. // This shouldn't be exposed to the user. @@ -69,32 +69,38 @@ function clearConsole() { } var compiler = webpack(config, handleCompile); -detect(PORT, (error, _port) => { - - if (PORT !== _port) { - var rl = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - - rl.question('Something is already running at http://localhost:' + PORT + '. Would you like to run the app at another port instead? [Y/n] ', (answer) => { - if(answer === 'Y') { - PORT = _port; - // Replace the port in webpack config - config.entry = config.entry.map(c => c.replace(/(\d+)/g, PORT)); - compiler = webpack(config, handleCompile); - setupCompiler(); - runDevServer(); - } - rl.close(); - }); - } else { - runDevServer(); - setupCompiler(); - } -}); -function setupCompiler() { +function promptForPort(suggestedPort) { + return new Promise((resolve, reject) => { + if (DEFAULT_PORT !== suggestedPort) { + var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + + var question = chalk.red('Something is already running at port ' + suggestedPort) + + '\nWould you like to run the app at another port instead? [Y/n] '; + + rl.question(question, answer => { + var shouldChangePort = ( + answer.length === 0 || + answer.match(/yes|y/i) + ); + + if (shouldChangePort) { + // Replace the port in webpack config + config.entry.map(c => c.replace(/(\d+)/g, suggestedPort)); + resolve(suggestedPort); + } + rl.close(); + }); + } else { + resolve(DEFAULT_PORT); + } + }); +} + +function setupCompiler(port) { compiler.plugin('invalid', function() { clearConsole(); console.log('Compiling...'); @@ -107,7 +113,7 @@ function setupCompiler() { if (!hasErrors && !hasWarnings) { console.log(chalk.green('Compiled successfully!')); console.log(); - console.log('The app is running at http://localhost:' + PORT + '/'); + console.log('The app is running at http://localhost:' + port + '/'); console.log(); return; } @@ -173,13 +179,13 @@ function openBrowser(port) { opn('http://localhost:' + port + '/'); } -function runDevServer() { +function runDevServer(port) { new WebpackDevServer(compiler, { historyApiFallback: true, hot: true, // Note: only CSS is currently hot reloaded publicPath: config.output.publicPath, quiet: true - }).listen(PORT, 'localhost', (err, result) => { + }).listen(port, (err, result) => { if (err) { return console.log(err); } @@ -187,6 +193,13 @@ function runDevServer() { clearConsole(); console.log(chalk.cyan('Starting the development server...')); console.log(); - openBrowser(PORT); + openBrowser(port); }); } + +detect(DEFAULT_PORT) + .then(promptForPort) + .then(port => { + setupCompiler(port); + runDevServer(port); + }); From 245676c97c38a7afc425b3ac3ce363c59f7adf30 Mon Sep 17 00:00:00 2001 From: Jeff L Date: Mon, 25 Jul 2016 16:08:53 -0400 Subject: [PATCH 8/9] Port error msg Updated the error msg to reference default port instead of suggested port --- scripts/start.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/start.js b/scripts/start.js index 98bd886cbae..ef11f556507 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -78,7 +78,7 @@ function promptForPort(suggestedPort) { output: process.stdout }); - var question = chalk.red('Something is already running at port ' + suggestedPort) + + var question = chalk.red('Something is already running at port ' + DEFAULT_PORT) + '\nWould you like to run the app at another port instead? [Y/n] '; rl.question(question, answer => { From 78c4bf574f928851ee38e94e0c8e372d16ba83cf Mon Sep 17 00:00:00 2001 From: Peter Indiola Date: Tue, 26 Jul 2016 12:43:50 +0800 Subject: [PATCH 9/9] added prompt utility. adress pr comments --- scripts/eject.js | 13 +------ scripts/start.js | 68 ++++++++++++++++--------------------- scripts/utilities/prompt.js | 13 +++++++ 3 files changed, 44 insertions(+), 50 deletions(-) create mode 100644 scripts/utilities/prompt.js diff --git a/scripts/eject.js b/scripts/eject.js index efdff07400c..9963e7e77ac 100644 --- a/scripts/eject.js +++ b/scripts/eject.js @@ -9,20 +9,9 @@ var fs = require('fs'); var path = require('path'); -var rl = require('readline'); var rimrafSync = require('rimraf').sync; var spawnSync = require('cross-spawn').sync; - -var prompt = function(question, cb) { - var rlInterface = rl.createInterface({ - input: process.stdin, - output: process.stdout, - }); - rlInterface.question(question + '\n', function(answer) { - rlInterface.close(); - cb(answer); - }) -} +var prompt = require('./utilities/prompt'); prompt('Are you sure you want to eject? This action is permanent. [y/N]', function(answer) { var shouldEject = answer && ( diff --git a/scripts/start.js b/scripts/start.js index ef11f556507..1d02fe5fdec 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -17,8 +17,9 @@ var config = require('../config/webpack.config.dev'); var execSync = require('child_process').execSync; var opn = require('opn'); var detect = require('detect-port'); -var readline = require('readline'); +var prompt = require('./utilities/prompt'); var DEFAULT_PORT = 3000; +var compiler; // TODO: hide this behind a flag and eliminate dead code on eject. // This shouldn't be exposed to the user. @@ -68,39 +69,12 @@ function clearConsole() { process.stdout.write('\x1B[2J\x1B[0f'); } -var compiler = webpack(config, handleCompile); - -function promptForPort(suggestedPort) { - return new Promise((resolve, reject) => { - if (DEFAULT_PORT !== suggestedPort) { - var rl = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - - var question = chalk.red('Something is already running at port ' + DEFAULT_PORT) + - '\nWould you like to run the app at another port instead? [Y/n] '; - - rl.question(question, answer => { - var shouldChangePort = ( - answer.length === 0 || - answer.match(/yes|y/i) - ); - - if (shouldChangePort) { - // Replace the port in webpack config - config.entry.map(c => c.replace(/(\d+)/g, suggestedPort)); - resolve(suggestedPort); - } - rl.close(); - }); - } else { - resolve(DEFAULT_PORT); - } +function setupCompiler(port) { + var copyConfig = Object.assign({}, config, { + entry: config.entry.map(c => c.replace(/(\d+)/g, port)) }); -} -function setupCompiler(port) { + compiler = webpack(copyConfig, handleCompile); compiler.plugin('invalid', function() { clearConsole(); console.log('Compiling...'); @@ -197,9 +171,27 @@ function runDevServer(port) { }); } -detect(DEFAULT_PORT) - .then(promptForPort) - .then(port => { - setupCompiler(port); - runDevServer(port); - }); +function run(port) { + setupCompiler(port); + runDevServer(port); +} + +detect(DEFAULT_PORT).then(port => { + + if (port !== DEFAULT_PORT) { + var question = chalk.red('Something is already running at port ' + DEFAULT_PORT) + + '\nWould you like to run the app at another port instead? [y/N]'; + + prompt(question, answer => { + var shouldChangePort = ( + answer.length === 0 || + answer.match(/^yes|y$/i) + ); + if (shouldChangePort) { + run(port); + } + }); + } else { + run(port); + } +}); diff --git a/scripts/utilities/prompt.js b/scripts/utilities/prompt.js new file mode 100644 index 00000000000..888f7266da5 --- /dev/null +++ b/scripts/utilities/prompt.js @@ -0,0 +1,13 @@ +var rl = require('readline'); + +module.exports = function (question, cb) { + var rlInterface = rl.createInterface({ + input: process.stdin, + output: process.stdout, + }); + + rlInterface.question(question + '\n', function(answer) { + rlInterface.close(); + cb(answer); + }); +};