ssqch 0.7.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -4
- package/bin/ssqch.js +14 -48
- package/bundled/README.md +1314 -0
- package/bundled/completions/bash +840 -0
- package/bundled/completions/elvish +286 -0
- package/bundled/completions/fish +182 -0
- package/bundled/completions/powershell +318 -0
- package/bundled/completions/zsh +510 -0
- package/bundled/ssqch.d.ts +540 -0
- package/bundled/starter/engines/index.edge +11 -0
- package/bundled/starter/engines/index.ejs +11 -0
- package/bundled/starter/engines/index.hbs +11 -0
- package/bundled/starter/engines/index.liquid +11 -0
- package/bundled/starter/engines/layout.edge +35 -0
- package/bundled/starter/engines/layout.ejs +35 -0
- package/bundled/starter/engines/layout.hbs +35 -0
- package/bundled/starter/engines/layout.liquid +35 -0
- package/bundled/starter/gitignore +3 -0
- package/bundled/starter/jsconfig.json +11 -0
- package/bundled/starter/src/404.md +8 -0
- package/bundled/starter/src/_blueprints/posts.md +5 -0
- package/bundled/starter/src/_data/site.json +4 -0
- package/bundled/starter/src/_includes/layout.jinja +35 -0
- package/bundled/starter/src/index.jinja +11 -0
- package/bundled/starter/src/posts/i-know-what-i-saw.md +11 -0
- package/bundled/starter/src/posts/posts.json +4 -0
- package/bundled/starter/src/posts/raiders-of-the-lost-path.md +27 -0
- package/bundled/starter/src/posts/the-cache-of-the-wild.md +26 -0
- package/bundled/starter/src/posts/the-stump-has-moved.md +15 -0
- package/bundled/starter/src/posts/upon-further-review-a-stump.md +18 -0
- package/bundled/starter/ssqch.config.js +29 -0
- package/bundled/starter-docs/engines/edge/base.edge +41 -0
- package/bundled/starter-docs/engines/edge/containers/details.edge +5 -0
- package/bundled/starter-docs/engines/edge/containers/tab.edge +4 -0
- package/bundled/starter-docs/engines/edge/containers/tabs.edge +5 -0
- package/bundled/starter-docs/engines/edge/doc.edge +59 -0
- package/bundled/starter-docs/engines/edge/hooks/heading.edge +2 -0
- package/bundled/starter-docs/engines/ejs/base.ejs +41 -0
- package/bundled/starter-docs/engines/ejs/containers/details.ejs +5 -0
- package/bundled/starter-docs/engines/ejs/containers/tab.ejs +4 -0
- package/bundled/starter-docs/engines/ejs/containers/tabs.ejs +5 -0
- package/bundled/starter-docs/engines/ejs/doc.ejs +59 -0
- package/bundled/starter-docs/engines/ejs/hooks/heading.ejs +2 -0
- package/bundled/starter-docs/engines/hbs/base.hbs +41 -0
- package/bundled/starter-docs/engines/hbs/containers/details.hbs +5 -0
- package/bundled/starter-docs/engines/hbs/containers/tab.hbs +4 -0
- package/bundled/starter-docs/engines/hbs/containers/tabs.hbs +5 -0
- package/bundled/starter-docs/engines/hbs/doc.hbs +58 -0
- package/bundled/starter-docs/engines/hbs/hooks/heading.hbs +2 -0
- package/bundled/starter-docs/engines/liquid/base.liquid +41 -0
- package/bundled/starter-docs/engines/liquid/containers/details.liquid +5 -0
- package/bundled/starter-docs/engines/liquid/containers/tab.liquid +4 -0
- package/bundled/starter-docs/engines/liquid/containers/tabs.liquid +5 -0
- package/bundled/starter-docs/engines/liquid/doc.liquid +60 -0
- package/bundled/starter-docs/engines/liquid/hooks/heading.liquid +2 -0
- package/bundled/starter-docs/gitignore +4 -0
- package/bundled/starter-docs/src/404.md +12 -0
- package/bundled/starter-docs/src/_blueprints/docs.md +5 -0
- package/bundled/starter-docs/src/_data/nav.yml +10 -0
- package/bundled/starter-docs/src/_data/site.json +5 -0
- package/bundled/starter-docs/src/_includes/base.jinja +41 -0
- package/bundled/starter-docs/src/_includes/containers/details.jinja +5 -0
- package/bundled/starter-docs/src/_includes/containers/tab.jinja +4 -0
- package/bundled/starter-docs/src/_includes/containers/tabs.jinja +5 -0
- package/bundled/starter-docs/src/_includes/doc.jinja +59 -0
- package/bundled/starter-docs/src/_includes/hooks/heading.jinja +2 -0
- package/bundled/starter-docs/src/assets/docs.css +257 -0
- package/bundled/starter-docs/src/assets/docs.js +67 -0
- package/bundled/starter-docs/src/docs/docs.json +1 -0
- package/bundled/starter-docs/src/docs/guides/deploying.md +77 -0
- package/bundled/starter-docs/src/docs/guides/writing.md +65 -0
- package/bundled/starter-docs/src/docs/index.md +29 -0
- package/bundled/starter-docs/src/docs/install.md +42 -0
- package/bundled/starter-docs/src/index.md +13 -0
- package/bundled/starter-docs/ssqch.config.js +40 -0
- package/bundled/trailheads/from-component-frameworks.md +116 -0
- package/bundled/trailheads/from-docusaurus.md +55 -0
- package/bundled/trailheads/from-eleventy.md +140 -0
- package/bundled/trailheads/from-hexo.md +92 -0
- package/bundled/trailheads/from-jekyll.md +79 -0
- package/bundled/trailheads/from-mkdocs.md +45 -0
- package/bundled/trailheads/from-zola.md +71 -0
- package/lib/assets.js +235 -3
- package/lib/chunk-3A647I3N.js +384 -0
- package/lib/chunk-3GYLOC6Y.js +1017 -0
- package/lib/chunk-3K3ANWZ2.js +118 -0
- package/lib/chunk-3NYSSA2C.js +22 -0
- package/lib/chunk-47P4YREZ.js +1287 -0
- package/lib/chunk-4Q6WMTER.js +32 -0
- package/lib/chunk-76NTVE34.js +338 -0
- package/lib/chunk-7PWCYHTJ.js +1350 -0
- package/lib/chunk-7RDRZHJD.js +217 -0
- package/lib/chunk-7SD7K567.js +57 -0
- package/lib/chunk-7XLK2GEG.js +54 -0
- package/lib/chunk-AD3HAW3W.js +10 -0
- package/lib/chunk-BEGC7YP6.js +502 -0
- package/lib/chunk-CMUWRPSQ.js +113 -0
- package/lib/chunk-CZ4XQC56.js +999 -0
- package/lib/chunk-D2XE2GTY.js +71 -0
- package/lib/chunk-DDV6JEC2.js +46 -0
- package/lib/chunk-DELWRJAO.js +37409 -0
- package/lib/chunk-DELWRJAO.js.LEGAL.txt +27 -0
- package/lib/chunk-F65YUXZ7.js +334 -0
- package/lib/chunk-FVZ5JBRI.js +115 -0
- package/lib/chunk-GRWMGC7D.js +4435 -0
- package/lib/chunk-GRWMGC7D.js.LEGAL.txt +41 -0
- package/lib/chunk-GUKSZ2XM.js +207 -0
- package/lib/chunk-HPXAZ2HU.js +251 -0
- package/lib/chunk-HXCLCXRT.js +20 -0
- package/lib/chunk-I3PS26CI.js +48 -0
- package/lib/chunk-IAFM5ITE.js +11 -0
- package/lib/chunk-II5DEH3Q.js +209 -0
- package/lib/chunk-IJWHOFRP.js +44 -0
- package/lib/chunk-LPHNACE4.js +29 -0
- package/lib/chunk-MI6EJWA2.js +175 -0
- package/lib/chunk-NCWFYT74.js +1541 -0
- package/lib/chunk-NKXEPHRZ.js +136 -0
- package/lib/chunk-NNUKIGOO.js +160 -0
- package/lib/chunk-R7K3D5W5.js +3957 -0
- package/lib/chunk-RNJFPFXE.js +29 -0
- package/lib/chunk-SF4I6Z7S.js +25 -0
- package/lib/chunk-SURPOAW7.js +44 -0
- package/lib/chunk-SXMJ52IK.js +572 -0
- package/lib/chunk-TICWN55Z.js +20 -0
- package/lib/chunk-TVU3OPPK.js +146 -0
- package/lib/chunk-UU43WIZG.js +29 -0
- package/lib/chunk-VJWQSYEG.js +119 -0
- package/lib/chunk-XMXBHXZR.js +75 -0
- package/lib/chunk-YTHQBMDJ.js +211 -0
- package/lib/cli.js +547 -0
- package/lib/elements.js +180 -3
- package/lib/image.js +27 -3
- package/lib/links.js +464 -3
- package/lib/minify.js +69 -3
- package/lib/redirects.js +152 -3
- package/lib/rss.js +139 -3
- package/lib/sass.js +256 -3
- package/lib/sitemap.js +113 -3
- package/lib/ssqch.js +28 -0
- package/package.json +83 -80
- package/src/serve/live.js +174 -0
- package/src/serve/not-found.html +31 -0
- package/types/globals.d.ts +3 -2
- package/types/image.d.ts +7 -3
- package/types/index.d.ts +28 -9
- package/types/links.d.ts +1 -1
- package/types/minify.d.ts +1 -1
- package/types/rss.d.ts +1 -1
- package/types/sass.d.ts +1 -1
- package/types/sitemap.d.ts +2 -0
- package/wasm/markdown.wasm +0 -0
- package/wasm/minify.wasm +0 -0
- package/wasm/sass.wasm +0 -0
- package/lib/index.js +0 -3
- package/platforms.json +0 -6
|
@@ -0,0 +1,999 @@
|
|
|
1
|
+
import { createRequire as __ssqchCreateRequire } from "node:module";
|
|
2
|
+
const require = __ssqchCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
Live,
|
|
5
|
+
assetStamps,
|
|
6
|
+
fingerprint,
|
|
7
|
+
isAsset,
|
|
8
|
+
sameStamps
|
|
9
|
+
} from "./chunk-GUKSZ2XM.js";
|
|
10
|
+
import {
|
|
11
|
+
commandLine
|
|
12
|
+
} from "./chunk-4Q6WMTER.js";
|
|
13
|
+
import {
|
|
14
|
+
Builder,
|
|
15
|
+
stopBuilders
|
|
16
|
+
} from "./chunk-MI6EJWA2.js";
|
|
17
|
+
import {
|
|
18
|
+
CONFIG_FILES,
|
|
19
|
+
dirsOf
|
|
20
|
+
} from "./chunk-DDV6JEC2.js";
|
|
21
|
+
import {
|
|
22
|
+
closestPath
|
|
23
|
+
} from "./chunk-FVZ5JBRI.js";
|
|
24
|
+
import {
|
|
25
|
+
CAMPSITE_DISTURBED,
|
|
26
|
+
FRESH_TRACKS,
|
|
27
|
+
SQUATCH_WATCH
|
|
28
|
+
} from "./chunk-UU43WIZG.js";
|
|
29
|
+
import {
|
|
30
|
+
BuildError,
|
|
31
|
+
aliasesUsed,
|
|
32
|
+
messageOf,
|
|
33
|
+
progress,
|
|
34
|
+
red,
|
|
35
|
+
warn,
|
|
36
|
+
warning
|
|
37
|
+
} from "./chunk-GRWMGC7D.js";
|
|
38
|
+
import {
|
|
39
|
+
isInside,
|
|
40
|
+
realPath,
|
|
41
|
+
relative,
|
|
42
|
+
slashes,
|
|
43
|
+
walkFiles
|
|
44
|
+
} from "./chunk-VJWQSYEG.js";
|
|
45
|
+
import "./chunk-I3PS26CI.js";
|
|
46
|
+
|
|
47
|
+
// src/serve/server.ts
|
|
48
|
+
import crypto from "node:crypto";
|
|
49
|
+
import fs3 from "node:fs";
|
|
50
|
+
import http from "node:http";
|
|
51
|
+
import path3 from "node:path";
|
|
52
|
+
import { setTimeout as sleep2 } from "node:timers/promises";
|
|
53
|
+
|
|
54
|
+
// src/serve/companions.ts
|
|
55
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
56
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
57
|
+
var running = /* @__PURE__ */ new Set();
|
|
58
|
+
var shuttingDown = false;
|
|
59
|
+
var everStarted = false;
|
|
60
|
+
var environment = (ctx) => [ctx.root, ctx.env, ctx.input, ctx.output, String(Boolean(ctx.quiet))];
|
|
61
|
+
var Companions = class _Companions {
|
|
62
|
+
commands;
|
|
63
|
+
/** What they were started with (root, env, input and output folders), so a reload that changes those starts them again. */
|
|
64
|
+
startedWith;
|
|
65
|
+
running = [];
|
|
66
|
+
constructor(commands, startedWith) {
|
|
67
|
+
this.commands = commands;
|
|
68
|
+
this.startedWith = startedWith;
|
|
69
|
+
}
|
|
70
|
+
/** No companions. */
|
|
71
|
+
static none() {
|
|
72
|
+
return new _Companions([], []);
|
|
73
|
+
}
|
|
74
|
+
/** Start `commands`. One that can't start is an error naming it (the ones before it are stopped again). */
|
|
75
|
+
static async start(ctx, commands) {
|
|
76
|
+
const companions = new _Companions([...commands], environment(ctx));
|
|
77
|
+
for (const command of commands) {
|
|
78
|
+
const name = toolName(command);
|
|
79
|
+
progress(`[ssqch] serve.run: ${command}`);
|
|
80
|
+
const { file, args, options } = commandLine(command, ctx.root, { SSQCH_ENV: ctx.env, SSQCH_SERVING: "true", SSQCH_QUIET: ctx.quiet ? "true" : "false", SSQCH_INPUT: ctx.input, SSQCH_OUTPUT: ctx.output });
|
|
81
|
+
const one = { child: void 0, stopping: false, exited: false };
|
|
82
|
+
try {
|
|
83
|
+
one.child = spawn(file, args, { ...options, stdio: ["pipe", "pipe", "pipe"], detached: process.platform !== "win32", windowsHide: true });
|
|
84
|
+
await new Promise((resolve, reject) => {
|
|
85
|
+
one.child.once("spawn", resolve);
|
|
86
|
+
one.child.once("error", reject);
|
|
87
|
+
});
|
|
88
|
+
} catch (e) {
|
|
89
|
+
await companions.stop();
|
|
90
|
+
throw new BuildError(`serve.run: couldn't run \`${command}\`: ${e instanceof Error ? e.message : String(e)}`);
|
|
91
|
+
}
|
|
92
|
+
everStarted = true;
|
|
93
|
+
running.add(one);
|
|
94
|
+
companions.running.push(one);
|
|
95
|
+
const { child } = one;
|
|
96
|
+
child.stdin?.on("error", () => {
|
|
97
|
+
});
|
|
98
|
+
const shown = [relay(child.stdout, name, false), relay(child.stderr, name, true)];
|
|
99
|
+
child.on("error", () => {
|
|
100
|
+
});
|
|
101
|
+
child.once("exit", (code, signal) => {
|
|
102
|
+
one.exited = true;
|
|
103
|
+
running.delete(one);
|
|
104
|
+
if (one.stopping || shuttingDown) return;
|
|
105
|
+
const how = code === 0 ? "finished" : code !== null ? `exited with status ${code}` : signal ? "was stopped" : "couldn't be waited for";
|
|
106
|
+
void Promise.race([Promise.all(shown), sleep(250, void 0, { ref: false })]).then(() => {
|
|
107
|
+
process.stderr.write(`warn: serve.run: \`${command}\` ${how}. The server keeps going without it; save the config file to start it again.
|
|
108
|
+
`);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return companions;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Whether these are the companions `commands` would start in `ctx`, all still running. A
|
|
116
|
+
* reload keeps them only then: one that stopped starts again, and so do all of them when the
|
|
117
|
+
* output folder or the environment they were given changed.
|
|
118
|
+
*/
|
|
119
|
+
runs(ctx, commands) {
|
|
120
|
+
const same = (a, b) => a.length === b.length && a.every((item, i) => item === b[i]);
|
|
121
|
+
return same(this.commands, commands) && same(this.startedWith, environment(ctx)) && !this.running.some((r) => r.exited);
|
|
122
|
+
}
|
|
123
|
+
/** The tools' names (`postcss`, `esbuild`), for messages about a file one of them should have written. */
|
|
124
|
+
names() {
|
|
125
|
+
return this.commands.map(toolName);
|
|
126
|
+
}
|
|
127
|
+
/** Stop them, and what they started. Done when they're gone. */
|
|
128
|
+
async stop() {
|
|
129
|
+
const all = this.running;
|
|
130
|
+
this.running = [];
|
|
131
|
+
await Promise.all(all.map(stop));
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
async function stopAll() {
|
|
135
|
+
shuttingDown = true;
|
|
136
|
+
await Promise.all([...running].map(stop));
|
|
137
|
+
}
|
|
138
|
+
function stopAllNow() {
|
|
139
|
+
shuttingDown = true;
|
|
140
|
+
for (const one of [...running]) {
|
|
141
|
+
one.stopping = true;
|
|
142
|
+
running.delete(one);
|
|
143
|
+
one.child.stdin?.destroy();
|
|
144
|
+
const pid = one.child.pid;
|
|
145
|
+
if (pid === void 0) continue;
|
|
146
|
+
if (process.platform === "win32") spawnSync("taskkill", ["/PID", String(pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
|
|
147
|
+
else try {
|
|
148
|
+
process.kill(-pid, "SIGTERM");
|
|
149
|
+
} catch {
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
var alive = (group) => {
|
|
154
|
+
try {
|
|
155
|
+
process.kill(group, 0);
|
|
156
|
+
return true;
|
|
157
|
+
} catch (e) {
|
|
158
|
+
return e.code === "EPERM";
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
async function stop(one) {
|
|
162
|
+
one.stopping = true;
|
|
163
|
+
running.delete(one);
|
|
164
|
+
const { child } = one;
|
|
165
|
+
child.stdin?.destroy();
|
|
166
|
+
const pid = child.pid;
|
|
167
|
+
if (pid === void 0) return;
|
|
168
|
+
if (process.platform === "win32") {
|
|
169
|
+
spawnSync("taskkill", ["/PID", String(pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const group = -pid;
|
|
173
|
+
try {
|
|
174
|
+
process.kill(group, "SIGTERM");
|
|
175
|
+
} catch {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
for (let i = 0; i < 20; i++) {
|
|
179
|
+
if (!alive(group)) return;
|
|
180
|
+
await sleep(50);
|
|
181
|
+
}
|
|
182
|
+
try {
|
|
183
|
+
process.kill(group, "SIGKILL");
|
|
184
|
+
} catch {
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function relay(stream, name, error) {
|
|
188
|
+
if (!stream) return Promise.resolve();
|
|
189
|
+
const show = (line) => {
|
|
190
|
+
if (shuttingDown && process.platform === "win32") return;
|
|
191
|
+
const shown = `[${name}] ${line.trimEnd()}`;
|
|
192
|
+
if (error) process.stderr.write(shown + "\n");
|
|
193
|
+
else progress(shown);
|
|
194
|
+
};
|
|
195
|
+
let rest = "";
|
|
196
|
+
stream.setEncoding("utf8");
|
|
197
|
+
stream.on("data", (chunk) => {
|
|
198
|
+
const lines = (rest + chunk).split("\n");
|
|
199
|
+
rest = lines.pop();
|
|
200
|
+
for (const line of lines) show(line);
|
|
201
|
+
});
|
|
202
|
+
return new Promise((resolve) => {
|
|
203
|
+
const done = () => {
|
|
204
|
+
if (rest !== "") show(rest);
|
|
205
|
+
rest = "";
|
|
206
|
+
resolve();
|
|
207
|
+
};
|
|
208
|
+
stream.once("end", done);
|
|
209
|
+
stream.once("error", done);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
function toolName(command) {
|
|
213
|
+
const RUNNERS = ["npx", "pnpx", "bunx", "pnpm", "yarn", "npm", "bun", "deno", "node", "exec", "run", "dlx", "x", "env"];
|
|
214
|
+
let word = command.split(/\s+/).filter(Boolean).find((w) => !RUNNERS.includes(w) && !w.startsWith("-") && !w.includes("=")) ?? "run";
|
|
215
|
+
word = word.replace(/^["']+|["']+$/g, "");
|
|
216
|
+
word = word.startsWith("@") ? word.slice(1).split("/")[0] : word.split(/[/\\]/).pop();
|
|
217
|
+
word = word.split("@")[0];
|
|
218
|
+
word = word.endsWith(".js") ? word.slice(0, -3) : word.endsWith(".mjs") ? word.slice(0, -4) : word;
|
|
219
|
+
return word === "" ? "run" : word;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// src/serve/http.ts
|
|
223
|
+
import fs from "node:fs";
|
|
224
|
+
import path from "node:path";
|
|
225
|
+
import zlib from "node:zlib";
|
|
226
|
+
var LIVE_SCRIPT = fs.readFileSync(new URL("./live.js", new URL("../src/serve/http.ts", import.meta.url).href), "utf8");
|
|
227
|
+
var NOT_FOUND_PAGE = fs.readFileSync(new URL("./not-found.html", new URL("../src/serve/http.ts", import.meta.url).href), "utf8");
|
|
228
|
+
var PATCH = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" role="img" aria-label="A trail patch with one very large footprint"><defs><clipPath id="ssqch-patch"><circle cx="100" cy="100" r="96"/></clipPath></defs><g clip-path="url(#ssqch-patch)"><rect width="200" height="200" fill="#e8af43"/><rect y="40" width="200" height="40" fill="#a9b66a"/><rect y="120" width="200" height="40" fill="#a9b66a"/></g><circle cx="100" cy="100" r="92" fill="none" stroke="#233b2b" stroke-width="8"/><g fill="#233b2b" transform="rotate(12 100 100) translate(48 20) scale(0.8)">';
|
|
229
|
+
var ICON = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200"><circle cx="100" cy="100" r="100" fill="{circle}"/><g fill="#fff3d6" transform="rotate(10 100 100) translate(42 5) scale(0.9)">';
|
|
230
|
+
var FOOTPRINT = '<path d="M62 72c27 0 39 22 35 56-4 31-13 64-37 64s-32-33-35-64c-3-34 10-56 37-56z"/><circle cx="20" cy="62" r="8"/><circle cx="37" cy="44" r="9"/><circle cx="57" cy="33" r="10"/><circle cx="79" cy="30" r="11"/><circle cx="102" cy="42" r="14"/></g></svg>';
|
|
231
|
+
var patch = () => `${PATCH}${FOOTPRINT}`;
|
|
232
|
+
var icon = (broken) => `${ICON.replace("{circle}", broken ? "#a23e20" : "#233b2b")}${FOOTPRINT}`;
|
|
233
|
+
function escapeHtml(s) {
|
|
234
|
+
return s.replace(/[&<>"'`]/g, (c) => c === "&" ? "&" : c === "<" ? "<" : c === ">" ? ">" : c === '"' ? """ : c === "'" ? "'" : "`");
|
|
235
|
+
}
|
|
236
|
+
var isFile = (file) => {
|
|
237
|
+
try {
|
|
238
|
+
return fs.statSync(file).isFile();
|
|
239
|
+
} catch {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
var isDir = (file) => {
|
|
244
|
+
try {
|
|
245
|
+
return fs.statSync(file).isDirectory();
|
|
246
|
+
} catch {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
function percentDecode(s) {
|
|
251
|
+
if (!s.includes("%")) return s;
|
|
252
|
+
const bytes = Buffer.from(s, "utf8");
|
|
253
|
+
const out = Buffer.alloc(bytes.length);
|
|
254
|
+
let n = 0;
|
|
255
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
256
|
+
if (bytes[i] === 37 && i + 2 < bytes.length) {
|
|
257
|
+
const hex = bytes.toString("latin1", i + 1, i + 3);
|
|
258
|
+
if (/^[0-9a-fA-F]{2}$/.test(hex)) {
|
|
259
|
+
out[n++] = Number.parseInt(hex, 16);
|
|
260
|
+
i += 2;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
out[n++] = bytes[i];
|
|
265
|
+
}
|
|
266
|
+
return out.toString("utf8", 0, n);
|
|
267
|
+
}
|
|
268
|
+
function queryValue(query, key) {
|
|
269
|
+
for (const pair of query.split("&")) {
|
|
270
|
+
const at = pair.indexOf("=");
|
|
271
|
+
if (at !== -1 && pair.slice(0, at) === key) return percentDecode(pair.slice(at + 1));
|
|
272
|
+
}
|
|
273
|
+
return void 0;
|
|
274
|
+
}
|
|
275
|
+
function mounted(url, base) {
|
|
276
|
+
if (base === "") return { path: url };
|
|
277
|
+
if (url.startsWith(base)) {
|
|
278
|
+
const rest = url.slice(base.length);
|
|
279
|
+
if (rest.startsWith("/")) return { path: rest };
|
|
280
|
+
if (rest === "") return { redirect: `${base}/` };
|
|
281
|
+
}
|
|
282
|
+
return url === "/" ? { redirect: `${base}/` } : "outside";
|
|
283
|
+
}
|
|
284
|
+
function inside(rel) {
|
|
285
|
+
if (rel.includes("\\") || rel.includes("\0") || rel.startsWith("/")) return false;
|
|
286
|
+
if (process.platform === "win32" && rel.includes(":")) return false;
|
|
287
|
+
const parts = rel.split("/");
|
|
288
|
+
return parts[0] !== "." && !parts.includes("..");
|
|
289
|
+
}
|
|
290
|
+
function pageFile(url, base) {
|
|
291
|
+
const at = mounted(url, base);
|
|
292
|
+
const rel = (typeof at === "object" && "path" in at ? at.path : url).replace(/^\/+/, "");
|
|
293
|
+
return rel === "" || rel.endsWith("/") ? `${rel}index.html` : rel;
|
|
294
|
+
}
|
|
295
|
+
function contentType(file) {
|
|
296
|
+
switch (path.extname(file).slice(1)) {
|
|
297
|
+
case "html":
|
|
298
|
+
return "text/html; charset=utf-8";
|
|
299
|
+
case "css":
|
|
300
|
+
return "text/css; charset=utf-8";
|
|
301
|
+
case "js":
|
|
302
|
+
case "mjs":
|
|
303
|
+
return "text/javascript; charset=utf-8";
|
|
304
|
+
case "json":
|
|
305
|
+
return "application/json";
|
|
306
|
+
case "xml":
|
|
307
|
+
return "application/xml; charset=utf-8";
|
|
308
|
+
case "txt":
|
|
309
|
+
return "text/plain; charset=utf-8";
|
|
310
|
+
case "svg":
|
|
311
|
+
return "image/svg+xml";
|
|
312
|
+
case "png":
|
|
313
|
+
return "image/png";
|
|
314
|
+
case "jpg":
|
|
315
|
+
case "jpeg":
|
|
316
|
+
return "image/jpeg";
|
|
317
|
+
case "gif":
|
|
318
|
+
return "image/gif";
|
|
319
|
+
case "webp":
|
|
320
|
+
return "image/webp";
|
|
321
|
+
case "avif":
|
|
322
|
+
return "image/avif";
|
|
323
|
+
case "ico":
|
|
324
|
+
return "image/x-icon";
|
|
325
|
+
case "woff2":
|
|
326
|
+
return "font/woff2";
|
|
327
|
+
case "woff":
|
|
328
|
+
return "font/woff";
|
|
329
|
+
case "pdf":
|
|
330
|
+
return "application/pdf";
|
|
331
|
+
default:
|
|
332
|
+
return "application/octet-stream";
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function linksAnIcon(html) {
|
|
336
|
+
return /<link\b[^>]*\brel\s*=\s*["']?[^"'>]*\bicon\b/is.test(html);
|
|
337
|
+
}
|
|
338
|
+
function pageUrls(root) {
|
|
339
|
+
return walkFiles(root).filter((file) => path.extname(file) === ".html").map((file) => relative(root, file)).map((rel) => rel.endsWith("index.html") ? `/${rel.slice(0, -10)}` : `/${rel}`);
|
|
340
|
+
}
|
|
341
|
+
function notFoundPage(root, url, base) {
|
|
342
|
+
const found = closestPath(url, pageUrls(root));
|
|
343
|
+
const suggestion = found === void 0 ? "" : `<p>Did you mean <a href="${escapeHtml(base)}${escapeHtml(found)}">${escapeHtml(found)}</a>?</p>`;
|
|
344
|
+
const parts = { patch: patch(), url: escapeHtml(url), suggestion, home: `${escapeHtml(base)}/` };
|
|
345
|
+
return NOT_FOUND_PAGE.replace(/\{\{(patch|url|suggestion|home)\}\}/g, (_, name) => parts[name]);
|
|
346
|
+
}
|
|
347
|
+
var PLAIN = { "Content-Type": "text/plain; charset=UTF-8" };
|
|
348
|
+
function respond(res, status, headers, body = "") {
|
|
349
|
+
const bytes = typeof body === "string" ? Buffer.from(body) : body;
|
|
350
|
+
res.writeHead(status, { ...headers, "Content-Length": String(bytes.length) });
|
|
351
|
+
res.end(bytes);
|
|
352
|
+
}
|
|
353
|
+
function split(url) {
|
|
354
|
+
const whole = (url ?? "/").split("#")[0] || "/";
|
|
355
|
+
const at = whole.indexOf("?");
|
|
356
|
+
return at === -1 ? [whole, ""] : [whole.slice(0, at), whole.slice(at + 1)];
|
|
357
|
+
}
|
|
358
|
+
function locate(res, root, rawUrl, url) {
|
|
359
|
+
const rel = url.replace(/^\/+/, "");
|
|
360
|
+
if (rel.includes("\0") && inside(rel.replaceAll("\0", "_"))) return { status: 404, file: path.join(root, "404.html") };
|
|
361
|
+
if (!inside(rel)) {
|
|
362
|
+
respond(res, 400, {});
|
|
363
|
+
return void 0;
|
|
364
|
+
}
|
|
365
|
+
let file = path.join(root, rel);
|
|
366
|
+
if (isDir(file)) {
|
|
367
|
+
if (!url.endsWith("/")) {
|
|
368
|
+
respond(res, 301, { Location: `${rawUrl}/` });
|
|
369
|
+
return void 0;
|
|
370
|
+
}
|
|
371
|
+
file = path.join(file, "index.html");
|
|
372
|
+
}
|
|
373
|
+
return isFile(file) ? { status: 200, file } : { status: 404, file: path.join(root, "404.html") };
|
|
374
|
+
}
|
|
375
|
+
var read = (file) => {
|
|
376
|
+
try {
|
|
377
|
+
return fs.readFileSync(file);
|
|
378
|
+
} catch {
|
|
379
|
+
return void 0;
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
function handle(req, res, root, live, base) {
|
|
383
|
+
const [rawUrl, query] = split(req.url);
|
|
384
|
+
const noCache = { "Cache-Control": "no-store" };
|
|
385
|
+
if (rawUrl === "/__ssqch/missing") {
|
|
386
|
+
const url2 = queryValue(query, "path") ?? "";
|
|
387
|
+
const at2 = mounted(url2, base);
|
|
388
|
+
const rel = typeof at2 === "object" && "path" in at2 ? at2.path.replace(/^\/+/, "") : "";
|
|
389
|
+
const message = inside(rel) && !isFile(path.join(root, rel)) ? live.missing(rel, url2) : null;
|
|
390
|
+
return respond(res, 200, { "Content-Type": "application/json", ...noCache }, JSON.stringify({ message }));
|
|
391
|
+
}
|
|
392
|
+
if (rawUrl === "/__ssqch/build") {
|
|
393
|
+
const v = queryValue(query, "v");
|
|
394
|
+
const since = v !== void 0 && /^\+?\d+$/.test(v) ? Number(v) : void 0;
|
|
395
|
+
const page = pageFile(queryValue(query, "p") ?? "", base);
|
|
396
|
+
return respond(res, 200, { "Content-Type": "application/json", ...noCache }, JSON.stringify(live.answer(since, page, base)));
|
|
397
|
+
}
|
|
398
|
+
const wanted = rawUrl === "/__ssqch/favicon.svg" || rawUrl === "/favicon.ico" && !isFile(path.join(root, "favicon.ico")) ? false : rawUrl === "/__ssqch/favicon-error.svg" ? true : void 0;
|
|
399
|
+
if (wanted !== void 0) return respond(res, 200, { "Content-Type": "image/svg+xml", ...noCache }, icon(wanted));
|
|
400
|
+
const fullUrl = percentDecode(rawUrl);
|
|
401
|
+
const at = mounted(fullUrl, base);
|
|
402
|
+
if (at === "outside") return respond(res, 404, PLAIN, `Wrong trailhead: this site is served under ${base}/ (its \`base\`).`);
|
|
403
|
+
if ("redirect" in at) return respond(res, 302, { Location: at.redirect });
|
|
404
|
+
const url = at.path;
|
|
405
|
+
const found = locate(res, root, rawUrl, url);
|
|
406
|
+
if (!found) return;
|
|
407
|
+
let body = read(found.file);
|
|
408
|
+
let ctype = contentType(found.file);
|
|
409
|
+
if (!body) {
|
|
410
|
+
body = Buffer.from(notFoundPage(root, url, base));
|
|
411
|
+
ctype = "text/html; charset=utf-8";
|
|
412
|
+
}
|
|
413
|
+
if (ctype.startsWith("text/html")) {
|
|
414
|
+
let html = body.toString("utf8");
|
|
415
|
+
if (!isFile(path.join(root, "favicon.ico")) && !linksAnIcon(html)) {
|
|
416
|
+
const link = '<link rel="icon" href="/__ssqch/favicon.svg" type="image/svg+xml">';
|
|
417
|
+
const head = html.indexOf("</head>");
|
|
418
|
+
html = head === -1 ? `${link}${html}` : `${html.slice(0, head)}${link}${html.slice(head)}`;
|
|
419
|
+
}
|
|
420
|
+
const end = html.lastIndexOf("</body>");
|
|
421
|
+
body = Buffer.from(end === -1 ? `${html}<script>${LIVE_SCRIPT}</script>` : `${html.slice(0, end)}<script>${LIVE_SCRIPT}</script>${html.slice(end)}`);
|
|
422
|
+
}
|
|
423
|
+
respond(res, found.status, { "Content-Type": ctype, ...noCache, "X-Powered-By": "pine cones" }, body);
|
|
424
|
+
}
|
|
425
|
+
function takesGzip(req) {
|
|
426
|
+
return String(req.headers["accept-encoding"] ?? "").split(",").some((encoding) => encoding.trim().split(";")[0] === "gzip");
|
|
427
|
+
}
|
|
428
|
+
function handleBuilt(req, res, root, base) {
|
|
429
|
+
const [rawUrl] = split(req.url);
|
|
430
|
+
const fullUrl = percentDecode(rawUrl);
|
|
431
|
+
const at = mounted(fullUrl, base);
|
|
432
|
+
if (at === "outside") return respond(res, 404, PLAIN, `Not found: ${fullUrl} (this site is served under ${base}/)`);
|
|
433
|
+
if ("redirect" in at) return respond(res, 302, { Location: at.redirect });
|
|
434
|
+
const found = locate(res, root, rawUrl, at.path);
|
|
435
|
+
if (!found) return;
|
|
436
|
+
let body = read(found.file);
|
|
437
|
+
let ctype = contentType(found.file);
|
|
438
|
+
if (!body) {
|
|
439
|
+
body = Buffer.from(`Not found: ${fullUrl}`);
|
|
440
|
+
ctype = "text/plain; charset=utf-8";
|
|
441
|
+
}
|
|
442
|
+
const compressible = ctype.startsWith("text/") || ctype.startsWith("application/json") || ctype.startsWith("application/xml") || ctype === "image/svg+xml";
|
|
443
|
+
const gzip = compressible && body.length > 256 && takesGzip(req);
|
|
444
|
+
const headers = { "Content-Type": ctype };
|
|
445
|
+
if (compressible) headers.Vary = "Accept-Encoding";
|
|
446
|
+
if (gzip) headers["Content-Encoding"] = "gzip";
|
|
447
|
+
respond(res, found.status, headers, gzip ? zlib.gzipSync(body) : body);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// src/serve/watch.ts
|
|
451
|
+
import fs2 from "node:fs";
|
|
452
|
+
import path2 from "node:path";
|
|
453
|
+
var Channel = class {
|
|
454
|
+
items = [];
|
|
455
|
+
wake;
|
|
456
|
+
send(item) {
|
|
457
|
+
this.items.push(item);
|
|
458
|
+
this.wake?.();
|
|
459
|
+
}
|
|
460
|
+
/** The next item if one is waiting. */
|
|
461
|
+
tryRecv() {
|
|
462
|
+
return this.items.shift();
|
|
463
|
+
}
|
|
464
|
+
/** The next item, whenever it comes. */
|
|
465
|
+
async recv() {
|
|
466
|
+
while (!this.items.length) await new Promise((resolve) => {
|
|
467
|
+
this.wake = resolve;
|
|
468
|
+
});
|
|
469
|
+
this.wake = void 0;
|
|
470
|
+
return this.items.shift();
|
|
471
|
+
}
|
|
472
|
+
/** The next item, or nothing when none comes within `ms`. */
|
|
473
|
+
async recvTimeout(ms) {
|
|
474
|
+
if (!this.items.length && ms > 0) {
|
|
475
|
+
let timer;
|
|
476
|
+
await new Promise((resolve) => {
|
|
477
|
+
this.wake = resolve;
|
|
478
|
+
timer = setTimeout(resolve, ms);
|
|
479
|
+
});
|
|
480
|
+
clearTimeout(timer);
|
|
481
|
+
this.wake = void 0;
|
|
482
|
+
}
|
|
483
|
+
return this.items.shift();
|
|
484
|
+
}
|
|
485
|
+
/** Forget what's waiting. */
|
|
486
|
+
drain() {
|
|
487
|
+
this.items.length = 0;
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
var defaultStrategy = () => process.platform === "darwin" || process.platform === "win32" ? "tree" : "folders";
|
|
491
|
+
var SKIPPED = /* @__PURE__ */ new Set([".cache", ".git", "node_modules"]);
|
|
492
|
+
var Watcher = class {
|
|
493
|
+
onChange;
|
|
494
|
+
strategy;
|
|
495
|
+
watched = /* @__PURE__ */ new Map();
|
|
496
|
+
/** A folder below a watched one that couldn't be watched itself. */
|
|
497
|
+
onError = () => {
|
|
498
|
+
};
|
|
499
|
+
constructor(onChange, strategy = defaultStrategy()) {
|
|
500
|
+
this.onChange = onChange;
|
|
501
|
+
this.strategy = strategy;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Watch `dir`: the files in it, and with `recursive` everything below it. A folder that's
|
|
505
|
+
* watched already stays as it is, unless this asks for more of it. A folder that can't be
|
|
506
|
+
* watched throws; one below it that can't goes to `onError`.
|
|
507
|
+
*/
|
|
508
|
+
watch(dir, recursive) {
|
|
509
|
+
const had = this.watched.get(dir);
|
|
510
|
+
if (had && (had.recursive || !recursive)) return;
|
|
511
|
+
if (had) this.unwatch(dir);
|
|
512
|
+
const top = { recursive, folders: /* @__PURE__ */ new Map(), real: /* @__PURE__ */ new Set() };
|
|
513
|
+
if (this.strategy === "tree" || !recursive) this.listen(top, dir, recursive && this.strategy === "tree");
|
|
514
|
+
else this.walk(top, dir, true);
|
|
515
|
+
this.watched.set(dir, top);
|
|
516
|
+
}
|
|
517
|
+
unwatch(dir) {
|
|
518
|
+
const top = this.watched.get(dir);
|
|
519
|
+
if (!top) return;
|
|
520
|
+
for (const folder of top.folders.values()) folder.watcher.close();
|
|
521
|
+
this.watched.delete(dir);
|
|
522
|
+
}
|
|
523
|
+
close() {
|
|
524
|
+
for (const dir of [...this.watched.keys()]) this.unwatch(dir);
|
|
525
|
+
}
|
|
526
|
+
/** How many folders have a watcher (for tests). */
|
|
527
|
+
get size() {
|
|
528
|
+
let n = 0;
|
|
529
|
+
for (const top of this.watched.values()) n += top.folders.size;
|
|
530
|
+
return n;
|
|
531
|
+
}
|
|
532
|
+
listen(top, dir, tree, real) {
|
|
533
|
+
const watcher = fs2.watch(dir, { recursive: tree }, (event, name) => this.changed(top, dir, name === null ? void 0 : String(name), tree, event === "rename"));
|
|
534
|
+
watcher.on("error", () => this.forget(top, dir));
|
|
535
|
+
top.folders.set(dir, { watcher, real, children: /* @__PURE__ */ new Set() });
|
|
536
|
+
if (path2.dirname(dir) !== dir) top.folders.get(path2.dirname(dir))?.children.add(dir);
|
|
537
|
+
}
|
|
538
|
+
/** A watcher for `dir` and each folder below it. Linked folders are followed, as the build follows them. */
|
|
539
|
+
walk(top, dir, first = false) {
|
|
540
|
+
if (top.folders.has(dir)) return;
|
|
541
|
+
const real = realPath(dir);
|
|
542
|
+
if (top.real.has(real)) return;
|
|
543
|
+
try {
|
|
544
|
+
this.listen(top, dir, false, real);
|
|
545
|
+
} catch (e) {
|
|
546
|
+
if (first) throw e;
|
|
547
|
+
if (!top.failed) this.onError(dir, e);
|
|
548
|
+
top.failed = true;
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
top.real.add(real);
|
|
552
|
+
let entries;
|
|
553
|
+
try {
|
|
554
|
+
entries = fs2.readdirSync(dir, { withFileTypes: true });
|
|
555
|
+
} catch {
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
for (const entry of entries) {
|
|
559
|
+
if (SKIPPED.has(entry.name)) continue;
|
|
560
|
+
const file = path2.join(dir, entry.name);
|
|
561
|
+
if (entry.isDirectory() || entry.isSymbolicLink() && isDir2(file)) this.walk(top, file);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
changed(top, dir, name, tree, renamed) {
|
|
565
|
+
if (name === void 0) return this.onChange(dir);
|
|
566
|
+
const file = path2.join(dir, name);
|
|
567
|
+
this.onChange(file);
|
|
568
|
+
if (tree || !top.recursive) return;
|
|
569
|
+
if (isDir2(file)) {
|
|
570
|
+
if (SKIPPED.has(name)) return;
|
|
571
|
+
if (renamed && top.folders.has(file)) this.forget(top, file);
|
|
572
|
+
if (!top.folders.has(file)) {
|
|
573
|
+
this.walk(top, file);
|
|
574
|
+
for (const inside2 of filesIn(file)) this.onChange(inside2);
|
|
575
|
+
}
|
|
576
|
+
} else if (top.folders.has(file)) {
|
|
577
|
+
this.forget(top, file);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* `dir` is gone: so are its watcher and those of the folders that were in it. It costs only
|
|
582
|
+
* what was below it, and asks the file system nothing: when a tree of thousands of folders is
|
|
583
|
+
* deleted (`rm -rf dist`, a production build cleaning the output), this runs for each of them.
|
|
584
|
+
*/
|
|
585
|
+
forget(top, dir) {
|
|
586
|
+
const below = [dir];
|
|
587
|
+
for (let folder = below.pop(); folder !== void 0; folder = below.pop()) {
|
|
588
|
+
const gone = top.folders.get(folder);
|
|
589
|
+
if (!gone) continue;
|
|
590
|
+
gone.watcher.close();
|
|
591
|
+
top.folders.delete(folder);
|
|
592
|
+
if (gone.real !== void 0) top.real.delete(gone.real);
|
|
593
|
+
below.push(...gone.children);
|
|
594
|
+
}
|
|
595
|
+
top.folders.get(path2.dirname(dir))?.children.delete(dir);
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
var isDir2 = (file) => fs2.statSync(file, { throwIfNoEntry: false })?.isDirectory() ?? false;
|
|
599
|
+
function filesIn(dir) {
|
|
600
|
+
const out = [];
|
|
601
|
+
const visit = (folder, depth) => {
|
|
602
|
+
let entries;
|
|
603
|
+
try {
|
|
604
|
+
entries = fs2.readdirSync(folder, { withFileTypes: true });
|
|
605
|
+
} catch {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
for (const entry of entries) {
|
|
609
|
+
const file = path2.join(folder, entry.name);
|
|
610
|
+
if (entry.isDirectory()) {
|
|
611
|
+
if (!SKIPPED.has(entry.name) && depth < 32) visit(file, depth + 1);
|
|
612
|
+
} else out.push(file);
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
visit(dir, 0);
|
|
616
|
+
return out;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// src/serve/server.ts
|
|
620
|
+
var exists = (file) => fs3.existsSync(file);
|
|
621
|
+
var isFile2 = (file) => fs3.statSync(file, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
622
|
+
var isDir3 = (file) => fs3.statSync(file, { throwIfNoEntry: false })?.isDirectory() ?? false;
|
|
623
|
+
var say = (line) => {
|
|
624
|
+
process.stdout.write(line + "\n");
|
|
625
|
+
};
|
|
626
|
+
var CONFIG_NAMES = /* @__PURE__ */ new Set([...CONFIG_FILES, "sitesquatch.toml"]);
|
|
627
|
+
function sourceState(file) {
|
|
628
|
+
if (!isDir3(file)) return fingerprint(file);
|
|
629
|
+
try {
|
|
630
|
+
return `folder:${crypto.hash("sha1", fs3.readdirSync(file).sort().join("\n"), "base64")}`;
|
|
631
|
+
} catch {
|
|
632
|
+
return void 0;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
function writeReady(file, site, port, ok) {
|
|
636
|
+
const ready = {
|
|
637
|
+
schemaVersion: 1,
|
|
638
|
+
url: `http://localhost:${port}${site.base}/`,
|
|
639
|
+
port,
|
|
640
|
+
base: site.base,
|
|
641
|
+
output: realPath(site.output),
|
|
642
|
+
pid: process.pid,
|
|
643
|
+
ok
|
|
644
|
+
};
|
|
645
|
+
const ext = path3.extname(file);
|
|
646
|
+
const partial = `${ext === "" ? file : file.slice(0, -ext.length)}.partial`;
|
|
647
|
+
try {
|
|
648
|
+
fs3.writeFileSync(partial, JSON.stringify(ready, null, 2) + "\n");
|
|
649
|
+
} catch (e) {
|
|
650
|
+
throw new BuildError(`--ready-file: writing ${partial}: ${messageOf(e)}`);
|
|
651
|
+
}
|
|
652
|
+
try {
|
|
653
|
+
fs3.renameSync(partial, file);
|
|
654
|
+
} catch (e) {
|
|
655
|
+
throw new BuildError(`--ready-file: writing ${file}: ${messageOf(e)}`);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
function listen(port, answer) {
|
|
659
|
+
const server = http.createServer((req, res) => {
|
|
660
|
+
try {
|
|
661
|
+
answer(req, res);
|
|
662
|
+
} catch {
|
|
663
|
+
if (res.headersSent) res.destroy();
|
|
664
|
+
else res.writeHead(500, { "Content-Length": "0" }).end();
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
server.on("clientError", (e, socket) => {
|
|
668
|
+
if (e.code === "ECONNRESET" || !socket.writable) return socket.destroy();
|
|
669
|
+
const status = e.code === "HPE_HEADER_OVERFLOW" ? "431 Request Header Fields Too Large" : e.code === "HPE_CHUNK_EXTENSIONS_OVERFLOW" ? "413 Payload Too Large" : "400 Bad Request";
|
|
670
|
+
socket.end(`HTTP/1.1 ${status}\r
|
|
671
|
+
Content-Length: 0\r
|
|
672
|
+
Connection: close\r
|
|
673
|
+
\r
|
|
674
|
+
`);
|
|
675
|
+
});
|
|
676
|
+
return new Promise((resolve, reject) => {
|
|
677
|
+
server.once("error", (e) => {
|
|
678
|
+
reject(new BuildError(e.code === "EADDRINUSE" ? `can't listen on port ${port}: something else is squatting on it (--port 0 picks a free one)` : `can't listen on port ${port}: ${e.message}`));
|
|
679
|
+
});
|
|
680
|
+
server.listen(port, "127.0.0.1", () => {
|
|
681
|
+
const address = server.address();
|
|
682
|
+
resolve(typeof address === "object" && address !== null ? address.port : port);
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
async function serveBuilt(config, options) {
|
|
687
|
+
const dirs = dirsOf(config);
|
|
688
|
+
const site = { output: dirs.output, base: config.base };
|
|
689
|
+
const shown = isInside(dirs.root, site.output) ? relative(dirs.root, site.output) : slashes(site.output);
|
|
690
|
+
if (!isDir3(site.output)) throw new BuildError(`${shown} doesn't exist: build the site first (ssqch build), then serve it with ssqch serve --built`);
|
|
691
|
+
if (options.readyFile !== void 0) fs3.rmSync(options.readyFile, { force: true });
|
|
692
|
+
const port = await listen(options.port, (req, res) => handleBuilt(req, res, site.output, site.base));
|
|
693
|
+
if (options.readyFile !== void 0) writeReady(options.readyFile, site, port, true);
|
|
694
|
+
const url = `http://localhost:${port}${site.base}/`;
|
|
695
|
+
say(config.quiet ? url : `Serving ${shown} as built at ${url}. It stays as it is until the next ssqch build.`);
|
|
696
|
+
return new Promise(() => {
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
function banner(site, port, quiet) {
|
|
700
|
+
const here = realPath(process.cwd());
|
|
701
|
+
const output = realPath(site.output);
|
|
702
|
+
const shown = isInside(here, output) && output !== here ? relative(here, output) : site.output;
|
|
703
|
+
const serving2 = `Serving ${shown} at http://localhost:${port}${site.base}/`;
|
|
704
|
+
return quiet ? serving2 : `${serving2}. ${SQUATCH_WATCH}`;
|
|
705
|
+
}
|
|
706
|
+
function stopWithTheServer() {
|
|
707
|
+
let stopping = false;
|
|
708
|
+
const stop2 = (signal) => {
|
|
709
|
+
if (stopping) return;
|
|
710
|
+
stopping = true;
|
|
711
|
+
stopBuilders();
|
|
712
|
+
void stopAll().then(() => {
|
|
713
|
+
if (everStarted || process.platform === "win32") process.exit(130);
|
|
714
|
+
process.removeAllListeners(signal);
|
|
715
|
+
process.kill(process.pid, signal);
|
|
716
|
+
});
|
|
717
|
+
};
|
|
718
|
+
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) process.on(signal, () => stop2(signal));
|
|
719
|
+
process.on("exit", () => {
|
|
720
|
+
stopBuilders();
|
|
721
|
+
stopAllNow();
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
async function serve(args, options) {
|
|
725
|
+
stopWithTheServer();
|
|
726
|
+
const first = await Builder.start(args, { reload: false, said: aliasesUsed().map((row) => `${row.kind}:${row.word}`) });
|
|
727
|
+
if (!first) return 1;
|
|
728
|
+
const live = new Live();
|
|
729
|
+
const site = { output: first.site.output, base: first.site.base };
|
|
730
|
+
if (options.readyFile !== void 0) {
|
|
731
|
+
try {
|
|
732
|
+
fs3.rmSync(options.readyFile);
|
|
733
|
+
} catch (e) {
|
|
734
|
+
if (e.code !== "ENOENT") throw new BuildError(`--ready-file: removing ${options.readyFile}: ${messageOf(e)}`);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
const { quiet } = options;
|
|
738
|
+
try {
|
|
739
|
+
const port = await listen(options.port, (req, res) => handle(req, res, site.output, live, site.base));
|
|
740
|
+
return await serving(first, { args, options, live, site, port, quiet });
|
|
741
|
+
} catch (e) {
|
|
742
|
+
stopBuilders();
|
|
743
|
+
await stopAll();
|
|
744
|
+
throw e;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
async function serving(first, { args, options, live, site, port, quiet }) {
|
|
748
|
+
const sayWhere = () => say(banner(site, port, quiet));
|
|
749
|
+
let builder = first;
|
|
750
|
+
let companions = Companions.none();
|
|
751
|
+
for (; ; ) {
|
|
752
|
+
const info = { ...builder.site, quiet };
|
|
753
|
+
Object.assign(site, { output: info.output, base: info.base });
|
|
754
|
+
const startedTools = !companions.runs(info, info.serveRun);
|
|
755
|
+
if (startedTools) {
|
|
756
|
+
await companions.stop();
|
|
757
|
+
companions = Companions.none();
|
|
758
|
+
companions = await Companions.start(info, info.serveRun);
|
|
759
|
+
}
|
|
760
|
+
live.setSite(companions.names(), info.outputName);
|
|
761
|
+
const firstBuild = (ok) => {
|
|
762
|
+
if (options.readyFile === void 0) return;
|
|
763
|
+
try {
|
|
764
|
+
writeReady(options.readyFile, site, port, ok);
|
|
765
|
+
} catch (e) {
|
|
766
|
+
process.stderr.write(`${red("error:")} ${messageOf(e)}
|
|
767
|
+
`);
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
const restart = async () => {
|
|
771
|
+
if (!info.serveRun.length) return;
|
|
772
|
+
await companions.stop();
|
|
773
|
+
companions = Companions.none();
|
|
774
|
+
try {
|
|
775
|
+
companions = await Companions.start(info, info.serveRun);
|
|
776
|
+
} catch (e) {
|
|
777
|
+
process.stderr.write(`${red("error:")} ${messageOf(e)}
|
|
778
|
+
`);
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
builder = await watch({ builder, live, args, quiet, startedTools, firstBuild, sayWhere, restart });
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
var QUIET = 150;
|
|
785
|
+
var FIRST_COMPILE = 2e3;
|
|
786
|
+
var KEEPS_WRITING = 2e3;
|
|
787
|
+
async function watch({ builder, live, args, quiet, startedTools, firstBuild, sayWhere, restart }) {
|
|
788
|
+
const site = builder.site;
|
|
789
|
+
const tools = live.tools.length > 0;
|
|
790
|
+
if (tools && startedTools) await firstCompile(site.output, quiet);
|
|
791
|
+
const afterBuild = assetStamps(site.output);
|
|
792
|
+
const report = await buildAndReport(builder, live);
|
|
793
|
+
const ok = report.ok;
|
|
794
|
+
for (const asset of report.assets) {
|
|
795
|
+
const file = realPath(asset);
|
|
796
|
+
const stat = fs3.statSync(file, { throwIfNoEntry: false });
|
|
797
|
+
if (stat) afterBuild.set(file, [stat.size, stat.mtimeMs]);
|
|
798
|
+
else afterBuild.delete(file);
|
|
799
|
+
}
|
|
800
|
+
const rx = new Channel();
|
|
801
|
+
const watcher = new Watcher((file) => rx.send([file]));
|
|
802
|
+
watcher.onError = (dir, e) => warn(warning("build", `can't watch ${dir}, or some of the folders beside it: ${messageOf(e)}. A change there won't be seen until the server starts again`));
|
|
803
|
+
try {
|
|
804
|
+
const watching = (dir, recursive) => {
|
|
805
|
+
try {
|
|
806
|
+
watcher.watch(dir, recursive);
|
|
807
|
+
} catch (e) {
|
|
808
|
+
throw new BuildError(`watching ${dir}: ${messageOf(e)}`);
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
const watchPaths = site.watch.map(realPath);
|
|
812
|
+
for (const target of watchPaths) {
|
|
813
|
+
if (isDir3(target)) watching(target, true);
|
|
814
|
+
else if (exists(target)) watching(path3.dirname(target), false);
|
|
815
|
+
}
|
|
816
|
+
const output = realPath(site.output);
|
|
817
|
+
const watchOutput = (must) => {
|
|
818
|
+
if (!isDir3(output) || watchPaths.some((p) => isInside(p, output))) return;
|
|
819
|
+
try {
|
|
820
|
+
watching(output, true);
|
|
821
|
+
} catch (e) {
|
|
822
|
+
if (must) throw e;
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
watchOutput(true);
|
|
826
|
+
const watchedFiles = /* @__PURE__ */ new Set();
|
|
827
|
+
const watchConfigFiles = () => {
|
|
828
|
+
for (const file of [...builder.files, ...builder.read]) {
|
|
829
|
+
if (isInside(output, file) || watchPaths.some((p) => isInside(p, file)) || watchedFiles.has(file)) continue;
|
|
830
|
+
watchedFiles.add(file);
|
|
831
|
+
try {
|
|
832
|
+
watcher.watch(path3.dirname(file), false);
|
|
833
|
+
} catch {
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
watchConfigFiles();
|
|
838
|
+
const root = realPath(site.root);
|
|
839
|
+
if (!watchPaths.some((p) => isInside(p, root))) {
|
|
840
|
+
try {
|
|
841
|
+
watcher.watch(root, false);
|
|
842
|
+
} catch {
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
const carried = [];
|
|
846
|
+
if (ok) {
|
|
847
|
+
if (tools) await settle(rx, output, afterBuild, QUIET, carried);
|
|
848
|
+
await publish(site, live);
|
|
849
|
+
}
|
|
850
|
+
firstBuild(ok);
|
|
851
|
+
sayWhere();
|
|
852
|
+
const contents = /* @__PURE__ */ new Map();
|
|
853
|
+
const show = (file) => isInside(site.root, file) && file !== site.root ? path3.relative(site.root, file) : file;
|
|
854
|
+
const own = (file) => isInside(site.root, file) && path3.relative(site.root, file).split(path3.sep).some((part) => part === ".cache" || part === ".git" || part === "node_modules");
|
|
855
|
+
for (; ; ) {
|
|
856
|
+
const paths = carried.length ? carried.splice(0) : await rx.recv();
|
|
857
|
+
const burst = performance.now();
|
|
858
|
+
while (performance.now() - burst < 1e3) {
|
|
859
|
+
const more = await rx.recvTimeout(120);
|
|
860
|
+
if (!more) break;
|
|
861
|
+
paths.push(...more);
|
|
862
|
+
}
|
|
863
|
+
const changed = paths.map(realPath).filter((file) => !own(file));
|
|
864
|
+
const outputs = changed.filter((file) => isInside(output, file));
|
|
865
|
+
const sources = changed.filter((file) => !isInside(output, file)).filter((file) => {
|
|
866
|
+
const now = sourceState(file);
|
|
867
|
+
const seen = contents.has(file);
|
|
868
|
+
const before = contents.get(file);
|
|
869
|
+
contents.set(file, now);
|
|
870
|
+
return !seen || before !== now;
|
|
871
|
+
});
|
|
872
|
+
const isConfig = (file) => builder.files.has(file) || path3.dirname(file) === root && CONFIG_NAMES.has(path3.basename(file));
|
|
873
|
+
const inSources = (file) => watchPaths.some((p) => isInside(p, file)) || isConfig(file) || builder.read.has(file);
|
|
874
|
+
const config = sources.find(isConfig);
|
|
875
|
+
const source = config === void 0 ? sources.find(inSources) : void 0;
|
|
876
|
+
if (config !== void 0) {
|
|
877
|
+
if (!quiet) say(`${FRESH_TRACKS} ${show(config)} (reading the configuration again)`);
|
|
878
|
+
const next = await Builder.start(args, { reload: true, fallback: builder.alive, said: builder.said });
|
|
879
|
+
if (next) {
|
|
880
|
+
builder.stop();
|
|
881
|
+
return next;
|
|
882
|
+
}
|
|
883
|
+
} else if (source !== void 0) {
|
|
884
|
+
if (!quiet) say(`${FRESH_TRACKS} ${show(source)}`);
|
|
885
|
+
await runBuild(builder, live, async () => {
|
|
886
|
+
if (tools) await settle(rx, output, assetStamps(output), QUIET, carried);
|
|
887
|
+
});
|
|
888
|
+
if (!quiet) sayWhere();
|
|
889
|
+
watchConfigFiles();
|
|
890
|
+
} else if (outputs.some((file) => isAsset(file) || !exists(file))) {
|
|
891
|
+
const differs = await live.changed(site.output);
|
|
892
|
+
let gone = differs.filter((file) => isAsset(file) && !exists(path3.join(site.output, file)));
|
|
893
|
+
if (gone.length && await quietNow(rx)) {
|
|
894
|
+
gone = gone.filter((file) => !exists(path3.join(site.output, file)));
|
|
895
|
+
if (!gone.length) await live.changed(site.output);
|
|
896
|
+
}
|
|
897
|
+
if (gone.length) {
|
|
898
|
+
const names = live.tools;
|
|
899
|
+
const shown = gone.slice(0, 3).map((file) => show(path3.join(site.output, file)));
|
|
900
|
+
const more = gone.length > 3 ? ` and ${gone.length - 3} more` : "";
|
|
901
|
+
const then = names.length === 0 ? "Rebuilding" : names.length === 1 ? `Starting serve.run's ${names[0]} again and rebuilding` : `Starting serve.run's ${names.slice(0, -1).join(", ")} and ${names[names.length - 1]} again and rebuilding`;
|
|
902
|
+
say(`${CAMPSITE_DISTURBED} ${shown.join(", ")}${more} deleted by something other than ssqch serve (another build into ${site.outputName}?). ${then}.`);
|
|
903
|
+
await recover(rx, gone.map((file) => path3.join(site.output, file)), restart);
|
|
904
|
+
watcher.unwatch(output);
|
|
905
|
+
watchOutput(false);
|
|
906
|
+
await runBuild(builder, live, async () => {
|
|
907
|
+
});
|
|
908
|
+
sayWhere();
|
|
909
|
+
} else {
|
|
910
|
+
const file = differs.find(isAsset);
|
|
911
|
+
if (file !== void 0 && !quiet) say(`${FRESH_TRACKS} ${show(path3.join(site.output, file))} (${differs.every((f) => f.endsWith(".css")) ? "swapping it in" : "reloading"})`);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
} finally {
|
|
916
|
+
watcher.close();
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
async function quietNow(rx) {
|
|
920
|
+
const started = performance.now();
|
|
921
|
+
while (performance.now() - started < 6e4) {
|
|
922
|
+
if (!await rx.recvTimeout(2e3)) return true;
|
|
923
|
+
}
|
|
924
|
+
return false;
|
|
925
|
+
}
|
|
926
|
+
async function recover(rx, gone, restart) {
|
|
927
|
+
await restart();
|
|
928
|
+
const started = performance.now();
|
|
929
|
+
while (performance.now() - started < 15e3 && !gone.every(isFile2)) await sleep2(100);
|
|
930
|
+
rx.drain();
|
|
931
|
+
}
|
|
932
|
+
async function runBuild(builder, live, hold) {
|
|
933
|
+
const { ok } = await buildAndReport(builder, live);
|
|
934
|
+
if (ok) {
|
|
935
|
+
await hold();
|
|
936
|
+
await publish(builder.site, live);
|
|
937
|
+
}
|
|
938
|
+
return ok;
|
|
939
|
+
}
|
|
940
|
+
async function buildAndReport(builder, live) {
|
|
941
|
+
const built = await builder.build(live.error !== null);
|
|
942
|
+
if (!built.ok) live.failed(built.error ?? "");
|
|
943
|
+
return built;
|
|
944
|
+
}
|
|
945
|
+
async function publish(site, live) {
|
|
946
|
+
live.repeatMissing(site.output);
|
|
947
|
+
await live.changed(site.output);
|
|
948
|
+
}
|
|
949
|
+
async function firstCompile(output, quiet) {
|
|
950
|
+
const started = performance.now();
|
|
951
|
+
let last = assetStamps(output);
|
|
952
|
+
let wroteAt;
|
|
953
|
+
while (performance.now() - started < FIRST_COMPILE + KEEPS_WRITING) {
|
|
954
|
+
await sleep2(40);
|
|
955
|
+
const now = assetStamps(output);
|
|
956
|
+
if (!sameStamps(now, last)) {
|
|
957
|
+
last = now;
|
|
958
|
+
wroteAt = performance.now();
|
|
959
|
+
} else if (wroteAt !== void 0) {
|
|
960
|
+
if (performance.now() - wroteAt >= QUIET) return;
|
|
961
|
+
} else if (performance.now() - started >= FIRST_COMPILE) {
|
|
962
|
+
if (!quiet) progress(`[ssqch] serve.run's tools wrote no CSS or JavaScript in ${FIRST_COMPILE / 1e3} s; building without it`);
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
async function settle(rx, output, before, first, carried) {
|
|
968
|
+
const started = performance.now();
|
|
969
|
+
const cap = started + first + KEEPS_WRITING;
|
|
970
|
+
let wrote = !sameStamps(assetStamps(output), before);
|
|
971
|
+
let until = wrote ? started + QUIET : started + first;
|
|
972
|
+
for (; ; ) {
|
|
973
|
+
const left = Math.min(until, cap) - performance.now();
|
|
974
|
+
if (left <= 0) return;
|
|
975
|
+
const paths = await rx.recvTimeout(left);
|
|
976
|
+
if (!paths) return;
|
|
977
|
+
for (const given of paths) {
|
|
978
|
+
const file = realPath(given);
|
|
979
|
+
if (!isInside(output, file)) {
|
|
980
|
+
carried.push(file);
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
if (!isAsset(file)) continue;
|
|
984
|
+
const stat = fs3.statSync(file, { throwIfNoEntry: false });
|
|
985
|
+
const was = before.get(file);
|
|
986
|
+
const same = stat === void 0 ? was === void 0 : was !== void 0 && was[0] === stat.size && was[1] === stat.mtimeMs;
|
|
987
|
+
if (wrote || !same) {
|
|
988
|
+
wrote = true;
|
|
989
|
+
until = performance.now() + QUIET;
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
export {
|
|
995
|
+
QUIET,
|
|
996
|
+
serve,
|
|
997
|
+
serveBuilt,
|
|
998
|
+
settle
|
|
999
|
+
};
|