@unbase-app/layer 0.0.0-stage → 2.0.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/LICENSE +21 -0
- package/README.md +400 -2
- package/bin/unbase-layer.mjs +28 -0
- package/cli/artifact-style.mjs +89 -0
- package/cli/fetch.mjs +142 -0
- package/cli/lib.mjs +160 -0
- package/cli/live.mjs +35 -0
- package/cli/overview-artifact.mjs +142 -0
- package/cli/plan-artifact.mjs +337 -0
- package/cli/post.mjs +309 -0
- package/cli/redispatch.mjs +99 -0
- package/cli/screenshot.mjs +189 -0
- package/cli/status.mjs +50 -0
- package/cli/store.mjs +143 -0
- package/dist/client/start.d.ts +4 -0
- package/dist/client/start.js +12888 -0
- package/package.json +119 -4
- package/skills/build-comment/SKILL.md +61 -0
- package/skills/get-comments/SKILL.md +127 -0
- package/skills/process-comments/SKILL.md +115 -0
- package/skills/propose-comment/SKILL.md +79 -0
- package/src/astro/component-marker.mjs +161 -0
- package/src/astro/content-map-extra.mjs +192 -0
- package/src/astro/content-map.mjs +584 -0
- package/src/astro/deploy-context.mjs +66 -0
- package/src/astro/index.d.ts +97 -0
- package/src/astro/index.mjs +277 -0
- package/src/astro/pages.mjs +192 -0
- package/src/astro/version.mjs +13 -0
- package/src/edit/index.d.mts +178 -0
- package/src/edit/index.mjs +1002 -0
- package/src/page/index.d.mts +71 -0
- package/src/page/index.mjs +216 -0
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// CONTENT MAP — the content map for the Edit tool (0.11.0).
|
|
3
|
+
//
|
|
4
|
+
// A site that has its text as JSON in the repository (Astro content
|
|
5
|
+
// collections, e.g. src/content/verlangens/*.json) sets in the integration
|
|
6
|
+
//
|
|
7
|
+
// commentModus({ project: "…", edit: { content: "src/content" } })
|
|
8
|
+
//
|
|
9
|
+
// Then the integration makes /comment-modus/content-map.json: every string in
|
|
10
|
+
// those JSON files, with where it is ("file#/json/pointer"). The layer lays
|
|
11
|
+
// text on the page against it (src/lib/content.ts): a unique match is
|
|
12
|
+
// editable, the rest is not. The map is only fetched when an admin turns on
|
|
13
|
+
// the pencil or there are drafts; ordinary visitors never fetch it.
|
|
14
|
+
//
|
|
15
|
+
// - Dev: a middleware builds it again on every request (a few ms).
|
|
16
|
+
// - Build: in astro:build:done, next to the rest of the static output.
|
|
17
|
+
//
|
|
18
|
+
// Which fields may carry markup (`inline`: only em, strong, a[href]) is in the
|
|
19
|
+
// schema: a string with .meta({ "x-inline": true }). Astro writes a JSON
|
|
20
|
+
// Schema of every collection schema in .astro/collections/; the integration
|
|
21
|
+
// copies those to <content>/.schemas/ (only when they changed), so they are in
|
|
22
|
+
// the repository. publish (Unbase) reads them from main and validates every
|
|
23
|
+
// change against them. So commit that folder too.
|
|
24
|
+
//
|
|
25
|
+
// When (1.7.0): at the build, at the start of dev, and in dev every time Astro
|
|
26
|
+
// syncs its content again (a new or changed collection in content.config).
|
|
27
|
+
// Astro keeps its data store in .astro/ in dev, in cacheDir at the build: the
|
|
28
|
+
// integration reads it from where the running command keeps it. Whoever wants
|
|
29
|
+
// to know that the map is ready (Unbase's runner before it saves a turn) asks
|
|
30
|
+
// POST /comment-modus/schemas in dev: it waits until Astro has synced what
|
|
31
|
+
// changed in content.config, copies, and says which files it wrote (`stale`
|
|
32
|
+
// when Astro does not sync within 15 s).
|
|
33
|
+
//
|
|
34
|
+
// Lists and the block editor (B4) need more than the strings:
|
|
35
|
+
// lists per file every array: pointer → { n, h } (item count and the
|
|
36
|
+
// fingerprint publish checks again on main, list.ts)
|
|
37
|
+
// other per file the values that are not a string (number, boolean,
|
|
38
|
+
// null), so the layer can show an item in full
|
|
39
|
+
// schemas collection → JSON Schema, for forms and default values
|
|
40
|
+
// refs collection → [{ id, label }], the choices for an x-ref field
|
|
41
|
+
//
|
|
42
|
+
// Shared texts (0.12.1): a collection whose schema carries
|
|
43
|
+
// .meta({ "x-shared": "the whole site" }) at the top (LEGACY-NL: "x-gedeeld"
|
|
44
|
+
// before 1.11) is one file that is on many pages (a template's fixed texts,
|
|
45
|
+
// the menu and the footer).
|
|
46
|
+
// shared file → that description. The layer says it when editing and in
|
|
47
|
+
// the drawer.
|
|
48
|
+
// interface the shared files that also have .meta({ "x-interface": true }):
|
|
49
|
+
// the text of menu, header and footer. Only those match there.
|
|
50
|
+
// ============================================================================
|
|
51
|
+
|
|
52
|
+
import { readFile, readdir, mkdir, stat, writeFile } from "node:fs/promises";
|
|
53
|
+
import { watch } from "node:fs";
|
|
54
|
+
import { fileURLToPath } from "node:url";
|
|
55
|
+
import path from "node:path";
|
|
56
|
+
import { createHash } from "node:crypto";
|
|
57
|
+
import { extraOf, isSiteSettings, routeCollection } from "./content-map-extra.mjs";
|
|
58
|
+
|
|
59
|
+
export const CONTENT_MAP_PATH = "/comment-modus/content-map.json";
|
|
60
|
+
export const SCHEMA_DIR = ".schemas";
|
|
61
|
+
/** In <content>/.schemas: which file belongs to which collection, for the
|
|
62
|
+
* files where that is not the first folder (e.g. src/content/paginas/home.json
|
|
63
|
+
* in the collection home). publish reads it from main. */
|
|
64
|
+
export const COLLECTIONS_FILE = "collections.json";
|
|
65
|
+
/** LEGACY-NL: the same list under its name from before 1.11, still written beside COLLECTIONS_FILE
|
|
66
|
+
* in 2.0. Unbase's kit and publish read both and merge them (kit/pages/core.mjs), so a copy that
|
|
67
|
+
* stopped being written would go stale in a site's repository while it is still read: it goes
|
|
68
|
+
* together with that read, and the file is removed then. */
|
|
69
|
+
export const LEGACY_COLLECTIONS_FILE = "collecties.json";
|
|
70
|
+
/** Dev: wait until the schemas are ready, copy them, and say what changed. */
|
|
71
|
+
export const SCHEMAS_PATH = "/comment-modus/schemas";
|
|
72
|
+
/** This long Astro has to be quiet before its schemas count as ready. */
|
|
73
|
+
const QUIET_MS = 800;
|
|
74
|
+
/** SCHEMAS_PATH waits no longer than this: after that it copies what is there. */
|
|
75
|
+
const MAX_WAIT_MS = 15_000;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Which JSON file belongs to which collection, from Astro's data store
|
|
79
|
+
* (<cacheDir>/data-store.json, in devalue's form: a flat array, [0] is a Map
|
|
80
|
+
* of collection → Map of id → entry with filePath). File relative to the
|
|
81
|
+
* root → collection; empty when the store is not there (yet).
|
|
82
|
+
*/
|
|
83
|
+
export async function collectionsFromStore(storePath) {
|
|
84
|
+
/** @type {Record<string, string>} */
|
|
85
|
+
const out = {};
|
|
86
|
+
let arr;
|
|
87
|
+
try {
|
|
88
|
+
arr = JSON.parse(await readFile(storePath, "utf8"));
|
|
89
|
+
} catch {
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
const top = Array.isArray(arr) ? arr[0] : null;
|
|
93
|
+
if (!Array.isArray(top) || top[0] !== "Map") return out;
|
|
94
|
+
const at = (i) => (typeof i === "number" && i >= 0 ? arr[i] : undefined);
|
|
95
|
+
for (let i = 1; i + 1 < top.length; i += 2) {
|
|
96
|
+
const name = at(top[i]);
|
|
97
|
+
const items = at(top[i + 1]);
|
|
98
|
+
if (typeof name !== "string" || !Array.isArray(items) || items[0] !== "Map") continue;
|
|
99
|
+
for (let j = 2; j < items.length; j += 2) {
|
|
100
|
+
const e = at(items[j]);
|
|
101
|
+
const fp = e && typeof e === "object" && !Array.isArray(e) ? at(e.filePath) : undefined;
|
|
102
|
+
if (typeof fp === "string" && fp.endsWith(".json")) out[fp.split(path.sep).join("/")] = name;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** "~1" and "~0" as in a JSON Pointer (RFC 6901). */
|
|
109
|
+
const escapePart = (s) => String(s).replace(/~/g, "~0").replace(/\//g, "~1");
|
|
110
|
+
|
|
111
|
+
/** Every string in a JSON value: pointer → value. */
|
|
112
|
+
export function strings(value, pointer = "", out = {}) {
|
|
113
|
+
if (typeof value === "string") out[pointer] = value;
|
|
114
|
+
else if (Array.isArray(value)) value.forEach((v, i) => strings(v, `${pointer}/${i}`, out));
|
|
115
|
+
else if (value && typeof value === "object") {
|
|
116
|
+
for (const [k, v] of Object.entries(value)) {
|
|
117
|
+
if (k === "$schema") continue;
|
|
118
|
+
strings(v, `${pointer}/${escapePart(k)}`, out);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Every array (pointer → { n, h }) and every value that is not a string or a
|
|
125
|
+
* container (pointer → number, boolean or null). */
|
|
126
|
+
export function shape(value, pointer = "", lists = {}, other = {}) {
|
|
127
|
+
if (Array.isArray(value)) {
|
|
128
|
+
lists[pointer] = { n: value.length, h: fingerprint(value) };
|
|
129
|
+
value.forEach((v, i) => shape(v, `${pointer}/${i}`, lists, other));
|
|
130
|
+
} else if (value && typeof value === "object") {
|
|
131
|
+
for (const [k, v] of Object.entries(value)) {
|
|
132
|
+
if (k === "$schema") continue;
|
|
133
|
+
shape(v, `${pointer}/${escapePart(k)}`, lists, other);
|
|
134
|
+
}
|
|
135
|
+
} else if (typeof value !== "string" && pointer) other[pointer] = value ?? null;
|
|
136
|
+
return { lists, other };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** JSON with sorted keys; exactly like canonical() in src/lib/shared/list.ts,
|
|
140
|
+
* which Unbase's Edge Function publish uses too (npm test keeps them
|
|
141
|
+
* equal). */
|
|
142
|
+
export function canonical(w) {
|
|
143
|
+
if (w === null || typeof w !== "object") return JSON.stringify(w) ?? "null";
|
|
144
|
+
if (Array.isArray(w)) return `[${w.map(canonical).join(",")}]`;
|
|
145
|
+
return `{${Object.keys(w)
|
|
146
|
+
.filter((k) => w[k] !== undefined)
|
|
147
|
+
.sort()
|
|
148
|
+
.map((k) => `${JSON.stringify(k)}:${canonical(w[k])}`)
|
|
149
|
+
.join(",")}}`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** SHA-256 of canonical(w), the first 16 hex characters. */
|
|
153
|
+
export function fingerprint(w) {
|
|
154
|
+
return createHash("sha256").update(canonical(w)).digest("hex").slice(0, 16);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The fields an item's label is taken from: the site's names first (titel, naam, kop, functie). */
|
|
158
|
+
const LABEL_FIELDS = ["titel", "title", "naam", "name", "label", "kop", "functie"];
|
|
159
|
+
const bare = (t) => String(t).replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
|
|
160
|
+
|
|
161
|
+
/** The label of an item for a choice field: the field from x-ref-label, or
|
|
162
|
+
* the first of LABEL_FIELDS with text, or the id. */
|
|
163
|
+
function refLabel(data, id, field) {
|
|
164
|
+
for (const k of field ? [field, ...LABEL_FIELDS] : LABEL_FIELDS) {
|
|
165
|
+
if (data && typeof data[k] === "string" && bare(data[k])) return bare(data[k]).slice(0, 120);
|
|
166
|
+
}
|
|
167
|
+
return id;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The subschema at a pointer: follows properties, items, anyOf/oneOf and
|
|
171
|
+
* $ref. With a choice (anyOf) the first part that knows the key. */
|
|
172
|
+
export function subschema(schema, pointer, root = schema) {
|
|
173
|
+
let s = schema;
|
|
174
|
+
const parts = pointer.split("/").slice(1).map((p) => p.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
175
|
+
const resolve = (x) => {
|
|
176
|
+
for (let i = 0; i < 10 && x && x.$ref; i++) {
|
|
177
|
+
const refPath = String(x.$ref).replace(/^#\//, "").split("/");
|
|
178
|
+
x = refPath.reduce((o, k) => o?.[k], root);
|
|
179
|
+
}
|
|
180
|
+
return x;
|
|
181
|
+
};
|
|
182
|
+
for (const part of parts) {
|
|
183
|
+
s = resolve(s);
|
|
184
|
+
if (!s) return null;
|
|
185
|
+
const options = [s, ...(s.anyOf ?? []), ...(s.oneOf ?? []), ...(s.allOf ?? [])].map(resolve).flatMap((o) => [o, ...(o?.oneOf ?? []), ...(o?.anyOf ?? [])].map(resolve));
|
|
186
|
+
let next = null;
|
|
187
|
+
for (const o of options) {
|
|
188
|
+
if (!o) continue;
|
|
189
|
+
if (/^\d+$/.test(part) && (o.items || o.prefixItems)) {
|
|
190
|
+
next = o.prefixItems?.[Number(part)] ?? o.items;
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
if (o.properties && part in o.properties) {
|
|
194
|
+
next = o.properties[part];
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
if (o.additionalProperties && typeof o.additionalProperties === "object") next ??= o.additionalProperties;
|
|
198
|
+
}
|
|
199
|
+
s = next;
|
|
200
|
+
}
|
|
201
|
+
return resolve(s);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const isInlineSchema = (s) => Boolean(s && (s["x-inline"] === true || (s.anyOf ?? []).some((o) => o?.["x-inline"] === true)));
|
|
205
|
+
/** Without a schema: a value with <em>, <strong> or <a> is inline. */
|
|
206
|
+
const looksInline = (v) => /<(em|strong|a)(\s[^>]*)?>/i.test(v);
|
|
207
|
+
|
|
208
|
+
async function jsonFiles(dir) {
|
|
209
|
+
const out = [];
|
|
210
|
+
let items = [];
|
|
211
|
+
try {
|
|
212
|
+
items = await readdir(dir, { withFileTypes: true });
|
|
213
|
+
} catch {
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
for (const item of items) {
|
|
217
|
+
if (item.name.startsWith(".") || item.name === "node_modules") continue;
|
|
218
|
+
const full = path.join(dir, item.name);
|
|
219
|
+
if (item.isDirectory()) out.push(...(await jsonFiles(full)));
|
|
220
|
+
else if (item.isFile() && item.name.endsWith(".json")) out.push(full);
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {{ content?: string, schemas?: boolean }} options
|
|
227
|
+
*/
|
|
228
|
+
export function contentMap(options = {}) {
|
|
229
|
+
const content = (options.content ?? "src/content").replace(/^\.?\/+|\/+$/g, "");
|
|
230
|
+
/** @type {string} */
|
|
231
|
+
let root = process.cwd();
|
|
232
|
+
/** @type {URL | null} */
|
|
233
|
+
let dotAstro = null;
|
|
234
|
+
/** @type {URL | null} */
|
|
235
|
+
let cacheDir = null;
|
|
236
|
+
let dev = false;
|
|
237
|
+
|
|
238
|
+
const schemaDir = () => path.join(root, content, SCHEMA_DIR);
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Astro's data store, where the running command keeps it: in .astro/ in
|
|
242
|
+
* dev, in cacheDir at the build (astro/dist/content/paths.js). When it is
|
|
243
|
+
* not there, the other place (an older Astro).
|
|
244
|
+
*/
|
|
245
|
+
const storeFiles = () =>
|
|
246
|
+
[dev ? dotAstro : cacheDir, dev ? cacheDir : dotAstro].filter(Boolean).map((u) => fileURLToPath(new URL("data-store.json", u)));
|
|
247
|
+
|
|
248
|
+
/** File (relative to the root) → collection, for the files where that is
|
|
249
|
+
* not the first folder under content/: from Astro's data store, completed
|
|
250
|
+
* with what is in .schemas/collections.json already. */
|
|
251
|
+
async function collectionsByFile() {
|
|
252
|
+
const out = {};
|
|
253
|
+
for (const name of [LEGACY_COLLECTIONS_FILE, COLLECTIONS_FILE]) {
|
|
254
|
+
try {
|
|
255
|
+
Object.assign(out, JSON.parse(await readFile(path.join(schemaDir(), name), "utf8")));
|
|
256
|
+
} catch {}
|
|
257
|
+
}
|
|
258
|
+
let store = {};
|
|
259
|
+
for (const f of storeFiles()) {
|
|
260
|
+
store = await collectionsFromStore(f);
|
|
261
|
+
if (Object.keys(store).length) break;
|
|
262
|
+
}
|
|
263
|
+
for (const [b, c] of Object.entries(store)) {
|
|
264
|
+
if (!b.startsWith(`${content}/`)) continue;
|
|
265
|
+
if (firstDir(b) === c) delete out[b];
|
|
266
|
+
else out[b] = c;
|
|
267
|
+
}
|
|
268
|
+
// A file that is gone no longer belongs anywhere.
|
|
269
|
+
for (const b of Object.keys(out)) if (!(await stat(path.join(root, b)).catch(() => null))) delete out[b];
|
|
270
|
+
return out;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** "src/content/verlangens/x.json" → "verlangens"; "src/content/x.json" → "x". */
|
|
274
|
+
const firstDir = (rel) => path.posix.relative(content, rel).split("/")[0].replace(/\.json$/, "");
|
|
275
|
+
|
|
276
|
+
/** The collection of a file (relative to the root). */
|
|
277
|
+
const collectionOf = (rel, byFile) => byFile[rel] ?? firstDir(rel);
|
|
278
|
+
|
|
279
|
+
/** collection → JSON Schema, from the copied schemas, or else from
|
|
280
|
+
* .astro/collections. Which file belongs to which collection: collectionsByFile(). */
|
|
281
|
+
async function schemas() {
|
|
282
|
+
const out = {};
|
|
283
|
+
for (const source of [dotAstro ? fileURLToPath(new URL("collections/", dotAstro)) : null, schemaDir()]) {
|
|
284
|
+
if (!source) continue;
|
|
285
|
+
let names = [];
|
|
286
|
+
try {
|
|
287
|
+
names = await readdir(source);
|
|
288
|
+
} catch {
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
for (const n of names.filter((n) => n.endsWith(".schema.json"))) {
|
|
292
|
+
try {
|
|
293
|
+
out[n.replace(/\.schema\.json$/, "")] ??= JSON.parse(await readFile(path.join(source, n), "utf8"));
|
|
294
|
+
} catch {
|
|
295
|
+
/* half written or invalid: skip */
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async function make() {
|
|
303
|
+
const base = path.join(root, content);
|
|
304
|
+
const files = {};
|
|
305
|
+
const inline = {};
|
|
306
|
+
const lists = {};
|
|
307
|
+
const other = {};
|
|
308
|
+
/** @type {Record<string, { id: string, label: string }[]>} */
|
|
309
|
+
const refs = {};
|
|
310
|
+
/** @type {Record<string, Record<string, unknown>>} */
|
|
311
|
+
const extra = {};
|
|
312
|
+
/** @type {Record<string, string>} */
|
|
313
|
+
const shared = {};
|
|
314
|
+
/** @type {string[]} */
|
|
315
|
+
const iface = [];
|
|
316
|
+
const schema = await schemas();
|
|
317
|
+
const byFile = await collectionsByFile();
|
|
318
|
+
for (const full of (await jsonFiles(base)).sort()) {
|
|
319
|
+
const rel = path.relative(root, full).split(path.sep).join("/");
|
|
320
|
+
let data;
|
|
321
|
+
try {
|
|
322
|
+
data = JSON.parse(await readFile(full, "utf8"));
|
|
323
|
+
} catch {
|
|
324
|
+
continue; // invalid JSON: Astro reports that itself
|
|
325
|
+
}
|
|
326
|
+
const parts = path.relative(base, full).split(path.sep);
|
|
327
|
+
const collection = parts[0].replace(/\.json$/, "");
|
|
328
|
+
// The ids for x-ref (README → x-ref): a file in a folder is one item
|
|
329
|
+
// (id = file name); one file per collection has them as an array with
|
|
330
|
+
// id, or as an object.
|
|
331
|
+
if (parts.length === 2) (refs[collection] ??= []).push({ id: parts[1].replace(/\.json$/, ""), label: refLabel(data, parts[1].replace(/\.json$/, "")) });
|
|
332
|
+
else if (parts.length === 1 && Array.isArray(data)) refs[collection] = data.filter((x) => typeof x?.id === "string").map((x) => ({ id: x.id, label: refLabel(x, x.id) }));
|
|
333
|
+
else if (parts.length === 1 && data && typeof data === "object") refs[collection] = Object.keys(data).filter((k) => k !== "$schema").map((k) => ({ id: k, label: refLabel(data[k], k) }));
|
|
334
|
+
const v = shape(data);
|
|
335
|
+
if (Object.keys(v.lists).length) lists[rel] = v.lists;
|
|
336
|
+
if (Object.keys(v.other).length) other[rel] = v.other;
|
|
337
|
+
const s = schema[collectionOf(rel, byFile)];
|
|
338
|
+
// The site settings are no page text (content-map-extra.mjs): only in `site`.
|
|
339
|
+
if (isSiteSettings(s)) {
|
|
340
|
+
const x = extraOf(data, s, subschema);
|
|
341
|
+
if (Object.keys(x.site).length) (extra.site ??= {})[rel] = x.site;
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
const fields = strings(data);
|
|
345
|
+
if (!Object.keys(fields).length) continue;
|
|
346
|
+
files[rel] = fields;
|
|
347
|
+
const inlineKeys = Object.entries(fields)
|
|
348
|
+
.filter(([p, w]) => (s ? isInlineSchema(subschema(s, p)) : false) || looksInline(w))
|
|
349
|
+
.map(([p]) => p);
|
|
350
|
+
if (inlineKeys.length) inline[rel] = inlineKeys;
|
|
351
|
+
// LEGACY-NL: "x-gedeeld" is the name from before 1.11 (agendavoorhethart's content.config).
|
|
352
|
+
const sharedOn = s?.["x-shared"] ?? s?.["x-gedeeld"];
|
|
353
|
+
if (typeof sharedOn === "string" && sharedOn.trim()) {
|
|
354
|
+
shared[rel] = sharedOn.trim();
|
|
355
|
+
if (s["x-interface"] === true) iface.push(rel);
|
|
356
|
+
}
|
|
357
|
+
// Page settings and images (B2, B3): content-map-extra.mjs.
|
|
358
|
+
const x = extraOf(data, s ?? null, subschema);
|
|
359
|
+
for (const [k, w] of Object.entries(x)) if (Object.keys(w).length) (extra[k] ??= {})[rel] = w;
|
|
360
|
+
}
|
|
361
|
+
// Only the schemas of collections with content, and per collection at
|
|
362
|
+
// most once (the layer only fetches the map for an admin with the pencil).
|
|
363
|
+
const used = {};
|
|
364
|
+
/** Only the files with another collection than their first folder. */
|
|
365
|
+
const elsewhere = {};
|
|
366
|
+
for (const rel of new Set([...Object.keys(files), ...Object.keys(lists)])) {
|
|
367
|
+
const c = collectionOf(rel, byFile);
|
|
368
|
+
if (schema[c]) used[c] = schema[c];
|
|
369
|
+
if (c !== firstDir(rel)) elsewhere[rel] = c;
|
|
370
|
+
}
|
|
371
|
+
// The labels are the site's own text: sorted as Dutch.
|
|
372
|
+
for (const r of Object.values(refs)) r.sort((a, b) => a.label.localeCompare(b.label, "nl"));
|
|
373
|
+
const routes = routeCollection(schema, content, subschema);
|
|
374
|
+
return {
|
|
375
|
+
v: 1, content, files, inline, lists, other, schemas: used, refs,
|
|
376
|
+
...(Object.keys(elsewhere).length ? { collections: elsewhere } : {}),
|
|
377
|
+
...(Object.keys(shared).length ? { shared } : {}),
|
|
378
|
+
...(iface.length ? { interface: iface } : {}),
|
|
379
|
+
...extra, ...(routes ? { routes } : {}),
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** The schemas from .astro/collections to <content>/.schemas, only when
|
|
384
|
+
* they changed (otherwise the watcher sees a change every time). */
|
|
385
|
+
async function copySchemas(logger) {
|
|
386
|
+
/** @type {string[]} The files it wrote, relative to the root. */
|
|
387
|
+
const written = [];
|
|
388
|
+
if (options.schemas === false || !dotAstro) return written;
|
|
389
|
+
const source = fileURLToPath(new URL("collections/", dotAstro));
|
|
390
|
+
let names = [];
|
|
391
|
+
try {
|
|
392
|
+
names = (await readdir(source)).filter((n) => n.endsWith(".schema.json"));
|
|
393
|
+
} catch {
|
|
394
|
+
return written;
|
|
395
|
+
}
|
|
396
|
+
// Only collections with files under content/: a folder with their name,
|
|
397
|
+
// or files that belong to them by Astro's data store (collectionsByFile()).
|
|
398
|
+
const dirs = new Set((await readdir(path.join(root, content), { withFileTypes: true }).catch(() => [])).filter((d) => d.isDirectory()).map((d) => d.name));
|
|
399
|
+
const byFile = await collectionsByFile();
|
|
400
|
+
for (const c of Object.values(byFile)) dirs.add(c);
|
|
401
|
+
const rel = (full) => path.relative(root, full).split(path.sep).join("/");
|
|
402
|
+
// Which file belongs to which collection, where that is not the folder.
|
|
403
|
+
const listText = JSON.stringify(Object.fromEntries(Object.entries(byFile).sort(([a], [b]) => a.localeCompare(b))), null, 2) + "\n";
|
|
404
|
+
for (const name of [COLLECTIONS_FILE, LEGACY_COLLECTIONS_FILE]) {
|
|
405
|
+
const listPath = path.join(schemaDir(), name);
|
|
406
|
+
const oldList = await readFile(listPath, "utf8").catch(() => null);
|
|
407
|
+
if (Object.keys(byFile).length && oldList !== listText) {
|
|
408
|
+
await mkdir(schemaDir(), { recursive: true });
|
|
409
|
+
await writeFile(listPath, listText);
|
|
410
|
+
written.push(rel(listPath));
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
for (const n of names) {
|
|
414
|
+
if (!dirs.has(n.replace(/\.schema\.json$/, ""))) continue;
|
|
415
|
+
const fresh = await readFile(path.join(source, n), "utf8");
|
|
416
|
+
const target = path.join(schemaDir(), n);
|
|
417
|
+
const old = await readFile(target, "utf8").catch(() => null);
|
|
418
|
+
if (old === fresh) continue;
|
|
419
|
+
await mkdir(schemaDir(), { recursive: true });
|
|
420
|
+
await writeFile(target, fresh);
|
|
421
|
+
written.push(rel(target));
|
|
422
|
+
}
|
|
423
|
+
if (written.length) logger?.info(`${written.length} schema(s) updated in ${content}/${SCHEMA_DIR}/ (commit them too)`);
|
|
424
|
+
return written;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const mtime = async (f) => (await stat(f).catch(() => null))?.mtimeMs ?? 0;
|
|
428
|
+
|
|
429
|
+
/** When Astro's store or one of its schemas last changed (ms), 0 without. */
|
|
430
|
+
async function lastWritten() {
|
|
431
|
+
let t = await mtime(storeFiles()[0]);
|
|
432
|
+
if (!dotAstro) return t;
|
|
433
|
+
const dir = fileURLToPath(new URL("collections/", dotAstro));
|
|
434
|
+
for (const n of await readdir(dir).catch(() => [])) t = Math.max(t, await mtime(path.join(dir, n)));
|
|
435
|
+
return t;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** When the content config last changed (ms), 0 without. */
|
|
439
|
+
async function configChanged() {
|
|
440
|
+
let t = 0;
|
|
441
|
+
for (const f of ["src/content.config.ts", "src/content.config.mts", "src/content.config.js", "src/content.config.mjs", "src/content/config.ts", "src/content/config.mjs"]) {
|
|
442
|
+
t = Math.max(t, await mtime(path.join(root, f)));
|
|
443
|
+
}
|
|
444
|
+
return t;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** Dev: Astro's refreshContent (astro:server:setup), to make it sync its content once more. */
|
|
448
|
+
/** @type {null | (() => Promise<void>)} */
|
|
449
|
+
let refresh = null;
|
|
450
|
+
/** Up to which change to the content config that second sync already ran (ms). */
|
|
451
|
+
let refreshedFor = 0;
|
|
452
|
+
/** One at a time: otherwise the watcher and SCHEMAS_PATH get in each other's way. */
|
|
453
|
+
let queue = Promise.resolve();
|
|
454
|
+
|
|
455
|
+
const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
456
|
+
|
|
457
|
+
/** Until `ready()` (asked every 100 ms) or `until` (ms since the start `t0`): whether it worked. */
|
|
458
|
+
async function waitFor(ready, t0, until) {
|
|
459
|
+
for (;;) {
|
|
460
|
+
if (await ready()) return true;
|
|
461
|
+
if (Date.now() - t0 >= until) return false;
|
|
462
|
+
await sleep(100);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** Astro wrote its store after `t` and nothing for QUIET_MS after that. */
|
|
467
|
+
const writtenAfter = (t) => async () => {
|
|
468
|
+
const store = await mtime(storeFiles()[0]);
|
|
469
|
+
return store >= t && Date.now() - (await lastWritten()) >= QUIET_MS;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Dev: the schemas as soon as Astro has synced what changed in the content
|
|
474
|
+
* config, then copy. Astro 7 syncs a new collection at the first sync after
|
|
475
|
+
* the change into its schemas but not into its store (the page cannot read
|
|
476
|
+
* it then either); so after such a change one more sync (refreshContent),
|
|
477
|
+
* and only then copy. `stale` when Astro does not sync within MAX_WAIT_MS
|
|
478
|
+
* (an error in the config): then what was there stays.
|
|
479
|
+
*/
|
|
480
|
+
function update(logger) {
|
|
481
|
+
const job = queue.then(async () => {
|
|
482
|
+
const t0 = Date.now();
|
|
483
|
+
const config = await configChanged();
|
|
484
|
+
const store = await mtime(storeFiles()[0]);
|
|
485
|
+
// No store: no content layer, nothing to wait for.
|
|
486
|
+
let stale = !!store && !(await waitFor(writtenAfter(config), t0, MAX_WAIT_MS));
|
|
487
|
+
if (!stale && store && refresh && config > refreshedFor) {
|
|
488
|
+
refreshedFor = config;
|
|
489
|
+
const t1 = Date.now();
|
|
490
|
+
await refresh();
|
|
491
|
+
// When the sync wrote nothing (nothing changed), go on after Astro's debounce.
|
|
492
|
+
await waitFor(async () => (await writtenAfter(t1)()) || Date.now() - t1 >= 1500 + QUIET_MS, t0, MAX_WAIT_MS);
|
|
493
|
+
}
|
|
494
|
+
return { written: await copySchemas(logger), waited: Date.now() - t0, stale };
|
|
495
|
+
});
|
|
496
|
+
queue = job.then(() => {}, () => {});
|
|
497
|
+
return job;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return {
|
|
501
|
+
path: CONTENT_MAP_PATH,
|
|
502
|
+
/** astro:config:setup; `isDev` with `astro dev`, where Astro keeps its store in .astro/. */
|
|
503
|
+
setRoot(r, dot, cache, isDev = false) {
|
|
504
|
+
root = fileURLToPath(r);
|
|
505
|
+
dotAstro = dot ?? new URL(".astro/", r);
|
|
506
|
+
cacheDir = cache ?? new URL("node_modules/.astro/", r);
|
|
507
|
+
dev = isDev;
|
|
508
|
+
},
|
|
509
|
+
/** astro:server:setup: Astro's refreshContent. */
|
|
510
|
+
setRefresh(f) {
|
|
511
|
+
refresh = typeof f === "function" ? () => f() : null;
|
|
512
|
+
},
|
|
513
|
+
make,
|
|
514
|
+
copySchemas,
|
|
515
|
+
update,
|
|
516
|
+
vitePlugin() {
|
|
517
|
+
return {
|
|
518
|
+
name: "comment-modus:content-map",
|
|
519
|
+
apply: "serve",
|
|
520
|
+
configureServer(server) {
|
|
521
|
+
const logger = { info: (m) => server.config.logger.info(`[comment-modus] ${m}`, { timestamp: true }) };
|
|
522
|
+
// Astro syncs its content again when content.config or a collection
|
|
523
|
+
// changes, and then writes .astro/: shortly after, the schemas too
|
|
524
|
+
// (update, with the second sync after a new collection).
|
|
525
|
+
let timer = null;
|
|
526
|
+
const later = () => {
|
|
527
|
+
clearTimeout(timer);
|
|
528
|
+
timer = setTimeout(() => void update(logger).catch(() => {}), QUIET_MS);
|
|
529
|
+
};
|
|
530
|
+
const watchers = [];
|
|
531
|
+
if (options.schemas !== false && dotAstro) {
|
|
532
|
+
for (const dir of [fileURLToPath(dotAstro), fileURLToPath(new URL("collections/", dotAstro))]) {
|
|
533
|
+
try {
|
|
534
|
+
watchers.push(watch(dir, (_e, name) => (!name || /data-store|\.schema\.json$/.test(String(name))) && later()));
|
|
535
|
+
} catch {
|
|
536
|
+
/* the folder is not there (yet): the start and SCHEMAS_PATH cover it */
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
server.httpServer?.once("close", () => {
|
|
541
|
+
clearTimeout(timer);
|
|
542
|
+
for (const w of watchers) w.close();
|
|
543
|
+
});
|
|
544
|
+
server.middlewares.use(SCHEMAS_PATH, async (req, res) => {
|
|
545
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
546
|
+
res.setHeader("Cache-Control", "no-store");
|
|
547
|
+
if (req.method !== "POST") {
|
|
548
|
+
res.statusCode = 405;
|
|
549
|
+
return res.end(JSON.stringify({ error: "POST" }));
|
|
550
|
+
}
|
|
551
|
+
try {
|
|
552
|
+
const { written, waited, stale } = await update(logger);
|
|
553
|
+
res.end(JSON.stringify({ written, waited, ...(stale ? { stale } : {}) }));
|
|
554
|
+
} catch (e) {
|
|
555
|
+
res.statusCode = 500;
|
|
556
|
+
res.end(JSON.stringify({ error: String(e?.message ?? e) }));
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
server.middlewares.use(CONTENT_MAP_PATH, async (_req, res) => {
|
|
560
|
+
try {
|
|
561
|
+
const map = await make();
|
|
562
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
563
|
+
res.setHeader("Cache-Control", "no-store");
|
|
564
|
+
res.end(JSON.stringify(map));
|
|
565
|
+
} catch (e) {
|
|
566
|
+
res.statusCode = 500;
|
|
567
|
+
res.end(JSON.stringify({ error: String(e?.message ?? e) }));
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
},
|
|
571
|
+
};
|
|
572
|
+
},
|
|
573
|
+
/** astro:build:done */
|
|
574
|
+
async write({ dir, logger }) {
|
|
575
|
+
await copySchemas(logger);
|
|
576
|
+
const map = await make();
|
|
577
|
+
const target = new URL(`.${CONTENT_MAP_PATH}`, dir);
|
|
578
|
+
await mkdir(new URL(".", target), { recursive: true });
|
|
579
|
+
await writeFile(target, JSON.stringify(map));
|
|
580
|
+
const n = Object.values(map.files).reduce((t, v) => t + Object.keys(v).length, 0);
|
|
581
|
+
logger.info(`${n} texts from ${Object.keys(map.files).length} files in ${CONTENT_MAP_PATH}`);
|
|
582
|
+
},
|
|
583
|
+
};
|
|
584
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// DEPLOY CONTEXT — which build this is: production, preview or development.
|
|
3
|
+
//
|
|
4
|
+
// The same rule as Unbase's draft-pages kit, so that "kept out of production"
|
|
5
|
+
// means the same build for a draft page and for a marker: `deployContext` in
|
|
6
|
+
// @unbase-app/pages (unbase kit/pages/core.mjs; docs/plan-pages.md, "The
|
|
7
|
+
// deployment context, explicit"). Copied, not imported: the kit is not a
|
|
8
|
+
// dependency of this package. Change both or neither.
|
|
9
|
+
//
|
|
10
|
+
// 1. UNBASE_DEPLOY_CONTEXT (production | preview | development) when set;
|
|
11
|
+
// an unknown value counts as production.
|
|
12
|
+
// 2. A dev server is development. Unbase's sandbox runs the site's dev
|
|
13
|
+
// server, so the sandbox is development too.
|
|
14
|
+
// 3. The host: Vercel's VERCEL_ENV; Netlify's CONTEXT, only with NETLIFY=true.
|
|
15
|
+
// On a staged project (Unbase ADR-0010) Vercel builds the live branch as
|
|
16
|
+
// production and Main and the working versions as previews.
|
|
17
|
+
// 4. Otherwise production: a local `astro build`, or a host it does not know.
|
|
18
|
+
//
|
|
19
|
+
// NODE_ENV is never read: previews are production-optimised builds too.
|
|
20
|
+
// ============================================================================
|
|
21
|
+
|
|
22
|
+
const CONTEXTS = new Set(["production", "preview", "development"]);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {{ env?: Record<string, string | undefined>, command?: "build" | "dev" }} [input]
|
|
26
|
+
* @returns {{ context: "production" | "preview" | "development", source: string }}
|
|
27
|
+
*/
|
|
28
|
+
export function deployContext({ env = {}, command = "build" } = {}) {
|
|
29
|
+
const own = env.UNBASE_DEPLOY_CONTEXT;
|
|
30
|
+
if (own) return CONTEXTS.has(own) ? { context: /** @type {any} */ (own), source: "UNBASE_DEPLOY_CONTEXT" } : { context: "production", source: `UNBASE_DEPLOY_CONTEXT=${own} (unknown)` };
|
|
31
|
+
if (command === "dev") return { context: "development", source: "dev server" };
|
|
32
|
+
const vercel = env.VERCEL_ENV;
|
|
33
|
+
if (vercel) return { context: vercel === "preview" ? "preview" : vercel === "development" ? "development" : "production", source: `VERCEL_ENV=${vercel}` };
|
|
34
|
+
if (env.NETLIFY === "true" && env.CONTEXT) {
|
|
35
|
+
const c = env.CONTEXT;
|
|
36
|
+
return { context: c === "deploy-preview" || c === "branch-deploy" ? "preview" : c === "dev" ? "development" : "production", source: `CONTEXT=${c}` };
|
|
37
|
+
}
|
|
38
|
+
return { context: "production", source: "no deployment context (counts as production)" };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** When the component marker runs: "always" (every build) or "preview" (every build but production). */
|
|
42
|
+
export const MARKER_WHEN = /** @type {const} */ (["always", "preview"]);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The marker settings from the integration option, or null when the marker is off.
|
|
46
|
+
* `componentMarker`: false | "always" | "preview" | { dir, skip, when }. Absent: "always".
|
|
47
|
+
* @param {unknown} option
|
|
48
|
+
* @returns {{ when: "always" | "preview", dir?: string, skip?: string[] } | null}
|
|
49
|
+
*/
|
|
50
|
+
export function markerSettings(option) {
|
|
51
|
+
if (option === false) return null;
|
|
52
|
+
if (option === undefined || option === null || option === true) return { when: "always" };
|
|
53
|
+
const settings = typeof option === "string" ? { when: option } : typeof option === "object" && !Array.isArray(option) ? { ...option } : null;
|
|
54
|
+
if (!settings) throw new Error(`@unbase-app/layer: componentMarker is false, "always", "preview" or { dir, skip, when }, not ${JSON.stringify(option)}`);
|
|
55
|
+
const when = /** @type {any} */ (settings).when ?? "always";
|
|
56
|
+
if (!MARKER_WHEN.includes(when)) throw new Error(`@unbase-app/layer: componentMarker's when is "always" or "preview", not ${JSON.stringify(when)}`);
|
|
57
|
+
return { ...settings, when };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether this build gets data-c markers: with "always" every build, with "preview" every build
|
|
62
|
+
* that is not production by `deployContext`.
|
|
63
|
+
* @param {"always" | "preview"} when
|
|
64
|
+
* @param {{ context: string }} ctx
|
|
65
|
+
*/
|
|
66
|
+
export const markersInBuild = (when, ctx) => when === "always" || ctx.context !== "production";
|