@dogsbay/ssg-cli 0.0.0-stage → 0.2.0-beta.167.1
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/LICENSE +201 -0
- package/NOTICE +17 -0
- package/README.md +62 -2
- package/THIRD-PARTY-NOTICES.txt +2363 -0
- package/chunks/bash-WN2IW2MU.mjs +9 -0
- package/chunks/bat-KLYVST7G.mjs +12 -0
- package/chunks/build-command-WQJANVGR.mjs +409 -0
- package/chunks/c-NBVQ7G3X.mjs +9 -0
- package/chunks/check-command-QTJ6BUNL.mjs +215 -0
- package/chunks/chunk-4KDUWLRS.mjs +12 -0
- package/chunks/chunk-CMGKSS45.mjs +34 -0
- package/chunks/chunk-CUTL24GH.mjs +12 -0
- package/chunks/chunk-ECAZ77NS.mjs +12 -0
- package/chunks/chunk-F6VRYHSY.mjs +5048 -0
- package/chunks/chunk-FJPQZU44.mjs +12 -0
- package/chunks/chunk-G6OTOD3B.mjs +48 -0
- package/chunks/chunk-GIP2FIA6.mjs +12 -0
- package/chunks/chunk-IAR2KPZC.mjs +12 -0
- package/chunks/chunk-IZF74GZZ.mjs +7340 -0
- package/chunks/chunk-JABACD5T.mjs +93 -0
- package/chunks/chunk-JRPXZBM2.mjs +12 -0
- package/chunks/chunk-JXFQPMYJ.mjs +43806 -0
- package/chunks/chunk-JXM2XRAR.mjs +16 -0
- package/chunks/chunk-KEHZHWE6.mjs +12 -0
- package/chunks/chunk-LC74DHS3.mjs +28 -0
- package/chunks/chunk-LNWQN7K7.mjs +47 -0
- package/chunks/chunk-NJDUOQI7.mjs +12 -0
- package/chunks/chunk-OBUK53NJ.mjs +154 -0
- package/chunks/chunk-STRHO6SY.mjs +44 -0
- package/chunks/chunk-TIRB4ROQ.mjs +20 -0
- package/chunks/chunk-UKZGJ5AP.mjs +11201 -0
- package/chunks/chunk-USOBJLOJ.mjs +12 -0
- package/chunks/chunk-X4ZSG4OM.mjs +12 -0
- package/chunks/chunk-XABJJODS.mjs +16 -0
- package/chunks/chunk-XYPTZFVB.mjs +31503 -0
- package/chunks/chunk-YVSP26OS.mjs +175 -0
- package/chunks/chunk-Z7WUDBZV.mjs +5577 -0
- package/chunks/chunk-ZPII4MCC.mjs +12 -0
- package/chunks/chunk-ZW635UYF.mjs +44 -0
- package/chunks/core-Z65HALUT.mjs +5926 -0
- package/chunks/cpp-JNQSOSM7.mjs +11 -0
- package/chunks/csharp-UTVXXLBH.mjs +12 -0
- package/chunks/css-DR3MZC2R.mjs +9 -0
- package/chunks/css-build-WUW5N4IC.mjs +6290 -0
- package/chunks/dev-X3Y5P4KI.mjs +979 -0
- package/chunks/diff-GTIUNONZ.mjs +12 -0
- package/chunks/docker-2O2WO47V.mjs +12 -0
- package/chunks/go-KFRO4WHX.mjs +12 -0
- package/chunks/graphql-H4XAFKUQ.mjs +13 -0
- package/chunks/groovy-IAOB4GSA.mjs +12 -0
- package/chunks/hcl-OV4NBCSV.mjs +12 -0
- package/chunks/highlight-XB2G5VI5.mjs +16 -0
- package/chunks/html-HNUETIJT.mjs +11 -0
- package/chunks/http-POY7GQIM.mjs +33 -0
- package/chunks/ini-ZWSSZBAD.mjs +9 -0
- package/chunks/java-UNHKAALA.mjs +9 -0
- package/chunks/javascript-5I4F2MOK.mjs +9 -0
- package/chunks/json-HUSK22EN.mjs +9 -0
- package/chunks/jsonc-3FJRQVYX.mjs +12 -0
- package/chunks/jsx-MPXQICPA.mjs +9 -0
- package/chunks/katex-assets-2JWTS7KK.mjs +11 -0
- package/chunks/kotlin-44HLBZQT.mjs +12 -0
- package/chunks/makefile-MN7GTSOT.mjs +12 -0
- package/chunks/markdown-BSJW56ZV.mjs +12 -0
- package/chunks/math-VI3BIAAZ.mjs +14509 -0
- package/chunks/nginx-THNEASQW.mjs +17 -0
- package/chunks/pele-SYMSY5GD.mjs +20582 -0
- package/chunks/powershell-FLIIURPX.mjs +12 -0
- package/chunks/properties-5JRW4GXO.mjs +9 -0
- package/chunks/publish-command-J7KGJTLD.mjs +858 -0
- package/chunks/python-AMYOSLZN.mjs +12 -0
- package/chunks/release-diff-JMNCG2T7.mjs +108 -0
- package/chunks/ruby-45KKBAF3.mjs +69 -0
- package/chunks/rust-HHHRMWV3.mjs +12 -0
- package/chunks/scss-SXP65CI5.mjs +16 -0
- package/chunks/serve-CUWABTUV.mjs +66 -0
- package/chunks/shellsession-Z5NIGXRL.mjs +16 -0
- package/chunks/sql-BZT6ET7H.mjs +9 -0
- package/chunks/toml-U6LUKYDZ.mjs +12 -0
- package/chunks/topicmap-V7FYXTVQ.mjs +383 -0
- package/chunks/tsx-65AAFPTH.mjs +9 -0
- package/chunks/typescript-P7INMWMZ.mjs +9 -0
- package/chunks/xml-OWRWNCOT.mjs +10 -0
- package/chunks/xsl-CDSW6KM4.mjs +17 -0
- package/chunks/yaml-GNE6PFDT.mjs +9 -0
- package/cli.mjs +83 -0
- package/highlight-worker.mjs +20 -0
- package/package.json +25 -4
|
@@ -0,0 +1,979 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
contentType
|
|
5
|
+
} from "./chunk-CMGKSS45.mjs";
|
|
6
|
+
import {
|
|
7
|
+
cliMemorySettings,
|
|
8
|
+
diagramRenderers,
|
|
9
|
+
iconResolver
|
|
10
|
+
} from "./chunk-OBUK53NJ.mjs";
|
|
11
|
+
import {
|
|
12
|
+
ConfigError,
|
|
13
|
+
PageClashError,
|
|
14
|
+
TOPIC_MAP,
|
|
15
|
+
assetVerdict,
|
|
16
|
+
build,
|
|
17
|
+
copiedPath,
|
|
18
|
+
dataDirOf,
|
|
19
|
+
defaultTables,
|
|
20
|
+
filePagesIndex,
|
|
21
|
+
insideProject,
|
|
22
|
+
languageTables,
|
|
23
|
+
memberPath,
|
|
24
|
+
parseConfig,
|
|
25
|
+
projectFile,
|
|
26
|
+
projectRoots,
|
|
27
|
+
readProject,
|
|
28
|
+
readText,
|
|
29
|
+
readTopicMaps,
|
|
30
|
+
rebuildUnitLazy,
|
|
31
|
+
siteCssBuilder,
|
|
32
|
+
siteFileFor,
|
|
33
|
+
sourceRoot,
|
|
34
|
+
staticPathServed,
|
|
35
|
+
translationTables,
|
|
36
|
+
versionsOf,
|
|
37
|
+
writeTranslationStubs
|
|
38
|
+
} from "./chunk-XYPTZFVB.mjs";
|
|
39
|
+
import "./chunk-F6VRYHSY.mjs";
|
|
40
|
+
import "./chunk-YVSP26OS.mjs";
|
|
41
|
+
import "./chunk-Z7WUDBZV.mjs";
|
|
42
|
+
import {
|
|
43
|
+
collectClasses,
|
|
44
|
+
isRef,
|
|
45
|
+
isStream
|
|
46
|
+
} from "./chunk-JABACD5T.mjs";
|
|
47
|
+
import "./chunk-G6OTOD3B.mjs";
|
|
48
|
+
import "./chunk-STRHO6SY.mjs";
|
|
49
|
+
|
|
50
|
+
// src/node/dev.ts
|
|
51
|
+
import { timingSafeEqual } from "node:crypto";
|
|
52
|
+
import { existsSync, lstatSync, readFileSync, realpathSync, watch } from "node:fs";
|
|
53
|
+
import { createServer } from "node:http";
|
|
54
|
+
import { extname, join, resolve, sep } from "node:path";
|
|
55
|
+
|
|
56
|
+
// src/core/dev.ts
|
|
57
|
+
var CONFIG_FILES = /* @__PURE__ */ new Set(["dogsbay.config.yml", "dogsbay.config.yaml"]);
|
|
58
|
+
var NAV_FILES = /(^|\/)nav\.(ya?ml|json)$/;
|
|
59
|
+
var START_ONLY = /(^|\/)(_dogsbay\/search\/|llms(-full)?\.txt$)/;
|
|
60
|
+
var SITE_WIDE = /^(_dogsbay\/assets\/|_dogsbay\/(site|versions)\.js$|robots\.txt$|_headers$|_redirects$|sitemap-index\.xml$)/;
|
|
61
|
+
var UNIT_CSS = /(^|\/)_dogsbay\/site\.css$/;
|
|
62
|
+
async function openDevSession(input, o = {}) {
|
|
63
|
+
let out = /* @__PURE__ */ new Map();
|
|
64
|
+
let units = /* @__PURE__ */ new Map();
|
|
65
|
+
let stale = /* @__PURE__ */ new Set();
|
|
66
|
+
const rendering = /* @__PURE__ */ new Map();
|
|
67
|
+
let ctx;
|
|
68
|
+
let config;
|
|
69
|
+
let parsedConfig;
|
|
70
|
+
let taken = 0;
|
|
71
|
+
let takenProblems = 0;
|
|
72
|
+
let buildError;
|
|
73
|
+
let buildProblem;
|
|
74
|
+
let diagnosed = /* @__PURE__ */ new Map();
|
|
75
|
+
let scanProblems = /* @__PURE__ */ new Map();
|
|
76
|
+
const watchScan = (c, into) => {
|
|
77
|
+
let from = 0;
|
|
78
|
+
c.state.scanned = (unit, at) => {
|
|
79
|
+
if (at === "start") from = c.problems.length;
|
|
80
|
+
else into.set(unit.prefix, c.problems.slice(from));
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const failed = (e) => {
|
|
84
|
+
buildError = e.message || String(e);
|
|
85
|
+
buildProblem = problemOf(e, buildError);
|
|
86
|
+
};
|
|
87
|
+
const full = async () => {
|
|
88
|
+
const next = /* @__PURE__ */ new Map();
|
|
89
|
+
const nextUnits = /* @__PURE__ */ new Map();
|
|
90
|
+
const nextDiagnosed = /* @__PURE__ */ new Map();
|
|
91
|
+
const nextScan = /* @__PURE__ */ new Map();
|
|
92
|
+
let nextCtx;
|
|
93
|
+
const sink = async (path, content) => {
|
|
94
|
+
next.set(path, isStream(content) ? await joinChunks(content.chunks()) : content);
|
|
95
|
+
};
|
|
96
|
+
const file = input.files.get("dogsbay.config.yml") ?? input.files.get("dogsbay.config.yaml");
|
|
97
|
+
const text = (file !== void 0 ? await textOf(file) : void 0) ?? input.configText ?? (typeof input.config === "string" ? input.config : void 0);
|
|
98
|
+
const name = input.files.has("dogsbay.config.yml") ? "dogsbay.config.yml" : input.files.has("dogsbay.config.yaml") ? "dogsbay.config.yaml" : void 0;
|
|
99
|
+
let parsed;
|
|
100
|
+
try {
|
|
101
|
+
parsed = typeof input.config === "object" && file === void 0 ? input.config : parseConfig(text);
|
|
102
|
+
} catch (e) {
|
|
103
|
+
throw inConfig(e, name);
|
|
104
|
+
}
|
|
105
|
+
const nextConfig = { ...parsed, llmsTxt: o.llms === true && parsed.llmsTxt, search: o.search === false ? false : parsed.search, math: o.math === false ? false : parsed.math };
|
|
106
|
+
try {
|
|
107
|
+
await build(
|
|
108
|
+
// Drafts are shown while writing, as `dogsbay site dev` shows them.
|
|
109
|
+
{ ...input, config: nextConfig, sink, includeDrafts: true },
|
|
110
|
+
{
|
|
111
|
+
keep: true,
|
|
112
|
+
lazy: o.lazy,
|
|
113
|
+
context: (c) => {
|
|
114
|
+
nextCtx = c;
|
|
115
|
+
watchScan(c, nextScan);
|
|
116
|
+
},
|
|
117
|
+
diagnostics: (f, list) => nextDiagnosed.set(f, list),
|
|
118
|
+
dataLines: o.dataLines,
|
|
119
|
+
// A copy: the build keeps adding the classes of pages it renders later to its own set.
|
|
120
|
+
unitBuilt: (unit, handle, classes) => nextUnits.set(unit.prefix, { unit, handle, classes: new Set(classes) })
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
} catch (e) {
|
|
124
|
+
throw e instanceof ConfigError ? inConfig(e, name) : e;
|
|
125
|
+
}
|
|
126
|
+
parsedConfig = parsed;
|
|
127
|
+
out = next;
|
|
128
|
+
units = nextUnits;
|
|
129
|
+
ctx = nextCtx;
|
|
130
|
+
diagnosed = nextDiagnosed;
|
|
131
|
+
scanProblems = nextScan;
|
|
132
|
+
config = nextCtx?.cfg ?? nextConfig;
|
|
133
|
+
taken = 0;
|
|
134
|
+
takenProblems = 0;
|
|
135
|
+
stale = /* @__PURE__ */ new Set();
|
|
136
|
+
if (o.lazy) for (const u of units.values()) for (const p of u.handle.pageOuts) stale.add(p);
|
|
137
|
+
};
|
|
138
|
+
const noteItems = () => [...ctx?.state.notes.values() ?? []].flat().map((n) => ({ ...n }));
|
|
139
|
+
const unitOf = (path) => {
|
|
140
|
+
let best;
|
|
141
|
+
let bestLen = -1;
|
|
142
|
+
for (const u of units.values()) {
|
|
143
|
+
for (const m of u.unit.members) {
|
|
144
|
+
const root = sourceRoot(m.source);
|
|
145
|
+
const holds = path.startsWith(root) || m.source.from === "openapi" && path === m.source.path.replace(/^\.\//, "");
|
|
146
|
+
if (holds && root.length > bestLen) {
|
|
147
|
+
best = u;
|
|
148
|
+
bestLen = root.length;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return best;
|
|
153
|
+
};
|
|
154
|
+
const unitForSitePath = (path) => {
|
|
155
|
+
let best;
|
|
156
|
+
let bestLen = -1;
|
|
157
|
+
for (const u of units.values()) {
|
|
158
|
+
const pre = u.unit.prefix ? u.unit.prefix + "/" : "";
|
|
159
|
+
if (path.startsWith(pre) && pre.length > bestLen) {
|
|
160
|
+
best = u;
|
|
161
|
+
bestLen = pre.length;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return best;
|
|
165
|
+
};
|
|
166
|
+
const restyle = async (u) => {
|
|
167
|
+
const css = await (await siteCssBuilder(input))({ theme: config.theme, themeCss: ctx.theme.themeCss, extra: ctx.theme.extraCss, candidates: u.classes });
|
|
168
|
+
out.set(`${u.unit.prefix ? u.unit.prefix + "/" : ""}_dogsbay/site.css`, css);
|
|
169
|
+
};
|
|
170
|
+
let requests = Promise.resolve();
|
|
171
|
+
const budgeted = (work) => {
|
|
172
|
+
const run = requests.then(async () => {
|
|
173
|
+
const budget = ctx?.state.templateBudget;
|
|
174
|
+
budget?.reset();
|
|
175
|
+
try {
|
|
176
|
+
return await work();
|
|
177
|
+
} finally {
|
|
178
|
+
const spent = ctx?.state.templateBudget === budget ? budget?.problem() : void 0;
|
|
179
|
+
if (spent) ctx.report("template", spent);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
requests = run.catch(() => void 0);
|
|
183
|
+
return run;
|
|
184
|
+
};
|
|
185
|
+
const renderStale = async (path, u) => {
|
|
186
|
+
await budgeted(() => u.handle.render(path));
|
|
187
|
+
stale.delete(path);
|
|
188
|
+
const html = out.get(path);
|
|
189
|
+
if (typeof html === "string") {
|
|
190
|
+
const used = /* @__PURE__ */ new Set();
|
|
191
|
+
collectClasses(html, used);
|
|
192
|
+
const added = [...used].filter((c) => !u.classes.has(c));
|
|
193
|
+
if (added.length) {
|
|
194
|
+
for (const c of added) u.classes.add(c);
|
|
195
|
+
await restyle(u);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
try {
|
|
200
|
+
await full();
|
|
201
|
+
} catch (e) {
|
|
202
|
+
failed(e);
|
|
203
|
+
}
|
|
204
|
+
const get = async (path) => {
|
|
205
|
+
if (stale.has(path)) {
|
|
206
|
+
const u = [...units.values()].find((x) => x.handle.pageOuts.includes(path));
|
|
207
|
+
if (u) {
|
|
208
|
+
let p = rendering.get(path);
|
|
209
|
+
if (!p) {
|
|
210
|
+
p = renderStale(path, u).finally(() => rendering.delete(path));
|
|
211
|
+
rendering.set(path, p);
|
|
212
|
+
}
|
|
213
|
+
await p;
|
|
214
|
+
} else {
|
|
215
|
+
stale.delete(path);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const c = out.get(path);
|
|
219
|
+
if (c === void 0) return void 0;
|
|
220
|
+
if (isRef(c)) return await c.read();
|
|
221
|
+
const lazy = await textOf(c);
|
|
222
|
+
return lazy ?? c;
|
|
223
|
+
};
|
|
224
|
+
const pageFor = (source) => unitOf(source)?.handle.pageForSource(source);
|
|
225
|
+
const fresh = async (path) => {
|
|
226
|
+
try {
|
|
227
|
+
await get(path);
|
|
228
|
+
} catch {
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
return {
|
|
232
|
+
get config() {
|
|
233
|
+
return config;
|
|
234
|
+
},
|
|
235
|
+
get buildError() {
|
|
236
|
+
return buildError;
|
|
237
|
+
},
|
|
238
|
+
get buildProblem() {
|
|
239
|
+
return buildProblem;
|
|
240
|
+
},
|
|
241
|
+
pageFor,
|
|
242
|
+
async record(path) {
|
|
243
|
+
if (!o.dataLines) return void 0;
|
|
244
|
+
const u = [...units.values()].find((x) => x.handle.pageOuts.includes(path));
|
|
245
|
+
if (!u) return void 0;
|
|
246
|
+
await fresh(path);
|
|
247
|
+
return u.handle.record(path);
|
|
248
|
+
},
|
|
249
|
+
async pagesOf(file, options) {
|
|
250
|
+
if (!o.dataLines) return [];
|
|
251
|
+
if (options?.render !== false) for (const p of [...stale]) await fresh(p);
|
|
252
|
+
const pages = new Set(filePagesIndex([...units.values()].flatMap((u) => u.handle.records()))[file] ?? []);
|
|
253
|
+
if (options?.render === false) {
|
|
254
|
+
for (const u of units.values()) for (const r of u.handle.bodyReaders(file)) pages.add(r);
|
|
255
|
+
const own = pageFor(file);
|
|
256
|
+
if (own) pages.add(own);
|
|
257
|
+
}
|
|
258
|
+
return [...pages].sort();
|
|
259
|
+
},
|
|
260
|
+
sourceFor(path) {
|
|
261
|
+
return unitForSitePath(path)?.handle.sourceForPage(path);
|
|
262
|
+
},
|
|
263
|
+
notes() {
|
|
264
|
+
return noteItems().map((n) => n.message);
|
|
265
|
+
},
|
|
266
|
+
noteItems,
|
|
267
|
+
takeWarnings() {
|
|
268
|
+
const all = ctx?.warnings ?? [];
|
|
269
|
+
const fresh2 = all.slice(taken);
|
|
270
|
+
taken = all.length;
|
|
271
|
+
return fresh2;
|
|
272
|
+
},
|
|
273
|
+
takeProblems() {
|
|
274
|
+
const all = ctx?.problems ?? [];
|
|
275
|
+
const fresh2 = all.slice(takenProblems);
|
|
276
|
+
takenProblems = all.length;
|
|
277
|
+
return fresh2;
|
|
278
|
+
},
|
|
279
|
+
async diagnostics(source) {
|
|
280
|
+
const page = pageFor(source);
|
|
281
|
+
if (page === void 0) return [];
|
|
282
|
+
try {
|
|
283
|
+
await get(page);
|
|
284
|
+
} catch {
|
|
285
|
+
}
|
|
286
|
+
return diagnosed.get(source) ?? [];
|
|
287
|
+
},
|
|
288
|
+
get,
|
|
289
|
+
async change(paths) {
|
|
290
|
+
const t0 = Date.now();
|
|
291
|
+
const before = allFiles();
|
|
292
|
+
const done = (r, marked) => {
|
|
293
|
+
const stale2 = marked ?? /* @__PURE__ */ new Set([...before, ...allFiles()]);
|
|
294
|
+
return { ...r, stale: [...stale2].sort(), ms: Date.now() - t0 };
|
|
295
|
+
};
|
|
296
|
+
try {
|
|
297
|
+
if (buildError !== void 0) {
|
|
298
|
+
await full();
|
|
299
|
+
buildError = void 0;
|
|
300
|
+
buildProblem = void 0;
|
|
301
|
+
return done({ css: true, handled: "full" });
|
|
302
|
+
}
|
|
303
|
+
const marked = /* @__PURE__ */ new Set();
|
|
304
|
+
const r = await budgeted(() => changeParts(paths, marked));
|
|
305
|
+
return done(r, r.handled === "full" ? void 0 : marked);
|
|
306
|
+
} catch (e) {
|
|
307
|
+
failed(e);
|
|
308
|
+
return done({ css: false, handled: "full" }, /* @__PURE__ */ new Set());
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
function allFiles() {
|
|
313
|
+
const all = /* @__PURE__ */ new Set();
|
|
314
|
+
for (const u of units.values()) for (const o2 of u.handle.pageOuts) {
|
|
315
|
+
const src = u.handle.sourceForPage(o2);
|
|
316
|
+
if (src !== void 0) all.add(src);
|
|
317
|
+
}
|
|
318
|
+
for (const list of scanProblems.values()) for (const p of list) if (p.file !== void 0) all.add(p.file);
|
|
319
|
+
return all;
|
|
320
|
+
}
|
|
321
|
+
async function changeParts(paths, marked) {
|
|
322
|
+
const again = /* @__PURE__ */ new Map();
|
|
323
|
+
const markStale = (page, u = unitForSitePath(page), rebuilt = false) => {
|
|
324
|
+
stale.add(page);
|
|
325
|
+
const src = u?.handle.sourceForPage(page);
|
|
326
|
+
if (src === void 0) return;
|
|
327
|
+
marked.add(src);
|
|
328
|
+
if (!rebuilt) again.set(src, u.unit.prefix);
|
|
329
|
+
};
|
|
330
|
+
const themeDir = config.themeDir ? config.themeDir.replace(/\/+$/, "") + "/" : void 0;
|
|
331
|
+
const extraCss = new Set(config.css.map((c) => c.replace(/^\.\//, "")));
|
|
332
|
+
const logos = new Set([config.chrome.logo?.light, config.chrome.logo?.dark].filter((x) => !!x));
|
|
333
|
+
const lazy = /* @__PURE__ */ new Set();
|
|
334
|
+
if (paths.some((p) => CONFIG_FILES.has(p))) {
|
|
335
|
+
const next = await attributesOnly();
|
|
336
|
+
if (!next) {
|
|
337
|
+
await full();
|
|
338
|
+
return { css: true, handled: "full" };
|
|
339
|
+
}
|
|
340
|
+
ctx.cfg = { ...ctx.cfg, attributes: next.attributes };
|
|
341
|
+
config = { ...config, attributes: next.attributes };
|
|
342
|
+
const same = JSON.stringify(next.attributes) === JSON.stringify(parsedConfig?.attributes);
|
|
343
|
+
parsedConfig = next;
|
|
344
|
+
if (!same) for (const prefix of units.keys()) lazy.add(prefix);
|
|
345
|
+
}
|
|
346
|
+
if (paths.some((p) => themeDir && p.startsWith(themeDir) || extraCss.has(p) || logos.has(p))) {
|
|
347
|
+
await full();
|
|
348
|
+
return { css: true, handled: "full" };
|
|
349
|
+
}
|
|
350
|
+
for (const p of paths) ctx.state.scan?.changed(p);
|
|
351
|
+
for (const u of units.values()) u.handle.changed(paths);
|
|
352
|
+
let handled = lazy.size ? "unit" : "none";
|
|
353
|
+
const mark = (h) => {
|
|
354
|
+
const rank = ["none", "asset", "page", "unit", "full"];
|
|
355
|
+
if (rank.indexOf(h) > rank.indexOf(handled)) handled = h;
|
|
356
|
+
};
|
|
357
|
+
for (const p of paths) {
|
|
358
|
+
if (CONFIG_FILES.has(p)) continue;
|
|
359
|
+
const readers = [...units.values()].filter((u2) => u2.handle.navInputs.includes(p) || u2.unit.members.some((m) => m.source.nav === p));
|
|
360
|
+
if (readers.length) {
|
|
361
|
+
for (const u2 of [...readers, unitOf(p)]) if (u2) lazy.add(u2.unit.prefix);
|
|
362
|
+
mark("unit");
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (TOPIC_MAP.test(p)) continue;
|
|
366
|
+
if (config.staticDir && p.startsWith(config.staticDir + "/")) {
|
|
367
|
+
const rel = p.slice(config.staticDir.length + 1);
|
|
368
|
+
if (rel === "_headers" || rel === "_redirects" || !staticPathServed(rel)) continue;
|
|
369
|
+
const files = ctx.state.staticFiles;
|
|
370
|
+
if (!files.has(rel) && out.has(rel)) continue;
|
|
371
|
+
mark("asset");
|
|
372
|
+
const content2 = input.files.get(p);
|
|
373
|
+
if (content2 !== void 0 && (!input.untrusted || await assetVerdict(rel, content2, true, true) === "copy")) {
|
|
374
|
+
files.set(rel, content2);
|
|
375
|
+
out.set(rel, content2);
|
|
376
|
+
} else {
|
|
377
|
+
files.delete(rel);
|
|
378
|
+
out.delete(rel);
|
|
379
|
+
}
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (config.examplesDir && p.startsWith(`${config.examplesDir}/`)) {
|
|
383
|
+
for (const x of units.values()) {
|
|
384
|
+
for (const page of x.handle.exampleReaders(p)) {
|
|
385
|
+
markStale(page, x);
|
|
386
|
+
mark("page");
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const dataDirs = (x) => [...new Set(x.unit.members.map((m) => dataDirOf(config, m.source)))].filter((d) => p.startsWith(`${d}/`));
|
|
392
|
+
const dataUnits = [...units.values()].filter((x) => dataDirs(x).length);
|
|
393
|
+
if (dataUnits.length) {
|
|
394
|
+
for (const x of dataUnits) {
|
|
395
|
+
if (x.handle.dataInTitles()) {
|
|
396
|
+
lazy.add(x.unit.prefix);
|
|
397
|
+
mark("unit");
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
x.handle.invalidateData();
|
|
401
|
+
for (const page of x.handle.dataReaders(p)) {
|
|
402
|
+
markStale(page, x);
|
|
403
|
+
mark("page");
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
const u = unitOf(p);
|
|
409
|
+
if (!u) continue;
|
|
410
|
+
const content = input.files.get(p);
|
|
411
|
+
if (/\.(md|markdown)$/i.test(p)) {
|
|
412
|
+
const page = u.handle.pageForSource(p);
|
|
413
|
+
const text = content === void 0 ? void 0 : await textOf(content);
|
|
414
|
+
const same = page !== void 0 && text !== void 0 && u.handle.sameFrontMatter(p, text);
|
|
415
|
+
const readers2 = (same || !page && u.handle.neverPage(p)) && u.handle.included(p) ? u.handle.includeReaders(p) : void 0;
|
|
416
|
+
if (same && !u.handle.included(p)) {
|
|
417
|
+
markStale(page, u);
|
|
418
|
+
mark("page");
|
|
419
|
+
} else if (readers2) {
|
|
420
|
+
u.handle.forget(p);
|
|
421
|
+
if (page) markStale(page, u);
|
|
422
|
+
for (const r of readers2) markStale(r, u);
|
|
423
|
+
mark("page");
|
|
424
|
+
} else {
|
|
425
|
+
u.handle.forget(p);
|
|
426
|
+
lazy.add(u.unit.prefix);
|
|
427
|
+
mark("unit");
|
|
428
|
+
}
|
|
429
|
+
} else if (NAV_FILES.test(p)) {
|
|
430
|
+
lazy.add(u.unit.prefix);
|
|
431
|
+
mark("unit");
|
|
432
|
+
} else if (content === void 0 || /\.(md|markdown|ya?ml|json|txt|html?|csv)$/i.test(p)) {
|
|
433
|
+
u.handle.forget(p);
|
|
434
|
+
lazy.add(u.unit.prefix);
|
|
435
|
+
mark("unit");
|
|
436
|
+
} else {
|
|
437
|
+
mark("asset");
|
|
438
|
+
const at = memberPath(u.unit, p)?.path ?? p.slice(sourceRoot(u.unit.source).length);
|
|
439
|
+
const key = u.unit.prefix ? `${u.unit.prefix}/${at}` : at;
|
|
440
|
+
const sanitizing = (config.html ?? input.defaultHtml ?? "keep") === "sanitize";
|
|
441
|
+
if (key === "_headers" || key === "_redirects") continue;
|
|
442
|
+
if (copiedPath(at) && await assetVerdict(at, content, input.untrusted === true, sanitizing) === "copy") out.set(key, content);
|
|
443
|
+
else out.delete(key);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (lazy.size) for (const s of ctx.state.stubs) out.delete(s);
|
|
447
|
+
const pagesBefore = new Map([...lazy].map((prefix) => [prefix, [...units.get(prefix).handle.pageOuts].sort().join("\n")]));
|
|
448
|
+
for (const prefix of lazy) {
|
|
449
|
+
const u = units.get(prefix);
|
|
450
|
+
for (const key of [...out.keys()]) {
|
|
451
|
+
if (START_ONLY.test(key) || SITE_WIDE.test(key) || UNIT_CSS.test(key) || ctx.state.staticFiles?.has(key)) continue;
|
|
452
|
+
if (unitForSitePath(key) === u) out.delete(key);
|
|
453
|
+
}
|
|
454
|
+
for (const s of [...stale]) if (unitForSitePath(s) === u) stale.delete(s);
|
|
455
|
+
for (const o2 of u.handle.pageOuts) {
|
|
456
|
+
const src = u.handle.sourceForPage(o2);
|
|
457
|
+
if (src !== void 0) marked.add(src);
|
|
458
|
+
}
|
|
459
|
+
const named = (list) => {
|
|
460
|
+
for (const p of list ?? []) if (p.file !== void 0) marked.add(p.file);
|
|
461
|
+
};
|
|
462
|
+
named(scanProblems.get(prefix));
|
|
463
|
+
const handle = await rebuildUnitLazy({ ...ctx, emit: emitInto }, u.unit);
|
|
464
|
+
named(scanProblems.get(prefix));
|
|
465
|
+
const next = { ...u, handle };
|
|
466
|
+
units.set(prefix, next);
|
|
467
|
+
for (const o2 of handle.pageOuts) markStale(o2, next, true);
|
|
468
|
+
}
|
|
469
|
+
if (lazy.size && ctx.state.siteFile && [...lazy].some((prefix) => pagesBefore.get(prefix) !== [...units.get(prefix).handle.pageOuts].sort().join("\n"))) {
|
|
470
|
+
const all = [...units.values()].map((u) => u.unit);
|
|
471
|
+
ctx.state.siteFile = await siteFileFor(config, all, input.files, true);
|
|
472
|
+
for (const o2 of ctx.state.crossLinked) {
|
|
473
|
+
const u = unitForSitePath(o2);
|
|
474
|
+
if (u && !lazy.has(u.unit.prefix)) markStale(o2, u);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const isDefault = (u) => u.version !== void 0 && u.version === versionsOf(config, u.product).defaultVersion;
|
|
478
|
+
const changedDefaults = [...lazy].map((prefix) => units.get(prefix)?.unit).filter((u) => !!u && isDefault(u));
|
|
479
|
+
if (changedDefaults.length) {
|
|
480
|
+
const all = [...units.values()].map((u) => u.unit);
|
|
481
|
+
ctx.state.canonical = await defaultTables(config, all, all, input.files, readText);
|
|
482
|
+
for (const u of units.values()) if (u.unit.version !== void 0 && !isDefault(u.unit) && changedDefaults.some((d) => (d.product ?? "") === (u.unit.product ?? ""))) for (const o2 of u.handle.pageOuts) markStale(o2, u);
|
|
483
|
+
}
|
|
484
|
+
if (lazy.size && ctx.state.languages) {
|
|
485
|
+
const all = [...units.values()].map((u) => u.unit);
|
|
486
|
+
const versions = new Set([...lazy].map((prefix) => units.get(prefix)?.unit.version));
|
|
487
|
+
ctx.state.languages = await languageTables(config, all, all, input.files, readText);
|
|
488
|
+
for (const u of units.values()) if (versions.has(u.unit.version) && !lazy.has(u.unit.prefix)) for (const o2 of u.handle.pageOuts) markStale(o2, u);
|
|
489
|
+
}
|
|
490
|
+
if (lazy.size) {
|
|
491
|
+
const all = [...units.values()].map((u) => u.unit);
|
|
492
|
+
const pages = [...units.values()].flatMap((u) => u.handle.pageOuts);
|
|
493
|
+
ctx.state.stubs = await writeTranslationStubs(
|
|
494
|
+
config,
|
|
495
|
+
all,
|
|
496
|
+
all,
|
|
497
|
+
await translationTables(config, all, input.files, readText, true),
|
|
498
|
+
// What is served now, not what the start's build wrote: a removed translation's path is free.
|
|
499
|
+
{ written: new Set(out.keys()), pageOut: new Map(pages.map((p) => [p, p])) },
|
|
500
|
+
emitInto,
|
|
501
|
+
(p) => stale.has(p)
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
for (const [src, prefix] of again) {
|
|
505
|
+
if (lazy.has(prefix)) continue;
|
|
506
|
+
for (const p of scanProblems.get(prefix) ?? []) {
|
|
507
|
+
if (p.file !== src) continue;
|
|
508
|
+
ctx.problems.push(p);
|
|
509
|
+
ctx.warnings.push(p.text);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return { css: false, handled };
|
|
513
|
+
}
|
|
514
|
+
async function attributesOnly() {
|
|
515
|
+
const file = input.files.get("dogsbay.config.yml") ?? input.files.get("dogsbay.config.yaml");
|
|
516
|
+
const text = file !== void 0 ? await textOf(file) : void 0;
|
|
517
|
+
if (text === void 0 || !parsedConfig) return void 0;
|
|
518
|
+
let next;
|
|
519
|
+
try {
|
|
520
|
+
next = parseConfig(text);
|
|
521
|
+
} catch {
|
|
522
|
+
return void 0;
|
|
523
|
+
}
|
|
524
|
+
const rest = (c) => JSON.stringify({ ...c, attributes: void 0 });
|
|
525
|
+
return rest(next) === rest(parsedConfig) ? next : void 0;
|
|
526
|
+
}
|
|
527
|
+
async function emitInto(path, content) {
|
|
528
|
+
if (START_ONLY.test(path)) return;
|
|
529
|
+
if (path.split("/").some((s) => s === ".." || s === "." || s === "") || path.startsWith("/")) return;
|
|
530
|
+
const c = await resolveContent(content);
|
|
531
|
+
out.set(path, c);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
async function resolveContent(c) {
|
|
535
|
+
if (isStream(c)) return joinChunks(c.chunks());
|
|
536
|
+
const lazy = await textOf(c);
|
|
537
|
+
return lazy ?? c;
|
|
538
|
+
}
|
|
539
|
+
async function textOf(c) {
|
|
540
|
+
if (typeof c === "string") return c;
|
|
541
|
+
const lazy = c;
|
|
542
|
+
if (lazy?.lazyText && lazy.read) return await lazy.read();
|
|
543
|
+
return void 0;
|
|
544
|
+
}
|
|
545
|
+
async function joinChunks(chunks) {
|
|
546
|
+
let s = "";
|
|
547
|
+
for await (const c of chunks) s += c;
|
|
548
|
+
return s;
|
|
549
|
+
}
|
|
550
|
+
function inConfig(e, file) {
|
|
551
|
+
if (e && typeof e === "object") Object.assign(e, { inConfig: true }, file ? { configFile: file } : {});
|
|
552
|
+
return e;
|
|
553
|
+
}
|
|
554
|
+
function problemOf(e, text) {
|
|
555
|
+
const err = e ?? {};
|
|
556
|
+
const config = err.inConfig === true || e instanceof ConfigError;
|
|
557
|
+
const file = err.configFile ?? (e instanceof PageClashError ? e.file : void 0);
|
|
558
|
+
const at = typeof err.mark?.line === "number" ? { line: err.mark.line + 1, column: (err.mark.column ?? 0) + 1 } : {};
|
|
559
|
+
return { code: config ? "config" : "build", ...file ? { file } : {}, ...at, message: err.reason || text.split("\n")[0], text };
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// src/node/dev.ts
|
|
563
|
+
var HTML = contentType("index.html");
|
|
564
|
+
var EVENTS = "/_dogsbay/dev/events";
|
|
565
|
+
var CONFIG_NAMES = ["dogsbay.config.yml", "dogsbay.config.yaml"];
|
|
566
|
+
var CLIENT = "/_dogsbay/dev/client.js";
|
|
567
|
+
var CHANGE = "/_dogsbay/dev/change";
|
|
568
|
+
var MAX_BODY = 1 << 20;
|
|
569
|
+
var CLIENT_JS = `(() => {
|
|
570
|
+
let es;
|
|
571
|
+
// The count when this page was served: a tab opened in the background catches up when shown.
|
|
572
|
+
let seen = window.__DOGSBAY_DEV_GEN__;
|
|
573
|
+
const overlay = (list) => {
|
|
574
|
+
let box = document.getElementById("dogsbay-dev-warnings");
|
|
575
|
+
if (!list.length) { if (box) box.remove(); return; }
|
|
576
|
+
if (!box) {
|
|
577
|
+
box = document.createElement("div");
|
|
578
|
+
box.id = "dogsbay-dev-warnings";
|
|
579
|
+
box.setAttribute("role", "status");
|
|
580
|
+
box.style.cssText = "position:fixed;bottom:12px;right:12px;z-index:2147483647;max-width:min(560px,90vw);max-height:40vh;overflow:auto;background:#1f2937;color:#f9fafb;font:12px/1.5 ui-monospace,monospace;padding:10px 12px;border-radius:8px;box-shadow:0 4px 16px rgba(0,0,0,.3)";
|
|
581
|
+
document.body.appendChild(box);
|
|
582
|
+
}
|
|
583
|
+
box.innerHTML = "";
|
|
584
|
+
const close = document.createElement("button");
|
|
585
|
+
close.textContent = "Dismiss";
|
|
586
|
+
close.style.cssText = "float:right;margin-left:8px;background:none;border:1px solid #6b7280;color:inherit;border-radius:4px;cursor:pointer";
|
|
587
|
+
close.onclick = () => box.remove();
|
|
588
|
+
box.appendChild(close);
|
|
589
|
+
for (const w of list) { const p = document.createElement("div"); p.textContent = w; box.appendChild(p); }
|
|
590
|
+
};
|
|
591
|
+
const open = () => {
|
|
592
|
+
if (es) return;
|
|
593
|
+
es = new EventSource(${JSON.stringify(EVENTS)});
|
|
594
|
+
es.addEventListener("hello", (e) => {
|
|
595
|
+
if (seen !== undefined && e.data !== seen) return location.reload();
|
|
596
|
+
seen = e.data;
|
|
597
|
+
});
|
|
598
|
+
es.addEventListener("reload", () => location.reload());
|
|
599
|
+
es.addEventListener("css", (e) => {
|
|
600
|
+
seen = e.data;
|
|
601
|
+
// An error page has no stylesheets to swap: it shows the site again.
|
|
602
|
+
if (document.querySelector('meta[name="dogsbay-dev-error"]')) return location.reload();
|
|
603
|
+
for (const l of document.querySelectorAll('link[rel="stylesheet"]')) {
|
|
604
|
+
const u = new URL(l.href); u.searchParams.set("v", String(Date.now())); l.href = u.href;
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
es.addEventListener("warnings", (e) => overlay(JSON.parse(e.data)));
|
|
608
|
+
};
|
|
609
|
+
const close = () => { if (es) es.close(); es = undefined; };
|
|
610
|
+
document.addEventListener("visibilitychange", () => (document.hidden ? close() : open()));
|
|
611
|
+
addEventListener("pagehide", close);
|
|
612
|
+
addEventListener("pageshow", () => { if (!document.hidden) open(); });
|
|
613
|
+
if (!document.hidden) open();
|
|
614
|
+
})();
|
|
615
|
+
`;
|
|
616
|
+
function readyLine(server) {
|
|
617
|
+
return `DOGSBAY_DEV_READY ${serverUrl(server)}`;
|
|
618
|
+
}
|
|
619
|
+
function serverUrl(server) {
|
|
620
|
+
const a = server.address();
|
|
621
|
+
const host = a.family === "IPv6" ? `[${a.address}]` : a.address;
|
|
622
|
+
return `http://${host}:${a.port}/`;
|
|
623
|
+
}
|
|
624
|
+
async function dev(projectDir, port, o = {}) {
|
|
625
|
+
const project = resolve(projectDir);
|
|
626
|
+
const log = o.log ?? ((m) => console.log(m));
|
|
627
|
+
const findConfig = () => CONFIG_NAMES.find((f) => existsSync(join(project, f)));
|
|
628
|
+
let cfgName = findConfig();
|
|
629
|
+
const cfgText = cfgName ? readFileSync(join(project, cfgName), "utf8") : void 0;
|
|
630
|
+
const files = /* @__PURE__ */ new Map();
|
|
631
|
+
const input = { files };
|
|
632
|
+
let cfg;
|
|
633
|
+
let roots = [];
|
|
634
|
+
let navMaps = /* @__PURE__ */ new Set();
|
|
635
|
+
const outDir = join(project, "out");
|
|
636
|
+
const layout = async (text) => {
|
|
637
|
+
try {
|
|
638
|
+
cfg = parseConfig(text);
|
|
639
|
+
} catch {
|
|
640
|
+
if (cfg) return false;
|
|
641
|
+
cfg = parseConfig(void 0);
|
|
642
|
+
}
|
|
643
|
+
if (!cfgName && !existsSync(join(project, "content"))) cfg.sources = [{ path: "." }];
|
|
644
|
+
if (cfgName) delete input.config;
|
|
645
|
+
else input.config = cfg;
|
|
646
|
+
roots = cfg.sources.map((s) => s.path === "." ? "" : s.path.replace(/^\.\//, "").replace(/\/+$/, ""));
|
|
647
|
+
files.clear();
|
|
648
|
+
for (const [k, v] of readProject(project, projectRoots(cfg), [outDir])) files.set(k, v);
|
|
649
|
+
if (cfgName && text !== void 0) files.set(cfgName, text);
|
|
650
|
+
navMaps = new Set(await readTopicMaps(project, cfg, files, [outDir]));
|
|
651
|
+
return true;
|
|
652
|
+
};
|
|
653
|
+
await layout(cfgText);
|
|
654
|
+
const { highlightAhead: _, ...memory } = cliMemorySettings(false);
|
|
655
|
+
Object.assign(input, {
|
|
656
|
+
...memory,
|
|
657
|
+
diagrams: o.diagrams === false ? void 0 : diagramRenderers(),
|
|
658
|
+
icons: await iconResolver()
|
|
659
|
+
});
|
|
660
|
+
const session = await openDevSession(input, { lazy: o.lazy, search: o.search, llms: o.llms });
|
|
661
|
+
const clients = /* @__PURE__ */ new Set();
|
|
662
|
+
let generation = 0;
|
|
663
|
+
const send = (event, data) => {
|
|
664
|
+
if (event !== "warnings") generation++;
|
|
665
|
+
for (const c of clients) c.write(`event: ${event}
|
|
666
|
+
data: ${data ?? generation}
|
|
667
|
+
|
|
668
|
+
`);
|
|
669
|
+
};
|
|
670
|
+
const report = () => {
|
|
671
|
+
const fresh = session.takeWarnings();
|
|
672
|
+
for (const w of fresh) log(`warn ${w}`);
|
|
673
|
+
if (fresh.length) send("warnings", JSON.stringify(fresh));
|
|
674
|
+
return fresh;
|
|
675
|
+
};
|
|
676
|
+
let failure = session.buildError;
|
|
677
|
+
const watched = (rel) => CONFIG_NAMES.includes(rel) || [...roots, ...projectRoots(cfg)].some((r) => r === "" || rel === r || rel.startsWith(r + "/")) || navMaps.has(rel) || rel.startsWith(cfg.themeDir.replace(/\/+$/, "") + "/") || cfg.css.some((c) => c.replace(/^\.\//, "") === rel);
|
|
678
|
+
const inExamples = (rel) => cfg.examplesDir !== void 0 && rel.startsWith(cfg.examplesDir + "/") && !/(^|\/)\.[^/]*$|~$|\.sw.$/.test(rel);
|
|
679
|
+
const ignored = (rel) => !inExamples(rel) && (/^out(\/|$)|(^|\/)(\.git|node_modules|\.scratch|\.dogsbay)(\/|$)/.test(rel) || /(^|\/)\.[^/]*$|~$|\.sw.$/.test(rel));
|
|
680
|
+
let pending = /* @__PURE__ */ new Set();
|
|
681
|
+
let timer;
|
|
682
|
+
let running = Promise.resolve();
|
|
683
|
+
const queue = (f) => {
|
|
684
|
+
const p = running.then(f);
|
|
685
|
+
running = p.catch(() => {
|
|
686
|
+
});
|
|
687
|
+
return p;
|
|
688
|
+
};
|
|
689
|
+
const bases = () => [.../* @__PURE__ */ new Set([...roots, cfg.themeDir, ...cfg.css, ...projectRoots(cfg)])].filter(Boolean).map((r) => join(project, r));
|
|
690
|
+
const urlOf = (path) => `/${cfg.basePath ? cfg.basePath + "/" : ""}${path.replace(/(^|\/)index\.html$/, "$1")}`;
|
|
691
|
+
const apply = async (written, removed, folders) => {
|
|
692
|
+
const result = { pages: [], removed: [], warnings: [], error: null };
|
|
693
|
+
const changed = /* @__PURE__ */ new Set();
|
|
694
|
+
const wasFailing = failure !== void 0;
|
|
695
|
+
for (const p of [...written, ...removed]) {
|
|
696
|
+
if (!/\.(md|markdown)$/i.test(p) || written.includes(p) && projectFile(project, p, bases())) continue;
|
|
697
|
+
const page = session.pageFor(p);
|
|
698
|
+
if (page) result.removed.push({ path: p, url: urlOf(page) });
|
|
699
|
+
}
|
|
700
|
+
try {
|
|
701
|
+
if ([...written, ...removed].some((p) => CONFIG_NAMES.includes(p))) {
|
|
702
|
+
const before = cfgName;
|
|
703
|
+
let text;
|
|
704
|
+
cfgName = findConfig();
|
|
705
|
+
try {
|
|
706
|
+
text = cfgName ? readFileSync(join(project, cfgName), "utf8") : void 0;
|
|
707
|
+
} catch {
|
|
708
|
+
cfgName = void 0;
|
|
709
|
+
}
|
|
710
|
+
if (before && before !== cfgName) files.delete(before);
|
|
711
|
+
if (!await layout(text) && cfgName && text !== void 0) files.set(cfgName, text);
|
|
712
|
+
for (const n of /* @__PURE__ */ new Set([before, cfgName])) if (n) changed.add(n);
|
|
713
|
+
} else {
|
|
714
|
+
for (const p of removed) {
|
|
715
|
+
if (files.delete(p)) changed.add(p);
|
|
716
|
+
}
|
|
717
|
+
for (const p of written) {
|
|
718
|
+
const f = projectFile(project, p, bases());
|
|
719
|
+
if (f) {
|
|
720
|
+
files.set(p, f);
|
|
721
|
+
changed.add(p);
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
if (!folders) {
|
|
725
|
+
if (files.delete(p)) changed.add(p);
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
for (const k of [...files.keys()]) {
|
|
729
|
+
if (k === p || k.startsWith(p + "/")) {
|
|
730
|
+
files.delete(k);
|
|
731
|
+
changed.add(k);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
if (isProjectFolder(project, p, bases())) {
|
|
735
|
+
for (const [k, v] of readProject(project, [p], [join(project, "out")])) {
|
|
736
|
+
files.set(k, v);
|
|
737
|
+
changed.add(k);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
if ([...changed].some((p) => TOPIC_MAP.test(p) || navMaps.has(p))) {
|
|
743
|
+
const before = new Set(files.keys());
|
|
744
|
+
navMaps = new Set(await readTopicMaps(project, cfg, files, [outDir]));
|
|
745
|
+
for (const k of files.keys()) if (!before.has(k)) changed.add(k);
|
|
746
|
+
}
|
|
747
|
+
} catch (e) {
|
|
748
|
+
result.error = `reading the project: ${e.message}`;
|
|
749
|
+
}
|
|
750
|
+
if (!changed.size) return result;
|
|
751
|
+
const list = [...changed];
|
|
752
|
+
const t = performance.now();
|
|
753
|
+
await session.change(list);
|
|
754
|
+
failure = session.buildError;
|
|
755
|
+
if (failure !== void 0) {
|
|
756
|
+
log(`error ${failure}`);
|
|
757
|
+
result.error = failure;
|
|
758
|
+
result.warnings = report();
|
|
759
|
+
send("reload");
|
|
760
|
+
return result;
|
|
761
|
+
}
|
|
762
|
+
for (const p of written) {
|
|
763
|
+
if (!/\.(md|markdown)$/i.test(p)) continue;
|
|
764
|
+
const page = session.pageFor(p);
|
|
765
|
+
if (!page) continue;
|
|
766
|
+
result.pages.push({ path: p, url: urlOf(page) });
|
|
767
|
+
try {
|
|
768
|
+
await session.get(page);
|
|
769
|
+
} catch (e) {
|
|
770
|
+
const m = `${p}: ${e.message || String(e)}`;
|
|
771
|
+
log(`error ${m}`);
|
|
772
|
+
if (result.error === null) result.error = m;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
log(`changed ${list.length > 5 ? `${list.slice(0, 5).join(", ")} and ${list.length - 5} more` : list.join(", ")} (${Math.round(performance.now() - t)} ms)`);
|
|
776
|
+
result.warnings = report();
|
|
777
|
+
send(!wasFailing && list.every((p) => p.endsWith(".css")) ? "css" : "reload");
|
|
778
|
+
return result;
|
|
779
|
+
};
|
|
780
|
+
const flush = () => {
|
|
781
|
+
const paths = [...pending];
|
|
782
|
+
pending = /* @__PURE__ */ new Set();
|
|
783
|
+
void queue(() => apply(paths, [], true)).catch((e) => log(`error ${e.message}`));
|
|
784
|
+
};
|
|
785
|
+
let watcher;
|
|
786
|
+
if (o.watch !== false) try {
|
|
787
|
+
watcher = watch(project, { recursive: true }, (_e, name) => {
|
|
788
|
+
if (!name) return;
|
|
789
|
+
const rel = name.split(sep).join("/");
|
|
790
|
+
if (ignored(rel) || !watched(rel)) return;
|
|
791
|
+
pending.add(rel);
|
|
792
|
+
clearTimeout(timer);
|
|
793
|
+
timer = setTimeout(flush, 100);
|
|
794
|
+
});
|
|
795
|
+
} catch (e) {
|
|
796
|
+
log(`warn cannot watch ${project}: ${e.message}; pages will not update`);
|
|
797
|
+
}
|
|
798
|
+
let examplesWatcher;
|
|
799
|
+
const ex = cfg.examplesDir;
|
|
800
|
+
if (o.watch !== false && ex) {
|
|
801
|
+
const dir = join(project, ex);
|
|
802
|
+
try {
|
|
803
|
+
if (realpathSync(dir) !== join(realpathSync(project), ex)) {
|
|
804
|
+
examplesWatcher = watch(dir, { recursive: true }, (_e, name) => {
|
|
805
|
+
if (!name) return;
|
|
806
|
+
const rel = `${ex}/${name.split(sep).join("/")}`;
|
|
807
|
+
if (ignored(rel)) return;
|
|
808
|
+
pending.add(rel);
|
|
809
|
+
clearTimeout(timer);
|
|
810
|
+
timer = setTimeout(flush, 100);
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
} catch {
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
const server = createServer(async (req, res) => {
|
|
817
|
+
const base = cfg.basePath ? `/${cfg.basePath}` : "";
|
|
818
|
+
try {
|
|
819
|
+
const url = new URL(`http://x${req.url?.startsWith("/") ? "" : "/"}${req.url ?? "/"}`);
|
|
820
|
+
if (url.pathname === EVENTS) {
|
|
821
|
+
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache", connection: "keep-alive" });
|
|
822
|
+
res.write(`: dogsbay dev
|
|
823
|
+
|
|
824
|
+
event: hello
|
|
825
|
+
data: ${generation}
|
|
826
|
+
|
|
827
|
+
`);
|
|
828
|
+
clients.add(res);
|
|
829
|
+
req.on("close", () => clients.delete(res));
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
if (url.pathname === CLIENT) {
|
|
833
|
+
res.writeHead(200, { "content-type": contentType("client.js"), "cache-control": "no-cache" });
|
|
834
|
+
return res.end(CLIENT_JS);
|
|
835
|
+
}
|
|
836
|
+
if (url.pathname === CHANGE && o.token) return await control(req, res);
|
|
837
|
+
let path = decodeURIComponent(url.pathname);
|
|
838
|
+
if (base && (path === base || path.startsWith(base + "/"))) path = path.slice(base.length);
|
|
839
|
+
path = path.replace(/^\/+/, "");
|
|
840
|
+
if (path.split("/").some((s) => s === "..")) {
|
|
841
|
+
res.writeHead(400, { "content-type": "text/plain" });
|
|
842
|
+
return res.end("Bad request");
|
|
843
|
+
}
|
|
844
|
+
const candidates = path === "" || path.endsWith("/") ? [`${path}index.html`] : [path, `${path}.html`, `${path}/index.html`];
|
|
845
|
+
if (failure !== void 0 && !isAsset(path)) return sendError(res, "The site could not be built", failure);
|
|
846
|
+
for (const c of candidates) {
|
|
847
|
+
let body;
|
|
848
|
+
try {
|
|
849
|
+
body = await session.get(c);
|
|
850
|
+
} catch (e) {
|
|
851
|
+
if (!c.endsWith(".html")) throw e;
|
|
852
|
+
report();
|
|
853
|
+
const source = session.sourceFor(c);
|
|
854
|
+
return sendError(res, `${source ?? c} could not be rendered`, e.stack ?? String(e));
|
|
855
|
+
}
|
|
856
|
+
if (body === void 0) continue;
|
|
857
|
+
if (c === `${path}/index.html`) {
|
|
858
|
+
res.writeHead(301, { location: url.pathname.replace(/^\/+/, "/") + "/" + url.search });
|
|
859
|
+
return res.end();
|
|
860
|
+
}
|
|
861
|
+
const type = contentType(c);
|
|
862
|
+
res.writeHead(200, { "content-type": type, "cache-control": "no-store" });
|
|
863
|
+
report();
|
|
864
|
+
return res.end(type === HTML ? inject(String(typeof body === "string" ? body : new TextDecoder().decode(body)), generation) : body);
|
|
865
|
+
}
|
|
866
|
+
let nf;
|
|
867
|
+
for (let d = path.split("/").slice(0, -1); !nf; d = d.slice(0, -1)) {
|
|
868
|
+
nf = await session.get([...d, "404.html"].join("/"));
|
|
869
|
+
if (!d.length) break;
|
|
870
|
+
}
|
|
871
|
+
res.writeHead(404, { "content-type": nf ? HTML : "text/plain" });
|
|
872
|
+
res.end(nf ? inject(typeof nf === "string" ? nf : new TextDecoder().decode(nf), generation) : "Not found");
|
|
873
|
+
} catch (e) {
|
|
874
|
+
if (res.headersSent) return res.destroy();
|
|
875
|
+
res.writeHead(e instanceof URIError ? 400 : 500, { "content-type": "text/plain" });
|
|
876
|
+
res.end(e instanceof URIError ? "Bad request" : `Server error: ${e.message}`);
|
|
877
|
+
}
|
|
878
|
+
});
|
|
879
|
+
async function control(req, res) {
|
|
880
|
+
const json = (status, body2) => {
|
|
881
|
+
res.writeHead(status, { "content-type": "application/json", "cache-control": "no-store" });
|
|
882
|
+
res.end(JSON.stringify(body2));
|
|
883
|
+
};
|
|
884
|
+
if (req.headers.origin !== void 0) return json(403, { error: "requests from browsers are refused" });
|
|
885
|
+
if (!authorized(req.headers.authorization, o.token)) return json(401, { error: "missing or wrong token" });
|
|
886
|
+
if (req.method !== "POST") {
|
|
887
|
+
res.setHeader("allow", "POST");
|
|
888
|
+
return json(405, { error: "POST only" });
|
|
889
|
+
}
|
|
890
|
+
const chunks = [];
|
|
891
|
+
let size = 0;
|
|
892
|
+
for await (const chunk of req) {
|
|
893
|
+
size += chunk.length;
|
|
894
|
+
if (size > MAX_BODY) return json(413, { error: "request too large" });
|
|
895
|
+
chunks.push(chunk);
|
|
896
|
+
}
|
|
897
|
+
let body;
|
|
898
|
+
try {
|
|
899
|
+
body = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
900
|
+
} catch {
|
|
901
|
+
return json(400, { error: "the body is not JSON" });
|
|
902
|
+
}
|
|
903
|
+
const list = (v) => v === void 0 ? [] : Array.isArray(v) && v.every((x) => typeof x === "string") ? v : void 0;
|
|
904
|
+
const written = list(body?.written);
|
|
905
|
+
const removed = list(body?.removed);
|
|
906
|
+
if (!written || !removed) return json(400, { error: "expected {written: string[], removed: string[]}" });
|
|
907
|
+
const bad = [...written, ...removed].filter((p) => !projectPath(p) || ignored(p) || failure === void 0 && !watched(p));
|
|
908
|
+
if (bad.length) return json(400, { error: "paths outside what the site reads", paths: bad });
|
|
909
|
+
return json(200, await queue(() => apply(written, removed, false)));
|
|
910
|
+
}
|
|
911
|
+
function sendError(res, title, detail) {
|
|
912
|
+
res.writeHead(500, { "content-type": HTML, "cache-control": "no-store" });
|
|
913
|
+
res.end(inject(errorPage(title, detail), generation));
|
|
914
|
+
}
|
|
915
|
+
await new Promise((r) => server.listen(port, o.host ?? "127.0.0.1", () => r()));
|
|
916
|
+
o.listening?.(server);
|
|
917
|
+
report();
|
|
918
|
+
if (failure) log(`error ${failure}`);
|
|
919
|
+
return {
|
|
920
|
+
server,
|
|
921
|
+
session,
|
|
922
|
+
async close() {
|
|
923
|
+
watcher?.close();
|
|
924
|
+
examplesWatcher?.close();
|
|
925
|
+
clearTimeout(timer);
|
|
926
|
+
for (const c of clients) c.end();
|
|
927
|
+
const closed = new Promise((r) => server.close(() => r()));
|
|
928
|
+
server.closeAllConnections();
|
|
929
|
+
await closed;
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
function isProjectFolder(project, p, bases) {
|
|
934
|
+
const full = join(project, p);
|
|
935
|
+
try {
|
|
936
|
+
return lstatSync(full).isDirectory() && insideProject(project, full, bases);
|
|
937
|
+
} catch {
|
|
938
|
+
return false;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
function isAsset(path) {
|
|
942
|
+
const ext = extname(path).toLowerCase();
|
|
943
|
+
return ext !== "" && ext !== ".html" && contentType(path) !== "application/octet-stream";
|
|
944
|
+
}
|
|
945
|
+
function projectPath(p) {
|
|
946
|
+
return p !== "" && !p.includes("\\") && !p.includes("\0") && p.split("/").every((s) => s !== "" && s !== "." && s !== "..");
|
|
947
|
+
}
|
|
948
|
+
function authorized(header, token) {
|
|
949
|
+
const m = /^Bearer (.+)$/.exec(header ?? "");
|
|
950
|
+
if (!m) return false;
|
|
951
|
+
const a = Buffer.from(m[1]);
|
|
952
|
+
const b = Buffer.from(token);
|
|
953
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
954
|
+
}
|
|
955
|
+
var escapeHtml = (s) => s.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
956
|
+
function errorPage(title, detail) {
|
|
957
|
+
return `<!doctype html>
|
|
958
|
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="dogsbay-dev-error" content=""><title>Error: ${escapeHtml(title)}</title>
|
|
959
|
+
<style>
|
|
960
|
+
:root{color-scheme:light dark;--bg:#fff;--fg:#1f2937;--muted:#6b7280;--accent:#b91c1c;--code:#f3f4f6}
|
|
961
|
+
@media (prefers-color-scheme:dark){:root{--bg:#111827;--fg:#f9fafb;--muted:#9ca3af;--accent:#f87171;--code:#1f2937}}
|
|
962
|
+
body{margin:0;background:var(--bg);color:var(--fg);font:16px/1.5 system-ui,sans-serif}
|
|
963
|
+
main{max-width:56rem;margin:0 auto;padding:2rem 1rem}
|
|
964
|
+
h1{font-size:1.5rem;color:var(--accent);margin:0 0 .5rem}
|
|
965
|
+
p{color:var(--muted);margin:0 0 1rem}
|
|
966
|
+
pre{background:var(--code);padding:1rem;border-radius:6px;overflow:auto;font:13px/1.5 ui-monospace,monospace;white-space:pre-wrap}
|
|
967
|
+
</style></head>
|
|
968
|
+
<body><main><h1>${escapeHtml(title)}</h1><p>The page reloads when the next change is applied.</p><pre>${escapeHtml(detail)}</pre></main></body></html>`;
|
|
969
|
+
}
|
|
970
|
+
function inject(html, generation) {
|
|
971
|
+
const tag = `<script>window.__DOGSBAY_DEV_GEN__="${generation}"</script><script src="${CLIENT}"></script>`;
|
|
972
|
+
const i = html.lastIndexOf("</body>");
|
|
973
|
+
return i < 0 ? html + tag : html.slice(0, i) + tag + html.slice(i);
|
|
974
|
+
}
|
|
975
|
+
export {
|
|
976
|
+
dev,
|
|
977
|
+
readyLine,
|
|
978
|
+
serverUrl
|
|
979
|
+
};
|