|
| 1 | +import path from "path"; |
| 2 | +import { spawn } from "child_process"; |
| 3 | +import { fileURLToPath } from "url"; |
| 4 | +import { linkTheme } from "./link-theme.mjs"; |
| 5 | + |
| 6 | +const SCRIPT_PATH = fileURLToPath(import.meta.url); |
| 7 | +const THEME_ROOT = path.resolve(path.dirname(SCRIPT_PATH), ".."); |
| 8 | +const SITE_ROOT = path.join(THEME_ROOT, "dev", "site"); |
| 9 | +const HEXO_PATH = path.join(SITE_ROOT, "node_modules", ".bin", "hexo"); |
| 10 | + |
| 11 | +const children = []; |
| 12 | +let shuttingDown = false; |
| 13 | + |
| 14 | +const shutdown = (exitCode = 0) => { |
| 15 | + if (shuttingDown) return; |
| 16 | + shuttingDown = true; |
| 17 | + children.forEach((child) => { |
| 18 | + if (!child.killed) child.kill("SIGTERM"); |
| 19 | + }); |
| 20 | + process.exitCode = exitCode; |
| 21 | +}; |
| 22 | + |
| 23 | +const start = (command, args, cwd) => { |
| 24 | + const child = spawn(command, args, { |
| 25 | + cwd, |
| 26 | + env: { ...process.env, HUSKY: "0", NODE_ENV: "development" }, |
| 27 | + stdio: "inherit", |
| 28 | + }); |
| 29 | + children.push(child); |
| 30 | + return child; |
| 31 | +}; |
| 32 | + |
| 33 | +const main = () => { |
| 34 | + linkTheme(); |
| 35 | + const hexo = start(HEXO_PATH, ["server", ...process.argv.slice(2)], SITE_ROOT); |
| 36 | + const css = start("pnpm", ["run", "watch:css"], THEME_ROOT); |
| 37 | + |
| 38 | + hexo.on("exit", (code) => { |
| 39 | + if (!shuttingDown) shutdown(code || 0); |
| 40 | + }); |
| 41 | + css.on("exit", (code) => { |
| 42 | + if (!shuttingDown && code !== 0) shutdown(code || 1); |
| 43 | + }); |
| 44 | + |
| 45 | + process.on("SIGINT", () => shutdown()); |
| 46 | + process.on("SIGTERM", () => shutdown()); |
| 47 | + |
| 48 | + console.log("Demo site -> http://127.0.0.1:4000"); |
| 49 | +}; |
| 50 | + |
| 51 | +main(); |
0 commit comments