@cagov/ca-ds 1.0.0 → 1.2.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.
Files changed (101) hide show
  1. package/README.md +43 -26
  2. package/THIRD-PARTY-LICENSES.txt +690 -0
  3. package/ca-ds.js +48390 -0
  4. package/package.json +5 -31
  5. package/bin/ca-ds.js +0 -5
  6. package/src/cli/add.js +0 -134
  7. package/src/cli/build.js +0 -15
  8. package/src/cli/diff.js +0 -39
  9. package/src/cli/eject.js +0 -112
  10. package/src/cli/exit.js +0 -22
  11. package/src/cli/format.js +0 -52
  12. package/src/cli/init.js +0 -148
  13. package/src/cli/install.js +0 -178
  14. package/src/cli/items.js +0 -10
  15. package/src/cli/login.js +0 -331
  16. package/src/cli/main.js +0 -108
  17. package/src/cli/new.js +0 -230
  18. package/src/cli/outdated.js +0 -74
  19. package/src/cli/pack.js +0 -75
  20. package/src/cli/preview.js +0 -31
  21. package/src/cli/publish.js +0 -295
  22. package/src/cli/pull.js +0 -208
  23. package/src/cli/remote.js +0 -52
  24. package/src/cli/remove.js +0 -69
  25. package/src/cli/update.js +0 -223
  26. package/src/cli/validate.js +0 -131
  27. package/src/lib/browser.js +0 -12
  28. package/src/lib/build.js +0 -249
  29. package/src/lib/bundle.js +0 -148
  30. package/src/lib/catalog.js +0 -44
  31. package/src/lib/checks.js +0 -31
  32. package/src/lib/collection.js +0 -65
  33. package/src/lib/emblem.js +0 -29
  34. package/src/lib/fixtures.js +0 -59
  35. package/src/lib/floor.js +0 -79
  36. package/src/lib/glyph.js +0 -148
  37. package/src/lib/image.js +0 -138
  38. package/src/lib/json-text.js +0 -213
  39. package/src/lib/keys.js +0 -322
  40. package/src/lib/links.js +0 -321
  41. package/src/lib/markdown.js +0 -408
  42. package/src/lib/merge.js +0 -93
  43. package/src/lib/pack.js +0 -30
  44. package/src/lib/page-schema.js +0 -368
  45. package/src/lib/page.js +0 -372
  46. package/src/lib/patch.js +0 -9
  47. package/src/lib/regions.js +0 -173
  48. package/src/lib/registry.js +0 -371
  49. package/src/lib/release.js +0 -119
  50. package/src/lib/repoints.js +0 -120
  51. package/src/lib/resolve.js +0 -32
  52. package/src/lib/schema.js +0 -28
  53. package/src/lib/serial.js +0 -4
  54. package/src/lib/serialize.js +0 -444
  55. package/src/lib/structured.js +0 -72
  56. package/src/lib/system.js +0 -13
  57. package/src/lib/targets.js +0 -85
  58. package/src/lib/terms.js +0 -11
  59. package/src/lib/token-view.js +0 -289
  60. package/src/lib/tokens.js +0 -247
  61. package/src/lib/version.js +0 -34
  62. package/src/preview/frame.js +0 -75
  63. package/src/preview/pages.js +0 -128
  64. package/src/preview/screenshots.js +0 -72
  65. package/src/preview/server.js +0 -378
  66. package/src/preview/shell.js +0 -153
  67. package/src/schema/a11y.schema.json +0 -56
  68. package/src/schema/collection.lock.schema.json +0 -58
  69. package/src/schema/collection.schema.json +0 -65
  70. package/src/schema/composition.schema.json +0 -98
  71. package/src/schema/contract.schema.json +0 -116
  72. package/src/schema/interface.schema.json +0 -257
  73. package/src/schema/item.schema.json +0 -362
  74. package/src/schema/release.schema.json +0 -39
  75. package/src/schema/system.schema.json +0 -75
  76. package/src/schema/variants.schema.json +0 -60
  77. package/src/validate/collection.js +0 -19
  78. package/src/validate/collisions.js +0 -124
  79. package/src/validate/context.js +0 -357
  80. package/src/validate/facts.js +0 -253
  81. package/src/validate/files.js +0 -103
  82. package/src/validate/findings.js +0 -19
  83. package/src/validate/glyphs.js +0 -126
  84. package/src/validate/html.js +0 -215
  85. package/src/validate/imports.js +0 -29
  86. package/src/validate/index.js +0 -75
  87. package/src/validate/item.js +0 -47
  88. package/src/validate/links.js +0 -113
  89. package/src/validate/manifest.js +0 -147
  90. package/src/validate/media.js +0 -65
  91. package/src/validate/page.js +0 -74
  92. package/src/validate/prefixes.js +0 -58
  93. package/src/validate/relations.js +0 -310
  94. package/src/validate/release.js +0 -109
  95. package/src/validate/skill.js +0 -122
  96. package/src/validate/structured.js +0 -335
  97. package/src/validate/tasc.js +0 -80
  98. package/src/validate/template.js +0 -129
  99. package/src/validate/tokens.js +0 -154
  100. package/src/validate/zip.js +0 -40
  101. /package/{src/preview/preview.css → preview.css} +0 -0
