fork.js
1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = start;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _child_process() {
const data = require("child_process");
_child_process = function _child_process() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const usedPorts = [];
let CURRENT_PORT;
function start({
scriptPath
}) {
const execArgv = process.execArgv.slice(0);
const inspectArgvIndex = execArgv.findIndex(argv => argv.includes('--inspect-brk'));
if (inspectArgvIndex > -1) {
const inspectArgv = execArgv[inspectArgvIndex];
execArgv.splice(inspectArgvIndex, 1, inspectArgv.replace(/--inspect-brk=(.*)/, (match, s1) => {
let port;
try {
port = parseInt(s1) + 1;
} catch (e) {
port = 9230; // node default inspect port plus 1.
}
if (usedPorts.includes(port)) {
port += 1;
}
usedPorts.push(port);
return `--inspect-brk=${port}`;
}));
} // set port to env when current port has value
if (CURRENT_PORT) {
// @ts-ignore
process.env.PORT = CURRENT_PORT;
}
const child = (0, _child_process().fork)(scriptPath, process.argv.slice(2), {
execArgv
});
child.on('message', data => {
var _process$send, _process;
const type = data && data.type || null;
if (type === 'RESTART') {
child.kill();
start({
scriptPath
});
} else if (type === 'UPDATE_PORT') {
// set current used port
CURRENT_PORT = data.port;
}
(_process$send = (_process = process).send) === null || _process$send === void 0 ? void 0 : _process$send.call(_process, data);
});
return child;
}