Skip to content

Commit a01f754

Browse files
committed
Create helper to setup exit handlers
1 parent 4053774 commit a01f754

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

packages/react-dev-utils/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ module.exports = {
105105
}
106106
```
107107

108+
#### `addProcessExitHandlers(): void`
109+
110+
Exits the process when stdin is closed by an external program.<br>
111+
This is useful when the dev server is started by some other tool (i.e.: Phoenix)
112+
which then uses the standard Unix convention of closing the standard input to
113+
signal the end of the program.
114+
115+
```js
116+
var addProcessExitHandlers = require('react-dev-utils/addProcessExitHandlers');
117+
118+
addProcessExitHandlers();
119+
```
120+
108121
#### `checkRequiredFiles(files: Array<string>): boolean`
109122

110123
Makes sure that all passed files exist.<br>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
function addProcessExitHandlers() {
11+
// Issue: https://github.com/facebookincubator/create-react-app/issues/1753
12+
// The below lines are added to make sure that this process is
13+
// exited when stdin is ended. The consequence of not doing this means
14+
// that all watch processes will stay running despite the process that spawned
15+
// them being closed.
16+
process.stdin.on('end', function() {
17+
process.exit(0);
18+
});
19+
process.stdin.resume();
20+
}
21+
22+
module.exports = addProcessExitHandlers;

packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"node": ">=6"
1212
},
1313
"files": [
14+
"addProcessExitHandlers.js",
1415
"checkRequiredFiles.js",
1516
"clearConsole.js",
1617
"crashOverlay.js",

packages/react-scripts/scripts/start.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const openBrowser = require('react-dev-utils/openBrowser');
3838
const paths = require('../config/paths');
3939
const config = require('../config/webpack.config.dev');
4040
const createDevServerConfig = require('../config/webpackDevServer.config');
41+
const addProcessExitHandlers = require('react-dev-utils/addProcessExitHandlers');
4142

4243
const useYarn = fs.existsSync(paths.yarnLockFile);
4344
const isInteractive = process.stdout.isTTY;
@@ -47,15 +48,7 @@ if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
4748
process.exit(1);
4849
}
4950

50-
// Issue: https://github.com/facebookincubator/create-react-app/issues/1753
51-
// The below lines are added to make sure that this process is
52-
// exited when stdin is ended. The consequence of not doing this means
53-
// that all watch processes will stay running despite the process that spawned
54-
// them being closed.
55-
process.stdin.on('end', function() {
56-
process.exit(0);
57-
});
58-
process.stdin.resume();
51+
addProcessExitHandlers();
5952

6053
// Tools like Cloud9 rely on this.
6154
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;

packages/react-scripts/scripts/test.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ process.on('unhandledRejection', err => {
2323
// Ensure environment variables are read.
2424
require('../config/env');
2525

26+
const addProcessExitHandlers = require('react-dev-utils/addProcessExitHandlers');
2627
const jest = require('jest');
2728
const argv = process.argv.slice(2);
2829

@@ -31,16 +32,7 @@ const argv = process.argv.slice(2);
3132
if (!process.env.CI && argv.indexOf('--coverage') < 0) {
3233
argv.push('--watch');
3334

34-
// Issue: https://github.com/facebookincubator/create-react-app/issues/1753
35-
// The below lines are added to make sure that this process is
36-
// exited when stdin is ended. The consequence of not doing this means
37-
// that all watch processes will stay running despite the process that spawned
38-
// them being closed.
39-
40-
process.stdin.on('end', function() {
41-
process.exit(0);
42-
});
43-
process.stdin.resume();
35+
addProcessExitHandlers();
4436
}
4537

4638
// @remove-on-eject-begin

0 commit comments

Comments
 (0)