yadflow 4.10.0 → 4.12.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/CHANGELOG.md +152 -0
- package/README.md +14 -3
- package/bin/commands.mjs +7 -1
- package/bin/yad.mjs +163 -16
- package/cli/agent-guard-worker.mjs +13 -0
- package/cli/agent-guard.mjs +1053 -57
- package/cli/agent-rules.mjs +59 -3
- package/cli/argv-flags.mjs +2 -2
- package/cli/brain/vendor/LICENSE-graphology-library.txt +21 -0
- package/cli/brain/vendor/LICENSE-graphology.txt +21 -0
- package/cli/brain/vendor/LICENSE-sigma.txt +12 -0
- package/cli/brain/vendor/graphology-library.min.js +2 -0
- package/cli/brain/vendor/graphology.umd.min.js +2 -0
- package/cli/brain/vendor/sigma.min.js +1369 -0
- package/cli/brain/view/app.js +382 -0
- package/cli/brain/view/index.html +63 -0
- package/cli/brain/view/live.css +228 -0
- package/cli/brain/view/live.html +110 -0
- package/cli/brain/view/live.js +884 -0
- package/cli/brain/view/style.css +141 -0
- package/cli/brain-about.mjs +206 -0
- package/cli/brain-activity.mjs +285 -0
- package/cli/brain-skills.mjs +118 -0
- package/cli/brain-vendor.mjs +71 -0
- package/cli/brain-view.mjs +345 -0
- package/cli/brain.mjs +810 -0
- package/cli/brand-cmd.mjs +326 -0
- package/cli/brand.mjs +327 -0
- package/cli/ci-check.mjs +3 -3
- package/cli/doctor.mjs +145 -14
- package/cli/epic-state.mjs +93 -1
- package/cli/epic.mjs +15 -3
- package/cli/errors.mjs +2 -1
- package/cli/find-product.mjs +69 -15
- package/cli/foundation-publish.mjs +256 -0
- package/cli/gate.mjs +29 -3
- package/cli/git-read.mjs +38 -0
- package/cli/guard-stream.mjs +409 -0
- package/cli/guide.mjs +26 -3
- package/cli/hook.mjs +59 -9
- package/cli/issues-act.mjs +57 -13
- package/cli/issues-write.mjs +19 -6
- package/cli/issues.mjs +69 -24
- package/cli/lib.mjs +37 -2
- package/cli/local-page.mjs +138 -0
- package/cli/local-setup-record.mjs +71 -0
- package/cli/local-setup-steps.mjs +186 -0
- package/cli/local-setup.mjs +611 -0
- package/cli/machine.mjs +1 -1
- package/cli/manifest.mjs +55 -0
- package/cli/map/view/app.js +712 -0
- package/cli/map/view/index.html +50 -0
- package/cli/map/view/style.css +142 -0
- package/cli/map-view.mjs +190 -0
- package/cli/map.mjs +637 -0
- package/cli/members.mjs +9 -15
- package/cli/next.mjs +1 -1
- package/cli/openpr.mjs +2 -2
- package/cli/plain-text.mjs +22 -0
- package/cli/plan.mjs +310 -36
- package/cli/platform.mjs +5 -5
- package/cli/product-index.mjs +1 -1
- package/cli/protection.mjs +32 -0
- package/cli/reconcile.mjs +61 -13
- package/cli/report.mjs +9 -3
- package/cli/reports-dir.mjs +393 -0
- package/cli/reports.mjs +65 -0
- package/cli/run-checks.mjs +5 -2
- package/cli/scan.mjs +4 -6
- package/cli/secret-text.mjs +66 -0
- package/cli/setup.mjs +54 -8
- package/cli/standup.mjs +94 -72
- package/cli/status.mjs +6 -5
- package/cli/test-agents-file.mjs +614 -8
- package/cli/test-brain-activity.mjs +753 -0
- package/cli/test-brain-view.mjs +381 -0
- package/cli/test-brain.mjs +795 -0
- package/cli/test-brand.mjs +558 -0
- package/cli/test-guide.mjs +438 -0
- package/cli/test-local-setup.mjs +1188 -0
- package/cli/test-machine.mjs +59 -0
- package/cli/test-map.mjs +615 -0
- package/cli/test-reports.mjs +382 -0
- package/cli/test-run-checks.mjs +4 -0
- package/cli/test-worktree.mjs +24 -3
- package/cli/toolbox-connect.mjs +21 -6
- package/cli/toolbox.mjs +74 -5
- package/cli/update-commit.mjs +118 -9
- package/cli/usage.mjs +17 -7
- package/cli/workspace.mjs +11 -2
- package/package.json +8 -4
- package/skills/sdlc/module-help.csv +1 -0
- package/skills/yad-architecture/SKILL.md +22 -4
- package/skills/yad-brand/SKILL.md +69 -0
- package/skills/yad-change/SKILL.md +3 -1
- package/skills/yad-change/references/triage.md +2 -0
- package/skills/yad-checks/SKILL.md +2 -0
- package/skills/yad-checks/references/check-gates.md +8 -5
- package/skills/yad-checks/templates/checks/build-test-lint.sh +7 -0
- package/skills/yad-checks/templates/checks/install-deps.sh +6 -0
- package/skills/yad-checks/templates/checks/package-manager.sh +42 -0
- package/skills/yad-checks/templates/github/yad-checks.yml +3 -2
- package/skills/yad-checks/templates/gitlab/yad-checks.gitlab-ci.yml +3 -2
- package/skills/yad-checks/templates/hooks/local-setup-notice.mjs +96 -0
- package/skills/yad-checks/templates/hooks/yad-activity.mjs +238 -0
- package/skills/yad-connect-repos/SKILL.md +14 -0
- package/skills/yad-discovery/SKILL.md +11 -0
- package/skills/yad-discovery/references/foundation-schema.md +11 -0
- package/skills/yad-docs/templates/app/package-lock.json +18 -10
- package/skills/yad-docs/templates/app/package.json +6 -1
- package/skills/yad-epic/SKILL.md +3 -0
- package/skills/yad-epic/references/state-schema.md +35 -1
- package/skills/yad-guide/SKILL.md +10 -7
- package/skills/yad-implement/SKILL.md +76 -15
- package/skills/yad-issues/SKILL.md +2 -1
- package/skills/yad-pr-template/templates/checks/pr-template.sh +8 -4
- package/skills/yad-pr-template/templates/checks/pr-title.sh +8 -4
- package/skills/yad-spec/SKILL.md +19 -0
- package/skills/yad-stories/SKILL.md +22 -4
- package/skills/yad-stories/references/story-schema.md +1 -1
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
// `yad brand [show] | init | check` (E184) — the Product's brand file, `.sdlc/brand.json`. See cli/brand.mjs.
|
|
2
|
+
//
|
|
3
|
+
// `init` fills the file from the Product's design system, on its own, when that is machine-readable:
|
|
4
|
+
// - a DESIGN.md with YAML front matter: `colors:` (named colours) and `typography:` (each style's fontFamily);
|
|
5
|
+
// - a DESIGN.md with a CSS block holding `:root { --name: value; }` (and a dark block, when there is one).
|
|
6
|
+
// Each colour ROLE is filled from the first name that means it (the table below), and the report says which
|
|
7
|
+
// name filled it — or that nothing did, so yadflow's value is used. It never invents a colour. A DESIGN.md in
|
|
8
|
+
// prose (tables) is the yad-brand skill's job: the agent reads it and writes the same file.
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { c, log, ok, info, warn, refuse, emitJSON, forTerminal, writeJSON } from './lib.mjs';
|
|
12
|
+
import { productConfigPath } from './manifest.mjs';
|
|
13
|
+
import { BRAND_FILE, BRAND_VERSION, ROLES, isColor, FAMILY_RE, IMAGE_CAP, checkBrand, readBrand, contrastProblems, sourceFingerprint, brandPath } from './brand.mjs';
|
|
14
|
+
|
|
15
|
+
export const BRAND_USAGE = 'usage: yad brand [show] [--json] · yad brand init [--from <DESIGN.md>] [--logo <file>] [--mark <file>] [--force] · yad brand check [--json]';
|
|
16
|
+
|
|
17
|
+
// The names that mean each role, most specific first. Compared lower-case, without a leading `--` or `color-`.
|
|
18
|
+
export const ROLE_NAMES = Object.freeze({
|
|
19
|
+
bg: ['bg', 'background', 'page', 'canvas', 'bg-primary', 'surface-page', 'base'],
|
|
20
|
+
surface: ['surface', 'card', 'panel', 'raised', 'bg-secondary', 'surface-1'],
|
|
21
|
+
text: ['ink', 'text', 'fg', 'foreground', 'text-primary', 'on-surface', 'on-background', 'body'],
|
|
22
|
+
muted: ['ink-secondary', 'muted', 'text-secondary', 'secondary-text', 'subtle', 'ink-muted', 'faint'],
|
|
23
|
+
border: ['border', 'line', 'hairline', 'divider', 'outline', 'stroke'],
|
|
24
|
+
primary: ['primary', 'brand', 'action', 'action-fill', 'primary-strong', 'primary-500', 'main'],
|
|
25
|
+
accent: ['accent', 'secondary', 'highlight', 'gold', 'accent-strong'],
|
|
26
|
+
});
|
|
27
|
+
const FONT_NAMES = Object.freeze({ heading: ['display', 'headline', 'heading', 'title', 'h1'], body: ['body', 'text', 'paragraph', 'base', 'sans'], mono: ['mono', 'code', 'monospace'] });
|
|
28
|
+
|
|
29
|
+
const norm = (k) => String(k).toLowerCase().replace(/^--/, '').replace(/^colou?r-/, '').trim();
|
|
30
|
+
|
|
31
|
+
// A small YAML reader for front matter: nested maps of scalars by indentation, `#` comments, quoted values.
|
|
32
|
+
// Lists and anchors are not read (a design system's colours and type are maps).
|
|
33
|
+
export function readFrontMatter(text) {
|
|
34
|
+
const m = /^---\r?\n([\s\S]*?)\r?\n---/.exec(String(text || ''));
|
|
35
|
+
if (!m) return null;
|
|
36
|
+
const root = {};
|
|
37
|
+
const stack = [{ indent: -1, obj: root }];
|
|
38
|
+
for (const raw of m[1].split(/\r?\n/)) {
|
|
39
|
+
if (!raw.trim() || /^\s*#/.test(raw) || /^\s*-/.test(raw)) continue;
|
|
40
|
+
const k = /^(\s*)([A-Za-z0-9_.-]+)\s*:\s*(.*)$/.exec(raw);
|
|
41
|
+
if (!k) continue;
|
|
42
|
+
const indent = k[1].length;
|
|
43
|
+
while (stack.length > 1 && indent <= stack[stack.length - 1].indent) stack.pop();
|
|
44
|
+
const parent = stack[stack.length - 1].obj;
|
|
45
|
+
let v = k[3].replace(/\s+#.*$/, '').trim();
|
|
46
|
+
if (v === '') { parent[k[2]] = {}; stack.push({ indent, obj: parent[k[2]] }); continue; }
|
|
47
|
+
v = v.replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1');
|
|
48
|
+
parent[k[2]] = v;
|
|
49
|
+
}
|
|
50
|
+
return root;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// A DESIGN.md's CSS as rules, in ONE pass (linear; reviews 1–3): comments and quoted text are skipped, so a
|
|
54
|
+
// brace or a selector inside them never counts. Each rule: { selector, text (its own declarations, never a
|
|
55
|
+
// nested rule's), rules (nested) }. A selector is the text since the last rule or statement, cut at a blank
|
|
56
|
+
// line or a code fence line (with its info string), so prose before a CSS block is never part of one.
|
|
57
|
+
// `css`: the text is CSS only (a fence's content), so a comment or a string starts anywhere, as in CSS.
|
|
58
|
+
export const CSS_DEPTH = 16;
|
|
59
|
+
export function cssRules(src, { css = false } = {}) {
|
|
60
|
+
const top = { selector: '', text: '', rules: [] };
|
|
61
|
+
const stack = [top];
|
|
62
|
+
let pending = '', deeper = 0; // braces past CSS_DEPTH are counted, never kept: no design system nests so far
|
|
63
|
+
const flush = () => { if (stack.length > 1) stack[stack.length - 1].text += `${pending};`; pending = ''; };
|
|
64
|
+
for (let i = 0; i < src.length; i++) {
|
|
65
|
+
const ch = src[i];
|
|
66
|
+
// A comment: inside a rule, or outside one after a space, a brace, `;` or `,` (or at the start) — never the
|
|
67
|
+
// `/*` of a path like `styles/*.css` in prose (reviews 5, 6).
|
|
68
|
+
if (ch === '/' && src[i + 1] === '*' && (css || stack.length > 1 || i === 0 || /[\s{};,]/.test(src[i - 1]))) {
|
|
69
|
+
const end = src.indexOf('*/', i + 2);
|
|
70
|
+
i = end < 0 ? src.length : end + 1;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
// A quoted string, only inside a rule (an apostrophe in prose is not one: review 4). It ends at its quote
|
|
74
|
+
// or at a newline, as CSS ends a string; a backslash escapes the next character.
|
|
75
|
+
if ((ch === '"' || ch === "'") && (css || stack.length > 1)) {
|
|
76
|
+
let j = i + 1;
|
|
77
|
+
while (j < src.length && src[j] !== ch && src[j] !== '\n') j += src[j] === '\\' ? 2 : 1;
|
|
78
|
+
pending += src.slice(i, Math.min(j + 1, src.length));
|
|
79
|
+
i = j;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (deeper) { if (ch === '{') deeper++; else if (ch === '}') deeper--; continue; }
|
|
83
|
+
if (ch === '{' && stack.length > CSS_DEPTH) { deeper = 1; pending = ''; continue; }
|
|
84
|
+
if (ch === '{') {
|
|
85
|
+
const rule = { selector: pending.split(/\n\s*\n|```[^\n]*/).pop().trim(), text: '', rules: [] };
|
|
86
|
+
stack[stack.length - 1].rules.push(rule);
|
|
87
|
+
stack.push(rule);
|
|
88
|
+
pending = '';
|
|
89
|
+
} else if (ch === '}') {
|
|
90
|
+
flush();
|
|
91
|
+
if (stack.length > 1) stack.pop();
|
|
92
|
+
} else if (ch === ';') flush();
|
|
93
|
+
else pending += ch;
|
|
94
|
+
}
|
|
95
|
+
return top.rules;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// `.dark`, `:root.dark`, `html.dark`, `.dark-mode`, `.dark-theme` — never `.darkmode` or `.dark-blue`. The media
|
|
99
|
+
// query is matched on the selector's own line, so a heading or prose line just above it never hides it.
|
|
100
|
+
const DARK_SELECTOR = /\[data-theme=["']?dark["']?\]|\.dark(?:-mode|-theme)?(?![\w-])/;
|
|
101
|
+
const DARK_MEDIA = /(?:^|\n)\s*@media\b[^{\n]*prefers-color-scheme\s*:\s*dark/;
|
|
102
|
+
const ROOT = /(?:^|[\s,]):root\b/;
|
|
103
|
+
const hasVars = (r) => /--[A-Za-z0-9_-]+\s*:/.test(r.text);
|
|
104
|
+
// A selector without its `:not(…)` groups, nested brackets and all: `:root:not(.dark)` is the light look,
|
|
105
|
+
// never a dark one (reviews 5, 6).
|
|
106
|
+
function bare(sel) {
|
|
107
|
+
let out = '', depth = 0;
|
|
108
|
+
for (let i = 0; i < sel.length; i++) {
|
|
109
|
+
if (!depth && sel.slice(i, i + 5).toLowerCase() === ':not(') { depth = 1; i += 4; continue; }
|
|
110
|
+
if (depth) { if (sel[i] === '(') depth++; else if (sel[i] === ')') depth--; continue; }
|
|
111
|
+
out += sel[i];
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
const isRootish = (r) => /(?:^|[\s,])(?::root|html|body)\b/.test(r.selector);
|
|
116
|
+
|
|
117
|
+
// A markdown file's fenced code blocks, as CommonMark reads them (review 8): an opening run of 3 or more
|
|
118
|
+
// backticks or tildes (a backtick fence's label holds no backtick), closed by a run of the same character at
|
|
119
|
+
// least as long with nothing after it. → [{ label, body }]; an unclosed fence runs to the end. One pass by line.
|
|
120
|
+
export function fencedBlocks(text) {
|
|
121
|
+
const out = [];
|
|
122
|
+
let open = null;
|
|
123
|
+
for (const raw of String(text).split('\n')) {
|
|
124
|
+
const line = raw.replace(/\r$/, '');
|
|
125
|
+
if (!open) {
|
|
126
|
+
const m = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
|
|
127
|
+
if (m && !(m[1][0] === '`' && m[2].includes('`'))) { open = { run: m[1], label: m[2].trim().split(/\s+/)[0].replace(/^\{?\.?|\}$/g, '').toLowerCase(), body: [] }; out.push(open); }
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const close = /^ {0,3}(`{3,}|~{3,})\s*$/.exec(line);
|
|
131
|
+
if (close && close[1][0] === open.run[0] && close[1].length >= open.run.length) { open = null; continue; }
|
|
132
|
+
open.body.push(line);
|
|
133
|
+
}
|
|
134
|
+
return out.map((f) => ({ label: f.label, body: f.body.join('\n') }));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const CSS_LABELS = new Set(['css', 'scss', 'less', 'postcss']);
|
|
138
|
+
|
|
139
|
+
// The `--name: value` pairs of the first `:root` rule that is not a dark one, and of the dark rule when there
|
|
140
|
+
// is one: an `@media (prefers-color-scheme: dark)` block's `:root`, `html` or `body` rule (else its first rule
|
|
141
|
+
// with a `--` name), or a `[data-theme="dark"]` / `.dark` rule anywhere, inside another `@media` too.
|
|
142
|
+
export function readRootVars(text) {
|
|
143
|
+
const src = String(text || '');
|
|
144
|
+
// A DESIGN.md's CSS fences (or, with none, its fences with no label), each read on its own as CSS, so nothing
|
|
145
|
+
// in one fence (a shell glob, an unclosed comment) reaches another. With no such fence, or one that gives no
|
|
146
|
+
// light colours, the whole text is read with the prose rules (reviews 7, 8).
|
|
147
|
+
const fences = fencedBlocks(src);
|
|
148
|
+
const styled = fences.filter((f) => CSS_LABELS.has(f.label));
|
|
149
|
+
const chosen = styled.length ? styled : fences.filter((f) => !f.label);
|
|
150
|
+
const read = (rules) => {
|
|
151
|
+
const all = [];
|
|
152
|
+
const walk = (list, darkMedia) => { for (const r of list) { all.push({ r, darkMedia }); walk(r.rules, darkMedia || DARK_MEDIA.test(r.selector)); } };
|
|
153
|
+
walk(rules, false);
|
|
154
|
+
const vars = (block) => Object.fromEntries([...block.matchAll(/(--[A-Za-z0-9_-]+)\s*:\s*([^;]+)/g)].map((x) => [x[1], x[2].trim()]));
|
|
155
|
+
// The first light :root rule with a `--` name: `:root { color-scheme: light dark }` before it is skipped (review 9).
|
|
156
|
+
const light = all.find(({ r, darkMedia }) => !darkMedia && ROOT.test(r.selector) && !DARK_SELECTOR.test(bare(r.selector)) && hasVars(r));
|
|
157
|
+
let dark = null;
|
|
158
|
+
for (const { r } of all) {
|
|
159
|
+
if (DARK_MEDIA.test(r.selector)) dark = r.rules.find((x) => isRootish(x) && hasVars(x)) || r.rules.find(hasVars) || null;
|
|
160
|
+
else if (DARK_SELECTOR.test(bare(r.selector)) && hasVars(r)) dark = r;
|
|
161
|
+
if (dark) break;
|
|
162
|
+
}
|
|
163
|
+
return light ? { light: vars(light.r.text), dark: dark ? vars(dark.text) : null } : null;
|
|
164
|
+
};
|
|
165
|
+
const fenced = chosen.length ? read(chosen.flatMap((f) => cssRules(f.body, { css: true }))) : null;
|
|
166
|
+
return fenced && Object.keys(fenced.light).length ? fenced : read(cssRules(src));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Fill each role from a { name: value } map. → { colors: { role: value }, filledFrom: { role: name } }
|
|
170
|
+
export function rolesFrom(map, { darkPrefix = false } = {}) {
|
|
171
|
+
const colors = {}, filledFrom = {};
|
|
172
|
+
const entries = Object.entries(map || {}).filter(([, v]) => isColor(v));
|
|
173
|
+
const pick = (k) => { const n = norm(k); if (darkPrefix) { const m = /^(?:dark-(.+)|(.+)-dark)$/.exec(n); return m ? (m[1] || m[2]) : null; } return /(^dark-|-dark$)/.test(n) ? null : n; };
|
|
174
|
+
for (const role of ROLES) {
|
|
175
|
+
for (const want of ROLE_NAMES[role]) {
|
|
176
|
+
const hit = entries.find(([k]) => pick(k) === want);
|
|
177
|
+
if (hit) { colors[role] = hit[1].trim(); filledFrom[role] = hit[0]; break; }
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return { colors, filledFrom };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function familyOf(v) {
|
|
184
|
+
const first = String(v || '').split(',')[0].trim().replace(/^["']|["']$/g, '');
|
|
185
|
+
return FAMILY_RE.test(first) ? first : null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Find files in the Product's design folders: depth 3, no links, never node_modules or .git.
|
|
189
|
+
function findFiles(root, test) {
|
|
190
|
+
const out = [];
|
|
191
|
+
const walk = (dir, depth) => {
|
|
192
|
+
let list;
|
|
193
|
+
try { list = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
194
|
+
for (const d of list) {
|
|
195
|
+
if (d.name.startsWith('.') || d.name === 'node_modules') continue;
|
|
196
|
+
const full = path.join(dir, d.name);
|
|
197
|
+
if (d.isSymbolicLink()) continue;
|
|
198
|
+
if (d.isDirectory() && depth > 0) walk(full, depth - 1);
|
|
199
|
+
else if (d.isFile() && test(d.name)) out.push(path.relative(root, full).split(path.sep).join('/'));
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
for (const top of ['design', 'designs', 'design-system', 'brand', 'assets']) walk(path.join(root, top), 3);
|
|
203
|
+
return out.sort();
|
|
204
|
+
}
|
|
205
|
+
const smallEnough = (root, rel) => { try { return fs.statSync(path.join(root, rel)).size <= IMAGE_CAP; } catch { return false; } };
|
|
206
|
+
const prefer = (list) => list.find((f) => f.endsWith('.svg')) || list.find((f) => f.endsWith('.png')) || list[0] || null;
|
|
207
|
+
|
|
208
|
+
// Build a brand.json from a DESIGN.md's text. → { brand, report: { filled, missing, fonts, logo, mark } }
|
|
209
|
+
export function brandFromDesign(root, text, { name = null, from = 'DESIGN.md', logo = null, mark = null } = {}) {
|
|
210
|
+
const fm = readFrontMatter(text);
|
|
211
|
+
const css = readRootVars(text);
|
|
212
|
+
let light = { colors: {}, filledFrom: {} }, dark = { colors: {}, filledFrom: {} }, how = null;
|
|
213
|
+
if (fm && fm.colors && typeof fm.colors === 'object') {
|
|
214
|
+
light = rolesFrom(fm.colors); dark = rolesFrom(fm.colors, { darkPrefix: true }); how = 'front matter';
|
|
215
|
+
}
|
|
216
|
+
if (Object.keys(light.colors).length < 3 && css) {
|
|
217
|
+
const l = rolesFrom(css.light);
|
|
218
|
+
if (Object.keys(l.colors).length > Object.keys(light.colors).length) { light = l; dark = css.dark ? rolesFrom(css.dark) : { colors: {}, filledFrom: {} }; how = ':root block'; }
|
|
219
|
+
}
|
|
220
|
+
const fonts = { heading: null, body: null, mono: null };
|
|
221
|
+
const fontFrom = {};
|
|
222
|
+
if (fm && fm.typography && typeof fm.typography === 'object') {
|
|
223
|
+
for (const [slot, names] of Object.entries(FONT_NAMES)) {
|
|
224
|
+
for (const n of names) { const f = familyOf(fm.typography[n]?.fontFamily); if (f) { fonts[slot] = { family: f, files: [] }; fontFrom[slot] = `typography.${n}`; break; } }
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (css) {
|
|
228
|
+
for (const [slot, names] of Object.entries(FONT_NAMES)) {
|
|
229
|
+
if (fonts[slot]) continue;
|
|
230
|
+
for (const n of names) { const f = familyOf(css.light[`--font-${n}`]); if (f) { fonts[slot] = { family: f, files: [] }; fontFrom[slot] = `--font-${n}`; break; } }
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (!fonts.body && fonts.heading) fonts.body = { ...fonts.heading, files: [] };
|
|
234
|
+
for (const slot of Object.keys(fonts)) {
|
|
235
|
+
if (!fonts[slot]) continue;
|
|
236
|
+
const key = fonts[slot].family.toLowerCase().replace(/[\s_-]/g, '');
|
|
237
|
+
fonts[slot].files = findFiles(root, (n) => /\.(woff2|woff|ttf|otf)$/i.test(n) && n.toLowerCase().replace(/[\s_-]/g, '').includes(key)).slice(0, 4);
|
|
238
|
+
}
|
|
239
|
+
const images = findFiles(root, (n) => /\.(svg|png|webp|jpe?g)$/i.test(n)).filter((f) => smallEnough(root, f));
|
|
240
|
+
const logoFile = logo || prefer(images.filter((f) => /logo|wordmark/i.test(path.posix.basename(f))));
|
|
241
|
+
const markFile = mark || prefer(images.filter((f) => /mark|icon|favicon|symbol/i.test(path.posix.basename(f)))) || logoFile;
|
|
242
|
+
const productName = (() => { try { const n = JSON.parse(fs.readFileSync(productConfigPath(root), 'utf8')).name; return typeof n === 'string' && n.trim() ? n : null; } catch { return null; } })();
|
|
243
|
+
const title = name || (typeof fm?.name === 'string' && fm.name) || (typeof fm?.title === 'string' && fm.title) || productName || (/^#\s+(.+)$/m.exec(String(text).replace(/^---[\s\S]*?\n---/, ''))?.[1]) || path.basename(root);
|
|
244
|
+
const cleanName = String(title).split(/\s+[—–-]\s+|\s+\(/)[0].trim().slice(0, 80);
|
|
245
|
+
const brand = {
|
|
246
|
+
version: BRAND_VERSION, name: cleanName, source: from, sourceHash: sourceFingerprint(text),
|
|
247
|
+
logo: logoFile, mark: markFile,
|
|
248
|
+
colors: { light: light.colors, dark: Object.keys(dark.colors).length >= 3 ? dark.colors : null },
|
|
249
|
+
fonts, status: null,
|
|
250
|
+
};
|
|
251
|
+
return { brand, report: { how, filled: { light: light.filledFrom, dark: dark.filledFrom }, missing: ROLES.filter((r) => !light.colors[r]), fonts: fontFrom, logo: logoFile, mark: markFile } };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export async function runBrand(root, { action = 'show', args = [], from = null, logo = null, mark = null, force = false, json = false } = {}) {
|
|
255
|
+
if (args.length) return refuse(`yad brand ${action} takes no words (got: ${args.map(forTerminal).join(' ')})`, BRAND_USAGE, { json });
|
|
256
|
+
|
|
257
|
+
if (action === 'show' || action === 'check') {
|
|
258
|
+
const r = readBrand(root);
|
|
259
|
+
const exists = fs.existsSync(r.file);
|
|
260
|
+
const contrastLines = contrastProblems(r.brand, { links: true });
|
|
261
|
+
let stale = null;
|
|
262
|
+
if (r.origin === 'product' && r.brand.source) {
|
|
263
|
+
const src = brandPath(root, r.brand.source);
|
|
264
|
+
let text = null;
|
|
265
|
+
try { text = src ? fs.readFileSync(src, 'utf8') : null; } catch { /* a folder, or unreadable */ }
|
|
266
|
+
if (!src) stale = `its source ${r.brand.source} is not in the Product any more`;
|
|
267
|
+
else if (text === null) stale = `its source ${r.brand.source} cannot be read`;
|
|
268
|
+
else if (!r.brand.sourceHash) stale = null;
|
|
269
|
+
else if (sourceFingerprint(text) !== r.brand.sourceHash) stale = `${r.brand.source} changed since ${BRAND_FILE} was made — run \`yad brand init --force\`, or update the file`;
|
|
270
|
+
}
|
|
271
|
+
// A file written by hand has no fingerprint, so nothing can say when its design system moves on (review 1).
|
|
272
|
+
if (r.origin === 'product' && !r.brand.sourceHash) r.warnings = [...r.warnings, `no sourceHash in ${BRAND_FILE}, so yad cannot tell when the design system changes — \`yad brand init --force\` stamps it, if the design system is one it reads`];
|
|
273
|
+
if (action === 'check' && exists && r.problems.length) process.exitCode = 1;
|
|
274
|
+
const payload = { origin: r.origin, file: BRAND_FILE, exists, name: r.brand.name, logo: r.brand.logo, mark: r.brand.mark, colors: r.brand.colors, fonts: r.brand.fonts, status: r.brand.status,
|
|
275
|
+
problems: r.problems, warnings: r.warnings, contrast: contrastLines, stale };
|
|
276
|
+
if (json) return emitJSON(action === 'check' ? { ...payload, ok: !(exists && r.problems.length) } : payload);
|
|
277
|
+
log(c.bold(`\nyad brand${action === 'check' ? ' check' : ''}`));
|
|
278
|
+
if (r.origin === 'yadflow') info(exists ? `${BRAND_FILE} cannot be used, so pages wear yadflow's look:` : `no ${BRAND_FILE} — pages wear yadflow's look. \`yad brand init\` makes one from the Product's DESIGN.md`);
|
|
279
|
+
else ok(`${forTerminal(r.brand.name)} — from ${BRAND_FILE}${r.brand.source ? ` (made from ${forTerminal(r.brand.source)})` : ''}`);
|
|
280
|
+
for (const p of r.problems) warn(forTerminal(p));
|
|
281
|
+
if (r.origin === 'product') {
|
|
282
|
+
info(`logo: ${forTerminal(r.brand.logo || '—')} · mark: ${forTerminal(r.brand.mark || '—')}`);
|
|
283
|
+
for (const scheme of ['light', 'dark']) info(`${scheme}${scheme === 'dark' && r.brand.colors.derivedDark ? ' (made from light)' : ''}: ${ROLES.map((role) => `${role} ${r.brand.colors[scheme][role]}`).join(' · ')}`);
|
|
284
|
+
info(`fonts: ${['heading', 'body', 'mono'].map((s) => `${s} ${r.brand.fonts?.[s] ? `${r.brand.fonts[s].family}${r.brand.fonts[s].files.length ? ' (embedded)' : ''}` : 'system'}`).join(' · ')}`);
|
|
285
|
+
}
|
|
286
|
+
for (const w of r.warnings) info(c.dim(forTerminal(w)));
|
|
287
|
+
for (const x of contrastLines) warn(x);
|
|
288
|
+
if (stale) warn(forTerminal(stale));
|
|
289
|
+
return payload;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (action === 'init') {
|
|
293
|
+
const file = path.join(root, BRAND_FILE);
|
|
294
|
+
if (fs.existsSync(file) && !force) return refuse(`${BRAND_FILE} is already there`, 'add --force to make it again from the design system (your edits to it are replaced)', { json });
|
|
295
|
+
const srcRel = from || 'DESIGN.md';
|
|
296
|
+
const src = brandPath(root, srcRel);
|
|
297
|
+
if (!src) return refuse(`no ${forTerminal(srcRel)} in the Product`, 'name the design system with --from <file> (a DESIGN.md), or ask the agent to fill the brand with the yad-brand skill', { json });
|
|
298
|
+
for (const [flag, rel] of [['--logo', logo], ['--mark', mark]]) if (rel && !brandPath(root, rel)) return refuse(`${flag} ${forTerminal(rel)} is not a file inside the Product`, null, { json });
|
|
299
|
+
let text;
|
|
300
|
+
try { text = fs.readFileSync(src, 'utf8'); } catch { return refuse(`${forTerminal(srcRel)} cannot be read as a file`, null, { json }); }
|
|
301
|
+
// Relative to the Product's REAL path: `src` is real (brandPath), and a Product reached through a link
|
|
302
|
+
// must never write a path that leaves it, or this machine's folders, into a committed file (review 1).
|
|
303
|
+
const { brand, report } = brandFromDesign(root, text, { from: path.relative(fs.realpathSync(root), src).split(path.sep).join('/'), logo, mark });
|
|
304
|
+
if (!Object.keys(brand.colors.light).length) return refuse(`${forTerminal(srcRel)} has no colours yadflow can read (no front matter \`colors:\` and no \`:root\` block)`, 'ask the agent to fill the brand with the yad-brand skill — it reads prose and design tools', { json });
|
|
305
|
+
const checked = checkBrand(root, brand);
|
|
306
|
+
if (checked.problems.length) return refuse(`the brand made from ${forTerminal(srcRel)} cannot be used: ${checked.problems.join('; ')}`, null, { json });
|
|
307
|
+
const { fontBytes, ...toWrite } = checked.brand;
|
|
308
|
+
void fontBytes;
|
|
309
|
+
writeJSON(file, toWrite);
|
|
310
|
+
if (json) return emitJSON({ file: BRAND_FILE, brand: toWrite, report, warnings: checked.warnings });
|
|
311
|
+
log(c.bold('\nyad brand init'));
|
|
312
|
+
ok(`wrote ${BRAND_FILE} for ${forTerminal(toWrite.name)} from ${forTerminal(toWrite.source)} (${report.how})`);
|
|
313
|
+
for (const scheme of ['light', 'dark']) {
|
|
314
|
+
const f = report.filled[scheme];
|
|
315
|
+
if (Object.keys(f).length) info(`${scheme}: ${Object.entries(f).map(([role, n]) => `${role} ← ${forTerminal(n)}`).join(' · ')}`);
|
|
316
|
+
}
|
|
317
|
+
if (report.missing.length) warn(`not found, so yadflow's is used: ${report.missing.join(', ')}`);
|
|
318
|
+
if (!toWrite.colors.dark) info('no dark colours found: a dark look is made from a neutral dark with your primary and accent');
|
|
319
|
+
info(`fonts: ${Object.entries(toWrite.fonts).map(([s, f]) => `${s} ${f ? `${f.family}${f.files.length ? ` (${f.files.length} file${f.files.length > 1 ? 's' : ''})` : ' (name only)'}` : 'system'}`).join(' · ')}`);
|
|
320
|
+
info(`logo: ${forTerminal(toWrite.logo || 'none found')} · mark: ${forTerminal(toWrite.mark || 'none found')}${toWrite.logo ? '' : ' — pass --logo <file>'}`);
|
|
321
|
+
info('check it with `yad brand`, then commit it: every page yad makes for this Product wears it');
|
|
322
|
+
return { file: BRAND_FILE, brand: toWrite, report };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return refuse(`unknown action: yad brand ${forTerminal(action)}`, BRAND_USAGE, { json });
|
|
326
|
+
}
|