2025-04-14 14:54:18 +00:00

50 lines
1.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
const path = require("node:path")
const childProcess = require("node:child_process")
const startFileWatcher = require("./startFileWatcher.js")
const bootloaderPath = path.resolve(__dirname, "boot.js")
const mainModulePath = process.argv[2]
const mainModuleSrc = path.resolve(process.cwd(), path.dirname(mainModulePath))
let childProcessInstance = null
let reloadTimeout = null
function selfReload() {
if (!childProcessInstance) {
console.error(
"[BOOT] Cannot self-reload. Missing childProcessInstance.",
)
return process.exit(0)
}
console.log("[BOOT] Reloading...")
childProcessInstance.kill()
boot()
}
function selfReloadDebounce() {
if (reloadTimeout) {
clearTimeout(reloadTimeout)
}
reloadTimeout = setTimeout(selfReload, 300)
}
function boot() {
childProcessInstance = childProcess.fork(bootloaderPath, [mainModulePath], {
stdio: "inherit",
})
}
// if --watch flag exist, start file watcher
if (process.argv.includes("--watch")) {
startFileWatcher(mainModuleSrc, {
onReload: selfReloadDebounce,
})
}
boot()