@@ -4,6 +4,7 @@ import process from 'node:process'
44
55import c from 'ansis'
66import cac from 'cac'
7+ import { createDevServer , resolveDevServerPort } from 'devframe/adapters/dev'
78import {
89 DEVTOOLS_CONNECTION_META_FILENAME ,
910 DEVTOOLS_RPC_DUMP_DIRNAME ,
@@ -13,12 +14,9 @@ import {
1314 collectStaticRpcDump ,
1415 createH3DevToolsHost ,
1516 createHostContext ,
16- startHttpAndWs ,
1717} from 'devframe/node'
1818import { strictJsonStringify , structuredCloneStringify } from 'devframe/rpc'
19- import { createApp , eventHandler , fromNodeMiddleware } from 'h3'
2019import { dirname , relative , resolve } from 'pathe'
21- import sirv from 'sirv'
2220import { glob } from 'tinyglobby'
2321import { distDir } from '../dirs'
2422import { MARK_CHECK , MARK_NODE } from './constants'
@@ -117,58 +115,29 @@ cli
117115 . option ( '--port <port>' , 'Port' , { default : process . env . PORT || 9999 } )
118116 . option ( '--open' , 'Open browser' , { default : true } )
119117 . action ( async ( options ) => {
120- const { getPort } = await import ( 'get-port-please' )
121- const open = ( await import ( 'open' ) ) . default
122118 const host = options . host
123- const port = await getPort ( { port : Number ( options . port ) , portRange : [ 9999 , 15000 ] , host } )
124-
125- console . log ( c . green `${ MARK_NODE } Starting Node Modules Inspector at` , c . green ( `http://${ host === '127.0.0.1' ? 'localhost' : host } :${ port } ` ) , '\n' )
119+ const port = await resolveDevServerPort ( devtool , {
120+ host,
121+ defaultPort : Number ( options . port ) ,
122+ } )
123+ const url = `http://${ host === '127.0.0.1' ? 'localhost' : host } :${ port } `
126124
127- const origin = `http://${ host } :${ port } `
128- const app = createApp ( )
125+ console . log ( c . green `${ MARK_NODE } Starting Node Modules Inspector at` , c . green ( url ) , '\n' )
129126
130- const ctx = await createHostContext ( {
131- cwd : options . root ,
132- mode : 'dev' ,
133- host : createH3DevToolsHost ( { origin } ) ,
134- } )
135- await devtool . setup ( ctx , {
127+ const server = await createDevServer ( devtool , {
128+ host,
129+ port,
136130 flags : {
137131 root : options . root ,
138132 config : options . config ,
139133 depth : Number ( options . depth ) ,
140134 } ,
135+ openBrowser : options . open ? url : false ,
141136 } )
142137
143- const jsonSerializableMethods : string [ ] = [ ]
144- for ( const def of ctx . rpc . definitions . values ( ) ) {
145- if ( def . jsonSerializable === true )
146- jsonSerializableMethods . push ( def . name )
147- }
148-
149- app . use ( `/${ DEVTOOLS_CONNECTION_META_FILENAME } ` , eventHandler ( ( event ) => {
150- event . node . res . setHeader ( 'Content-Type' , 'application/json' )
151- return event . node . res . end ( JSON . stringify ( { backend : 'websocket' , websocket : port , jsonSerializableMethods } ) )
152- } ) )
153-
154- app . use ( '/' , fromNodeMiddleware ( sirv ( distDir , { dev : true , single : true } ) ) )
155-
156- setTimeout ( ( ) => {
157- const invoke = ctx . rpc . invokeLocal as ( method : string , ...args : any [ ] ) => Promise < any >
158- invoke ( 'nmi:get-payload' ) . catch ( ( ) => { } )
159- } , 1 )
160-
161- await startHttpAndWs ( {
162- context : ctx ,
163- host,
164- port,
165- app,
166- auth : false ,
167- onReady : async ( ) => {
168- if ( options . open )
169- await open ( `http://${ host === '127.0.0.1' ? 'localhost' : host } :${ port } ` )
170- } ,
171- } )
138+ // Warm the payload; rpcGroup.functions is a Proxy returning Promise<handler>.
139+ const handlers = server . rpcGroup . functions as Record < string , Promise < ( ...args : unknown [ ] ) => unknown > | undefined >
140+ handlers [ 'nmi:get-payload' ] ?. then ( fn => fn ?.( ) ) . catch ( ( ) => { } )
172141 } )
173142
174143cli . help ( )
0 commit comments