@fesjs/fes
Version:
一个好用的前端管理台快速开发框架
55 lines (53 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = start;
var _nodeChild_process = require("node:child_process");
var _nodeProcess = _interopRequireDefault(require("node:process"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const usedPorts = [];
let CURRENT_PORT;
function start({
scriptPath
}) {
const execArgv = _nodeProcess.default.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 = Number.parseInt(s1, 10) + 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) {
_nodeProcess.default.env.PORT = CURRENT_PORT;
}
const child = (0, _nodeChild_process.fork)(scriptPath, _nodeProcess.default.argv.slice(2), {
execArgv
});
child.on('message', data => {
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;
}
_nodeProcess.default.send && _nodeProcess.default.send(data);
});
return child;
}