@dogsbay/site-files 0.2.0-beta.146 → 0.2.0-beta.147

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.
@@ -28,15 +28,19 @@ function matcher(from) {
28
28
  // Tokens: a literal character, `*` (any characters) or `:name` (one segment). Matched by dynamic
29
29
  // programming over (token, position), O(tokens × length): a regex from a project's rule like
30
30
  // `/*a*a*a*a*b` backtracked exponentially, and a repo's _redirects could hang the build.
31
+ // The two special tokens are longer than one character, so no literal (one character) equals them:
32
+ // a literal `:` (not followed by a letter) stays a colon, not a placeholder
33
+ const ANY = "**any";
34
+ const SEGMENT = "**segment";
31
35
  const tokens = [];
32
36
  for (let i = 0; i < from.length;) {
33
37
  if (from[i] === "*") {
34
- if (tokens[tokens.length - 1] !== "*")
35
- tokens.push("*");
38
+ if (tokens[tokens.length - 1] !== ANY)
39
+ tokens.push(ANY);
36
40
  i++;
37
41
  }
38
42
  else if (from[i] === ":" && /[A-Za-z]/.test(from[i + 1] ?? "")) {
39
- tokens.push(":");
43
+ tokens.push(SEGMENT);
40
44
  i += 2;
41
45
  while (i < from.length && /\w/.test(from[i]))
42
46
  i++;
@@ -52,9 +56,9 @@ function matcher(from) {
52
56
  for (const t of tokens) {
53
57
  const cur = new Uint8Array(n + 1);
54
58
  for (let j = 0; j <= n; j++) {
55
- if (t === "*")
59
+ if (t === ANY)
56
60
  cur[j] = prev[j] || (j > 0 ? cur[j - 1] : 0);
57
- else if (t === ":")
61
+ else if (t === SEGMENT)
58
62
  cur[j] = j > 0 && path[j - 1] !== "/" ? prev[j - 1] || cur[j - 1] : 0;
59
63
  else
60
64
  cur[j] = j > 0 && path[j - 1] === t ? prev[j - 1] : 0;
@@ -267,3 +267,15 @@ export declare function toPagesScript(key: string, pages: readonly string[]): st
267
267
  * close a `<script>` or break the statement.
268
268
  */
269
269
  export declare function toScript(global: string, value: unknown): string;
270
+ /**
271
+ * A later selection added to a project's parsed config (`content` as written): its products and
272
+ * versions merged in, its sources appended. A product or version already there keeps its
273
+ * settings and default (the project's word wins); a source already there is not added twice.
274
+ * Site-wide `versions:` merge the same way. The result is checked by `productModel` (throws
275
+ * `ProductConfigError`), so a merge that would not build is refused here. The host writes YAML.
276
+ */
277
+ export declare function addToConfig<C extends ProductsContent & {
278
+ sources?: ProductSource[];
279
+ }>(content: C, addition: ProductsContent & {
280
+ sources?: ProductSource[];
281
+ }): C;
package/dist/products.js CHANGED
@@ -485,3 +485,81 @@ export function toScript(global, value) {
485
485
  const json = JSON.stringify(value).replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
486
486
  return `window.${global}=${json};\n`;
487
487
  }
488
+ /** A source as a config writes it; compared by everything that says what it reads and where it goes. */
489
+ const sourceKey = (s) => JSON.stringify(["name", "version", "locale", "path", "git", "ref", "subdir", "from", "mount"].map((k) => s[k] ?? null));
490
+ /**
491
+ * A later selection added to a project's parsed config (`content` as written): its products and
492
+ * versions merged in, its sources appended. A product or version already there keeps its
493
+ * settings and default (the project's word wins); a source already there is not added twice.
494
+ * Site-wide `versions:` merge the same way. The result is checked by `productModel` (throws
495
+ * `ProductConfigError`), so a merge that would not build is refused here. The host writes YAML.
496
+ */
497
+ export function addToConfig(content, addition) {
498
+ // A version added to versions already there is not the default: the root ("" path) stays the
499
+ // default's, so an added one is served at its id (products) or without a path (site-wide)
500
+ const mergeVersions = (into, more, siteWide = false) => {
501
+ if (!more?.length)
502
+ return into;
503
+ const out = [...(into ?? [])];
504
+ const ids = new Set(out.map((v) => String(v.id)));
505
+ for (const v of more) {
506
+ const add = v;
507
+ if (ids.has(String(add.id)))
508
+ continue;
509
+ if (into?.length && add.path === "") {
510
+ const { path: _root, ...rest } = add;
511
+ out.push(siteWide ? rest : { ...rest, path: String(add.id) });
512
+ }
513
+ else
514
+ out.push(v);
515
+ }
516
+ return out;
517
+ };
518
+ const out = { ...content };
519
+ if (addition.products) {
520
+ const products = Object.assign(Object.create(null), content.products ?? {});
521
+ for (const [name, add] of Object.entries(addition.products)) {
522
+ const have = products[name];
523
+ if (!have) {
524
+ products[name] = add;
525
+ continue;
526
+ }
527
+ const versions = mergeVersions(have.versions, add.versions);
528
+ products[name] = {
529
+ ...add,
530
+ ...have,
531
+ ...(versions ? { versions } : {}),
532
+ ...(have.defaultVersion === undefined && add.defaultVersion !== undefined && !have.versions ? { defaultVersion: add.defaultVersion } : {}),
533
+ };
534
+ }
535
+ out.products = products;
536
+ }
537
+ if (addition.versions?.length && out.products && Object.keys(out.products).length) {
538
+ throw new Error("a config with a products: block takes versions per product, not site-wide versions:");
539
+ }
540
+ if (addition.products && Object.keys(addition.products).length && content.versions?.length) {
541
+ throw new Error("a config with site-wide versions: cannot take a products: block; import the first product as a product too");
542
+ }
543
+ const versions = mergeVersions(content.versions, addition.versions, true);
544
+ if (versions)
545
+ out.versions = versions;
546
+ if (content.defaultVersion === undefined && addition.defaultVersion !== undefined)
547
+ out.defaultVersion = addition.defaultVersion;
548
+ // Site-wide versions: productModel checks only a products: block, so check the rule the loader has
549
+ for (const v of (out.versions ?? [])) {
550
+ if (v.path === "" && v.id !== out.defaultVersion)
551
+ throw new Error(`version "${String(v.id)}" is served at the site's root, which only the default version "${String(out.defaultVersion)}" may be`);
552
+ }
553
+ const seen = new Set((content.sources ?? []).map((s) => sourceKey(s)));
554
+ const folders = new Map((content.sources ?? []).map((s) => [String(s.path), sourceKey(s)]));
555
+ const added = (addition.sources ?? []).filter((s) => !seen.has(sourceKey(s)));
556
+ for (const s of added) {
557
+ const path = s.path;
558
+ // Another source's folder: the host would write the addition's files over it
559
+ if (typeof path === "string" && folders.has(path))
560
+ throw new Error(`source path "${path}" is already another source's; import into another folder`);
561
+ }
562
+ out.sources = [...(content.sources ?? []), ...added];
563
+ productModel(out);
564
+ return out;
565
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dogsbay/site-files",
3
- "version": "0.2.0-beta.146",
3
+ "version": "0.2.0-beta.147",
4
4
  "description": "robots.txt, _headers, llms.txt, sitemaps, redirects and JSON-LD for docs sites; shared by the dogsbay Astro pipeline and dogsbay-ssg, Worker-safe",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",