2023-03-03 13:45:38 +00:00
|
|
|
/**
|
|
|
|
* This file is used to wrap the CLI entrypoint in a restartable process.
|
|
|
|
*/
|
|
|
|
import { fileURLToPath } from 'node:url'
|
2023-03-03 19:24:49 +00:00
|
|
|
import { fork } from 'node:child_process'
|
|
|
|
import type { ChildProcess } from 'node:child_process'
|
2023-03-03 13:45:38 +00:00
|
|
|
|
|
|
|
const cliEntry = fileURLToPath(new URL('../dist/cli-run.mjs', import.meta.url))
|
|
|
|
|
2023-03-03 19:24:49 +00:00
|
|
|
// Only enable wrapper for nuxt dev command
|
|
|
|
if (process.argv[2] === 'dev') {
|
|
|
|
process.env.__CLI_ARGV__ = JSON.stringify(process.argv)
|
|
|
|
startSubprocess()
|
2023-03-03 13:45:38 +00:00
|
|
|
} else {
|
2023-03-03 13:51:47 +00:00
|
|
|
import(cliEntry)
|
2023-03-03 13:45:38 +00:00
|
|
|
}
|
2023-03-03 19:24:49 +00:00
|
|
|
|
|
|
|
function startSubprocess () {
|
|
|
|
let childProc: ChildProcess
|
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
if (childProc) {
|
|
|
|
childProc.kill()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
start()
|
|
|
|
|
|
|
|
function start () {
|
|
|
|
childProc = fork(cliEntry)
|
|
|
|
childProc.on('close', (code) => { if (code) { process.exit(code) } })
|
|
|
|
childProc.on('message', (message) => {
|
|
|
|
if ((message as { type: string })?.type === 'nuxt:restart') {
|
|
|
|
childProc.kill()
|
|
|
|
startSubprocess()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|