-
-
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
add logging of existing default port process on start #816
Merged
fson
merged 15 commits into
facebook:master
from
ianmcnally:guess-existing-port-process-441
Nov 22, 2016
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
626c8d0
add logging of existing port process on start
dc824f5
Move port process wording in start command on to next line
abeca8a
Color the named processes as cyan in terminal output
c08a9e1
Add handling for multiple processes on a part
e8d0053
Add process directory to existing port warning
ianmcnally e3dcaaf
Change output color to all cyan, except "in"
ianmcnally 9c2e3aa
Rename getProcessNameOnPort -> getProcessForPort
ianmcnally 52e70f4
Add checking if process is a CRA instance, to customize port running …
ianmcnally 85e8151
Move getProcessForPort to react-dev-utils
ianmcnally 29be9d6
Add documentation for getProcessForPort
ianmcnally 97e32ca
Add getProcessForPort to list of dev-scripts files
ianmcnally 43fd7bc
Use app's package name when CRA app is running on another port
ianmcnally 89ffa37
Filter port process by those listening
ianmcnally e25f113
Update README on port helpers, to specify only one port returned
ianmcnally 03122b5
Add ignore of stderr when executing process commands
ianmcnally File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
var chalk = require('chalk'); | ||
var execSync = require('child_process').execSync; | ||
var path = require('path'); | ||
|
||
var execOptions = { encoding: 'utf8' }; | ||
|
||
function isProcessAReactApp(processCommand) { | ||
return /^node .*react-scripts\/scripts\/start\.js\s?$/.test(processCommand); | ||
} | ||
|
||
function getProcessIdsOnPort(port) { | ||
return execSync('lsof -i:' + port + ' -P -t', execOptions).match(/(\S+)/g); | ||
} | ||
|
||
function getPackageNameInDirectory(directory) { | ||
var packagePath = path.join(directory.trim(), 'package.json'); | ||
|
||
try { | ||
return require(packagePath).name; | ||
} catch(e) { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
function getProcessCommand(processId, processDirectory) { | ||
var command = execSync('ps -o command -p ' + processId + ' | sed -n 2p', execOptions); | ||
|
||
if (isProcessAReactApp(command)) { | ||
const packageName = getPackageNameInDirectory(processDirectory); | ||
return (packageName) ? packageName + '\n' : command; | ||
} else { | ||
return command; | ||
} | ||
|
||
} | ||
|
||
function getDirectoryOfProcessById(processId) { | ||
return execSync('lsof -p '+ processId + ' | grep cwd | awk \'{print $9}\'', execOptions); | ||
} | ||
|
||
function getProcessForPort(port) { | ||
try { | ||
var processIds = getProcessIdsOnPort(port); | ||
|
||
var processCommandsAndDirectories = processIds.map(function(processId) { | ||
var directory = getDirectoryOfProcessById(processId); | ||
var command = getProcessCommand(processId, directory); | ||
return chalk.cyan(command) + chalk.blue(' in ') + chalk.cyan(directory); | ||
}); | ||
|
||
return processCommandsAndDirectories.join('\n '); | ||
} catch(e) { | ||
return null; | ||
} | ||
} | ||
|
||
module.exports = getProcessForPort; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Let's modify the command so that we only get listening processes. Adding
-sTCP:LISTEN
should do the trick.Otherwise looks great.
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.
excellent find, thanks @fson. i'll add it in
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.
Do we need to detect/support Windows here?
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.
It's fine to not support Windows for "niceties" like this as long as there's a reasonable fallback and it doesn't crash.
Somebody can then add Windows-specific code in a future PR.
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.
Agreed, just want to make sure it doesn't crash.