Skip to content

Commit

Permalink
Filter port process by those listening
Browse files Browse the repository at this point in the history
- Removed the handling of multiple process IDs since you can filtering by listening process (and not have the browser in the list of processes)
- Trimmed the terminal outputs for better matching (process id) and better terminal output (directory of process)
  • Loading branch information
ianmcnally committed Nov 22, 2016
1 parent 43fd7bc commit 89ffa37
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions packages/react-dev-utils/getProcessForPort.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ 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 getProcessIdOnPort(port) {
return execSync('lsof -i:' + port + ' -P -t -sTCP:LISTEN', execOptions).trim();
}

function getPackageNameInDirectory(directory) {
Expand All @@ -36,20 +36,15 @@ function getProcessCommand(processId, processDirectory) {
}

function getDirectoryOfProcessById(processId) {
return execSync('lsof -p '+ processId + ' | grep cwd | awk \'{print $9}\'', execOptions);
return execSync('lsof -p '+ processId + ' | grep cwd | awk \'{print $9}\'', execOptions).trim();
}

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 ');
var processId = getProcessIdOnPort(port);
var directory = getDirectoryOfProcessById(processId);
var command = getProcessCommand(processId, directory);
return chalk.cyan(command) + chalk.blue(' in ') + chalk.cyan(directory);
} catch(e) {
return null;
}
Expand Down

0 comments on commit 89ffa37

Please sign in to comment.