Skip to content
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
merged 15 commits into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/react-dev-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ compiler.plugin('done', function(stats) {
});
```

#### `getProcessForPort(port: number): string`

Finds the currently running process(es) on `port`.
Returns a string containing the name and directory, e.g.,

```
create-react-app
in /Users/developer/create-react-app
```

```js
var getProcessForPort = require('react-dev-utils/getProcessForPort');

getProcessForPort(3000);
```

#### `openBrowser(url: string): boolean`

Attempts to open the browser with a given URL.
Expand Down
59 changes: 59 additions & 0 deletions packages/react-dev-utils/getProcessForPort.js
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);
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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?

Copy link
Contributor

@gaearon gaearon Nov 22, 2016

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.

Copy link
Contributor

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.

}

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;

1 change: 1 addition & 0 deletions packages/react-dev-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"clearConsole.js",
"checkRequiredFiles.js",
"formatWebpackMessages.js",
"getProcessForPort.js",
"InterpolateHtmlPlugin.js",
"openChrome.applescript",
"openBrowser.js",
Expand Down
7 changes: 5 additions & 2 deletions packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var detect = require('detect-port');
var clearConsole = require('react-dev-utils/clearConsole');
var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
var formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
var getProcessForPort = require('react-dev-utils/getProcessForPort');
var openBrowser = require('react-dev-utils/openBrowser');
var prompt = require('react-dev-utils/prompt');
var config = require('../config/webpack.config.dev');
Expand Down Expand Up @@ -267,9 +268,11 @@ detect(DEFAULT_PORT).then(port => {
}

clearConsole();
var existingProcess = getProcessForPort(DEFAULT_PORT);
var question =
chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.') +
'\n\nWould you like to run the app on another port instead?';
chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.' +
((existingProcess) ? ' Probably:\n ' + existingProcess : '')) +
'\n\nWould you like to run the app on another port instead?';

prompt(question, true).then(shouldChangePort => {
if (shouldChangePort) {
Expand Down