From 06a2b6cb5f462e8a4b8ae8e7641e5842ec644fd1 Mon Sep 17 00:00:00 2001 From: millerm Date: Mon, 15 Aug 2016 21:42:27 -0700 Subject: [PATCH] Added function to dislpay names of existing processes running on default port --- scripts/start.js | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/scripts/start.js b/scripts/start.js index aa68ab40ff3..6030f3d73cc 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -16,6 +16,7 @@ var WebpackDevServer = require('webpack-dev-server'); var historyApiFallback = require('connect-history-api-fallback'); var httpProxyMiddleware = require('http-proxy-middleware'); var execSync = require('child_process').execSync; +var exec = require('child_process').exec; var opn = require('opn'); var detect = require('detect-port'); var prompt = require('./utils/prompt'); @@ -72,6 +73,20 @@ function clearConsole() { process.stdout.write('\x1bc'); } +function getExistingProcesses(port) { + //determine if another process running on the desired port + return new Promise(function (resolve, reject) { + var command = 'lsof -P | grep TCP | grep :' + port + ' | cut -d \' \' -f 1'; + exec(command, function (error, stdout, stderr) { + if (error) { + reject(error); + } + + resolve(stdout); + }); + }); +} + function setupCompiler(port) { // "Compiler" is a low-level interface to Webpack. // It lets us listen to some events and provide our own custom messages. @@ -259,6 +274,7 @@ function run(port) { runDevServer(port); } + // We attempt to use the default port but if it is busy, we offer the user to // run on a different port. `detect()` Promise resolves to the next free port. detect(DEFAULT_PORT).then(port => { @@ -268,13 +284,24 @@ detect(DEFAULT_PORT).then(port => { } clearConsole(); - var question = - chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.') + - '\n\nWould you like to run the app on another port instead?'; - prompt(question, true).then(shouldChangePort => { - if (shouldChangePort) { - run(port); - } - }); + + getExistingProcesses(DEFAULT_PORT) + .then(res => chalk.yellow( + `Something is already running on port ${DEFAULT_PORT}: + + The existing process is: ${res} + + Would you like to run the app on another port instead? + ` + )) + .then(question => prompt(question, true)) + .then((shouldChangePort) => { + if (shouldChangePort) { + run(port); + } + }) + .catch((err) => { + throw new Error(err); + }); });