@cagov/ca-ds 0.0.0-stage → 1.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 +600 -2
- package/bin/ca-ds.js +5 -0
- package/package.json +62 -4
- package/src/cli/add.js +134 -0
- package/src/cli/build.js +15 -0
- package/src/cli/diff.js +39 -0
- package/src/cli/eject.js +112 -0
- package/src/cli/exit.js +22 -0
- package/src/cli/format.js +52 -0
- package/src/cli/init.js +148 -0
- package/src/cli/install.js +178 -0
- package/src/cli/items.js +10 -0
- package/src/cli/login.js +331 -0
- package/src/cli/main.js +108 -0
- package/src/cli/new.js +230 -0
- package/src/cli/outdated.js +74 -0
- package/src/cli/pack.js +75 -0
- package/src/cli/preview.js +31 -0
- package/src/cli/publish.js +295 -0
- package/src/cli/pull.js +208 -0
- package/src/cli/remote.js +52 -0
- package/src/cli/remove.js +69 -0
- package/src/cli/update.js +223 -0
- package/src/cli/validate.js +131 -0
- package/src/lib/browser.js +12 -0
- package/src/lib/build.js +249 -0
- package/src/lib/bundle.js +148 -0
- package/src/lib/catalog.js +44 -0
- package/src/lib/checks.js +31 -0
- package/src/lib/collection.js +65 -0
- package/src/lib/emblem.js +29 -0
- package/src/lib/fixtures.js +59 -0
- package/src/lib/floor.js +79 -0
- package/src/lib/glyph.js +148 -0
- package/src/lib/image.js +138 -0
- package/src/lib/json-text.js +213 -0
- package/src/lib/keys.js +322 -0
- package/src/lib/links.js +321 -0
- package/src/lib/markdown.js +408 -0
- package/src/lib/merge.js +93 -0
- package/src/lib/pack.js +30 -0
- package/src/lib/page-schema.js +368 -0
- package/src/lib/page.js +372 -0
- package/src/lib/patch.js +9 -0
- package/src/lib/regions.js +173 -0
- package/src/lib/registry.js +371 -0
- package/src/lib/release.js +119 -0
- package/src/lib/repoints.js +120 -0
- package/src/lib/resolve.js +32 -0
- package/src/lib/schema.js +28 -0
- package/src/lib/serial.js +4 -0
- package/src/lib/serialize.js +444 -0
- package/src/lib/structured.js +72 -0
- package/src/lib/system.js +13 -0
- package/src/lib/targets.js +85 -0
- package/src/lib/terms.js +11 -0
- package/src/lib/token-view.js +289 -0
- package/src/lib/tokens.js +247 -0
- package/src/lib/version.js +34 -0
- package/src/preview/frame.js +75 -0
- package/src/preview/pages.js +128 -0
- package/src/preview/preview.css +58 -0
- package/src/preview/screenshots.js +72 -0
- package/src/preview/server.js +378 -0
- package/src/preview/shell.js +153 -0
- package/src/schema/a11y.schema.json +56 -0
- package/src/schema/collection.lock.schema.json +58 -0
- package/src/schema/collection.schema.json +65 -0
- package/src/schema/composition.schema.json +98 -0
- package/src/schema/contract.schema.json +116 -0
- package/src/schema/interface.schema.json +257 -0
- package/src/schema/item.schema.json +362 -0
- package/src/schema/release.schema.json +39 -0
- package/src/schema/system.schema.json +75 -0
- package/src/schema/variants.schema.json +60 -0
- package/src/validate/collection.js +19 -0
- package/src/validate/collisions.js +124 -0
- package/src/validate/context.js +357 -0
- package/src/validate/facts.js +253 -0
- package/src/validate/files.js +103 -0
- package/src/validate/findings.js +19 -0
- package/src/validate/glyphs.js +126 -0
- package/src/validate/html.js +215 -0
- package/src/validate/imports.js +29 -0
- package/src/validate/index.js +75 -0
- package/src/validate/item.js +47 -0
- package/src/validate/links.js +113 -0
- package/src/validate/manifest.js +147 -0
- package/src/validate/media.js +65 -0
- package/src/validate/page.js +74 -0
- package/src/validate/prefixes.js +58 -0
- package/src/validate/relations.js +310 -0
- package/src/validate/release.js +109 -0
- package/src/validate/skill.js +122 -0
- package/src/validate/structured.js +335 -0
- package/src/validate/tasc.js +80 -0
- package/src/validate/template.js +129 -0
- package/src/validate/tokens.js +154 -0
- package/src/validate/zip.js +40 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "system.schema.json",
|
|
4
|
+
"title": "system.json",
|
|
5
|
+
"description": "What no single item owns: the foundation, the fixtures, the presets, and the release pin set.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"required": ["foundation", "presets", "release"],
|
|
9
|
+
"properties": {
|
|
10
|
+
"$comment": { "type": "string" },
|
|
11
|
+
"foundation": {
|
|
12
|
+
"description": "The ordered set every component implicitly depends on. Resolve prepends it in this order.",
|
|
13
|
+
"type": "array",
|
|
14
|
+
"minItems": 1,
|
|
15
|
+
"uniqueItems": true,
|
|
16
|
+
"items": { "$ref": "#/$defs/scopedName" }
|
|
17
|
+
},
|
|
18
|
+
"fixtures": {
|
|
19
|
+
"description": "The media collection whose members answer /media/<slug>/<file> in every frame host, where <slug> is a member's folder name and <file> a file it declares. Reference pages link their demo images by those paths.",
|
|
20
|
+
"$ref": "#/$defs/scopedName"
|
|
21
|
+
},
|
|
22
|
+
"presets": {
|
|
23
|
+
"description": "Named sets built as bundles. The foundation is implied and not listed.",
|
|
24
|
+
"type": "object",
|
|
25
|
+
"required": ["everything", "foundation", "starter"],
|
|
26
|
+
"propertyNames": { "pattern": "^[a-z0-9]+(-[a-z0-9]+)*$" },
|
|
27
|
+
"additionalProperties": { "$ref": "#/$defs/preset" }
|
|
28
|
+
},
|
|
29
|
+
"release": {
|
|
30
|
+
"description": "The pinned set for the tag being cut. tag is null between releases.",
|
|
31
|
+
"type": "object",
|
|
32
|
+
"additionalProperties": false,
|
|
33
|
+
"required": ["tag", "items"],
|
|
34
|
+
"properties": {
|
|
35
|
+
"tag": {
|
|
36
|
+
"oneOf": [{ "type": "null" }, { "$ref": "#/$defs/tag" }]
|
|
37
|
+
},
|
|
38
|
+
"items": {
|
|
39
|
+
"type": "object",
|
|
40
|
+
"propertyNames": { "$ref": "#/$defs/scopedName" },
|
|
41
|
+
"additionalProperties": { "$ref": "#/$defs/semver" }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"$defs": {
|
|
47
|
+
"tag": {
|
|
48
|
+
"description": "A system release tag, v<semver>.",
|
|
49
|
+
"type": "string",
|
|
50
|
+
"pattern": "^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?$"
|
|
51
|
+
},
|
|
52
|
+
"scopedName": {
|
|
53
|
+
"type": "string",
|
|
54
|
+
"pattern": "^[a-z0-9]+(-[a-z0-9]+)*/[a-z0-9]+(-[a-z0-9]+)*$"
|
|
55
|
+
},
|
|
56
|
+
"semver": {
|
|
57
|
+
"type": "string",
|
|
58
|
+
"pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?(\\+[0-9A-Za-z.-]+)?$"
|
|
59
|
+
},
|
|
60
|
+
"preset": {
|
|
61
|
+
"type": "object",
|
|
62
|
+
"additionalProperties": false,
|
|
63
|
+
"required": ["description", "items"],
|
|
64
|
+
"properties": {
|
|
65
|
+
"description": { "type": "string", "minLength": 1 },
|
|
66
|
+
"items": {
|
|
67
|
+
"description": "Requested items in order. Resolve breaks ordering ties by this order.",
|
|
68
|
+
"type": "array",
|
|
69
|
+
"uniqueItems": true,
|
|
70
|
+
"items": { "$ref": "#/$defs/scopedName" }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "variants.schema.json",
|
|
4
|
+
"title": "<name>.variants.json",
|
|
5
|
+
"description": "A component's variants: the purpose-named configurations a consumer chooses between, each with when to use it, when not to, and the examples that show it.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"required": ["variants"],
|
|
9
|
+
"properties": {
|
|
10
|
+
"$comment": {
|
|
11
|
+
"description": "A note for maintainers. It carries no data.",
|
|
12
|
+
"type": "string"
|
|
13
|
+
},
|
|
14
|
+
"variants": {
|
|
15
|
+
"type": "array",
|
|
16
|
+
"minItems": 1,
|
|
17
|
+
"items": { "$ref": "#/$defs/variant" }
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"$defs": {
|
|
21
|
+
"text": { "type": "string", "minLength": 1 },
|
|
22
|
+
"variant": {
|
|
23
|
+
"type": "object",
|
|
24
|
+
"additionalProperties": false,
|
|
25
|
+
"required": ["id", "title", "recipe", "use", "examples"],
|
|
26
|
+
"properties": {
|
|
27
|
+
"id": {
|
|
28
|
+
"description": "Unique within the file.",
|
|
29
|
+
"type": "string",
|
|
30
|
+
"pattern": "^[a-z0-9]+(-[a-z0-9]+)*$"
|
|
31
|
+
},
|
|
32
|
+
"axis": {
|
|
33
|
+
"description": "The axis the variant is one value of, such as appearance or shape. state marks a state, such as disabled. A combination of several axes takes none. The docs site groups variants by axis.",
|
|
34
|
+
"type": "string",
|
|
35
|
+
"pattern": "^[a-z0-9]+(-[a-z0-9]+)*$"
|
|
36
|
+
},
|
|
37
|
+
"title": { "$ref": "#/$defs/text" },
|
|
38
|
+
"recipe": {
|
|
39
|
+
"description": "What the variant sets, in the shape of an example's shows. The validator checks that each listed example's shows contains it.",
|
|
40
|
+
"$ref": "item.schema.json#/$defs/shows"
|
|
41
|
+
},
|
|
42
|
+
"use": {
|
|
43
|
+
"description": "When to reach for it.",
|
|
44
|
+
"$ref": "#/$defs/text"
|
|
45
|
+
},
|
|
46
|
+
"avoid": {
|
|
47
|
+
"description": "When not to. It links a contract rule rather than restating one.",
|
|
48
|
+
"$ref": "#/$defs/text"
|
|
49
|
+
},
|
|
50
|
+
"examples": {
|
|
51
|
+
"description": "The item's example files that show the variant, the first one the default.",
|
|
52
|
+
"type": "array",
|
|
53
|
+
"minItems": 1,
|
|
54
|
+
"uniqueItems": true,
|
|
55
|
+
"items": { "$ref": "item.schema.json#/$defs/path" }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { COLLECTION, LOCK } from '../lib/collection.js';
|
|
2
|
+
|
|
3
|
+
export const check = 'collection';
|
|
4
|
+
|
|
5
|
+
export async function run(item, ctx, { reject }) {
|
|
6
|
+
const name = item.manifest?.name;
|
|
7
|
+
if (typeof name !== 'string' || item.authored == null) return;
|
|
8
|
+
const scope = name.includes('/') ? name.slice(0, name.indexOf('/')) : name;
|
|
9
|
+
if (!item.authored && !item.installed) {
|
|
10
|
+
reject('neither', `\`${name}\` is neither authored nor installed: \`${scope}\` is none of ${COLLECTION}'s namespaces, and ${LOCK} doesn't record it.`, { file: 'item.json' });
|
|
11
|
+
} else if (item.authored && item.installed) {
|
|
12
|
+
reject('both', `\`${name}\` is both authored and installed: \`${scope}\` is one of ${COLLECTION}'s namespaces, and ${LOCK} records it too. An item is one or the other.`, { file: 'item.json' });
|
|
13
|
+
}
|
|
14
|
+
const forkedFrom = item.manifest.forkedFrom;
|
|
15
|
+
const original = typeof forkedFrom === 'string' && forkedFrom.includes('@') ? forkedFrom.slice(0, forkedFrom.lastIndexOf('@')) : null;
|
|
16
|
+
if (original && original !== name && ctx.catalog.has(original)) {
|
|
17
|
+
reject('fork-and-original', `\`${name}\` forks \`${original}\`, and the collection holds \`${original}\` too. Remove one of them.`, { file: 'item.json' });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { declaredFiles } from './item.js';
|
|
2
|
+
import { stateItem } from './context.js';
|
|
3
|
+
import { interfacesOf, relations } from './relations.js';
|
|
4
|
+
import { collisionId, describeName } from '../lib/keys.js';
|
|
5
|
+
import { systemItem } from '../lib/system.js';
|
|
6
|
+
|
|
7
|
+
export const check = 'collisions';
|
|
8
|
+
|
|
9
|
+
const FRAME = 'ca-layout';
|
|
10
|
+
const FORMAT = 'format';
|
|
11
|
+
|
|
12
|
+
const indexes = new WeakMap();
|
|
13
|
+
function nameIndex(ctx) {
|
|
14
|
+
if (!indexes.has(ctx.catalog)) {
|
|
15
|
+
const rel = relations(ctx);
|
|
16
|
+
const index = new Map();
|
|
17
|
+
for (const name of ctx.catalog.keys()) {
|
|
18
|
+
for (const n of rel.own(name)) {
|
|
19
|
+
const id = collisionId(rel, name, n);
|
|
20
|
+
if (!index.has(id)) index.set(id, new Set());
|
|
21
|
+
index.get(id).add(name);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
indexes.set(ctx.catalog, index);
|
|
25
|
+
}
|
|
26
|
+
return indexes.get(ctx.catalog);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// null when no interface declares <ca-layout>. An extension record adding no format, as burger's, declares none.
|
|
30
|
+
function formats(items) {
|
|
31
|
+
let found = false;
|
|
32
|
+
const values = new Set();
|
|
33
|
+
for (const item of items) {
|
|
34
|
+
for (const iface of interfacesOf(item)) {
|
|
35
|
+
const el = iface.elements?.[FRAME];
|
|
36
|
+
if (!el || (el.extends && !(el.attributes ?? []).some((a) => a.name === FORMAT))) continue;
|
|
37
|
+
found = true;
|
|
38
|
+
for (const a of el.attributes ?? []) {
|
|
39
|
+
if (a.name !== FORMAT) continue;
|
|
40
|
+
for (const v of a.values) {
|
|
41
|
+
if (typeof v === 'string') values.add(v);
|
|
42
|
+
else if (typeof v?.value === 'string') values.add(v.value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return found ? values : null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// release.js's publishOrder reads it, so a framed example publishes after its format's declarer.
|
|
51
|
+
export function formatDeclarers(items) {
|
|
52
|
+
const out = new Map();
|
|
53
|
+
for (const item of items) {
|
|
54
|
+
const name = item.manifest?.name;
|
|
55
|
+
if (typeof name !== 'string') continue;
|
|
56
|
+
for (const iface of interfacesOf(item)) {
|
|
57
|
+
for (const a of iface.elements?.[FRAME]?.attributes ?? []) {
|
|
58
|
+
if (a.name !== FORMAT) continue;
|
|
59
|
+
for (const v of a.values) {
|
|
60
|
+
const value = typeof v === 'string' ? v : typeof v?.value === 'string' ? v.value : null;
|
|
61
|
+
if (value === null) continue;
|
|
62
|
+
if (!out.has(value)) out.set(value, new Set());
|
|
63
|
+
out.get(value).add(name);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function run(item, ctx, { reject, warn }) {
|
|
72
|
+
const m = item.manifest;
|
|
73
|
+
const name = typeof m.name === 'string' ? m.name : null;
|
|
74
|
+
|
|
75
|
+
const framed = declaredFiles(m).filter((f) => typeof f.frame?.format === 'string');
|
|
76
|
+
if (framed.length > 0) {
|
|
77
|
+
const layout = stateItem(ctx, systemItem('layout'));
|
|
78
|
+
const pool = [...ctx.catalog.values(), item, ...(layout ? [layout] : [])];
|
|
79
|
+
const declared = formats(pool);
|
|
80
|
+
if (declared === null) {
|
|
81
|
+
warn('frame-unchecked', `No item in the state declares \`<${FRAME}>\`, so the examples' frame formats go unchecked.`, { file: 'item.json' });
|
|
82
|
+
} else {
|
|
83
|
+
// A part uploaded ahead of its partOf chain warns, since the registry can't read the root yet.
|
|
84
|
+
const rel = item.authored == null && name ? relations(ctx, item) : null;
|
|
85
|
+
const pending = rel ? rel.unresolved(name).filter((n) => rel.partChain(name).includes(n)) : [];
|
|
86
|
+
// The registry's layout alone declares only the base's formats, so a miss may be an unheld remix's.
|
|
87
|
+
const remote = Boolean(layout) && !ctx.catalog.has(systemItem('layout')) && formats([...ctx.catalog.values(), item]) === null;
|
|
88
|
+
for (const f of framed) {
|
|
89
|
+
if (declared.has(f.frame.format)) continue;
|
|
90
|
+
if (remote) {
|
|
91
|
+
warn('frame-unchecked', `The example \`${f.path}\`'s frame names a format the registry's \`${systemItem('layout')}\` doesn't declare on \`<${FRAME}>\`, and the collection holds no remix that may add it, so the frame goes unchecked.`, { file: 'item.json' });
|
|
92
|
+
} else if (pending.length > 0) {
|
|
93
|
+
warn('frame-format-pending', `The example \`${f.path}\`'s frame names a format no published item declares on \`<${FRAME}>\`, while \`${pending[0]}\`, up the part's \`partOf\` chain, is unpublished too, so the frame goes unchecked. A first publish sends a part ahead of the parent that lists it.`, { file: 'item.json' });
|
|
94
|
+
} else {
|
|
95
|
+
reject('frame-format', `The example \`${f.path}\`'s frame names a format no item in the state declares on \`<${FRAME}>\`.`, { file: 'item.json' });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!name || item.authored == null) return;
|
|
102
|
+
for (const { other, name: n } of collisionsWith(ctx, item)) {
|
|
103
|
+
const under = n.kind === 'element' ? '' : ' under the same borrowed key';
|
|
104
|
+
reject('collision', `\`${other}\` also adds ${describeName(n)}${under}, so the two conflict when both load. A collection holds one of them.`, { file: 'item.json' });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function collisionsWith(ctx, item) {
|
|
109
|
+
const name = item.manifest?.name;
|
|
110
|
+
if (typeof name !== 'string') return [];
|
|
111
|
+
const index = nameIndex(ctx);
|
|
112
|
+
const rel = relations(ctx, item);
|
|
113
|
+
const out = [];
|
|
114
|
+
const elements = new Set();
|
|
115
|
+
for (const n of rel.own(name)) {
|
|
116
|
+
const others = [...(index.get(collisionId(rel, name, n)) ?? [])].filter((other) => other !== name);
|
|
117
|
+
for (const other of others) {
|
|
118
|
+
if (n.kind === 'element') elements.add(`${other}\0${n.name}`);
|
|
119
|
+
else if (elements.has(`${other}\0${n.element}`)) continue;
|
|
120
|
+
out.push({ other, name: n });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
// Locally the collection is the published state. The service passes its registry instead.
|
|
2
|
+
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { readFileSync } from 'node:fs';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { openCollection, foundationOf } from '../lib/collection.js';
|
|
7
|
+
import { fixtureFolders } from '../lib/fixtures.js';
|
|
8
|
+
import { resolve } from '../lib/resolve.js';
|
|
9
|
+
import { buildTokens, declarations, SCHEME_SUPPORTS, tokenLayers } from '../lib/tokens.js';
|
|
10
|
+
import { structuredFiles } from '../lib/structured.js';
|
|
11
|
+
import { surfaceOwners, interfacesFor, interfaceEntry } from '../lib/regions.js';
|
|
12
|
+
import { loadItem, declaredFiles } from './item.js';
|
|
13
|
+
import { itemFacts } from './facts.js';
|
|
14
|
+
|
|
15
|
+
// Names as the token build writes them.
|
|
16
|
+
export function tokenNames(json) {
|
|
17
|
+
const names = new Set();
|
|
18
|
+
const kebab = (s) => s.replace(/([a-z0-9])([A-Z])/g, '$1-$2').replace(/[\s_.]+/g, '-').toLowerCase();
|
|
19
|
+
const walk = (node, path) => {
|
|
20
|
+
if (!node || typeof node !== 'object') return;
|
|
21
|
+
if ('$value' in node) {
|
|
22
|
+
names.add(`--ca-${path.map(kebab).join('-')}`);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
for (const [key, value] of Object.entries(node)) if (!key.startsWith('$')) walk(value, [...path, key]);
|
|
26
|
+
};
|
|
27
|
+
walk(json, []);
|
|
28
|
+
return names;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// One spelling for a hex color or a nonzero length. null otherwise, so keywords and compound values never match.
|
|
32
|
+
export function literalKey(value) {
|
|
33
|
+
const v = value.trim().toLowerCase();
|
|
34
|
+
const hex = v.match(/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/);
|
|
35
|
+
if (hex) {
|
|
36
|
+
const h = hex[1];
|
|
37
|
+
return '#' + (h.length <= 4 ? [...h].map((c) => c + c).join('') : h);
|
|
38
|
+
}
|
|
39
|
+
const len = v.match(/^(-?\d*\.?\d+)(px|rem|em)$/);
|
|
40
|
+
if (len && Number(len[1]) !== 0) return `${Number(len[1])}${len[2]}`;
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Each var() chain is followed to its end, and a light-dark() pair through each scheme, before keying.
|
|
45
|
+
export async function tokenLiterals(sources) {
|
|
46
|
+
const component = new Set(sources.components.flatMap(({ json }) => [...tokenNames(json)]));
|
|
47
|
+
const css = await buildTokens({ sources });
|
|
48
|
+
const at = css.indexOf(SCHEME_SUPPORTS);
|
|
49
|
+
const root = declarations(at < 0 ? css : css.slice(0, at));
|
|
50
|
+
const dark = new Map();
|
|
51
|
+
for (const [name, value] of declarations(at < 0 ? '' : css.slice(at))) {
|
|
52
|
+
const pair = splitLightDark(value);
|
|
53
|
+
if (pair) dark.set(name, pair[1]);
|
|
54
|
+
}
|
|
55
|
+
const byKey = new Map();
|
|
56
|
+
for (const decls of [root, dark]) {
|
|
57
|
+
const end = (value, depth = 0) => {
|
|
58
|
+
const ref = value.match(/^var\((--ca-[\w-]+)\)$/);
|
|
59
|
+
if (!ref || depth > 20) return value;
|
|
60
|
+
return decls.has(ref[1]) ? end(decls.get(ref[1]), depth + 1) : value;
|
|
61
|
+
};
|
|
62
|
+
for (const [name, value] of decls) {
|
|
63
|
+
const key = literalKey(end(value));
|
|
64
|
+
if (!key) continue;
|
|
65
|
+
if (!byKey.has(key)) byKey.set(key, new Set());
|
|
66
|
+
byKey.get(key).add(name);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { byKey: new Map([...byKey].map(([k, names]) => [k, [...names].sort()])), component };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function splitLightDark(value) {
|
|
73
|
+
const m = value.match(/^light-dark\(([\s\S]*)\)$/);
|
|
74
|
+
if (!m) return null;
|
|
75
|
+
let depth = 0;
|
|
76
|
+
for (let i = 0; i < m[1].length; i++) {
|
|
77
|
+
const c = m[1][i];
|
|
78
|
+
if (c === '(') depth++;
|
|
79
|
+
else if (c === ')') depth--;
|
|
80
|
+
else if (c === ',' && depth === 0) return [m[1].slice(0, i).trim(), m[1].slice(i + 1).trim()];
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// stamp(item) changes whenever the item's files do, or is null for an item never kept, such as an upload.
|
|
86
|
+
export function contextMemo(stamp) {
|
|
87
|
+
return { stamp, facts: new Map(), literals: new Map() };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const KEPT_LITERALS = 4;
|
|
91
|
+
|
|
92
|
+
async function memoLiterals(memo, sources, build) {
|
|
93
|
+
if (!memo) return build();
|
|
94
|
+
const key = createHash('sha256').update(JSON.stringify(sources)).digest('hex');
|
|
95
|
+
if (!memo.literals.has(key)) {
|
|
96
|
+
memo.literals.set(key, build());
|
|
97
|
+
while (memo.literals.size > KEPT_LITERALS) memo.literals.delete(memo.literals.keys().next().value);
|
|
98
|
+
}
|
|
99
|
+
return memo.literals.get(key);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// published: name to published versions, which turns on the version check. available: the same, unyanked.
|
|
103
|
+
// registry: name to loaded item, where a check finds what the collection lacks; a name it lacks is unpublished.
|
|
104
|
+
// strictLinks: a lone publish, where an unresolved link rejects instead of pending.
|
|
105
|
+
// purged: names a registry purged, whose links warn instead of rejecting.
|
|
106
|
+
export async function loadContext(
|
|
107
|
+
collection,
|
|
108
|
+
{ published = null, available = published, foundation, fixtures, registry = null, sharedTokens, memo = null, strictLinks = false, purged = new Set() } = {},
|
|
109
|
+
) {
|
|
110
|
+
if (typeof collection === 'string') collection = openCollection(collection);
|
|
111
|
+
const { root, system } = collection;
|
|
112
|
+
const catalog = new Map();
|
|
113
|
+
const duplicates = new Set();
|
|
114
|
+
for (const { dir, authored, installed } of collection.items) {
|
|
115
|
+
const item = loadItem(join(root, dir));
|
|
116
|
+
Object.assign(item, { rel: dir, authored, installed });
|
|
117
|
+
const name = item.manifest?.name;
|
|
118
|
+
if (!name) continue;
|
|
119
|
+
if (catalog.has(name)) duplicates.add(name);
|
|
120
|
+
else catalog.set(name, item);
|
|
121
|
+
}
|
|
122
|
+
const sources = tokenLayers(
|
|
123
|
+
[...catalog.values()].map((item) => ({
|
|
124
|
+
base: item.rel,
|
|
125
|
+
manifest: item.manifest,
|
|
126
|
+
json: (path) => {
|
|
127
|
+
if (!item.present.has(path)) return null;
|
|
128
|
+
try {
|
|
129
|
+
return JSON.parse(readFileSync(join(item.dir, path), 'utf8'));
|
|
130
|
+
} catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
})),
|
|
135
|
+
sharedTokens ? { shared: sharedTokens } : {},
|
|
136
|
+
);
|
|
137
|
+
// A check page renders over the whole bundle, so it sees every layer.
|
|
138
|
+
const names = (files) => new Set(files.flatMap(({ json }) => [...tokenNames(json)]));
|
|
139
|
+
const tokens = names(sources.categories);
|
|
140
|
+
const everyToken = names([...sources.categories, ...sources.components]);
|
|
141
|
+
// A partial checkout may not build; a broken component layer falls back to the shared layers, else none.
|
|
142
|
+
const literals = await memoLiterals(memo, sources, () =>
|
|
143
|
+
tokenLiterals(sources)
|
|
144
|
+
.catch(() => tokenLiterals({ categories: sources.categories, components: [] }))
|
|
145
|
+
.catch(() => null),
|
|
146
|
+
);
|
|
147
|
+
const cache = new Map();
|
|
148
|
+
// A failed read is never kept.
|
|
149
|
+
const keptFacts = (item) => {
|
|
150
|
+
const stamp = memo?.stamp(item) ?? null;
|
|
151
|
+
if (stamp === null) return itemFacts(item);
|
|
152
|
+
const kept = memo.facts.get(item.dir);
|
|
153
|
+
if (kept?.stamp === stamp) return kept.facts;
|
|
154
|
+
const facts = itemFacts(item);
|
|
155
|
+
memo.facts.set(item.dir, { stamp, facts });
|
|
156
|
+
facts.catch(() => memo.facts.get(item.dir)?.facts === facts && memo.facts.delete(item.dir));
|
|
157
|
+
return facts;
|
|
158
|
+
};
|
|
159
|
+
return {
|
|
160
|
+
root,
|
|
161
|
+
namespaces: new Set(collection.config?.namespaces ?? []),
|
|
162
|
+
foundation: foundation ?? foundationOf(collection),
|
|
163
|
+
fixtures: fixtures !== undefined ? fixtures : (system?.fixtures ?? null),
|
|
164
|
+
catalog,
|
|
165
|
+
duplicates,
|
|
166
|
+
tokens,
|
|
167
|
+
everyToken,
|
|
168
|
+
literals,
|
|
169
|
+
published,
|
|
170
|
+
available,
|
|
171
|
+
registry,
|
|
172
|
+
strictLinks,
|
|
173
|
+
purged,
|
|
174
|
+
facts(item) {
|
|
175
|
+
if (!cache.has(item.dir)) cache.set(item.dir, keptFacts(item));
|
|
176
|
+
return cache.get(item.dir);
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function stateItem(ctx, name) {
|
|
182
|
+
return ctx.catalog.get(name) ?? ctx.registry?.get(name) ?? null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// The deepest folder wins.
|
|
186
|
+
export function holderOf(ctx, path) {
|
|
187
|
+
let found = null;
|
|
188
|
+
for (const member of ctx.catalog.values()) {
|
|
189
|
+
const rel = member.rel;
|
|
190
|
+
if (typeof rel !== 'string' || !path.startsWith(`${rel}/`)) continue;
|
|
191
|
+
if (!found || rel.length > found.member.rel.length) found = { member, file: path.slice(rel.length + 1) };
|
|
192
|
+
}
|
|
193
|
+
return found;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// standard/templates/homepage/homepage.md parses as a file of the index standard/templates, yet sits in another item's folder.
|
|
197
|
+
export function repoPathTarget(ctx, target) {
|
|
198
|
+
if (target.file === null) return false;
|
|
199
|
+
const holder = holderOf(ctx, `${target.name}/${target.file}`);
|
|
200
|
+
return holder !== null && holder.member.manifest?.name !== target.name;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Read from manifests, never disk, since at ingest the published state holds no raster rendition.
|
|
204
|
+
const folderCache = new WeakMap();
|
|
205
|
+
export function fixtureMembers(ctx) {
|
|
206
|
+
if (!folderCache.has(ctx.catalog)) folderCache.set(ctx.catalog, new Map());
|
|
207
|
+
const byCollection = folderCache.get(ctx.catalog);
|
|
208
|
+
if (!byCollection.has(ctx.fixtures)) {
|
|
209
|
+
const items = new Map([...ctx.catalog].map(([n, i]) => [n, { dir: i.rel, manifest: i.manifest }]));
|
|
210
|
+
byCollection.set(ctx.fixtures, fixtureFolders(ctx.catalog.get(ctx.fixtures)?.manifest ?? null, items));
|
|
211
|
+
}
|
|
212
|
+
return byCollection.get(ctx.fixtures);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function relatedNames(item) {
|
|
216
|
+
return [...new Set((item.manifest?.related ?? []).flatMap((r) => r.load ?? []).map((l) => l.item))];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// related takes in the items related escalations load, since an example may compose one.
|
|
220
|
+
export function dependencySet(item, ctx, { related = false } = {}) {
|
|
221
|
+
const name = item.manifest?.name;
|
|
222
|
+
const manifests = new Map([...ctx.catalog].map(([n, i]) => [n, i.manifest]));
|
|
223
|
+
const pool = new Map(ctx.catalog);
|
|
224
|
+
if (name) {
|
|
225
|
+
manifests.set(name, item.manifest);
|
|
226
|
+
pool.set(name, item);
|
|
227
|
+
}
|
|
228
|
+
const known = (m) => ({ ...m, dependencies: Object.fromEntries(Object.entries(m.dependencies ?? {}).filter(([d]) => manifests.has(d))) });
|
|
229
|
+
for (const [n, m] of manifests) manifests.set(n, known(m));
|
|
230
|
+
const names = name ? [name] : [];
|
|
231
|
+
if (related) for (const target of relatedNames(item)) if (manifests.has(target) && !names.includes(target)) names.push(target);
|
|
232
|
+
let order;
|
|
233
|
+
try {
|
|
234
|
+
order = resolve(names, manifests, { foundation: ctx.foundation });
|
|
235
|
+
} catch {
|
|
236
|
+
order = [...ctx.foundation, ...names];
|
|
237
|
+
}
|
|
238
|
+
return order.map((n) => pool.get(n)).filter(Boolean);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// A check page renders over the whole bundle, so it may use any of these.
|
|
242
|
+
const allClasses = new WeakMap();
|
|
243
|
+
export function catalogClasses(ctx) {
|
|
244
|
+
if (!allClasses.has(ctx.catalog)) {
|
|
245
|
+
allClasses.set(
|
|
246
|
+
ctx.catalog,
|
|
247
|
+
(async () => {
|
|
248
|
+
const classes = new Set();
|
|
249
|
+
for (const member of ctx.catalog.values()) {
|
|
250
|
+
if (!member.manifest) continue;
|
|
251
|
+
for (const css of (await ctx.facts(member)).css.values()) for (const c of css.classes) classes.add(c);
|
|
252
|
+
}
|
|
253
|
+
return classes;
|
|
254
|
+
})(),
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
return allClasses.get(ctx.catalog);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function partParents(item, ctx) {
|
|
261
|
+
const out = [];
|
|
262
|
+
const seen = new Set([item.manifest?.name]);
|
|
263
|
+
let at = item.manifest?.partOf;
|
|
264
|
+
while (typeof at === 'string' && !seen.has(at)) {
|
|
265
|
+
seen.add(at);
|
|
266
|
+
const parent = stateItem(ctx, at);
|
|
267
|
+
if (!parent?.manifest) break;
|
|
268
|
+
out.push(parent);
|
|
269
|
+
at = parent.manifest.partOf;
|
|
270
|
+
}
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// A family's first publish sends each part ahead of its parent, whose knobs aren't readable yet.
|
|
275
|
+
export function pendingParent(item, ctx) {
|
|
276
|
+
const seen = new Set([item.manifest?.name]);
|
|
277
|
+
let at = item.manifest?.partOf;
|
|
278
|
+
while (typeof at === 'string' && !seen.has(at)) {
|
|
279
|
+
seen.add(at);
|
|
280
|
+
const parent = stateItem(ctx, at);
|
|
281
|
+
if (!parent?.manifest) return at;
|
|
282
|
+
at = parent.manifest.partOf;
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function definitionScope(item, ctx, { related = false } = {}) {
|
|
288
|
+
const members = new Set(dependencySet(item, ctx, { related }));
|
|
289
|
+
for (const parent of partParents(item, ctx)) for (const member of dependencySet(parent, ctx)) members.add(member);
|
|
290
|
+
return members;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Takes in each item whose definitionScope reaches this one, such as a part reading its parent's knobs.
|
|
294
|
+
const dependents = new WeakMap();
|
|
295
|
+
export function readerScope(item, ctx) {
|
|
296
|
+
if (!dependents.has(ctx.catalog)) {
|
|
297
|
+
const byName = new Map();
|
|
298
|
+
for (const member of ctx.catalog.values()) {
|
|
299
|
+
if (!member.manifest) continue;
|
|
300
|
+
for (const reached of definitionScope(member, ctx, { related: true })) {
|
|
301
|
+
const name = reached.manifest?.name;
|
|
302
|
+
if (!byName.has(name)) byName.set(name, new Set());
|
|
303
|
+
byName.get(name).add(member);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
dependents.set(ctx.catalog, byName);
|
|
307
|
+
}
|
|
308
|
+
const name = item.manifest?.name;
|
|
309
|
+
const readers = definitionScope(item, ctx, { related: true });
|
|
310
|
+
for (const member of dependents.get(ctx.catalog).get(name) ?? []) if (member.manifest.name !== name) readers.add(member);
|
|
311
|
+
readers.add(item);
|
|
312
|
+
return readers;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// A var() with a fallback defines its name as a knob.
|
|
316
|
+
export async function definitions(item, ctx, { related = false } = {}) {
|
|
317
|
+
const classes = new Set();
|
|
318
|
+
const properties = new Set(ctx.tokens);
|
|
319
|
+
for (const member of definitionScope(item, ctx, { related })) {
|
|
320
|
+
const facts = await ctx.facts(member);
|
|
321
|
+
for (const css of facts.css.values()) {
|
|
322
|
+
for (const c of css.classes) classes.add(c);
|
|
323
|
+
for (const d of css.customDecls) properties.add(d.name);
|
|
324
|
+
for (const r of css.varRefs) if (r.fallback) properties.add(r.name);
|
|
325
|
+
}
|
|
326
|
+
for (const f of declaredFiles(member.manifest)) {
|
|
327
|
+
if (f.role !== 'tokens' || !member.present.has(f.path)) continue;
|
|
328
|
+
try {
|
|
329
|
+
for (const n of tokenNames(JSON.parse(readFileSync(join(member.dir, f.path), 'utf8')))) properties.add(n);
|
|
330
|
+
} catch {
|
|
331
|
+
// The files check reports it.
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return { classes, properties };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const ownersCache = new WeakMap();
|
|
339
|
+
export function interfaceOwners(ctx, scope) {
|
|
340
|
+
if (!ownersCache.has(ctx.catalog)) {
|
|
341
|
+
const interfaces = [];
|
|
342
|
+
for (const [name, member] of ctx.catalog) {
|
|
343
|
+
if (!member.manifest) continue;
|
|
344
|
+
const path = structuredFiles(member.manifest).interface;
|
|
345
|
+
if (!path || !member.present.has(path)) continue;
|
|
346
|
+
try {
|
|
347
|
+
interfaces.push(interfaceEntry(name, member.manifest, JSON.parse(readFileSync(join(member.dir, path), 'utf8'))));
|
|
348
|
+
} catch {
|
|
349
|
+
// The files check reports it.
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
ownersCache.set(ctx.catalog, { interfaces, byScope: new Map() });
|
|
353
|
+
}
|
|
354
|
+
const cached = ownersCache.get(ctx.catalog);
|
|
355
|
+
if (!cached.byScope.has(scope)) cached.byScope.set(scope, surfaceOwners(interfacesFor(cached.interfaces, scope)));
|
|
356
|
+
return cached.byScope.get(scope);
|
|
357
|
+
}
|