Skip to content

Commit

Permalink
feat: add debug url output
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Oct 16, 2024
1 parent 1dfc8e7 commit be0b328
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
26 changes: 24 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function run() {
parentArgs,
});
runChild.onServerReady(
async (serverReportOption, isFirstCallback, during) => {
async (serverReportOption, isFirstCallback, during, debugUrl) => {
if (isFirstCallback) {
// 第一次启动把端口等信息打印出来
console.log('');
Expand Down Expand Up @@ -230,15 +230,37 @@ function run() {
)}`
);
}
if (debugUrl) {
output(
`${colors.green('➜')} ${colors.dim(
`Debugger: devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=${debugUrl.replace(
'ws://',
''
)}`
)}`
);
}
console.log('');
}
triggerMessage('server-first-ready');
} else {
output('');
output(
`${colors.green('Node.js server')} ${colors.dim(
'restarted in'
)} ${during} ms\n`
)} ${during} ms`
);
if (debugUrl) {
output(
`${colors.green('➜')} ${colors.dim(
`Debugger: devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=${debugUrl.replace(
'ws://',
''
)}`
)}`
);
}
output('');
triggerMessage('server-ready');
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,21 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => {
execArgv: execArgv,
});
debug(`fork run process, pid = ${runChild.pid}`);
let currentDebugUrl;
const onServerReady = async data => {
try {
if (data.title === 'server-ready') {
onServerReadyCallback &&
(await onServerReadyCallback(
data,
isFirstFork,
Date.now() - startTime
Date.now() - startTime,
currentDebugUrl
));
runChild.removeListener('message', onServerReady);
lastBootstrapStatus = true;
} else if (data.title === 'debug-url') {
currentDebugUrl = data.debugUrl;
}
} catch (err) {
console.error(err);
Expand Down
13 changes: 13 additions & 0 deletions lib/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ const childPath = process.env.CHILD_CMD_PATH;
const childCwd = process.env.CHILD_CWD;
const { join, isAbsolute } = require('path');
const { CHILD_PROCESS_EXCEPTION_EXIT_CODE } = require('./constants');
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const inspector = require('inspector');

if (process.debugPort) {
const debugUrl = inspector.url();
// 防止拿失败
if (/ws/.test(debugUrl)) {
process.send({
title: 'debug-url',
debugUrl,
});
}
}

// get the child process execArgs
const runArgs = process.argv.slice(2);
Expand Down

0 comments on commit be0b328

Please sign in to comment.