@lariocpt/crust-site 0.0.0-stage → 0.4.2
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 +99 -2
- package/docs/USAGE.md +1128 -0
- package/package.json +33 -4
- package/src/assets.ts +875 -0
- package/src/build-lock.ts +214 -0
- package/src/build.ts +755 -0
- package/src/cli.ts +269 -0
- package/src/config.ts +538 -0
- package/src/content.ts +267 -0
- package/src/dev.ts +821 -0
- package/src/diagnostics.ts +382 -0
- package/src/emit.ts +260 -0
- package/src/fence.ts +593 -0
- package/src/frontmatter.ts +240 -0
- package/src/fs-path.ts +54 -0
- package/src/html.ts +400 -0
- package/src/index.ts +128 -0
- package/src/markdown.ts +444 -0
- package/src/pages.ts +169 -0
- package/src/publication.ts +245 -0
- package/src/render.ts +940 -0
- package/src/routes.ts +363 -0
- package/src/types.ts +250 -0
- package/src/url.ts +273 -0
- package/src/validate.ts +367 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
// The config contract: `defineSite` validates EVERYTHING at definition time
|
|
2
|
+
// (IDE-time for TS users, runtime for everyone) and collects ALL errors into
|
|
3
|
+
// one BuildError — never a throw on first, so a config with five problems
|
|
4
|
+
// reports five problems. Unknown keys at any level are errors, not ignored
|
|
5
|
+
// extras: a typo'd key that silently does nothing is a false pass.
|
|
6
|
+
|
|
7
|
+
import { BuildError, type Diagnostic } from "./diagnostics";
|
|
8
|
+
import { isWindowsPath, pathContains, filesystemPath as posix } from "./fs-path";
|
|
9
|
+
import type {
|
|
10
|
+
ContentConfig,
|
|
11
|
+
DevConfig,
|
|
12
|
+
LayoutFn,
|
|
13
|
+
ResolvedSite,
|
|
14
|
+
RobotsConfig,
|
|
15
|
+
Site,
|
|
16
|
+
SiteConfig,
|
|
17
|
+
} from "./types";
|
|
18
|
+
|
|
19
|
+
type Obj = Record<string, unknown>;
|
|
20
|
+
|
|
21
|
+
const ROOT_KEYS = [
|
|
22
|
+
"site",
|
|
23
|
+
"content",
|
|
24
|
+
"layouts",
|
|
25
|
+
"assets",
|
|
26
|
+
"output",
|
|
27
|
+
"sitemap",
|
|
28
|
+
"robots",
|
|
29
|
+
"dev",
|
|
30
|
+
"lang",
|
|
31
|
+
];
|
|
32
|
+
const CONTENT_KEYS = ["directory", "include", "exclude", "defaultLayout", "route", "parseData"];
|
|
33
|
+
const ASSETS_KEYS = ["scripts", "styles", "publicDirectory"];
|
|
34
|
+
const OUTPUT_KEYS = ["directory", "trailingSlash"];
|
|
35
|
+
const ROBOTS_KEYS = ["indexable"];
|
|
36
|
+
const DEV_KEYS = ["hostname", "port"];
|
|
37
|
+
|
|
38
|
+
const ASSET_NAME = /^[A-Za-z0-9_-]+$/;
|
|
39
|
+
|
|
40
|
+
function asObject(value: unknown): Obj | undefined {
|
|
41
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
|
|
42
|
+
return value as Obj;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function dotted(prefix: string, key: string): string {
|
|
46
|
+
return prefix === "" ? key : `${prefix}.${key}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function unknownKey(errors: Diagnostic[], path: string): void {
|
|
50
|
+
errors.push({
|
|
51
|
+
code: "config.unknown-key",
|
|
52
|
+
severity: "error",
|
|
53
|
+
message: `unknown config key "${path}"`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function typeError(errors: Diagnostic[], path: string, expected: string): void {
|
|
58
|
+
errors.push({
|
|
59
|
+
code: "config.type",
|
|
60
|
+
severity: "error",
|
|
61
|
+
message: `config key "${path}" must be ${expected}`,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function checkKeys(
|
|
66
|
+
obj: Obj,
|
|
67
|
+
allowed: readonly string[],
|
|
68
|
+
prefix: string,
|
|
69
|
+
errors: Diagnostic[],
|
|
70
|
+
): void {
|
|
71
|
+
for (const key of Object.keys(obj)) {
|
|
72
|
+
if (!allowed.includes(key)) unknownKey(errors, dotted(prefix, key));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function stringArray(value: unknown, path: string, errors: Diagnostic[]): string[] | undefined {
|
|
77
|
+
if (!Array.isArray(value) || value.some((v) => typeof v !== "string")) {
|
|
78
|
+
typeError(errors, path, "an array of strings");
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
return value as string[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Absolute http(s) URL, optional path prefix, no trailing slash, no
|
|
86
|
+
* query/fragment — and ALREADY in canonical form (see the strict check
|
|
87
|
+
* below). `canonicalUrl` is plain string concatenation of this value, so
|
|
88
|
+
* anything the WHATWG parser would silently normalize (padding, empty
|
|
89
|
+
* `?`/`#`, default ports, upper-case host) is a config error, never a
|
|
90
|
+
* silent fix.
|
|
91
|
+
*/
|
|
92
|
+
function validateSiteUrl(value: unknown, errors: Diagnostic[]): string {
|
|
93
|
+
const bad = (why: string): string => {
|
|
94
|
+
errors.push({
|
|
95
|
+
code: "config.site",
|
|
96
|
+
severity: "error",
|
|
97
|
+
message: `config key "site" must be an absolute http(s) URL${why === "" ? "" : ` ${why}`}, got ${JSON.stringify(value)}`,
|
|
98
|
+
});
|
|
99
|
+
return "";
|
|
100
|
+
};
|
|
101
|
+
if (typeof value !== "string" || value === "") return bad("");
|
|
102
|
+
// Literal `?`/`#` anywhere: an EMPTY query/fragment (`https://x.dev?`)
|
|
103
|
+
// parses with url.search === "" and would sail through the URL-object
|
|
104
|
+
// checks while the raw `?` is still in the string — and concatenation
|
|
105
|
+
// would then turn every later segment into query text.
|
|
106
|
+
if (value.includes("?") || value.includes("#"))
|
|
107
|
+
return bad('that must not contain a query or fragment ("?" and "#" are not allowed at all)');
|
|
108
|
+
let url: URL;
|
|
109
|
+
try {
|
|
110
|
+
url = new URL(value);
|
|
111
|
+
} catch {
|
|
112
|
+
return bad("");
|
|
113
|
+
}
|
|
114
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
115
|
+
return bad("with an http or https scheme");
|
|
116
|
+
// A public site URL never carries credentials. Beyond being a config
|
|
117
|
+
// mistake, `https://attacker@victim` is the userinfo-spoofing shape, and
|
|
118
|
+
// canonicalUrl is plain concatenation — the userinfo would be stamped into
|
|
119
|
+
// every canonical/og URL emitted.
|
|
120
|
+
if (url.username !== "" || url.password !== "")
|
|
121
|
+
return bad("that must not include a user name or password");
|
|
122
|
+
if (value.endsWith("/"))
|
|
123
|
+
return bad('that does not end in "/" (the trailing slash is an error, not stripped)');
|
|
124
|
+
// Strict canonical form: the value must be EXACTLY what the URL serializer
|
|
125
|
+
// produces (or, for a root path, that minus the trailing "/"). The parser
|
|
126
|
+
// silently normalizes several things we must not accept — measured:
|
|
127
|
+
// leading/trailing padding is trimmed, a dangling ":" or empty "?"/"#" is
|
|
128
|
+
// dropped, internal tab/newline is removed, the default port is dropped,
|
|
129
|
+
// and the scheme/host are lower-cased (IDN host becomes punycode). Each of
|
|
130
|
+
// those would be stamped verbatim into every canonical/og URL emitted.
|
|
131
|
+
const canonical = url.href;
|
|
132
|
+
if (value !== canonical && value !== canonical.slice(0, -1)) {
|
|
133
|
+
return bad(
|
|
134
|
+
`already in canonical form — new URL(${JSON.stringify(value)}) serializes as ${JSON.stringify(canonical)}`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function validateAssetMap(
|
|
141
|
+
value: unknown,
|
|
142
|
+
path: string,
|
|
143
|
+
errors: Diagnostic[],
|
|
144
|
+
): Record<string, string> {
|
|
145
|
+
if (value === undefined) return {};
|
|
146
|
+
const obj = asObject(value);
|
|
147
|
+
if (!obj) {
|
|
148
|
+
typeError(errors, path, "an object mapping asset names to entrypoint paths");
|
|
149
|
+
return {};
|
|
150
|
+
}
|
|
151
|
+
const pairs: [string, string][] = [];
|
|
152
|
+
for (const [name, entry] of Object.entries(obj)) {
|
|
153
|
+
if (!ASSET_NAME.test(name)) {
|
|
154
|
+
errors.push({
|
|
155
|
+
code: "config.asset-name",
|
|
156
|
+
severity: "error",
|
|
157
|
+
message: `asset name "${name}" (config key "${path}.${name}") must match ${ASSET_NAME}`,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if (typeof entry !== "string") typeError(errors, `${path}.${name}`, "a path string");
|
|
161
|
+
pairs.push([name, entry as string]);
|
|
162
|
+
}
|
|
163
|
+
// Object.fromEntries stores own DATA properties: a `__proto__` key (own,
|
|
164
|
+
// e.g. from JSON.parse) is stored as an entry, never invoked as the
|
|
165
|
+
// prototype setter — a plain `out[name] = entry` would clobber the map's
|
|
166
|
+
// own prototype and leak lookups through the user's object.
|
|
167
|
+
return Object.fromEntries(pairs);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Validate a site config and return the branded, opaque `Site`. Paths stay
|
|
172
|
+
* RAW (relative to the config file); resolve them with `resolveConfigPaths`.
|
|
173
|
+
* Throws `BuildError` with every problem found, sorted.
|
|
174
|
+
*/
|
|
175
|
+
export function defineSite(input: SiteConfig): Site {
|
|
176
|
+
const errors: Diagnostic[] = [];
|
|
177
|
+
const root = asObject(input);
|
|
178
|
+
if (!root) {
|
|
179
|
+
errors.push({
|
|
180
|
+
code: "config.type",
|
|
181
|
+
severity: "error",
|
|
182
|
+
message: "the site config must be an object",
|
|
183
|
+
});
|
|
184
|
+
throw new BuildError(errors);
|
|
185
|
+
}
|
|
186
|
+
checkKeys(root, ROOT_KEYS, "", errors);
|
|
187
|
+
|
|
188
|
+
const siteUrl = validateSiteUrl(root.site, errors);
|
|
189
|
+
|
|
190
|
+
// content
|
|
191
|
+
interface ContentDraft {
|
|
192
|
+
directory: string;
|
|
193
|
+
include: string[];
|
|
194
|
+
exclude: string[];
|
|
195
|
+
defaultLayout: string;
|
|
196
|
+
route?: (arg: { relativePath: string }) => string;
|
|
197
|
+
parseData?: (value: unknown, ctx: { sourcePath: string }) => unknown;
|
|
198
|
+
}
|
|
199
|
+
const content: ContentDraft = {
|
|
200
|
+
directory: "",
|
|
201
|
+
include: ["**/*.md"],
|
|
202
|
+
exclude: [],
|
|
203
|
+
defaultLayout: "",
|
|
204
|
+
};
|
|
205
|
+
const contentObj = asObject(root.content);
|
|
206
|
+
if (!contentObj) {
|
|
207
|
+
typeError(errors, "content", "an object");
|
|
208
|
+
} else {
|
|
209
|
+
checkKeys(contentObj, CONTENT_KEYS, "content", errors);
|
|
210
|
+
if (typeof contentObj.directory === "string" && contentObj.directory !== "") {
|
|
211
|
+
content.directory = contentObj.directory;
|
|
212
|
+
} else {
|
|
213
|
+
typeError(errors, "content.directory", "a non-empty path string");
|
|
214
|
+
}
|
|
215
|
+
if (contentObj.include !== undefined) {
|
|
216
|
+
const include = stringArray(contentObj.include, "content.include", errors);
|
|
217
|
+
if (include) content.include = include;
|
|
218
|
+
}
|
|
219
|
+
if (contentObj.exclude !== undefined) {
|
|
220
|
+
const exclude = stringArray(contentObj.exclude, "content.exclude", errors);
|
|
221
|
+
if (exclude) content.exclude = exclude;
|
|
222
|
+
}
|
|
223
|
+
if (typeof contentObj.defaultLayout !== "string" || contentObj.defaultLayout === "") {
|
|
224
|
+
typeError(errors, "content.defaultLayout", "a non-empty string naming a key of layouts");
|
|
225
|
+
} else {
|
|
226
|
+
content.defaultLayout = contentObj.defaultLayout;
|
|
227
|
+
}
|
|
228
|
+
if (contentObj.route !== undefined && typeof contentObj.route !== "function") {
|
|
229
|
+
typeError(errors, "content.route", "a function (relativePath) => route");
|
|
230
|
+
} else if (typeof contentObj.route === "function") {
|
|
231
|
+
content.route = contentObj.route as ContentConfig["route"];
|
|
232
|
+
}
|
|
233
|
+
if (contentObj.parseData !== undefined && typeof contentObj.parseData !== "function") {
|
|
234
|
+
typeError(errors, "content.parseData", "a function (value, ctx) => data");
|
|
235
|
+
} else if (typeof contentObj.parseData === "function") {
|
|
236
|
+
content.parseData = contentObj.parseData as ContentConfig["parseData"];
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// layouts — non-empty registry of functions. The registry is
|
|
241
|
+
// prototype-LESS and entries are stored with defineProperty: a `__proto__`
|
|
242
|
+
// layout name (own, e.g. from JSON.parse) must be a map entry, not a
|
|
243
|
+
// prototype assignment — and the defaultLayout lookup below must use
|
|
244
|
+
// hasOwn, because `in` walks a clobbered prototype and would match
|
|
245
|
+
// Function.prototype members ("call" in layouts) on a false pass.
|
|
246
|
+
const layouts = Object.create(null) as Record<string, LayoutFn>;
|
|
247
|
+
const layoutsObj = asObject(root.layouts);
|
|
248
|
+
if (!layoutsObj) {
|
|
249
|
+
typeError(errors, "layouts", "an object mapping layout names to functions");
|
|
250
|
+
} else {
|
|
251
|
+
for (const [name, fn] of Object.entries(layoutsObj)) {
|
|
252
|
+
if (typeof fn !== "function") {
|
|
253
|
+
errors.push({
|
|
254
|
+
code: "config.layout-type",
|
|
255
|
+
severity: "error",
|
|
256
|
+
message: `layout "${name}" (config key "layouts.${name}") must be a function`,
|
|
257
|
+
});
|
|
258
|
+
} else {
|
|
259
|
+
Object.defineProperty(layouts, name, {
|
|
260
|
+
value: fn as LayoutFn,
|
|
261
|
+
enumerable: true,
|
|
262
|
+
writable: true,
|
|
263
|
+
configurable: true,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (Object.keys(layoutsObj).length === 0) {
|
|
268
|
+
errors.push({
|
|
269
|
+
code: "config.layout-type",
|
|
270
|
+
severity: "error",
|
|
271
|
+
message: 'config key "layouts" must declare at least one layout',
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (
|
|
276
|
+
content.defaultLayout !== "" &&
|
|
277
|
+
layoutsObj &&
|
|
278
|
+
!Object.hasOwn(layouts, content.defaultLayout)
|
|
279
|
+
) {
|
|
280
|
+
errors.push({
|
|
281
|
+
code: "config.layout-type",
|
|
282
|
+
severity: "error",
|
|
283
|
+
message: `content.defaultLayout "${content.defaultLayout}" is not a key of layouts`,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// assets
|
|
288
|
+
let scripts: Record<string, string> = {};
|
|
289
|
+
let styles: Record<string, string> = {};
|
|
290
|
+
let publicDirectory: string | undefined;
|
|
291
|
+
if (root.assets !== undefined) {
|
|
292
|
+
const assets = asObject(root.assets);
|
|
293
|
+
if (!assets) {
|
|
294
|
+
typeError(errors, "assets", "an object");
|
|
295
|
+
} else {
|
|
296
|
+
checkKeys(assets, ASSETS_KEYS, "assets", errors);
|
|
297
|
+
scripts = validateAssetMap(assets.scripts, "assets.scripts", errors);
|
|
298
|
+
styles = validateAssetMap(assets.styles, "assets.styles", errors);
|
|
299
|
+
if (assets.publicDirectory !== undefined) {
|
|
300
|
+
if (typeof assets.publicDirectory === "string" && assets.publicDirectory !== "") {
|
|
301
|
+
publicDirectory = assets.publicDirectory;
|
|
302
|
+
} else {
|
|
303
|
+
typeError(errors, "assets.publicDirectory", "a non-empty path string");
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
// scripts and styles share ONE namespace: one manifest, one output dir.
|
|
307
|
+
for (const name of Object.keys(scripts).sort()) {
|
|
308
|
+
if (Object.hasOwn(styles, name)) {
|
|
309
|
+
errors.push({
|
|
310
|
+
code: "config.asset-name-clash",
|
|
311
|
+
severity: "error",
|
|
312
|
+
message: `asset name "${name}" is declared in both assets.scripts and assets.styles; they share one namespace`,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// output
|
|
320
|
+
let outputDir = "";
|
|
321
|
+
const trailingSlash = "never";
|
|
322
|
+
const outputObj = asObject(root.output);
|
|
323
|
+
if (!outputObj) {
|
|
324
|
+
typeError(errors, "output", "an object");
|
|
325
|
+
} else {
|
|
326
|
+
checkKeys(outputObj, OUTPUT_KEYS, "output", errors);
|
|
327
|
+
if (typeof outputObj.directory === "string" && outputObj.directory !== "") {
|
|
328
|
+
outputDir = outputObj.directory;
|
|
329
|
+
} else {
|
|
330
|
+
typeError(errors, "output.directory", "a non-empty path string");
|
|
331
|
+
}
|
|
332
|
+
if (outputObj.trailingSlash !== undefined && outputObj.trailingSlash !== "never") {
|
|
333
|
+
errors.push({
|
|
334
|
+
code: "config.type",
|
|
335
|
+
severity: "error",
|
|
336
|
+
message: `config key "output.trailingSlash" only accepts "never" in v1, got ${JSON.stringify(outputObj.trailingSlash)}`,
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// sitemap / robots / dev / lang
|
|
342
|
+
let sitemap = true;
|
|
343
|
+
if (root.sitemap !== undefined) {
|
|
344
|
+
if (typeof root.sitemap !== "boolean") typeError(errors, "sitemap", "a boolean");
|
|
345
|
+
else sitemap = root.sitemap;
|
|
346
|
+
}
|
|
347
|
+
let robots: RobotsConfig | undefined;
|
|
348
|
+
if (root.robots !== undefined) {
|
|
349
|
+
const robotsObj = asObject(root.robots);
|
|
350
|
+
if (!robotsObj) {
|
|
351
|
+
typeError(errors, "robots", "an object");
|
|
352
|
+
} else {
|
|
353
|
+
checkKeys(robotsObj, ROBOTS_KEYS, "robots", errors);
|
|
354
|
+
if (typeof robotsObj.indexable === "boolean") {
|
|
355
|
+
robots = { indexable: robotsObj.indexable };
|
|
356
|
+
} else {
|
|
357
|
+
typeError(errors, "robots.indexable", "a boolean");
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
let dev: DevConfig | undefined;
|
|
362
|
+
if (root.dev !== undefined) {
|
|
363
|
+
const devObj = asObject(root.dev);
|
|
364
|
+
if (!devObj) {
|
|
365
|
+
typeError(errors, "dev", "an object");
|
|
366
|
+
} else {
|
|
367
|
+
checkKeys(devObj, DEV_KEYS, "dev", errors);
|
|
368
|
+
dev = {};
|
|
369
|
+
if (devObj.hostname !== undefined) {
|
|
370
|
+
if (typeof devObj.hostname === "string" && devObj.hostname !== "")
|
|
371
|
+
dev.hostname = devObj.hostname;
|
|
372
|
+
else typeError(errors, "dev.hostname", "a non-empty string");
|
|
373
|
+
}
|
|
374
|
+
if (devObj.port !== undefined) {
|
|
375
|
+
const port = devObj.port;
|
|
376
|
+
if (typeof port !== "number" || !Number.isInteger(port) || port < 1 || port > 65535) {
|
|
377
|
+
// JSON.stringify(NaN) is "null" — a NaN port would be reported as
|
|
378
|
+
// "got null", which points the user at the wrong mistake.
|
|
379
|
+
const shown =
|
|
380
|
+
port === undefined ? "undefined" : Number.isNaN(port) ? "NaN" : JSON.stringify(port);
|
|
381
|
+
errors.push({
|
|
382
|
+
code: "config.port",
|
|
383
|
+
severity: "error",
|
|
384
|
+
message: `config key "dev.port" must be an integer between 1 and 65535, got ${shown}`,
|
|
385
|
+
});
|
|
386
|
+
} else {
|
|
387
|
+
dev.port = port;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
let lang = "en";
|
|
393
|
+
if (root.lang !== undefined) {
|
|
394
|
+
if (typeof root.lang !== "string" || root.lang === "")
|
|
395
|
+
typeError(errors, "lang", "a non-empty BCP 47 string");
|
|
396
|
+
else lang = root.lang;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (errors.length > 0) throw new BuildError(errors);
|
|
400
|
+
|
|
401
|
+
const draft = {
|
|
402
|
+
site: siteUrl,
|
|
403
|
+
content,
|
|
404
|
+
layouts,
|
|
405
|
+
assets: {
|
|
406
|
+
scripts,
|
|
407
|
+
styles,
|
|
408
|
+
...(publicDirectory !== undefined ? { publicDirectory } : {}),
|
|
409
|
+
},
|
|
410
|
+
output: { directory: outputDir, trailingSlash },
|
|
411
|
+
sitemap,
|
|
412
|
+
...(robots !== undefined ? { robots } : {}),
|
|
413
|
+
...(dev !== undefined ? { dev } : {}),
|
|
414
|
+
lang,
|
|
415
|
+
};
|
|
416
|
+
return draft as unknown as Site;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function toPosix(p: string): string {
|
|
420
|
+
return p.replace(/\\/g, "/");
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function isAbsolute(p: string): boolean {
|
|
424
|
+
return posix.isAbsolute(p);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function resolveUnder(configDir: string, rel: string): string {
|
|
428
|
+
const r = toPosix(rel);
|
|
429
|
+
// A root-relative Windows path inherits the CONFIG's drive/share, not cwd.
|
|
430
|
+
// Keep explicit POSIX fixtures portable when validating on Windows.
|
|
431
|
+
if (isWindowsPath(configDir)) return posix.resolve(configDir, r);
|
|
432
|
+
return posix.normalize(isAbsolute(r) ? r : posix.join(toPosix(configDir), r));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Resolve every stored path against the CONFIG FILE's directory — never cwd
|
|
437
|
+
* (a build must not depend on where it was invoked from). Returns a plain
|
|
438
|
+
* resolved view; the brand stops here, this is an internal working shape.
|
|
439
|
+
*/
|
|
440
|
+
export function resolveConfigPaths(site: Site, configDir: string): ResolvedSite {
|
|
441
|
+
const errors: Diagnostic[] = [];
|
|
442
|
+
const dir = toPosix(configDir);
|
|
443
|
+
if (typeof configDir !== "string" || !isAbsolute(dir)) {
|
|
444
|
+
errors.push({
|
|
445
|
+
code: "config.type",
|
|
446
|
+
severity: "error",
|
|
447
|
+
message: `configDir must be an absolute path, got ${JSON.stringify(configDir)}`,
|
|
448
|
+
});
|
|
449
|
+
throw new BuildError(errors);
|
|
450
|
+
}
|
|
451
|
+
return {
|
|
452
|
+
configDir: posix.normalize(dir),
|
|
453
|
+
site: site.site,
|
|
454
|
+
content: { ...site.content, directory: resolveUnder(dir, site.content.directory) },
|
|
455
|
+
layouts: site.layouts,
|
|
456
|
+
assets: {
|
|
457
|
+
scripts: Object.fromEntries(
|
|
458
|
+
Object.entries(site.assets.scripts).map(([name, p]) => [name, resolveUnder(dir, p)]),
|
|
459
|
+
),
|
|
460
|
+
styles: Object.fromEntries(
|
|
461
|
+
Object.entries(site.assets.styles).map(([name, p]) => [name, resolveUnder(dir, p)]),
|
|
462
|
+
),
|
|
463
|
+
...(site.assets.publicDirectory !== undefined
|
|
464
|
+
? { publicDirectory: resolveUnder(dir, site.assets.publicDirectory) }
|
|
465
|
+
: {}),
|
|
466
|
+
},
|
|
467
|
+
output: { ...site.output, directory: resolveUnder(dir, site.output.directory) },
|
|
468
|
+
sitemap: site.sitemap,
|
|
469
|
+
...(site.robots !== undefined ? { robots: site.robots } : {}),
|
|
470
|
+
...(site.dev !== undefined ? { dev: site.dev } : {}),
|
|
471
|
+
lang: site.lang,
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function overlaps(a: string, b: string): boolean {
|
|
476
|
+
return pathContains(a, b) || pathContains(b, a);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* The output directory must not overlap any input root — otherwise a build
|
|
481
|
+
* eats its own source:
|
|
482
|
+
* - content directory and publicDirectory: either containment direction is
|
|
483
|
+
* dangerous (emit into the sources, or the copy step copying a directory
|
|
484
|
+
* into itself);
|
|
485
|
+
* - every asset entrypoint: only the direction in which the OUTPUT
|
|
486
|
+
* CONTAINS the file is dangerous — emit writes, the staging→out rename
|
|
487
|
+
* and the startup `*.old`/`*.tmp-*` cleanup all write inside the output,
|
|
488
|
+
* so a source file living inside it can be clobbered. The reverse (output
|
|
489
|
+
* inside the entrypoint's directory) only adds new files to the source
|
|
490
|
+
* tree, clobbers nothing, and is the common layout (config + entrypoints
|
|
491
|
+
* at the project root, output in a subdirectory) — the same "output
|
|
492
|
+
* inside the config directory" case the one-way rule below allows.
|
|
493
|
+
* Compared by path COMPONENTS, so `/out` and `/output` do not false-match.
|
|
494
|
+
* Throws BuildError naming both paths of every overlap.
|
|
495
|
+
*/
|
|
496
|
+
export function checkOutputOverlap(resolved: ResolvedSite): void {
|
|
497
|
+
const out = toPosix(resolved.output.directory);
|
|
498
|
+
const inputs: [string, string][] = [["content.directory", toPosix(resolved.content.directory)]];
|
|
499
|
+
if (resolved.assets.publicDirectory !== undefined) {
|
|
500
|
+
inputs.push(["assets.publicDirectory", toPosix(resolved.assets.publicDirectory)]);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const errors: Diagnostic[] = [];
|
|
504
|
+
for (const [label, path] of inputs) {
|
|
505
|
+
if (overlaps(out, path)) {
|
|
506
|
+
errors.push({
|
|
507
|
+
code: "config.output-overlap",
|
|
508
|
+
severity: "error",
|
|
509
|
+
message: `output.directory "${out}" overlaps ${label} "${path}" (one contains the other or they are equal)`,
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
for (const [plane, map] of [
|
|
514
|
+
["assets.scripts", resolved.assets.scripts],
|
|
515
|
+
["assets.styles", resolved.assets.styles],
|
|
516
|
+
] as const) {
|
|
517
|
+
for (const [name, entrypoint] of Object.entries(map)) {
|
|
518
|
+
const file = toPosix(entrypoint);
|
|
519
|
+
if (pathContains(out, file)) {
|
|
520
|
+
errors.push({
|
|
521
|
+
code: "config.output-overlap",
|
|
522
|
+
severity: "error",
|
|
523
|
+
message: `output.directory "${out}" contains ${plane}.${name} "${file}" — the build writes and cleans up inside the output and would clobber its own entrypoint`,
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
// Only OUTPUT-containing-config is dangerous. The reverse is the normal
|
|
529
|
+
// project/dist layout, but an ancestor output would delete the config too.
|
|
530
|
+
if (pathContains(out, toPosix(resolved.configDir))) {
|
|
531
|
+
errors.push({
|
|
532
|
+
code: "config.output-overlap",
|
|
533
|
+
severity: "error",
|
|
534
|
+
message: `output.directory "${out}" contains the config directory "${resolved.configDir}" (or is the config directory itself) — publishing would delete its own config`,
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
if (errors.length > 0) throw new BuildError(errors);
|
|
538
|
+
}
|