Skip to content

Commit

Permalink
Use app's package name when CRA app is running on another port
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmcnally committed Oct 13, 2016
1 parent 97e32ca commit 43fd7bc
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/react-dev-utils/getProcessForPort.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var chalk = require('chalk');
var execSync = require('child_process').execSync;
var path = require('path');

var execOptions = { encoding: 'utf8' };

Expand All @@ -11,9 +12,27 @@ function getProcessIdsOnPort(port) {
return execSync('lsof -i:' + port + ' -P -t', execOptions).match(/(\S+)/g);
}

function getProcessCommandById(processId) {
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);
return (isProcessAReactApp(command)) ? 'create-react-app\n' : command;

if (isProcessAReactApp(command)) {
const packageName = getPackageNameInDirectory(processDirectory);
return (packageName) ? packageName + '\n' : command;
} else {
return command;
}

}

function getDirectoryOfProcessById(processId) {
Expand All @@ -25,8 +44,8 @@ function getProcessForPort(port) {
var processIds = getProcessIdsOnPort(port);

var processCommandsAndDirectories = processIds.map(function(processId) {
var command = getProcessCommandById(processId);
var directory = getDirectoryOfProcessById(processId);
var command = getProcessCommand(processId, directory);
return chalk.cyan(command) + chalk.blue(' in ') + chalk.cyan(directory);
});

Expand Down

0 comments on commit 43fd7bc

Please sign in to comment.