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
|
|
|
|
2023-03-14 14:06:48 +00:00
|
|
|
const cliEntry = new URL('../dist/cli-run.mjs', import.meta.url)
|
2023-03-03 13:45:38 +00:00
|
|
|
|
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-14 14:06:48 +00:00
|
|
|
import(cliEntry.href)
|
2023-03-03 13:45:38 +00:00
|
|
|
}
|
2023-03-03 19:24:49 +00:00
|
|
|
|
|
|
|
function startSubprocess () {
|
2023-03-06 14:42:20 +00:00
|
|
|
let childProc: ChildProcess | undefined
|
2023-03-03 19:24:49 +00:00
|
|
|
|
2023-03-06 14:42:20 +00:00
|
|
|
const onShutdown = () => {
|
2023-03-03 19:24:49 +00:00
|
|
|
if (childProc) {
|
|
|
|
childProc.kill()
|
2023-03-06 14:42:20 +00:00
|
|
|
childProc = undefined
|
2023-03-03 19:24:49 +00:00
|
|
|
}
|
2023-03-06 14:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on('exit', onShutdown)
|
|
|
|
process.on('SIGTERM', onShutdown) // Graceful shutdown
|
|
|
|
process.on('SIGINT', onShutdown) // Ctrl-C
|
|
|
|
process.on('SIGQUIT', onShutdown) // Ctrl-\
|
2023-03-03 19:24:49 +00:00
|
|
|
|
|
|
|
start()
|
|
|
|
|
|
|
|
function start () {
|
2023-03-14 14:06:48 +00:00
|
|
|
childProc = fork(fileURLToPath(cliEntry))
|
2023-03-03 19:24:49 +00:00
|
|
|
childProc.on('close', (code) => { if (code) { process.exit(code) } })
|
|
|
|
childProc.on('message', (message) => {
|
|
|
|
if ((message as { type: string })?.type === 'nuxt:restart') {
|
2023-03-06 14:42:20 +00:00
|
|
|
childProc?.kill()
|
2023-03-03 19:24:49 +00:00
|
|
|
startSubprocess()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|