Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getExistingProcesses() #454

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
Copy link

@TryingToImprove TryingToImprove Aug 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lsof, grep or cut is not valid windows commands..

netstat might be helpful, by requires administrative privileges..

C:\WINDOWS\system32>netstat -aon | findstr 3000
  TCP    127.0.0.1:3000         0.0.0.0:0              LISTENING       15564

C:\WINDOWS\system32>tasklist /fi "pid eq 15564"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
node.exe                     15564 Console                    5    125.580 K

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also run the second line I mentioned this way you get the entire command that started it instead of just "node". This way we get the entire path of the app running which should be more helpful :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For windows support, let's make it work on one platform and have the infra in place so that someone using windows can chime in and make it work over there with a good user experience!

exec(command, function (error, stdout, stderr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use execSync, it's going to make the code way easier to understand

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.
Expand Down Expand Up @@ -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 => {
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to abort the entire thing if we couldn't determine what's running on the port. Not printing the hint around what is running on that port would be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d prefer to print something along these lines:

// could not determine
Something is already running on port ${DEFAULT_PORT}:
// could determine
Something (probably, node) is already running on port ${DEFAULT_PORT}:

});
});