Skip to content

Commit 43fd7bc

Browse files
committed
Use app's package name when CRA app is running on another port
1 parent 97e32ca commit 43fd7bc

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

packages/react-dev-utils/getProcessForPort.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var chalk = require('chalk');
22
var execSync = require('child_process').execSync;
3+
var path = require('path');
34

45
var execOptions = { encoding: 'utf8' };
56

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

14-
function getProcessCommandById(processId) {
15+
function getPackageNameInDirectory(directory) {
16+
var packagePath = path.join(directory.trim(), 'package.json');
17+
18+
try {
19+
return require(packagePath).name;
20+
} catch(e) {
21+
return null;
22+
}
23+
24+
}
25+
26+
function getProcessCommand(processId, processDirectory) {
1527
var command = execSync('ps -o command -p ' + processId + ' | sed -n 2p', execOptions);
16-
return (isProcessAReactApp(command)) ? 'create-react-app\n' : command;
28+
29+
if (isProcessAReactApp(command)) {
30+
const packageName = getPackageNameInDirectory(processDirectory);
31+
return (packageName) ? packageName + '\n' : command;
32+
} else {
33+
return command;
34+
}
35+
1736
}
1837

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

2746
var processCommandsAndDirectories = processIds.map(function(processId) {
28-
var command = getProcessCommandById(processId);
2947
var directory = getDirectoryOfProcessById(processId);
48+
var command = getProcessCommand(processId, directory);
3049
return chalk.cyan(command) + chalk.blue(' in ') + chalk.cyan(directory);
3150
});
3251

0 commit comments

Comments
 (0)