-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d prefer to print something along these lines:
|
||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lsof
,grep
orcut
is not valid windows commands..netstat
might be helpful, by requires administrative privileges..There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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!