|
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 |
|
18 | | -const { execFileSync, execSync } = require("child_process"); |
| 18 | +const { execFileSync, execSync, spawn } = require("child_process"); |
19 | 19 | const { resolve } = require("path"); |
| 20 | +const fs = require("fs"); |
20 | 21 |
|
21 | 22 | const ROOT = resolve(__dirname); |
22 | 23 | const env = { ...process.env, PYTHONIOENCODING: "utf-8" }; |
@@ -48,13 +49,91 @@ function build(extraArgs) { |
48 | 49 | } |
49 | 50 |
|
50 | 51 | if (args.includes("--preview")) { |
| 52 | + // Full build once, then serve dist/ AND watch source files so edits made by |
| 53 | + // hand or through the CMS at /admin/ (incl. uploaded media) are picked up and |
| 54 | + // dist/ is rebuilt automatically — no manual rebuild needed. |
51 | 55 | build([]); |
| 56 | + |
52 | 57 | console.log("\nServing dist/ at http://localhost:8000 …"); |
53 | | - execFileSync(py, ["-m", "http.server", "8000", "-d", "dist"], { |
| 58 | + const server = spawn(py, ["-m", "http.server", "8000", "-d", "dist"], { |
54 | 59 | cwd: ROOT, |
55 | 60 | stdio: "inherit", |
56 | 61 | env, |
57 | 62 | }); |
| 63 | + |
| 64 | + // Source paths that feed the build. dist/ is deliberately NOT watched (would |
| 65 | + // loop). mkdocs-build*.yml are transient and live at the repo root, unwatched. |
| 66 | + const watchTargets = [ |
| 67 | + "docs", |
| 68 | + "data", |
| 69 | + "theme", |
| 70 | + "admin", |
| 71 | + "mkdocs.yml", |
| 72 | + "mkdocs-headless.yml", |
| 73 | + "showcase.html", |
| 74 | + "showcase.css", |
| 75 | + "main.py", |
| 76 | + "marketplace.json", |
| 77 | + ]; |
| 78 | + |
| 79 | + let rebuilding = false; |
| 80 | + let pending = false; |
| 81 | + let timer = null; |
| 82 | + |
| 83 | + function rebuild() { |
| 84 | + if (rebuilding) { |
| 85 | + pending = true; |
| 86 | + return; |
| 87 | + } |
| 88 | + rebuilding = true; |
| 89 | + console.log("\n↻ Change detected — rebuilding dist/ …"); |
| 90 | + try { |
| 91 | + execFileSync(py, ["scripts/pack.py", "--no-package"], { |
| 92 | + cwd: ROOT, |
| 93 | + stdio: "inherit", |
| 94 | + env: { ...env, SKIP_PIP_INSTALL: "1" }, |
| 95 | + }); |
| 96 | + console.log("✅ Rebuild complete — refresh your browser.\n"); |
| 97 | + } catch (e) { |
| 98 | + console.error("⚠ Rebuild failed:", e.status || e.message); |
| 99 | + } |
| 100 | + rebuilding = false; |
| 101 | + if (pending) { |
| 102 | + pending = false; |
| 103 | + schedule(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + function schedule() { |
| 108 | + clearTimeout(timer); |
| 109 | + timer = setTimeout(rebuild, 300); // debounce bursty CMS/file writes |
| 110 | + } |
| 111 | + |
| 112 | + const watchers = []; |
| 113 | + for (const target of watchTargets) { |
| 114 | + const p = resolve(ROOT, target); |
| 115 | + if (!fs.existsSync(p)) continue; |
| 116 | + try { |
| 117 | + watchers.push(fs.watch(p, { recursive: true }, schedule)); |
| 118 | + } catch { |
| 119 | + // recursive watch unsupported (older Node on Linux) — watch shallowly |
| 120 | + watchers.push(fs.watch(p, {}, schedule)); |
| 121 | + } |
| 122 | + } |
| 123 | + console.log(`Watching ${watchers.length} source path(s) — Ctrl+C to stop.`); |
| 124 | + |
| 125 | + function shutdown() { |
| 126 | + for (const w of watchers) { |
| 127 | + try { |
| 128 | + w.close(); |
| 129 | + } catch {} |
| 130 | + } |
| 131 | + if (server && !server.killed) server.kill(); |
| 132 | + process.exit(0); |
| 133 | + } |
| 134 | + process.on("SIGINT", shutdown); |
| 135 | + process.on("SIGTERM", shutdown); |
| 136 | + server.on("exit", (code) => process.exit(code || 0)); |
58 | 137 | } else if (args.includes("--dev")) { |
59 | 138 | try { |
60 | 139 | execFileSync(py, ["scripts/pack.py", "--serve"], { |
|
0 commit comments