From be0b32866a49aad73efca8f4c86dca2663d28e1a Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Wed, 16 Oct 2024 16:14:06 +0800 Subject: [PATCH] feat: add debug url output --- lib/index.js | 26 ++++++++++++++++++++++++-- lib/process.js | 6 +++++- lib/wrap.js | 13 +++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4bd7d70..84282c7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -200,7 +200,7 @@ function run() { parentArgs, }); runChild.onServerReady( - async (serverReportOption, isFirstCallback, during) => { + async (serverReportOption, isFirstCallback, during, debugUrl) => { if (isFirstCallback) { // 第一次启动把端口等信息打印出来 console.log(''); @@ -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'); } } diff --git a/lib/process.js b/lib/process.js index 2b2c352..82f8d94 100644 --- a/lib/process.js +++ b/lib/process.js @@ -139,6 +139,7 @@ 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') { @@ -146,10 +147,13 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => { (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); diff --git a/lib/wrap.js b/lib/wrap.js index 0be2076..adb7b1f 100644 --- a/lib/wrap.js +++ b/lib/wrap.js @@ -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);