intent-format 0.0.0-stage → 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.
- package/.github/ISSUE_TEMPLATE/1-something-wrong.yml +18 -0
- package/.github/ISSUE_TEMPLATE/2-idea.yml +11 -0
- package/.github/ISSUE_TEMPLATE/config.yml +5 -0
- package/AGENTS.md +11 -0
- package/CHANGELOG.md +287 -0
- package/CLAUDE.md +1 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/CONTRIBUTING.md +19 -0
- package/LICENSE +21 -0
- package/README.md +71 -2
- package/SECURITY.md +10 -0
- package/VERSION +1 -0
- package/examples/example-app/AGENTS.md +19 -0
- package/examples/example-app/CLAUDE.md +1 -0
- package/examples/example-app/V1.0/apis/_index.md +14 -0
- package/examples/example-app/V1.0/apis/ap000001-list-items.md +20 -0
- package/examples/example-app/V1.0/apis/ap000002-save-item.md +28 -0
- package/examples/example-app/V1.0/backend/_index.md +15 -0
- package/examples/example-app/V1.0/backend/bn000001-app-server.md +24 -0
- package/examples/example-app/V1.0/backend/bn000002-main-database.md +22 -0
- package/examples/example-app/V1.0/backend/bn000003-file-storage.md +22 -0
- package/examples/example-app/V1.0/comments/_index.md +13 -0
- package/examples/example-app/V1.0/comments/cm000001-empty-list-message.md +15 -0
- package/examples/example-app/V1.0/components/_index.md +16 -0
- package/examples/example-app/V1.0/components/cp000001-button.md +32 -0
- package/examples/example-app/V1.0/components/cp000002-text-field.md +31 -0
- package/examples/example-app/V1.0/components/cp000003-item-row.md +34 -0
- package/examples/example-app/V1.0/components/cp000004-top-bar.md +26 -0
- package/examples/example-app/V1.0/decisions/_index.md +13 -0
- package/examples/example-app/V1.0/decisions/dc000001-where-do-item-images-live.md +26 -0
- package/examples/example-app/V1.0/edge-cases/_index.md +13 -0
- package/examples/example-app/V1.0/edge-cases/ec000001-save-fails.md +24 -0
- package/examples/example-app/V1.0/features/_index.md +17 -0
- package/examples/example-app/V1.0/features/ft000001-items.md +22 -0
- package/examples/example-app/V1.0/features/ft000002-account.md +21 -0
- package/examples/example-app/V1.0/features/ft000003-browse-items.md +26 -0
- package/examples/example-app/V1.0/features/ft000004-edit-an-item.md +33 -0
- package/examples/example-app/V1.0/features/ft000005-change-settings.md +21 -0
- package/examples/example-app/V1.0/ia/_index.md +13 -0
- package/examples/example-app/V1.0/ia/ia000001-main-map.md +23 -0
- package/examples/example-app/V1.0/intent-format.md +12 -0
- package/examples/example-app/V1.0/project.md +51 -0
- package/examples/example-app/V1.0/roles/_index.md +14 -0
- package/examples/example-app/V1.0/roles/ro000001-member.md +22 -0
- package/examples/example-app/V1.0/roles/ro000002-admin.md +19 -0
- package/examples/example-app/V1.0/rules/_index.md +13 -0
- package/examples/example-app/V1.0/rules/rl000001-item-name-is-required.md +20 -0
- package/examples/example-app/V1.0/screens/_index.md +17 -0
- package/examples/example-app/V1.0/screens/sc000001-home.md +18 -0
- package/examples/example-app/V1.0/screens/sc000002-item-list.md +43 -0
- package/examples/example-app/V1.0/screens/sc000003-item-detail.md +24 -0
- package/examples/example-app/V1.0/screens/sc000004-edit-item.md +35 -0
- package/examples/example-app/V1.0/screens/sc000005-settings.md +19 -0
- package/examples/example-app/V1.0/shells/_index.md +8 -0
- package/examples/example-app/V1.0/tech/_index.md +14 -0
- package/examples/example-app/V1.0/tech/tc000001-ui-framework.md +23 -0
- package/examples/example-app/V1.0/tech/tc000002-relational-database.md +25 -0
- package/examples/example-app/V1.0/userflows/_index.md +14 -0
- package/examples/example-app/V1.0/userflows/uf000001-create-an-item.md +66 -0
- package/examples/example-app/V1.0/userflows/uf000002-edit-an-item.md +47 -0
- package/examples/example-app/V1.1/CHANGES.md +12 -0
- package/examples/example-app/V1.1/apis/_index.md +14 -0
- package/examples/example-app/V1.1/apis/ap000001-list-items.md +20 -0
- package/examples/example-app/V1.1/apis/ap000002-save-item.md +28 -0
- package/examples/example-app/V1.1/backend/_index.md +15 -0
- package/examples/example-app/V1.1/backend/bn000001-app-server.md +24 -0
- package/examples/example-app/V1.1/backend/bn000002-main-database.md +22 -0
- package/examples/example-app/V1.1/backend/bn000003-file-storage.md +22 -0
- package/examples/example-app/V1.1/comments/_index.md +14 -0
- package/examples/example-app/V1.1/comments/cm000001-empty-list-message.md +15 -0
- package/examples/example-app/V1.1/comments/cm000002-filter-placement.md +12 -0
- package/examples/example-app/V1.1/components/_index.md +16 -0
- package/examples/example-app/V1.1/components/cp000001-button.md +32 -0
- package/examples/example-app/V1.1/components/cp000002-text-field.md +32 -0
- package/examples/example-app/V1.1/components/cp000003-item-row.md +34 -0
- package/examples/example-app/V1.1/components/cp000004-top-bar.md +26 -0
- package/examples/example-app/V1.1/decisions/_index.md +13 -0
- package/examples/example-app/V1.1/decisions/dc000001-where-do-item-images-live.md +26 -0
- package/examples/example-app/V1.1/edge-cases/_index.md +13 -0
- package/examples/example-app/V1.1/edge-cases/ec000001-save-fails.md +24 -0
- package/examples/example-app/V1.1/features/_index.md +17 -0
- package/examples/example-app/V1.1/features/ft000001-items.md +22 -0
- package/examples/example-app/V1.1/features/ft000002-account.md +21 -0
- package/examples/example-app/V1.1/features/ft000003-browse-items.md +26 -0
- package/examples/example-app/V1.1/features/ft000004-edit-an-item.md +33 -0
- package/examples/example-app/V1.1/features/ft000005-change-settings.md +21 -0
- package/examples/example-app/V1.1/ia/_index.md +13 -0
- package/examples/example-app/V1.1/ia/ia000001-main-map.md +23 -0
- package/examples/example-app/V1.1/intent-format.md +13 -0
- package/examples/example-app/V1.1/project.md +51 -0
- package/examples/example-app/V1.1/roles/_index.md +14 -0
- package/examples/example-app/V1.1/roles/ro000001-member.md +22 -0
- package/examples/example-app/V1.1/roles/ro000002-admin.md +19 -0
- package/examples/example-app/V1.1/rules/_index.md +13 -0
- package/examples/example-app/V1.1/rules/rl000001-item-name-is-required.md +20 -0
- package/examples/example-app/V1.1/screens/_index.md +17 -0
- package/examples/example-app/V1.1/screens/sc000001-home.md +18 -0
- package/examples/example-app/V1.1/screens/sc000002-item-list.md +44 -0
- package/examples/example-app/V1.1/screens/sc000003-item-detail.md +24 -0
- package/examples/example-app/V1.1/screens/sc000004-edit-item.md +35 -0
- package/examples/example-app/V1.1/screens/sc000005-settings.md +19 -0
- package/examples/example-app/V1.1/shells/_index.md +8 -0
- package/examples/example-app/V1.1/tech/_index.md +14 -0
- package/examples/example-app/V1.1/tech/tc000001-ui-framework.md +23 -0
- package/examples/example-app/V1.1/tech/tc000002-relational-database.md +25 -0
- package/examples/example-app/V1.1/userflows/_index.md +14 -0
- package/examples/example-app/V1.1/userflows/uf000001-create-an-item.md +66 -0
- package/examples/example-app/V1.1/userflows/uf000002-edit-an-item.md +47 -0
- package/examples/example-app/_ids.md +78 -0
- package/examples/example-app/_index.md +14 -0
- package/examples/example-app/inputs/brief.md +3 -0
- package/examples/example-app/inputs/links.md +3 -0
- package/kit/cli/README.md +128 -0
- package/kit/cli/intent-format.mjs +342 -0
- package/kit/cli/lib/agents.mjs +39 -0
- package/kit/cli/lib/check.mjs +412 -0
- package/kit/cli/lib/create.mjs +190 -0
- package/kit/cli/lib/frame.mjs +182 -0
- package/kit/cli/lib/generate.mjs +418 -0
- package/kit/cli/lib/kit.mjs +203 -0
- package/kit/cli/lib/migrate.mjs +97 -0
- package/kit/cli/lib/model.mjs +409 -0
- package/kit/cli/lib/next.mjs +134 -0
- package/kit/cli/lib/open.mjs +34 -0
- package/kit/cli/lib/parse.mjs +260 -0
- package/kit/cli/lib/project.mjs +120 -0
- package/kit/cli/lib/regen.mjs +112 -0
- package/kit/cli/lib/relations.mjs +380 -0
- package/kit/cli/lib/rename.mjs +23 -0
- package/kit/cli/lib/schema.mjs +185 -0
- package/kit/cli/lib/templates.mjs +41 -0
- package/kit/cli/lib/util.mjs +165 -0
- package/kit/cli/test/nav.mjs +145 -0
- package/kit/cli/test/roles.mjs +85 -0
- package/kit/cli/test/smoke.mjs +716 -0
- package/kit/skills/intent-format/SKILL.md +91 -0
- package/kit/skills/intent-format/links.md +142 -0
- package/kit/skills/intent-format/types/_overview.md +73 -0
- package/kit/skills/intent-format/types/api.md +111 -0
- package/kit/skills/intent-format/types/backend.md +110 -0
- package/kit/skills/intent-format/types/comment.md +66 -0
- package/kit/skills/intent-format/types/component.md +139 -0
- package/kit/skills/intent-format/types/decision.md +91 -0
- package/kit/skills/intent-format/types/edge-case.md +107 -0
- package/kit/skills/intent-format/types/feature.md +154 -0
- package/kit/skills/intent-format/types/ia.md +106 -0
- package/kit/skills/intent-format/types/project.md +131 -0
- package/kit/skills/intent-format/types/role.md +98 -0
- package/kit/skills/intent-format/types/rule.md +100 -0
- package/kit/skills/intent-format/types/screen.md +153 -0
- package/kit/skills/intent-format/types/shell.md +156 -0
- package/kit/skills/intent-format/types/tech.md +109 -0
- package/kit/skills/intent-format/types/userflow.md +197 -0
- package/kit/skills/kb-cold-start/SKILL.md +124 -0
- package/kit/skills/kb-cold-start/kickoff-questionnaire.md +42 -0
- package/kit/skills/kb-cold-start/templates.md +108 -0
- package/package.json +30 -4
- package/rationale.md +180 -0
- package/schema.json +5895 -0
- package/spec.md +1173 -0
- package/templates/api.md +16 -0
- package/templates/backend.md +15 -0
- package/templates/comment.md +11 -0
- package/templates/component.md +15 -0
- package/templates/decision.md +23 -0
- package/templates/edge-case.md +14 -0
- package/templates/feature.md +14 -0
- package/templates/ia.md +14 -0
- package/templates/project.md +34 -0
- package/templates/role.md +14 -0
- package/templates/rule.md +14 -0
- package/templates/screen.md +14 -0
- package/templates/shell.md +24 -0
- package/templates/tech.md +18 -0
- package/templates/userflow.md +20 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// Cross-card checks, workspace checks and health numbers. Local diagnostics come from model.mjs.
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { slugify, exists, readText } from './util.mjs';
|
|
4
|
+
import { targetType, targetName } from './project.mjs';
|
|
5
|
+
import { ref, pinSub, changeOf, statusLine, renderFolderIndex, renderRootIndex, renderIds, renderChanges } from './generate.mjs';
|
|
6
|
+
|
|
7
|
+
export function checkProject(S, P, opts = {}) {
|
|
8
|
+
const diags = [];
|
|
9
|
+
const add = (code, rel, line, message) => diags.push({ severity: S.diag.get(code)?.severity || 'info', code, path: rel, line, message });
|
|
10
|
+
const fileDiags = (f) => { for (const d of f.diags) add(d.code, f.rel, d.line, d.msg); };
|
|
11
|
+
for (const f of P.rootFiles) fileDiags(f);
|
|
12
|
+
const types = new Map(); // core -> {type, rel}
|
|
13
|
+
const retired = new Set(P.ids.retired.map((r) => r.core));
|
|
14
|
+
const kbs = new Set();
|
|
15
|
+
for (const ws of P.workspaces) {
|
|
16
|
+
if (ws.manifest) fileDiags(ws.manifest);
|
|
17
|
+
for (const f of ws.files) fileDiags(f);
|
|
18
|
+
if (ws.kb) kbs.add(ws.kb);
|
|
19
|
+
checkWorkspace(S, P, ws, add);
|
|
20
|
+
for (const d of ws.dupCores) {
|
|
21
|
+
const where = (e) => e.card.rel + (e.sub ? '#' + e.sub.core.toLowerCase() : '');
|
|
22
|
+
add('E004', (d.b.card).rel, (d.b.sub ? d.b.sub.idx : 0) + 1, `core ${d.core} also declared in ${where(d.a)}`);
|
|
23
|
+
}
|
|
24
|
+
for (const [core, t] of ws.byCore) {
|
|
25
|
+
const ty = targetType(t);
|
|
26
|
+
const prev = types.get(core);
|
|
27
|
+
if (prev && prev.type !== ty) add('E005', t.card.rel, 1, `core ${core} is a ${ty} here but a ${prev.type} in ${prev.rel}`);
|
|
28
|
+
else if (!prev) types.set(core, { type: ty, rel: t.card.rel });
|
|
29
|
+
if (retired.has(core)) add('W041', t.card.rel, (t.sub ? t.sub.idx : 0) + 1, `core ${core} is listed as retired in _ids.md`);
|
|
30
|
+
}
|
|
31
|
+
checkReferences(S, ws, add);
|
|
32
|
+
checkNavigation(S, ws, add);
|
|
33
|
+
}
|
|
34
|
+
if (kbs.size > 1) add('I023', '_index.md', 1, `workspaces carry different kb: slugs (${[...kbs].join(', ')})`);
|
|
35
|
+
checkGenerated(S, P, add, opts);
|
|
36
|
+
const order = { error: 0, warning: 1, info: 2 };
|
|
37
|
+
let out = diags;
|
|
38
|
+
if (opts.ws) out = diags.filter((d) => d.path.startsWith(opts.ws + '/'));
|
|
39
|
+
out.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : a.line - b.line || order[a.severity] - order[b.severity] || (a.code < b.code ? -1 : 1)));
|
|
40
|
+
const counts = { error: 0, warning: 0, info: 0 };
|
|
41
|
+
for (const d of out) counts[d.severity]++;
|
|
42
|
+
const health = P.workspaces.filter((w) => !opts.ws || w.folder === opts.ws).map((w) => ({ workspace: w.folder, lifecycle: w.lifecycle, ...healthOf(S, w) }));
|
|
43
|
+
return { diagnostics: out, counts, health };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function healthOf(S, ws) {
|
|
47
|
+
const why = { filled: 0, inherited: 0, none: 0, missing: 0 };
|
|
48
|
+
const confidence = { likely: 0, assumed: 0 };
|
|
49
|
+
const asks = { open: 0, later: 0, due: 0 };
|
|
50
|
+
const again = S.raw.ask?.again ?? 2, today = Date.now();
|
|
51
|
+
for (const c of ws.cards) {
|
|
52
|
+
if (c.why) why[c.whyEffective || c.why.state]++;
|
|
53
|
+
for (const m of [c.why?.mark, c.outcomeMark]) if (m && m !== 'confirmed') confidence[m]++;
|
|
54
|
+
for (const a of c.asks || []) { if (!a.later) asks.open++; else { asks.later++; if ((today - Date.parse(a.later)) / 86400000 >= again) asks.due++; } }
|
|
55
|
+
}
|
|
56
|
+
let blind = 0;
|
|
57
|
+
const groups = new Set();
|
|
58
|
+
const byKind = Object.fromEntries(Object.keys(S.raw.health.numbers.blindSpots.kinds || {}).map((k) => [k, 0]));
|
|
59
|
+
for (const c of ws.cards) {
|
|
60
|
+
blind += c.blind;
|
|
61
|
+
for (const [k, v] of Object.entries(c.blindBy)) byKind[k] = (byKind[k] || 0) + v;
|
|
62
|
+
for (const p of c.placeholders) groups.add((p.types ? [...p.types].sort().join(',') : 'any') + '|' + p.name.replace(/\s+/g, ' ').trim());
|
|
63
|
+
}
|
|
64
|
+
const open = ws.cards.filter((c) => c.type === 'decision' && S.openStatuses.includes(c.status)).length;
|
|
65
|
+
return { cards: ws.cards.length, blindSpots: blind, blindSpotsByKind: byKind, unresolvedPlaceholders: groups.size, openDecisions: open, why, confidence, asks };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function checkWorkspace(S, P, ws, add) {
|
|
69
|
+
const R = S.R;
|
|
70
|
+
const mrel = `${ws.folder}/intent-format.md`;
|
|
71
|
+
if (!ws.manifest) add('W032', ws.folder + '/', 1, 'workspace has no intent-format.md');
|
|
72
|
+
else if (ws.manifest.kind === 'manifest') checkManifest(S, P, ws, add, mrel);
|
|
73
|
+
else if (ws.manifest.kind !== 'broken') add('W033', mrel, 1, 'intent-format.md must have type: manifest');
|
|
74
|
+
const projects = ws.cards.filter((c) => c.type === 'project');
|
|
75
|
+
if (!exists(path.join(ws.dir, 'project.md'))) add('W032', ws.folder + '/', 1, 'workspace has no project.md');
|
|
76
|
+
if (projects.length > 1) for (const p of projects.slice(1)) add('W032', p.rel, 1, 'more than one project card in the workspace');
|
|
77
|
+
for (const sf of ws.subfolders) add('W031', sf, 1, 'type folders are flat (no subfolders)');
|
|
78
|
+
for (const m of S.modules) {
|
|
79
|
+
const has = exists(path.join(ws.dir, m));
|
|
80
|
+
if (ws.modules.includes(m) && !has) add('I019', mrel, 1, `enabled module ${m}/ has no folder`);
|
|
81
|
+
if (!ws.modules.includes(m) && has && !ws.cards.some((c) => c.type === S.typeOfFolder[m])) add('I019', `${ws.folder}/${m}`, 1, `folder ${m}/ is not an enabled module and holds no cards`);
|
|
82
|
+
}
|
|
83
|
+
const vocab = ws.vocab;
|
|
84
|
+
const roleCards = ws.cards.filter((c) => c.type === 'role' && c.name);
|
|
85
|
+
const roleNames = new Set(roleCards.map((c) => c.name));
|
|
86
|
+
for (const c of ws.files) {
|
|
87
|
+
if (c.kind === 'none') { add('W031', c.rel, 1, '.md file without front matter in a workspace'); continue; }
|
|
88
|
+
if (c.kind === 'index' && c.rel !== `${ws.folder}/CHANGES.md` && path.basename(c.file) !== '_index.md') add('W031', c.rel, 1, 'index files are _index.md or CHANGES.md');
|
|
89
|
+
if (c.kind !== 'card') continue;
|
|
90
|
+
// placement
|
|
91
|
+
const relWs = path.relative(ws.dir, c.file).split(path.sep);
|
|
92
|
+
if (c.type === 'project') {
|
|
93
|
+
if (relWs.join('/') !== 'project.md') add('W031', c.rel, 1, 'the project card is <workspace>/project.md');
|
|
94
|
+
} else {
|
|
95
|
+
const folder = S.folderOfType[c.type];
|
|
96
|
+
if (relWs.length !== 2 || relWs[0] !== folder) add('W031', c.rel, 1, `a ${c.type} card lives in ${folder}/`);
|
|
97
|
+
else if (!ws.modules.includes(folder)) add('W034', c.rel, 1, `module ${folder} is not enabled in intent-format.md`);
|
|
98
|
+
const m = R.filename.exec(relWs[relWs.length - 1]);
|
|
99
|
+
if (!m) add('W031', c.rel, 1, 'filename must be <core-lowercase>-<slug>.md');
|
|
100
|
+
else if (c.id && m.groups.core !== c.id.toLowerCase()) add('W031', c.rel, 1, `filename core ${m.groups.core} differs from id ${c.id}`);
|
|
101
|
+
else if (c.name && m.groups.slug !== slugify(c.name, c.type)) add('I016', c.rel, 1, `filename slug differs from "${slugify(c.name, c.type)}"`);
|
|
102
|
+
}
|
|
103
|
+
// vocabulary values
|
|
104
|
+
for (const u of c.vocabUses) {
|
|
105
|
+
if (u.vocab === 'States') {
|
|
106
|
+
if (!vocab.declared.has('States')) { add('W009', c.rel, u.idx + 1, `${u.what} ${u.value} — project.md declares no States`); continue; }
|
|
107
|
+
}
|
|
108
|
+
if (u.vocab === 'Roles') {
|
|
109
|
+
// roles are cards; the deprecated ### Roles table resolves stories only while no role card exists
|
|
110
|
+
if (roleNames.size) { if (!roleNames.has(u.value.trim())) add(u.code, c.rel, u.idx + 1, `${u.what} "${u.value}" is not the name of a role card`); continue; }
|
|
111
|
+
if (!vocab.declared.has('Roles')) continue;
|
|
112
|
+
}
|
|
113
|
+
if (u.value === S.reserved[u.vocab]) continue;
|
|
114
|
+
if (!vocab.values[u.vocab] || !vocab.values[u.vocab].has(u.value)) add(u.code, c.rel, u.idx + 1, `${u.what} "${u.value}" is not in ### ${u.vocab} of project.md`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// roles: unique names, stories that name them, the deprecated table
|
|
118
|
+
const seenRole = new Map();
|
|
119
|
+
for (const r of roleCards) {
|
|
120
|
+
if (seenRole.has(r.name)) add('W049', r.rel, 1, `another role card is named "${r.name}": ${seenRole.get(r.name)}`);
|
|
121
|
+
else seenRole.set(r.name, r.rel);
|
|
122
|
+
}
|
|
123
|
+
const named = new Set(ws.cards.flatMap((c) => c.stories.map((x) => x.role)));
|
|
124
|
+
for (const r of roleCards) if (!named.has(r.name)) add('I029', r.rel, 1, `no user story names "${r.name}"`);
|
|
125
|
+
if (ws.project && !S.raw.lifecycles.readOnly.includes(ws.lifecycle)) {
|
|
126
|
+
const i = ws.project.lines.findIndex((l, k) => !ws.project.mask[k] && /^### Roles\s*$/.test(l));
|
|
127
|
+
if (i >= 0) add('W050', ws.project.rel, i + 1, 'user types are role cards now: run intent-format migrate (each row becomes a card in roles/)');
|
|
128
|
+
}
|
|
129
|
+
// borrowed whys: same as #id names any card; a target without a why makes this card count as missing
|
|
130
|
+
const resolveWhy = (c, seen = new Set()) => {
|
|
131
|
+
if (!c.why) return 'missing';
|
|
132
|
+
if (c.whyEffective) return c.whyEffective;
|
|
133
|
+
if (c.why.state !== 'inherited') return c.why.state;
|
|
134
|
+
if (seen.has(c)) return 'missing';
|
|
135
|
+
seen.add(c);
|
|
136
|
+
const t = c.why.sameAs ? ws.byCore.get(c.why.sameAs) : null;
|
|
137
|
+
if (!t || t.sub) return 'unresolved';
|
|
138
|
+
const r = resolveWhy(t.card, seen);
|
|
139
|
+
return r === 'filled' || r === 'none' || r === 'inherited' ? 'inherited' : 'missing';
|
|
140
|
+
};
|
|
141
|
+
for (const c of ws.cards) {
|
|
142
|
+
if (!c.why || c.why.state !== 'inherited') continue;
|
|
143
|
+
const r = resolveWhy(c);
|
|
144
|
+
if (r === 'unresolved') { add('W051', c.rel, c.why.idx + 1, `same as #${c.why.sameAs}: names no card in this workspace — write the why, none or —`); c.whyEffective = 'missing'; c.bs('why'); }
|
|
145
|
+
else if (r === 'missing') { add('I030', c.rel, c.why.idx + 1, `same as #${c.why.sameAs}: that card has no why yet — counted as missing here`); c.whyEffective = 'missing'; c.bs('why'); }
|
|
146
|
+
else c.whyEffective = 'inherited';
|
|
147
|
+
}
|
|
148
|
+
// Source links cite evidence: a URL or a file inside <root>/inputs/
|
|
149
|
+
const inputsDir = path.resolve(P.root, 'inputs') + path.sep;
|
|
150
|
+
for (const c of ws.cards) for (const r of c.rels) {
|
|
151
|
+
if (r.rel !== 'Link' || !r.quals.some((q) => q.key === 'type' && q.value === 'Source')) continue;
|
|
152
|
+
const target = r.atoms[0]?.target;
|
|
153
|
+
if (!target || /^[a-z][a-z0-9+.-]*:\/\//i.test(target)) continue;
|
|
154
|
+
const abs = path.resolve(path.dirname(c.file), target.split('#')[0]);
|
|
155
|
+
if (!abs.startsWith(inputsDir)) add('W053', c.rel, r.idx + 1, `Source ${target} is outside inputs/ — evidence lives in <root>/inputs/`);
|
|
156
|
+
}
|
|
157
|
+
// default maps, origins, homes
|
|
158
|
+
const defaults = new Map(), origins = new Map();
|
|
159
|
+
const hasFeature = ws.cards.some((c) => c.type === 'feature');
|
|
160
|
+
for (const c of ws.cards) {
|
|
161
|
+
const m = c.fm.map;
|
|
162
|
+
if (c.type === 'ia' && m.get('default')?.value === 'true') {
|
|
163
|
+
const st = m.get('state')?.value || S.reserved.States;
|
|
164
|
+
if (defaults.has(st)) add('W040', c.rel, m.get('default').idx + 1, `another default map for ${st}: ${defaults.get(st)}`);
|
|
165
|
+
else defaults.set(st, c.rel);
|
|
166
|
+
}
|
|
167
|
+
const o = m.get('origin')?.value;
|
|
168
|
+
if (o && o !== '—') { if (origins.has(o)) add('W043', c.rel, m.get('origin').idx + 1, `origin ${o} also on ${origins.get(o)}`); else origins.set(o, c.rel); }
|
|
169
|
+
if (hasFeature && (c.type === 'screen' || c.type === 'userflow') && !m.get('feature')) add('I025', c.rel, 1, 'no home feature (feature:)');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function checkManifest(S, P, ws, add, rel) {
|
|
174
|
+
const f = ws.manifest, m = f.fm.map, R = S.R;
|
|
175
|
+
const keys = S.raw.manifest.keys;
|
|
176
|
+
let max = -1, reported = false;
|
|
177
|
+
for (const e of f.fm.entries) {
|
|
178
|
+
if (/^x-/.test(e.key)) continue;
|
|
179
|
+
const pos = keys.findIndex((k) => k.key === e.key);
|
|
180
|
+
if (pos < 0) { add('W033', rel, e.idx + 1, `unknown manifest key ${e.key}:`); continue; }
|
|
181
|
+
if (pos < max && !reported) { add('W033', rel, e.idx + 1, `manifest keys out of order (${keys.map((k) => k.key).join(', ')})`); reported = true; }
|
|
182
|
+
max = Math.max(max, pos);
|
|
183
|
+
if (keys[pos].quoted && e.kind !== 'quoted') add('W008', rel, e.idx + 1, `${e.key}: is always quoted ("…")`);
|
|
184
|
+
}
|
|
185
|
+
for (const k of keys) if (k.required && (!m.get(k.key) || m.get(k.key).kind === 'empty')) add('W033', rel, 1, `manifest key ${k.key}: is missing`);
|
|
186
|
+
const v = m.get('version')?.value;
|
|
187
|
+
if (v && v !== ws.version) add('W033', rel, m.get('version').idx + 1, `version "${v}" does not match the folder ${ws.folder}`);
|
|
188
|
+
const kb = m.get('kb')?.value;
|
|
189
|
+
if (kb && !R.kbSlug.test(kb)) add('W033', rel, m.get('kb').idx + 1, `kb: "${kb}" is not a slug`);
|
|
190
|
+
const lc = m.get('lifecycle')?.value;
|
|
191
|
+
if (lc && !S.lifecycles.includes(lc)) add('W033', rel, m.get('lifecycle').idx + 1, `lifecycle "${lc}" is not one of ${S.lifecycles.join(' · ')}`);
|
|
192
|
+
const b = ws.basedOn;
|
|
193
|
+
if (b && (b === ws.version || !P.byVersion.has(b))) add('W033', rel, m.get('based-on').idx + 1, `based-on "${b}" names no other workspace`);
|
|
194
|
+
const mods = m.get('modules');
|
|
195
|
+
if (mods && mods.kind !== 'list') add('W033', rel, mods.idx + 1, 'modules: is a list [a, b]');
|
|
196
|
+
else if (mods) for (const x of mods.items) if (!S.modules.includes(x)) add('W033', rel, mods.idx + 1, `unknown module "${x}"`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function checkReferences(S, ws, add) {
|
|
200
|
+
const R = S.R;
|
|
201
|
+
const res = (core) => ws.byCore.get(core);
|
|
202
|
+
const allNames = new Map(); // name -> [{card,sub}]
|
|
203
|
+
for (const t of ws.byCore.values()) {
|
|
204
|
+
const n = targetName(t);
|
|
205
|
+
if (!n) continue;
|
|
206
|
+
if (!allNames.has(n)) allNames.set(n, []);
|
|
207
|
+
allNames.get(n).push(t);
|
|
208
|
+
}
|
|
209
|
+
const compGraph = new Map();
|
|
210
|
+
const isShell = (t) => !t.sub && t.card.type === 'shell';
|
|
211
|
+
for (const c of ws.cards) {
|
|
212
|
+
const rel = c.rel;
|
|
213
|
+
const stale = (idx, a, t) => {
|
|
214
|
+
const want = ref(c.file, t);
|
|
215
|
+
if (a.kind === 'bound' && c.lines[idx].slice(a.start, a.end) !== want) add('I007', rel, idx + 1, `reference text of #${a.core} is stale (regen rewrites it)`);
|
|
216
|
+
if (a.kind === 'hand') add('I007', rel, idx + 1, `#${a.core} will be upgraded to a bound reference`);
|
|
217
|
+
};
|
|
218
|
+
for (const r of c.rels) {
|
|
219
|
+
const def = S.relations[r.rel];
|
|
220
|
+
for (const a of r.atoms) {
|
|
221
|
+
if (a.kind !== 'bound' && a.kind !== 'hand') continue;
|
|
222
|
+
const t = res(a.core);
|
|
223
|
+
if (!t) { add('W019', rel, r.idx + 1, `#${a.core} does not resolve in ${ws.folder}`); continue; }
|
|
224
|
+
const ty = targetType(t);
|
|
225
|
+
if (r.rel === 'Part' && ty === 'shell') { add('W046', rel, r.idx + 1, `#${a.core} is a shell — it frames places, it is never a part`); continue; }
|
|
226
|
+
if (def.targetTypes.length && !def.targetTypes.includes(ty)) { add('W018', rel, r.idx + 1, `${r.rel} cannot point at a ${ty} (#${a.core})`); continue; }
|
|
227
|
+
stale(r.idx, a, t);
|
|
228
|
+
if (r.rel === 'Part' && c.type === 'component') {
|
|
229
|
+
if (!compGraph.has(c.id)) compGraph.set(c.id, new Set());
|
|
230
|
+
compGraph.get(c.id).add(t.card.id);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (r.variant) {
|
|
234
|
+
const a = r.atoms[r.atoms.length - 1];
|
|
235
|
+
const t = a && (a.kind === 'bound' || a.kind === 'hand') && res(a.core);
|
|
236
|
+
if (t && !t.sub) {
|
|
237
|
+
const ok = t.card.type === 'userflow' ? t.card.flowVariants.has(r.variant) : !!pinSub(t.card, r.variant);
|
|
238
|
+
if (!ok) add('W028', rel, r.idx + 1, `variant "${r.variant}" is not a variant of #${t.card.id}`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (const m of c.mentions) {
|
|
243
|
+
const t = res(m.core);
|
|
244
|
+
if (!t) { add('W019', rel, m.idx + 1, `#${m.core} does not resolve in ${ws.folder}`); continue; }
|
|
245
|
+
if (m.kind === 'bound' && c.lines[m.idx].slice(m.start, m.end) !== ref(c.file, t)) add('I007', rel, m.idx + 1, `reference text of #${m.core} is stale (regen rewrites it)`);
|
|
246
|
+
}
|
|
247
|
+
for (const q of c.qualRefs) {
|
|
248
|
+
const t = res(q.core);
|
|
249
|
+
if (!t) add('W019', rel, q.idx + 1, `#${q.core} does not resolve in ${ws.folder}`);
|
|
250
|
+
else if (!q.types.includes(targetType(t))) add('W021', rel, q.idx + 1, `${q.key}: #${q.core} is a ${targetType(t)}, not a ${q.types.join('/')}`);
|
|
251
|
+
else if (q.key === 'shell' && !isShell(t)) add('W044', rel, q.idx + 1, `shell: #${q.core} is not a shell`);
|
|
252
|
+
}
|
|
253
|
+
// framing: shell: on screens and in variant passports; flow: on stepper shells
|
|
254
|
+
const shellKey = (value, idx) => {
|
|
255
|
+
if (!value || !/^SH\d{6}$/.test(value)) return null;
|
|
256
|
+
const t = res(value);
|
|
257
|
+
if (!t) add('W019', rel, idx + 1, `shell: ${value} does not resolve in ${ws.folder}`);
|
|
258
|
+
else if (!isShell(t)) add('W044', rel, idx + 1, `shell: ${value} is not a shell`);
|
|
259
|
+
else return t;
|
|
260
|
+
return null;
|
|
261
|
+
};
|
|
262
|
+
if (c.type === 'screen') {
|
|
263
|
+
const sh = c.fm.map.get('shell');
|
|
264
|
+
if (sh) shellKey(sh.value, sh.idx);
|
|
265
|
+
for (const sd of c.subs) {
|
|
266
|
+
const p = sd.passport?.find((x) => x.key === 'shell');
|
|
267
|
+
if (p) shellKey(p.value, sd.passportIdx ?? sd.idx);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (c.type === 'shell') {
|
|
271
|
+
const fl = c.fm.map.get('flow');
|
|
272
|
+
if (fl && /^UF\d{6}$/.test(fl.value)) {
|
|
273
|
+
const t = res(fl.value);
|
|
274
|
+
if (!t) add('W019', rel, fl.idx + 1, `flow: ${fl.value} does not resolve in ${ws.folder}`);
|
|
275
|
+
else if (t.sub || t.card.type !== 'userflow') add('W018', rel, fl.idx + 1, `flow: ${fl.value} is not a user flow`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
for (const p of c.placeholders) {
|
|
279
|
+
const hits = (allNames.get(p.name.replace(/\s+/g, ' ').trim()) || []).filter((t) => !p.types || p.types.includes(targetType(t)));
|
|
280
|
+
if (hits.length) add('I014', rel, p.idx + 1, `[~ ${p.name}] can now link to #${hits[0].sub ? hits[0].sub.core : hits[0].card.id}`);
|
|
281
|
+
}
|
|
282
|
+
// membership keys
|
|
283
|
+
const fe = c.fm.map.get('feature');
|
|
284
|
+
if (fe && /^FT\d{6}$/.test(fe.value)) {
|
|
285
|
+
const t = res(fe.value);
|
|
286
|
+
if (!t) add('W019', rel, fe.idx + 1, `feature: ${fe.value} does not resolve in ${ws.folder}`);
|
|
287
|
+
else if (t.sub || t.card.type !== 'feature') add('W018', rel, fe.idx + 1, `feature: ${fe.value} is not a feature`);
|
|
288
|
+
}
|
|
289
|
+
const sb = c.fm.map.get('superseded-by');
|
|
290
|
+
if (sb && /^DC\d{6}$/.test(sb.value)) {
|
|
291
|
+
const t = res(sb.value);
|
|
292
|
+
if (!t) add('W019', rel, sb.idx + 1, `superseded-by: ${sb.value} does not resolve in ${ws.folder}`);
|
|
293
|
+
else if (t.sub || t.card.type !== 'decision') add('W018', rel, sb.idx + 1, `superseded-by: ${sb.value} is not a decision`);
|
|
294
|
+
else if (t.card === c) add('W052', rel, sb.idx + 1, 'a decision cannot supersede itself');
|
|
295
|
+
}
|
|
296
|
+
const pa = c.fm.map.get('parent');
|
|
297
|
+
if (pa && /^FT\d{6}$/.test(pa.value)) {
|
|
298
|
+
const t = res(pa.value);
|
|
299
|
+
if (!t) add('W019', rel, pa.idx + 1, `parent: ${pa.value} does not resolve in ${ws.folder}`);
|
|
300
|
+
else if (t.sub || t.card.type !== 'feature') add('W030', rel, pa.idx + 1, `parent: ${pa.value} is not a feature`);
|
|
301
|
+
else if (t.card === c) add('W030', rel, pa.idx + 1, 'a feature cannot be its own parent');
|
|
302
|
+
else if (t.card.fm.map.get('parent')) add('W030', rel, pa.idx + 1, `parent: ${pa.value} is a Sub-feature (max two levels)`);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// nesting cycles among components
|
|
306
|
+
const color = new Map();
|
|
307
|
+
const visit = (id, stack) => {
|
|
308
|
+
color.set(id, 1);
|
|
309
|
+
for (const n of compGraph.get(id) || []) {
|
|
310
|
+
if (color.get(n) === 1) {
|
|
311
|
+
const c = res(id)?.card;
|
|
312
|
+
if (c) add('W029', c.rel, 1, `Part lines form a cycle: ${[...stack, id, n].slice(stack.indexOf(n) >= 0 ? stack.indexOf(n) : 0).map((x) => '#' + x).join(' → ')}`);
|
|
313
|
+
} else if (!color.get(n)) visit(n, [...stack, id]);
|
|
314
|
+
}
|
|
315
|
+
color.set(id, 2);
|
|
316
|
+
};
|
|
317
|
+
for (const id of compGraph.keys()) if (!color.get(id)) visit(id, []);
|
|
318
|
+
// default-shell clash: two shells would frame the same placement by default
|
|
319
|
+
const defaults = new Map();
|
|
320
|
+
const steppers = new Map();
|
|
321
|
+
for (const c of ws.cards) {
|
|
322
|
+
if (c.type !== 'shell') continue;
|
|
323
|
+
const kind = c.fm.map.get('shell-type')?.value;
|
|
324
|
+
const fl = kind === 'stepper' && c.fm.map.get('flow')?.value;
|
|
325
|
+
if (fl && /^UF\d{6}$/.test(fl)) {
|
|
326
|
+
if (steppers.has(fl)) add('W045', c.rel, 1, `also the stepper of ${fl}: ${steppers.get(fl)} — a flow has one stepper`);
|
|
327
|
+
else steppers.set(fl, c.rel);
|
|
328
|
+
}
|
|
329
|
+
if (!['app', 'overlay', 'embedded'].includes(kind)) continue;
|
|
330
|
+
const ap = c.fm.map.get('opens-as');
|
|
331
|
+
if (kind === 'overlay' && (!ap || ap.kind === 'empty' || ap.kind === 'blind')) continue;
|
|
332
|
+
const listOf = (key, dflt) => { const e = c.fm.map.get(key); return e && e.kind !== 'empty' && e.kind !== 'blind' ? (e.kind === 'list' ? e.items : [e.value]) : [dflt]; };
|
|
333
|
+
for (const p of listOf('platform', S.reserved.Platforms)) for (const st of listOf('state', S.reserved.States)) {
|
|
334
|
+
const k = [kind, p, st, kind === 'overlay' ? ap.value : ''].join('|');
|
|
335
|
+
if (defaults.has(k)) add('W045', c.rel, 1, `also the default ${kind} shell for ${p} · ${st}${kind === 'overlay' ? ' · ' + ap.value : ''}: ${defaults.get(k)}`);
|
|
336
|
+
else defaults.set(k, c.rel);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Shell navigation and chrome (6.4): W048 · in: names no part of the shell; I027 a shell part also on a screen; I028 a line shown where its screen does not exist. */
|
|
342
|
+
function checkNavigation(S, ws, add) {
|
|
343
|
+
const res = (core) => ws.byCore.get(core);
|
|
344
|
+
const coreOf = (r) => r.atoms.find((a) => a.kind === 'bound' || a.kind === 'hand')?.core;
|
|
345
|
+
const states = ws.vocab.declared.has('States') ? [...(ws.vocab.values.States || [])] : [];
|
|
346
|
+
const statesOf = (e) => (!e || e.kind === 'empty' || e.kind === 'blind' || e.value === S.reserved.States ? states : e.kind === 'list' ? e.items : [e.value]);
|
|
347
|
+
const shellOfPart = new Map();
|
|
348
|
+
for (const c of ws.cards) {
|
|
349
|
+
if (c.type !== 'shell') continue;
|
|
350
|
+
const cores = c.rels.filter((r) => r.rel === 'Part').map(coreOf).filter(Boolean);
|
|
351
|
+
for (const k of cores) if (!shellOfPart.has(k)) shellOfPart.set(k, c);
|
|
352
|
+
const names = new Set(cores.map((k) => res(k)?.card?.name?.toLowerCase()).filter(Boolean));
|
|
353
|
+
const shown = statesOf(c.fm.map.get('state'));
|
|
354
|
+
for (const r of c.rels) {
|
|
355
|
+
if (r.rel !== 'Leads to') continue;
|
|
356
|
+
for (const p of r.inParts || []) {
|
|
357
|
+
const h = /^#?([A-Z]{2}\d{6})$/.exec(p);
|
|
358
|
+
if (h ? !cores.includes(h[1]) : !names.has(p.toLowerCase())) add('W048', c.rel, r.idx + 1, `in: ${p} is not a part of this shell`);
|
|
359
|
+
}
|
|
360
|
+
const t = res(coreOf(r) || '');
|
|
361
|
+
if (!states.length || !t || t.sub || t.card.type !== 'screen') continue;
|
|
362
|
+
const reach = statesOf(t.card.fm.map.get('state'));
|
|
363
|
+
const missing = (r.lineState ? [r.lineState] : shown).filter((st) => st !== S.reserved.States && !reach.includes(st));
|
|
364
|
+
if (missing.length) add('I028', c.rel, r.idx + 1, `#${t.card.id} does not exist in ${missing.join(', ')} — add · state: to this line`);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
for (const c of ws.cards) {
|
|
368
|
+
if (c.type !== 'screen') continue;
|
|
369
|
+
for (const r of c.rels) {
|
|
370
|
+
const k = r.rel === 'Part' && coreOf(r);
|
|
371
|
+
const sh = k && shellOfPart.get(k);
|
|
372
|
+
if (sh) add('I027', c.rel, r.idx + 1, `#${k} is also a part of shell #${sh.id} — frame parts belong on the shell`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** I001 (status lines) and I020 (generated files), compared with what regen would write. */
|
|
378
|
+
function checkGenerated(S, P, add, opts) {
|
|
379
|
+
if (opts.skipGenerated) return;
|
|
380
|
+
for (const ws of P.workspaces) {
|
|
381
|
+
if (S.raw.lifecycles.readOnly.includes(ws.lifecycle)) continue;
|
|
382
|
+
const changes = new Map();
|
|
383
|
+
for (const c of ws.cards) {
|
|
384
|
+
if (!c.id) continue;
|
|
385
|
+
const ch = changeOf(S, ws, c);
|
|
386
|
+
changes.set(c, ch);
|
|
387
|
+
const want = statusLine(S, ws, c, ch);
|
|
388
|
+
if (c.sk.statusIdx < 0) add('I001', c.rel, (c.sk.titleIdx >= 0 ? c.sk.titleIdx : 0) + 1, 'status line missing (regen writes it)');
|
|
389
|
+
else if (c.lines[c.sk.statusIdx] !== want) add('I001', c.rel, c.sk.statusIdx + 1, 'status line is stale (regen rewrites it)');
|
|
390
|
+
}
|
|
391
|
+
const stale = (file, want) => {
|
|
392
|
+
const rel = path.relative(P.root, file).split(path.sep).join('/');
|
|
393
|
+
if (!exists(file)) add('I020', rel, 1, 'generated file missing (run intent-format regen)');
|
|
394
|
+
else if (want !== null && readText(file) !== want) add('I020', rel, 1, 'generated file is stale (run intent-format regen)');
|
|
395
|
+
};
|
|
396
|
+
for (const folder of S.modules) {
|
|
397
|
+
if (ws.modules.includes(folder) || exists(path.join(ws.dir, folder))) stale(path.join(ws.dir, folder, '_index.md'), renderFolderIndex(S, ws, folder));
|
|
398
|
+
}
|
|
399
|
+
const ch = renderChanges(S, ws, changes);
|
|
400
|
+
const cf = path.join(ws.dir, 'CHANGES.md');
|
|
401
|
+
if (ch) stale(cf, ch);
|
|
402
|
+
else if (exists(cf)) add('I020', `${ws.folder}/CHANGES.md`, 1, 'CHANGES.md without based-on (regen removes it)');
|
|
403
|
+
}
|
|
404
|
+
stale2(P, add, path.join(P.root, '_index.md'), renderRootIndex(S, P));
|
|
405
|
+
stale2(P, add, path.join(P.root, '_ids.md'), renderIds(S, P));
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function stale2(P, add, file, want) {
|
|
409
|
+
const rel = path.relative(P.root, file).split(path.sep).join('/');
|
|
410
|
+
if (!exists(file)) add('I020', rel, 1, 'generated file missing (run intent-format regen)');
|
|
411
|
+
else if (readText(file) !== want) add('I020', rel, 1, 'generated file is stale (run intent-format regen)');
|
|
412
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// Card creation shared by `intent-format new`, `intent-format mint` and `intent-format scaffold`: minting, High-water marks, templates.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { exists, readText, slugify, pad6, toPosix } from './util.mjs';
|
|
5
|
+
import { highWater, parseIds } from './project.mjs';
|
|
6
|
+
import { renderIds } from './generate.mjs';
|
|
7
|
+
import { renderCard } from './templates.mjs';
|
|
8
|
+
|
|
9
|
+
export class Usage extends Error {}
|
|
10
|
+
export class Refused extends Error {}
|
|
11
|
+
|
|
12
|
+
/** Raise High-water marks in _ids.md without touching its other tables. */
|
|
13
|
+
export function bumpMarks(S, P, prefix, n) {
|
|
14
|
+
const file = path.join(P.root, '_ids.md');
|
|
15
|
+
const hw = highWater(P);
|
|
16
|
+
if (!hw.has(prefix) || hw.get(prefix) < n) hw.set(prefix, n);
|
|
17
|
+
if (!exists(file)) { fs.writeFileSync(file, renderIds(S, P, hw)); return; }
|
|
18
|
+
const text = readText(file);
|
|
19
|
+
const marks = parseIds(text).marks;
|
|
20
|
+
for (const [p, v] of hw) if (!marks.has(p) || marks.get(p) < v) marks.set(p, v);
|
|
21
|
+
const table = ['| Prefix | Highest |', '|---|---|', ...[...marks.keys()].sort().map((p) => `| ${p} | ${marks.get(p)} |`)];
|
|
22
|
+
const lines = text.replace(/\n$/, '').split('\n');
|
|
23
|
+
const i = lines.findIndex((l) => l.replace('⚙️', '⚙').trim() === `## ${S.gear} High-water marks`);
|
|
24
|
+
let out;
|
|
25
|
+
if (i < 0) out = [...lines, '', `## ${S.gear} High-water marks`, ...table];
|
|
26
|
+
else {
|
|
27
|
+
let j = i + 1;
|
|
28
|
+
while (j < lines.length && !/^## /.test(lines[j])) j++;
|
|
29
|
+
out = [...lines.slice(0, i + 1), ...table, ...(j < lines.length ? ['', ...lines.slice(j)] : [])];
|
|
30
|
+
}
|
|
31
|
+
fs.writeFileSync(file, out.join('\n') + '\n');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function mintCores(S, P, prefix, count) {
|
|
35
|
+
const start = (highWater(P).get(prefix) || 0) + 1;
|
|
36
|
+
const cores = [];
|
|
37
|
+
for (let k = 0; k < count; k++) cores.push(prefix + pad6(start + k));
|
|
38
|
+
const last = start + count - 1;
|
|
39
|
+
bumpMarks(S, P, prefix, last);
|
|
40
|
+
P.ids.marks.set(prefix, Math.max(P.ids.marks.get(prefix) || 0, last));
|
|
41
|
+
return cores;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Validate, mint a core and write the card from its template. opts: { state, feature, parent }. */
|
|
45
|
+
export function createCard(S, P, ws, type, name, opts = {}) {
|
|
46
|
+
if (!S.isCardType(type)) throw new Usage(`unknown type "${type}" (${S.roster.join(', ')})`);
|
|
47
|
+
name = String(name || '').trim();
|
|
48
|
+
if (!name || /[[\]]/.test(name)) throw new Usage('the name must be non-empty and contain no [ or ]');
|
|
49
|
+
if (S.raw.lifecycles.readOnly.includes(ws.lifecycle)) throw new Refused(`V${ws.version} is ${ws.lifecycle} (read-only)`);
|
|
50
|
+
const keys = S.keyOrder(type);
|
|
51
|
+
const folder = S.folderOfType[type];
|
|
52
|
+
if (type === 'project' && ws.project) throw new Refused(`V${ws.version} already has a project card`);
|
|
53
|
+
if (folder && !ws.modules.includes(folder)) throw new Refused(`module ${folder} is not enabled in V${ws.version}/intent-format.md`);
|
|
54
|
+
const values = { name };
|
|
55
|
+
if (opts.state !== undefined) {
|
|
56
|
+
if (!keys.includes('state')) throw new Usage(`--state is not allowed on ${type}`);
|
|
57
|
+
if (!ws.vocab.declared.has('States')) throw new Refused('project.md declares no States');
|
|
58
|
+
if (opts.state !== S.reserved.States && !ws.vocab.values.States.has(opts.state)) throw new Refused(`state "${opts.state}" is not in ### States`);
|
|
59
|
+
values.state = opts.state;
|
|
60
|
+
}
|
|
61
|
+
for (const k of ['feature', 'parent']) {
|
|
62
|
+
if (opts[k] === undefined) continue;
|
|
63
|
+
if (!keys.includes(k)) throw new Usage(`--${k} is not allowed on ${type}`);
|
|
64
|
+
const t = ws.byCore.get(opts[k]);
|
|
65
|
+
if (!t || t.sub || t.card.type !== 'feature') throw new Refused(`--${k} ${opts[k]} is not a feature in V${ws.version}`);
|
|
66
|
+
if (k === 'parent' && t.card.fm.map.get('parent')) throw new Refused(`--parent ${opts[k]} is a Sub-feature (max two levels)`);
|
|
67
|
+
values[k] = opts[k];
|
|
68
|
+
}
|
|
69
|
+
const [core] = mintCores(S, P, S.prefixOfType[type], 1);
|
|
70
|
+
values.id = core;
|
|
71
|
+
const file = folder ? path.join(ws.dir, folder, `${core.toLowerCase()}-${slugify(name, type)}.md`) : path.join(ws.dir, 'project.md');
|
|
72
|
+
if (exists(file)) throw new Refused(`${file} already exists`);
|
|
73
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
74
|
+
fs.writeFileSync(file, renderCard(S, type, values));
|
|
75
|
+
// register the new card so later calls in the same run can resolve it (scaffold)
|
|
76
|
+
const fmMap = new Map(values.parent ? [['parent', { value: values.parent }]] : []);
|
|
77
|
+
ws.byCore.set(core, { card: { id: core, type, name, file, fm: { map: fmMap }, subs: [] }, sub: null });
|
|
78
|
+
return { core, file, rel: toPosix(path.relative(P.root, file)) };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* ---------------- scaffold ---------------- */
|
|
82
|
+
|
|
83
|
+
const SCAFFOLD_ORDER = ['role', 'feature', 'tech', 'backend', 'api', 'component', 'shell', 'screen', 'ia', 'userflow', 'rule', 'edge-case', 'decision', 'comment'];
|
|
84
|
+
|
|
85
|
+
function splitRow(line) {
|
|
86
|
+
const cells = [];
|
|
87
|
+
let start = line.indexOf('|') + 1;
|
|
88
|
+
for (let i = start; i < line.length; i++) {
|
|
89
|
+
if (line[i] === '\\') { i++; continue; }
|
|
90
|
+
if (line[i] === '|') { cells.push({ text: line.slice(start, i), start, end: i }); start = i + 1; }
|
|
91
|
+
}
|
|
92
|
+
return cells;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function normType(S, raw) {
|
|
96
|
+
let x = raw.trim().toLowerCase().replace(/\s+/g, '-');
|
|
97
|
+
const alias = { 'sub-feature': 'feature', subfeature: 'feature', 'user-flow': 'userflow', flow: 'userflow', 'ia-map': 'ia', edgecase: 'edge-case' };
|
|
98
|
+
x = alias[x] || x;
|
|
99
|
+
if (!S.isCardType(x) && x.endsWith('s') && S.isCardType(x.slice(0, -1))) x = x.slice(0, -1);
|
|
100
|
+
return S.isCardType(x) ? x : null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const blank = (v) => !v || /^(—|-|–)$/.test(v);
|
|
104
|
+
const norm = (n) => String(n || '').replace(/\s+/g, ' ').trim().toLowerCase();
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Every inventory table with Type, Name and Core columns, in file order: { lines, rows, tables } or null.
|
|
108
|
+
* An inventory split into sections (one table per type or area) is read whole; each table's columns are located by its own header.
|
|
109
|
+
*/
|
|
110
|
+
export function readInventory(S, invFile) {
|
|
111
|
+
const lines = readText(invFile).split('\n');
|
|
112
|
+
const rows = [];
|
|
113
|
+
let tables = 0;
|
|
114
|
+
for (let i = 0; i < lines.length; i++) {
|
|
115
|
+
if (!/^\s*\|/.test(lines[i])) continue;
|
|
116
|
+
const head = splitRow(lines[i]).map((c) => c.text.trim().toLowerCase());
|
|
117
|
+
const cols = { type: head.indexOf('type'), name: head.indexOf('name'), core: head.indexOf('core'), home: head.findIndex((x) => x.startsWith('parent')) };
|
|
118
|
+
if (!(cols.type >= 0 && cols.name >= 0 && cols.core >= 0)) continue;
|
|
119
|
+
tables++;
|
|
120
|
+
let j = i + 1;
|
|
121
|
+
for (; j < lines.length && /^\s*\|/.test(lines[j]); j++) {
|
|
122
|
+
if (/^\s*\|(\s*:?-{3,}:?\s*\|)+\s*$/.test(lines[j])) continue;
|
|
123
|
+
const cells = splitRow(lines[j]);
|
|
124
|
+
const get = (k) => (cols[k] >= 0 && cells[cols[k]] ? cells[cols[k]].text.trim().replace(/\\\|/g, '|') : '');
|
|
125
|
+
rows.push({ idx: j, no: rows.length + 1, rawType: get('type'), type: normType(S, get('type')), name: get('name'), home: get('home'), core: get('core'), coreCell: cells[cols.core] });
|
|
126
|
+
}
|
|
127
|
+
i = j - 1;
|
|
128
|
+
}
|
|
129
|
+
if (!tables) return null;
|
|
130
|
+
return { lines, rows, tables };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Create a card for every inventory row without a Core; write each new core into that row's Core cell.
|
|
135
|
+
* Columns located by header: Type, Name, Core (required) and "Parent / home" (optional).
|
|
136
|
+
*/
|
|
137
|
+
export function scaffold(S, P, ws, invFile, log) {
|
|
138
|
+
const inv = readInventory(S, invFile);
|
|
139
|
+
if (!inv) throw new Refused(`${invFile}: no table with Type, Name and Core columns`);
|
|
140
|
+
const { lines, rows } = inv;
|
|
141
|
+
const stats = { created: 0, linked: 0, skipped: 0, problems: 0 };
|
|
142
|
+
const problem = (r, msg) => { stats.problems++; log(`row ${r.no} (${r.rawType} "${r.name}"): ${msg}`); };
|
|
143
|
+
const cardsNamed = (type, name) => [...new Set([...ws.byCore.values()].filter((t) => !t.sub && t.card.type === type && norm(t.card.name) === norm(name)).map((t) => t.card.id))];
|
|
144
|
+
const writeCore = (r, core) => {
|
|
145
|
+
const l = lines[r.idx];
|
|
146
|
+
lines[r.idx] = l.slice(0, r.coreCell.start) + ` ${core} ` + l.slice(r.coreCell.end);
|
|
147
|
+
r.core = core;
|
|
148
|
+
fs.writeFileSync(invFile, lines.join('\n'));
|
|
149
|
+
};
|
|
150
|
+
const queue = [];
|
|
151
|
+
for (const r of rows) {
|
|
152
|
+
if (!blank(r.core)) stats.skipped++;
|
|
153
|
+
else if (!r.type) problem(r, `unknown type "${r.rawType}" — skipped`);
|
|
154
|
+
else if (r.type === 'project') { stats.skipped++; log(`row ${r.no}: project skipped (intent-format init creates the project card)`); }
|
|
155
|
+
else if (!r.name) problem(r, 'no name — skipped');
|
|
156
|
+
else if (!r.coreCell) problem(r, 'row has no Core cell — skipped');
|
|
157
|
+
}
|
|
158
|
+
// every card type is scaffolded; a type the order does not list yet goes last, never skipped
|
|
159
|
+
for (const type of [...SCAFFOLD_ORDER, ...S.roster.filter((t) => t !== 'project' && !SCAFFOLD_ORDER.includes(t))]) {
|
|
160
|
+
const rs = rows.filter((r) => r.type === type && blank(r.core) && r.name && r.coreCell);
|
|
161
|
+
queue.push(...(type === 'feature' ? [...rs.filter((r) => blank(r.home)), ...rs.filter((r) => !blank(r.home))] : rs));
|
|
162
|
+
}
|
|
163
|
+
for (const r of queue) {
|
|
164
|
+
const existing = cardsNamed(r.type, r.name);
|
|
165
|
+
if (existing.length === 1) { writeCore(r, existing[0]); stats.linked++; log(`${existing[0]} already exists (${r.type} "${r.name}") — core written to the inventory`); continue; }
|
|
166
|
+
if (existing.length > 1) { problem(r, `several ${r.type} cards are named "${r.name}" (${existing.join(', ')}) — skipped`); continue; }
|
|
167
|
+
const opts = {};
|
|
168
|
+
if (!blank(r.home)) {
|
|
169
|
+
const key = r.type === 'feature' ? 'parent' : S.keyOrder(r.type).includes('feature') ? 'feature' : null;
|
|
170
|
+
if (!key) log(`row ${r.no}: Parent / home ignored for ${r.type}`);
|
|
171
|
+
else {
|
|
172
|
+
const hits = cardsNamed('feature', r.home);
|
|
173
|
+
if (hits.length !== 1) problem(r, `${key === 'parent' ? 'parent' : 'home'} feature "${r.home}" ${hits.length ? 'is ambiguous' : 'not found'} — created without ${key}:`);
|
|
174
|
+
else if (key === 'parent' && ws.byCore.get(hits[0]).card.fm.map.get('parent')) problem(r, `"${r.home}" is a Sub-feature (max two levels) — created without parent:`);
|
|
175
|
+
else opts[key] = hits[0];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
const { core, rel } = createCard(S, P, ws, r.type, r.name, opts);
|
|
180
|
+
writeCore(r, core);
|
|
181
|
+
stats.created++;
|
|
182
|
+
log(`${core} ${rel}${opts.parent ? ' · under: ' + opts.parent : ''}${opts.feature ? ' · feature: ' + opts.feature : ''}`);
|
|
183
|
+
} catch (e) {
|
|
184
|
+
if (e instanceof Refused || e instanceof Usage) problem(r, e.message);
|
|
185
|
+
else throw e;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
log(`scaffold: ${stats.created} created · ${stats.linked} linked to existing cards · ${stats.skipped} skipped (core present or project) · ${stats.problems} problem(s)`);
|
|
189
|
+
return stats;
|
|
190
|
+
}
|