@@ -1,215 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import { readFile } from 'node:fs/promises';
3
- import { join } from 'node:path';
4
- import { fileURLToPath } from 'node:url';
5
- import { catalogClasses, definitions, fixtureMembers, stateItem } from './context.js';
6
- import { cssFacts, walkHtml } from './facts.js';
7
- import { declaredFiles, CHECKS } from './item.js';
8
- import { mediaTarget } from '../lib/fixtures.js';
9
- import { EMBLEM, emblemDiff, masterPaths, masterRendition } from '../lib/emblem.js';
10
- import { NODES } from '../lib/page-schema.js';
11
-
12
- export const check = 'html';
13
-
14
- const SITE_CLASSES = Object.values(NODES)
15
- .filter((n) => n.site)
16
- .flatMap((n) => [n.match, ...(n.classes ?? []).flat()])
17
- .filter(Boolean);
18
-
19
- // The previewer's demo chrome, which only a check page may use.
20
- const PREVIEW = cssFacts(readFileSync(fileURLToPath(new URL('../preview/preview.css', import.meta.url)), 'utf8')).classes;
21
-
22
- // null for an unreadable master; undefined when the state lacks the logo item, so a copy goes unchecked.
23
- const masters = new WeakMap();
24
- function emblemPaths(ctx) {
25
- if (!masters.has(ctx.catalog)) {
26
- const item = stateItem(ctx, EMBLEM.item);
27
- const rendition = masterRendition(item?.manifest);
28
- const paths = !item
29
- ? Promise.resolve(undefined)
30
- : rendition
31
- ? readFile(join(item.dir, rendition.file), 'utf8').then(
32
- masterPaths,
33
- () => null,
34
- )
35
- : Promise.resolve(null);
36
- masters.set(ctx.catalog, paths);
37
- }
38
- return masters.get(ctx.catalog);
39
- }
40
-
41
- function titleOf(node) {
42
- const title = (node.childNodes ?? []).find((c) => c.tagName === 'title');
43
- return (title?.childNodes ?? []).map((c) => c.value ?? '').join('').trim().toLowerCase();
44
- }
45
-
46
- function pathData(node) {
47
- const data = [];
48
- for (const { node: child } of walkHtml(node)) {
49
- if (child.tagName !== 'path') continue;
50
- const d = (child.attrs ?? []).find((a) => a.name === 'd');
51
- if (d) data.push(d.value);
52
- }
53
- return data;
54
- }
55
-
56
- const TEXT = [
57
- [/\{\{[\s\S]*?\}\}/, 'a {{ }} expression'],
58
- [/\{%[\s\S]*?%\}/, 'a {% %} tag'],
59
- [/<%[\s\S]*?%>/, 'a <% %> block'],
60
- [/(^|[^\w@.])@(foreach|for|if|else|while|switch|using|model|inject|section|code|functions|Html\.|\{|\()/, 'a Razor @ directive'],
61
- [/\{[#/:@](if|each|else|await|html|key|const|debug)\b/, 'a Svelte block'],
62
- ];
63
-
64
- // parse5 lowercases attribute names.
65
- const ATTRIBUTE = [
66
- [/^v-/, 'a Vue directive'],
67
- [/^[:@#]/, 'a Vue shorthand binding'],
68
- [/^\*ng|^\[.*\]$|^\(.*\)$/, 'an Angular binding'],
69
- [/^ng-/, 'an AngularJS directive'],
70
- [/^x-(data|bind|on|if|for|show|model|text|html|init|ref|effect|transition|cloak)\b/, 'an Alpine directive'],
71
- [/^(on|bind|use|transition|animate|class|style):/, 'a Svelte directive'],
72
- ];
73
-
74
- const CANDIDATES = new Set(['srcset', 'imagesrcset']);
75
- const CSS_URL = /url\(\s*(['"]?)([^'")\s]+)\1\s*\)/g;
76
-
77
- const cssUrls = (css) => [...css.matchAll(CSS_URL)].map((m) => m[2]);
78
-
79
- function attributeUrls(attr) {
80
- if (CANDIDATES.has(attr.name)) return attr.value.split(',').map((c) => c.trim().split(/\s+/)[0]);
81
- if (attr.name === 'style') return cssUrls(attr.value);
82
- return [attr.value.trim()];
83
- }
84
-
85
- const attrOf = (node, name) => (node.attrs ?? []).find((a) => a.name === name)?.value;
86
-
87
- function named(node, key) {
88
- if (!node.tagName) return false;
89
- if (!key.startsWith('.')) return node.tagName === key;
90
- return (attrOf(node, 'class') ?? '').split(/\s+/).includes(key.slice(1));
91
- }
92
-
93
- const carries = (node, want) =>
94
- Object.entries(want).every(([name, value]) => {
95
- const has = attrOf(node, name);
96
- return value === true ? has !== undefined : has === value;
97
- });
98
-
99
- function shows(item, facts, reject) {
100
- for (const f of declaredFiles(item.manifest)) {
101
- const html = facts.html.get(f.path);
102
- if (f.role !== 'reference-html' || !html || typeof f.shows !== 'object' || f.shows === null) continue;
103
- for (const [key, want] of Object.entries(f.shows)) {
104
- if (typeof want !== 'object' || want === null) continue;
105
- let found = false;
106
- for (const { node } of walkHtml(html.tree)) if ((found = named(node, key) && carries(node, want))) break;
107
- if (found) continue;
108
- const attrs = Object.entries(want).map(([n, v]) => (v === true ? n : `${n}="${v}"`));
109
- const what = key.startsWith('.') ? `an element with the class \`${key}\`` : `a \`<${key}>\``;
110
- reject('shows', `The manifest says the example shows ${what}${attrs.length ? ` carrying \`${attrs.join(' ')}\`` : ''}, and the file holds none.`, { file: f.path });
111
- }
112
- }
113
- }
114
-
115
- export async function run(item, ctx, { reject, warn }) {
116
- if (!item.manifest) return;
117
- const facts = await ctx.facts(item);
118
- shows(item, facts, reject);
119
- const { classes } = await definitions(item, ctx, { related: true });
120
- const everywhere = await catalogClasses(ctx);
121
- const members = fixtureMembers(ctx);
122
- const deps = item.manifest.dependencies ?? {};
123
- const named = (url) => {
124
- const target = mediaTarget(url);
125
- if (target?.kind === 'fixture') return Boolean(members.get(target.dir)?.files.has(target.file));
126
- if (target?.kind !== 'item' || typeof deps !== 'object' || !Object.hasOwn(deps, target.name)) return false;
127
- const leaf = stateItem(ctx, target.name)?.manifest;
128
- return leaf?.kind === 'media' && !leaf.members && (leaf.files ?? []).some((f) => f.path === target.file);
129
- };
130
- const master = await emblemPaths(ctx);
131
-
132
- for (const [path, html] of facts.html) {
133
- for (const e of html.errors) reject('parse', `The HTML fails to parse: ${e.code}.`, { file: path, line: e.line });
134
-
135
- const local = new Set(html.styles.filter((s) => !s.attribute).flatMap((s) => [...s.classes]));
136
- if (path.startsWith(`${CHECKS}/`)) for (const name of [...PREVIEW, ...everywhere]) local.add(name);
137
- if (item.manifest.kind === 'page' && path === item.manifest.doc) for (const name of SITE_CLASSES) local.add(name);
138
- const unknown = new Map();
139
- const unlinked = new Map();
140
- let unchecked = false;
141
- const media = (urls, line) => {
142
- for (const url of urls) {
143
- if (!url.startsWith('/media/') || named(url)) continue;
144
- if (!unlinked.has(url)) unlinked.set(url, { line, count: 0 });
145
- unlinked.get(url).count++;
146
- }
147
- };
148
- for (const { node, ancestors } of walkHtml(html.tree)) {
149
- const line = node.sourceCodeLocation?.startLine ?? null;
150
- const inCode = ancestors.some((a) => ['script', 'style'].includes(a.tagName));
151
- if (node.nodeName === '#text' && ancestors.at(-1)?.tagName === 'style') media(cssUrls(node.value), line);
152
- if (node.nodeName === '#text' && !inCode) {
153
- for (const [pattern, what] of TEXT) {
154
- if (pattern.test(node.value)) reject('template-syntax', `The text carries ${what}. Reference HTML is static markup.`, { file: path, line });
155
- }
156
- }
157
- if (master === undefined && node.tagName === 'svg' && titleOf(node) === EMBLEM.title && !unchecked) {
158
- unchecked = true;
159
- warn(
160
- 'emblem-unchecked',
161
- `The inline \`<svg>\` titled \`${EMBLEM.title}\` copies the CA emblem, and \`${EMBLEM.item}\`, which owns its path data, is in neither the collection nor the registry, so the copy goes unchecked.`,
162
- { file: path, line },
163
- );
164
- }
165
- if (master && node.tagName === 'svg' && titleOf(node) === EMBLEM.title) {
166
- const diff = emblemDiff(master, pathData(node));
167
- if (diff) {
168
- reject(
169
- 'emblem',
170
- `The inline \`<svg>\` titled \`${EMBLEM.title}\` is a copy of \`${EMBLEM.item}\`'s \`${EMBLEM.variant}\` rendition, which owns the emblem's path data. ${diff}`,
171
- { file: path, line },
172
- );
173
- }
174
- }
175
- if (node.nodeName === '#comment' && /^\?php/i.test(node.data)) {
176
- reject('template-syntax', 'The file carries a PHP block. Reference HTML is static markup.', { file: path, line });
177
- }
178
- for (const attr of node.attrs ?? []) {
179
- for (const [pattern, what] of ATTRIBUTE) {
180
- if (pattern.test(attr.name)) reject('template-syntax', `The attribute \`${attr.name}\` is ${what}. Reference HTML is static markup.`, { file: path, line });
181
- }
182
- for (const [pattern, what] of TEXT) {
183
- if (pattern.test(attr.value)) reject('template-syntax', `The \`${attr.name}\` attribute's value carries ${what}. Reference HTML is static markup.`, { file: path, line });
184
- }
185
- media(attributeUrls(attr), line);
186
- if (attr.name === 'class') {
187
- for (const name of attr.value.split(/\s+/).filter(Boolean)) {
188
- if (classes.has(name) || local.has(name)) continue;
189
- if (!unknown.has(name)) unknown.set(name, { line, count: 0 });
190
- unknown.get(name).count++;
191
- }
192
- }
193
- }
194
- }
195
- for (const [name, { line, count }] of unknown) {
196
- const times = count > 1 ? ` (${count} uses)` : '';
197
- warn('undefined-class', `The class \`.${name}\`${times} is defined by no item this one is validated with.`, { file: path, line });
198
- }
199
- for (const [url, { line, count }] of unlinked) {
200
- const times = count > 1 ? ` (${count} uses)` : '';
201
- const target = mediaTarget(url);
202
- const message =
203
- target?.kind === 'item' && ctx.purged?.has(target.name)
204
- ? `The link \`${url}\`${times} names a file of \`${target.name}\`, which the registry purged, so the image shows nothing. Remove the image, or place another.`
205
- : target?.kind === 'item'
206
- ? `The link \`${url}\`${times} names a file of \`${target.name}\`, which \`dependencies\` leaves out, or which is no media leaf declaring that file. A published media leaf's link is \`/media/<scope>/<name>/<file>\`, where the item depends on the leaf and the leaf declares the file.`
207
- : target?.kind === 'draft'
208
- ? `The link \`${url}\`${times} names a file uploaded to a draft, which only the draft's own views answer. A publish rewrites it to the path of the media leaf it publishes.`
209
- : ctx.fixtures === null
210
- ? `The link \`${url}\`${times} names no fixtures file, since system.json names no fixtures collection.`
211
- : `The link \`${url}\`${times} names no file of a member of \`${ctx.fixtures}\`, the fixtures collection. A /media/ link is \`/media/<folder>/<file>\`, where the member in that folder declares the file.`;
212
- warn('media-link', message, { file: path, line });
213
- }
214
- }
215
- }
@@ -1,29 +0,0 @@
1
- import { posix } from 'node:path';
2
- import { declaredFiles } from './item.js';
3
-
4
- export const check = 'imports';
5
-
6
- export async function run(item, ctx, { reject, warn }) {
7
- if (!item.manifest) return;
8
- const facts = await ctx.facts(item);
9
- const declared = new Set(declaredFiles(item.manifest).map((f) => f.path));
10
- for (const [path, js] of facts.js) {
11
- if (js.error) {
12
- reject('parse', `The script fails to parse: ${js.error.message}`, { file: path, line: js.error.line });
13
- continue;
14
- }
15
- for (const { specifier, kind, line } of js.imports) {
16
- if (!/^\.\.?\//.test(specifier)) {
17
- reject('bare', `\`${specifier}\` is not a relative import. An item imports only its own files, by a path that starts with \`./\` or \`../\`.`, { file: path, line });
18
- continue;
19
- }
20
- const target = posix.normalize(posix.join(posix.dirname(path), specifier));
21
- if (target.startsWith('../')) {
22
- reject('outside', `\`${specifier}\` reaches outside the item folder.`, { file: path, line });
23
- } else if (!declared.has(target)) {
24
- reject('outside', `\`${specifier}\` resolves to \`${target}\`, which the item does not declare.`, { file: path, line });
25
- }
26
- if (kind === 'require-call') warn('require', `\`${specifier}\` is loaded with \`require()\`. The reference implementation is ES modules.`, { file: path, line });
27
- }
28
- }
29
- }
@@ -1,75 +0,0 @@
1
- import { relative, sep } from 'node:path';
2
- import { recorder } from './findings.js';
3
- import { loadContext } from './context.js';
4
- import { loadItem } from './item.js';
5
- import * as manifest from './manifest.js';
6
- import * as collection from './collection.js';
7
- import * as files from './files.js';
8
- import * as media from './media.js';
9
- import * as tokens from './tokens.js';
10
- import * as glyphs from './glyphs.js';
11
- import * as prefixes from './prefixes.js';
12
- import * as imports from './imports.js';
13
- import * as html from './html.js';
14
- import * as tasc from './tasc.js';
15
- import * as structured from './structured.js';
16
- import * as links from './links.js';
17
- import * as relations from './relations.js';
18
- import * as collisions from './collisions.js';
19
- import * as template from './template.js';
20
- import * as page from './page.js';
21
- import * as skill from './skill.js';
22
-
23
- export { loadContext, loadItem };
24
-
25
- export const GROUPS = [manifest, collection, files, media, tokens, glyphs, prefixes, imports, html, tasc, structured, links, relations, collisions, template, page, skill];
26
-
27
- export async function validate(item, ctx) {
28
- const findings = [];
29
- for (const group of GROUPS) {
30
- if (group !== manifest && !item.manifest) break;
31
- const record = recorder(findings, group.check);
32
- try {
33
- await group.run(item, ctx, record);
34
- } catch (err) {
35
- // A manifest the schema rejects can hold shapes a group can't read.
36
- record.reject('crash', `The ${group.check} checks could not run: ${err.message}`);
37
- }
38
- }
39
- return {
40
- name: item.manifest?.name ?? null,
41
- dir: item.rel ?? item.label ?? item.dir,
42
- rejections: findings.filter((f) => f.severity === 'reject'),
43
- warnings: findings.filter((f) => f.severity === 'warn'),
44
- };
45
- }
46
-
47
- export function itemAt(ctx, abs, label = abs) {
48
- const rel = relative(ctx.root, abs).split(sep).join('/');
49
- return [...ctx.catalog.values()].find((i) => i.rel === rel) ?? Object.assign(loadItem(abs), { label });
50
- }
51
-
52
- export async function validateRepo(collection, options) {
53
- const ctx = await loadContext(collection, options);
54
- const results = [];
55
- for (const item of ctx.catalog.values()) results.push(await validate(item, ctx));
56
- return results.sort((a, b) => a.dir.localeCompare(b.dir));
57
- }
58
-
59
- // releases are validateRelease reports, counted beside the items.
60
- export function summarize(results, releases = []) {
61
- const tally = (list) => {
62
- const by = {};
63
- for (const f of list) by[`${f.check}/${f.rule}`] = (by[`${f.check}/${f.rule}`] ?? 0) + 1;
64
- return Object.fromEntries(Object.entries(by).sort());
65
- };
66
- const rejections = [...releases, ...results].flatMap((r) => r.rejections);
67
- const warnings = [...releases, ...results].flatMap((r) => r.warnings);
68
- return {
69
- ...(releases.length > 0 ? { releases: releases.length } : {}),
70
- items: results.length,
71
- rejections: rejections.length,
72
- warnings: warnings.length,
73
- byRule: { rejections: tally(rejections), warnings: tally(warnings) },
74
- };
75
- }
@@ -1,47 +0,0 @@
1
- import { existsSync, readdirSync, readFileSync } from 'node:fs';
2
- import { join } from 'node:path';
3
- import { conflictLine } from '../lib/catalog.js';
4
-
5
- export { conflictLine };
6
-
7
- export function declaredFiles(manifest) {
8
- const files = Array.isArray(manifest?.files) ? manifest.files : [];
9
- return files.filter((f) => typeof f?.path === 'string');
10
- }
11
-
12
- export const CHECKS = 'checks';
13
-
14
- export const NOTES = 'notes.md';
15
-
16
- export function loadItem(dir) {
17
- const item = { dir, manifest: null, manifestError: null, present: new Set(), checks: new Set(), notes: new Set() };
18
- const manifestPath = join(dir, 'item.json');
19
- if (!existsSync(manifestPath)) {
20
- item.manifestError = 'The folder has no item.json.';
21
- } else {
22
- const text = readFileSync(manifestPath, 'utf8');
23
- try {
24
- item.manifest = JSON.parse(text);
25
- } catch (err) {
26
- const line = conflictLine(text);
27
- item.manifestError = line === null ? `item.json is not valid JSON: ${err.message}` : `item.json holds a git conflict marker at line ${line}. Resolve the conflict and remove the markers.`;
28
- }
29
- }
30
- const walk = (abs, rel) => {
31
- for (const entry of readdirSync(abs, { withFileTypes: true })) {
32
- if (entry.name.startsWith('.')) continue;
33
- const path = rel ? `${rel}/${entry.name}` : entry.name;
34
- if (entry.isDirectory()) {
35
- if (!existsSync(join(abs, entry.name, 'item.json'))) walk(join(abs, entry.name), path);
36
- } else if (path.startsWith(`${CHECKS}/`)) {
37
- item.checks.add(path);
38
- } else if (path === NOTES) {
39
- item.notes.add(path);
40
- } else if (path !== 'item.json') {
41
- item.present.add(path);
42
- }
43
- }
44
- };
45
- walk(dir, '');
46
- return item;
47
- }
@@ -1,113 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import { join } from 'node:path';
3
- import { itemPage, resolveTarget, linksOf, proseLinksOf, anchorCollisions, hasAnchor } from '../lib/links.js';
4
- import { SUFFIX } from '../lib/structured.js';
5
- import { SYSTEM_NAMESPACE } from '../lib/system.js';
6
- import { declaredFiles } from './item.js';
7
- import { stateItem, repoPathTarget } from './context.js';
8
-
9
- export const check = 'links';
10
-
11
- const LEGACY_REJECTS = true;
12
-
13
- const scopeOf = (name) => (typeof name === 'string' && name.includes('/') ? name.slice(0, name.indexOf('/')) : null);
14
-
15
- const read = (item) => (path) => {
16
- if (!item.present.has(path)) return null;
17
- try {
18
- return readFileSync(join(item.dir, path), 'utf8');
19
- } catch {
20
- return null;
21
- }
22
- };
23
-
24
- const pages = new WeakMap();
25
- function pageOfItem(item) {
26
- if (!pages.has(item)) pages.set(item, item.manifest ? itemPage(item.manifest, read(item)) : null);
27
- return pages.get(item);
28
- }
29
-
30
- const holdsWhole = (ctx, scope) => ctx.namespaces?.has(scope) || ctx.published !== null || ctx.registry !== null;
31
-
32
- const what = (r) => (r.missing === 'item' ? `no item the state holds` : r.missing === 'file' ? `\`${r.file}\`, a file \`${r.name}\` doesn't ship` : `an anchor ${r.file ? `\`${r.file}\` of \`${r.name}\`` : `\`${r.name}\`'s page`} doesn't carry`);
33
-
34
- export async function run(item, ctx, { reject, warn }) {
35
- const m = item.manifest;
36
- const own = typeof m.name === 'string' ? m.name : null;
37
- const scope = scopeOf(own);
38
- const page = pageOfItem(item);
39
- const lookup = (name) => {
40
- if (name === own) return page;
41
- const held = stateItem(ctx, name);
42
- return held ? pageOfItem(held) : null;
43
- };
44
- const severe = (target) => (target.scope === SYSTEM_NAMESPACE || target.scope === scope ? reject : warn);
45
- const legacy = LEGACY_REJECTS && scope === SYSTEM_NAMESPACE ? reject : warn;
46
-
47
- // self: the anchors a bare #fragment may name.
48
- const checkLinks = (file, links, self) => {
49
- const old = [];
50
- for (const l of links) {
51
- const at = { file, line: l.line };
52
- if (l.type === 'legacy') {
53
- old.push(l);
54
- continue;
55
- }
56
- if (l.self !== undefined) {
57
- if (!self || !hasAnchor(self, l.self)) reject('unresolved', `A link to \`#…\` names an anchor this doc doesn't carry.`, at);
58
- continue;
59
- }
60
- const r = resolveTarget(l.target, lookup);
61
- if (!r.missing) continue;
62
- // A repo path from the root parses as an index item's target.
63
- if (r.missing !== 'anchor' && repoPathTarget(ctx, l.target)) {
64
- old.push(l);
65
- continue;
66
- }
67
- // At ingest a batch publish sends items in either order, so a miss is pending. strictLinks marks a lone publish.
68
- if (ctx.published !== null && r.name !== own && !(ctx.strictLinks && severe(l.target) === reject)) {
69
- warn('pending', `A link to \`${r.name}\` names ${what(r)} yet, so it goes unchecked. A batch publish sends an item ahead of some item it links to.`, { ...at, needs: r.name });
70
- continue;
71
- }
72
- if (r.missing === 'item' && !holdsWhole(ctx, l.target.scope)) {
73
- warn('unchecked', `A link names \`${r.name}\`, which the collection lacks, so it goes unchecked. An online \`ca-ds validate\` asks the registry for it.`, { ...at, needs: r.name });
74
- continue;
75
- }
76
- const rule = r.missing === 'item' && severe(l.target) === warn ? 'missing' : 'unresolved';
77
- severe(l.target)(rule, `A link to \`${r.name}\` names ${what(r)}.`, at);
78
- }
79
- if (old.length > 0) {
80
- const first = old.find((l) => l.line !== null)?.line ?? null;
81
- legacy('legacy-pointer', `\`${file}\` holds ${old.length} legacy ${old.length === 1 ? 'pointer' : 'pointers'}, a repo path in a code span or a § *Title*${first ? `, the first at line ${first}` : ''}. Write each as a markdown link to the item's scoped name.`, { file, line: first });
82
- }
83
- };
84
-
85
- for (const f of declaredFiles(m)) {
86
- if (!item.present.has(f.path)) continue;
87
- if (f.path.endsWith('.md')) {
88
- const text = read(item)(f.path);
89
- if (text !== null) checkLinks(f.path, linksOf(text), page.fileAnchors(f.path));
90
- } else if (Object.hasOwn(SUFFIX, f.role) && f.path.endsWith(SUFFIX[f.role])) {
91
- let data;
92
- try {
93
- data = JSON.parse(read(item)(f.path));
94
- } catch {
95
- // The files check reports it.
96
- continue;
97
- }
98
- let links;
99
- try {
100
- links = proseLinksOf(f.role, data);
101
- } catch {
102
- // The structured check holds the file to its schema.
103
- continue;
104
- }
105
- checkLinks(f.path, links, page.anchors);
106
- }
107
- }
108
-
109
- for (const c of anchorCollisions(page.anchors)) {
110
- const kind = page.anchors.records.has(c.id) ? 'a record' : 'a section';
111
- reject('anchor-collision', `A heading of the doc takes the id of ${kind} of the ${c.role} file, so a link to either lands on one. Retitle the heading.`, { file: m.doc, line: c.line });
112
- }
113
- }
@@ -1,147 +0,0 @@
1
- import { posix } from 'node:path';
2
- import semver from 'semver';
3
- import { validateItem } from '../lib/schema.js';
4
- import { declaredFiles } from './item.js';
5
- import { SITE_NAMESPACE, SYSTEM_NAMESPACE } from '../lib/system.js';
6
-
7
- export const check = 'manifest';
8
-
9
- const SITE_FOLDER = `${SITE_NAMESPACE}/`;
10
-
11
- export async function run(item, ctx, { reject, warn }) {
12
- if (item.manifestError) {
13
- reject('schema', item.manifestError, { file: 'item.json' });
14
- return;
15
- }
16
- const m = item.manifest;
17
-
18
- const untasked = m.kind === 'page' || (m.kind === 'media' && m.members === undefined);
19
- if (!untasked && (!Array.isArray(m.tasks) || m.tasks.length === 0)) {
20
- reject('tasks', 'The item declares no tasks, so no task phrase routes an agent to it.', { file: 'item.json' });
21
- }
22
- if (!validateItem(m)) {
23
- const conditions = new Set();
24
- for (const e of validateItem.errors) {
25
- if (e.instancePath === '/tasks' || (e.keyword === 'required' && e.params.missingProperty === 'tasks')) continue;
26
- // A conditional rule's errors read "must NOT be valid", so its description reports once instead.
27
- // An error under a $ref indexes from that definition's root, hence FILE_CONDITIONS.
28
- const index = e.schemaPath.match(/^#\/allOf\/(\d+)\//)?.[1];
29
- const rule = (/^\/files\/\d+$/.test(e.instancePath) ? FILE_CONDITIONS : CONDITIONS)[index];
30
- // Reported above.
31
- if (rule === TASKS_RULE) continue;
32
- if (rule) {
33
- if (!conditions.has(rule)) reject('schema', rule, { file: 'item.json' });
34
- conditions.add(rule);
35
- continue;
36
- }
37
- const at = e.instancePath || '(root)';
38
- reject('schema', `${at} ${e.message}${e.params?.additionalProperty ? `: ${e.params.additionalProperty}` : ''}.`, { file: 'item.json' });
39
- }
40
- }
41
-
42
- const declared = new Set();
43
- for (const f of declaredFiles(m)) {
44
- if (declared.has(f.path)) warn('duplicate-file', `\`${f.path}\` is declared twice.`, { file: 'item.json' });
45
- declared.add(f.path);
46
- // A declared notes.md is the structured group's rejection.
47
- if (!item.present.has(f.path) && !item.notes?.has(f.path)) reject('missing-file', `\`${f.path}\` is declared and missing from the folder.`, { file: 'item.json' });
48
- }
49
- if (typeof m.doc === 'string' && !declared.has(m.doc)) {
50
- reject('missing-file', `The doc \`${m.doc}\` is not a declared file.`, { file: 'item.json' });
51
- }
52
- const renditions = m.kind === 'media' && Array.isArray(m.media?.renditions) ? m.media.renditions : [];
53
- for (const r of renditions) {
54
- if (typeof r?.file === 'string' && !declared.has(r.file)) {
55
- reject('missing-file', `The rendition \`${r.file}\` is not a declared file.`, { file: 'item.json' });
56
- }
57
- }
58
-
59
- const roles = new Map((Array.isArray(m.files) ? m.files : []).map((f) => [f?.path, f?.role]));
60
- for (const f of Array.isArray(m.files) ? m.files : []) {
61
- for (const p of [f?.image?.light, f?.image?.dark].filter((x) => typeof x === 'string')) {
62
- if (!declared.has(p)) reject('missing-file', `The example \`${f.path}\` names the image \`${p}\`, which is not a declared file.`, { file: 'item.json' });
63
- else if (roles.get(p) !== 'image') reject('example-image', `The example \`${f.path}\` names the image \`${p}\`, which is declared with role \`${roles.get(p) ?? 'none'}\`, not \`image\`.`, { file: 'item.json' });
64
- }
65
- }
66
-
67
- for (const [dep, range] of Object.entries(m.dependencies ?? {})) {
68
- const versions = publishedVersions(dep, ctx);
69
- if (versions.length === 0 && ctx.purged?.has(dep)) {
70
- warn('purged-media', `The dependency \`${dep}\` is an item the registry purged, so it loads nothing. Remove it from \`dependencies\`, and remove each link to its files.`, { file: 'item.json' });
71
- } else if (versions.length === 0) {
72
- const message = ctx.published?.has(dep)
73
- ? `Every published version of the dependency \`${dep}\` is yanked.`
74
- : `The dependency \`${dep}\` is no published item.`;
75
- reject('dependency', message, { file: 'item.json' });
76
- } else if (semver.validRange(range) && !versions.some((v) => semver.satisfies(v, range))) {
77
- reject('dependency', `No published version of \`${dep}\` satisfies \`${range}\`. Published: ${versions.join(', ')}.`, { file: 'item.json' });
78
- }
79
- if (ctx.foundation.includes(dep)) {
80
- warn('foundation-dependency', `\`${dep}\` is foundation, which every item loads after, so it is never a dependency.`, { file: 'item.json' });
81
- }
82
- }
83
-
84
- if (Array.isArray(m.members)) members(m, ctx, reject);
85
-
86
- const last = ctx.published?.get(m.name)?.filter((v) => semver.valid(v)).sort(semver.rcompare)[0];
87
- if (last && semver.valid(m.version) && !semver.gt(m.version, last)) {
88
- reject('version', `The version \`${m.version}\` is not higher than the last published version, \`${last}\`.`, { file: 'item.json' });
89
- }
90
-
91
- if (item.rel !== undefined && ctx.duplicates.has(m.name)) {
92
- reject('name', `Another item in the checkout is also named \`${m.name}\`.`, { file: 'item.json' });
93
- }
94
- // An installed item's source names its folder in its publisher's collection.
95
- if (item.rel !== undefined && !item.installed && m.source !== item.rel) {
96
- warn('source', `\`source\` is \`${m.source}\` and the folder is \`${item.rel}\`.`, { file: 'item.json' });
97
- }
98
- if (typeof m.name === 'string' && m.name.startsWith(`${SITE_NAMESPACE}/`) && !(typeof m.source === 'string' && m.source === posix.normalize(m.source) && m.source.startsWith(SITE_FOLDER))) {
99
- reject('source', `A \`${SITE_NAMESPACE}\` item's \`source\` sits under \`${SITE_FOLDER}\`, where ca-ds pull writes it in the repo, and \`source\` is ${JSON.stringify(m.source ?? null)}.`, { file: 'item.json' });
100
- }
101
-
102
- for (const target of (m.related ?? []).flatMap((r) => r.load ?? [])) {
103
- if (target.item !== m.name && !ctx.catalog.get(target.item)?.manifest) {
104
- warn('related', `The related item \`${target.item}\` is no published item.`, { file: 'item.json' });
105
- }
106
- }
107
- }
108
-
109
- const scopeOf = (name) => name.slice(0, name.indexOf('/'));
110
-
111
- const CONDITIONS = (validateItem.schema.allOf ?? []).map((c) => c.description ?? null);
112
- const FILE_CONDITIONS = (validateItem.schema.$defs?.file?.allOf ?? []).map((c) => c.description ?? null);
113
- const TASKS_RULE = 'Every kind but `page` carries `tasks`, and so does a media collection. A media leaf may carry none.';
114
-
115
- // The schema checks each member name's shape and that none repeats.
116
- function members(m, ctx, reject) {
117
- const names = m.members.filter((n) => typeof n === 'string' && n.includes('/'));
118
- const own = typeof m.name === 'string' && m.name.includes('/') ? scopeOf(m.name) : null;
119
- for (const member of names) {
120
- if (member === m.name) {
121
- reject('member', 'The item lists itself as a member.', { file: 'item.json' });
122
- continue;
123
- }
124
- if (!ctx.catalog.has(member)) reject('member', `The member \`${member}\` is no published item.`, { file: 'item.json' });
125
- const kind = ctx.catalog.get(member)?.manifest?.kind;
126
- if (m.kind === 'media' && kind !== undefined && kind !== 'media') {
127
- reject('member-kind', `The member \`${member}\` is kind \`${kind}\`. A media collection's members are media items.`, { file: 'item.json' });
128
- }
129
- const scope = scopeOf(member);
130
- if (own !== null && scope !== SYSTEM_NAMESPACE && scope !== own) {
131
- const holds = own === SYSTEM_NAMESPACE ? `\`${own}\` items alone` : `\`${own}\` items and \`${SYSTEM_NAMESPACE}\` ones`;
132
- reject('member-scope', `The member \`${member}\` is outside the item's scope. A \`${own}\` item's members are ${holds}.`, { file: 'item.json' });
133
- }
134
- if (Object.hasOwn(m.dependencies ?? {}, member)) {
135
- reject('member-dependency', `\`${member}\` is both a member and a dependency. A member is an item this one contains, and a dependency is one it loads after, so a name is one or the other.`, { file: 'item.json' });
136
- }
137
- }
138
- const at = names.findIndex((n, i) => i > 0 && names[i - 1] > n);
139
- if (at > 0) reject('member-order', `\`members\` is not sorted: \`${names[at]}\` follows \`${names[at - 1]}\`.`, { file: 'item.json' });
140
- }
141
-
142
- // The registry's unyanked versions, or the checkout's one version without a registry.
143
- function publishedVersions(name, ctx) {
144
- if (ctx.available?.has(name)) return ctx.available.get(name);
145
- const version = ctx.catalog.get(name)?.manifest.version;
146
- return version ? [version] : [];
147
- }