decklight 0.8.1 → 0.10.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/PLATFORM.md +707 -0
- package/README.md +226 -150
- package/SPEC.md +803 -888
- package/cli/agent-review.mjs +246 -0
- package/cli/agents.mjs +35 -9
- package/cli/associate.mjs +12 -12
- package/cli/audit.mjs +42 -10
- package/cli/banner.mjs +41 -17
- package/cli/bundle-audio.mjs +190 -0
- package/cli/bundle-module.mjs +95 -0
- package/cli/bundle.mjs +439 -35
- package/cli/cast.mjs +48 -12
- package/cli/check-fixes.mjs +186 -0
- package/cli/check.mjs +988 -0
- package/cli/clone-deck.mjs +179 -0
- package/cli/commands.mjs +345 -0
- package/cli/comments.mjs +64 -18
- package/cli/commit-flow.mjs +51 -22
- package/cli/commit-message.mjs +51 -74
- package/cli/commit-split.mjs +601 -0
- package/cli/deck-refs.mjs +64 -0
- package/cli/deck-routes.mjs +67 -0
- package/cli/deckfile.mjs +3 -5
- package/cli/decklight.mjs +68 -289
- package/cli/design-system-deps.mjs +209 -0
- package/cli/design-system-edit.mjs +194 -0
- package/cli/design-system-refs.mjs +666 -0
- package/cli/design-system.mjs +497 -0
- package/cli/doctor.mjs +115 -0
- package/cli/edit-git.mjs +353 -0
- package/cli/edit-handover.mjs +551 -0
- package/cli/edit-look.mjs +631 -0
- package/cli/edit-slides.mjs +683 -0
- package/cli/edit.mjs +2585 -855
- package/cli/enhance.mjs +485 -0
- package/cli/extension.mjs +10 -10
- package/cli/font-refs.mjs +273 -0
- package/cli/font.mjs +246 -0
- package/cli/git-tree.mjs +1 -1
- package/cli/git.mjs +78 -11
- package/cli/import.mjs +56 -10
- package/cli/init.mjs +407 -236
- package/cli/library.mjs +127 -0
- package/cli/loader.mjs +3 -1
- package/cli/marketplace-autocheck.mjs +200 -0
- package/cli/marketplace.mjs +460 -29
- package/cli/narrate.mjs +251 -0
- package/cli/open-browser.mjs +63 -0
- package/cli/open.mjs +961 -0
- package/cli/pdf.mjs +64 -17
- package/cli/pkg.mjs +44 -2
- package/cli/plugin.mjs +43 -41
- package/cli/port-conflict.mjs +53 -23
- package/cli/pptx-export.mjs +63 -27
- package/cli/publish.mjs +8 -4
- package/cli/qr.mjs +1 -1
- package/cli/record.mjs +85 -23
- package/cli/remote.mjs +43 -17
- package/cli/restore.mjs +11 -4
- package/cli/review-ref.mjs +125 -0
- package/cli/review-remote.mjs +92 -48
- package/cli/review-routes.mjs +254 -0
- package/cli/review-store.mjs +47 -7
- package/cli/review-submit.mjs +47 -57
- package/cli/runtime-link.mjs +560 -0
- package/cli/serve.mjs +236 -45
- package/cli/setup.mjs +112 -0
- package/cli/sign.mjs +1 -1
- package/cli/skill-content.mjs +229 -35
- package/cli/skills.mjs +353 -26
- package/cli/start.mjs +110 -0
- package/cli/supervise.mjs +6 -6
- package/cli/theme-refs.mjs +527 -0
- package/cli/theme.mjs +181 -137
- package/cli/trust.mjs +83 -0
- package/cli/units.mjs +112 -47
- package/cli/update-check.mjs +1 -5
- package/cli/upgrade.mjs +194 -28
- package/cli/upstream.mjs +4 -3
- package/cli/util.mjs +4 -4
- package/cli/wizard.mjs +6 -6
- package/dist/build-info.json +1 -1
- package/dist/decklight.css +1533 -59
- package/dist/decklight.js +258 -115
- package/dist/decklight.js.map +4 -4
- package/docs/GLOSSARY.md +141 -0
- package/docs/architecture.svg +17 -17
- package/package.json +4 -2
- package/themes/README.md +9 -8
- package/themes/reveal-solarized.css +10 -0
- package/tools/args.mjs +20 -0
- package/tools/atomic-write.mjs +48 -0
- package/tools/brand-palette.mjs +717 -0
- package/tools/bridge.mjs +1 -1
- package/tools/build-groups.mjs +42 -0
- package/tools/color.mjs +48 -0
- package/tools/deck-html.mjs +396 -11
- package/tools/design-system-format.mjs +778 -0
- package/tools/design-system-migrations.mjs +207 -0
- package/tools/elevenlabs-tts.mjs +110 -17
- package/tools/exec.mjs +11 -3
- package/tools/extension-check.mjs +31 -7
- package/tools/font-format.mjs +190 -0
- package/tools/font-stacks.mjs +24 -0
- package/tools/gemini-tts.mjs +3 -0
- package/tools/lipsync-config.mjs +103 -0
- package/tools/lipsync-engines.mjs +205 -2
- package/tools/lipsync-server.mjs +264 -41
- package/tools/lipsync.mjs +37 -18
- package/tools/local-voice.mjs +32 -42
- package/tools/narration-manifest.mjs +270 -0
- package/tools/narration-synth.mjs +416 -0
- package/tools/pptx.mjs +776 -19
- package/tools/publish-targets.mjs +1 -1
- package/tools/publish-voices.mjs +3 -3
- package/tools/recording-impact.mjs +7 -3
- package/tools/render-theme.mjs +33 -0
- package/tools/review-anchor.mjs +28 -12
- package/tools/semver.mjs +40 -0
- package/tools/sentences.mjs +180 -0
- package/tools/shot.mjs +14 -7
- package/tools/site-clip.mjs +114 -0
- package/tools/site-voices.mjs +232 -0
- package/tools/template-theme.mjs +1 -1
- package/tools/test-impact.mjs +17 -10
- package/tools/theme-check.mjs +30 -3
- package/tools/tts-cache.mjs +4 -3
- package/tools/tts-engines.mjs +38 -20
- package/tools/tts-setup.mjs +7 -7
- package/tools/video-options.mjs +34 -0
- package/tools/video.mjs +530 -54
- package/tools/voiceover-server.mjs +185 -104
- package/tools/voiceover.mjs +119 -261
- package/tools/zip.mjs +1 -1
- package/cli/dev.mjs +0 -623
- package/cli/present.mjs +0 -853
- package/cli/review.mjs +0 -361
- package/tools/publish-site-voices.mjs +0 -75
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// The commit window's other half (SPEC PRESENTING): uncommitted work cut into
|
|
5
|
+
// several commits, a tag on what was committed, and a push of it.
|
|
6
|
+
//
|
|
7
|
+
// SEVERAL COMMITS OUT OF ONE FILE. A deck is one file, so "these edits are two
|
|
8
|
+
// commits" means committing part of a file, which `git add -- <deck>` cannot
|
|
9
|
+
// do. The work is cut into UNITS: each hunk of the deck's diff against HEAD
|
|
10
|
+
// (at zero context, so two edits on one slide are two hunks), and each other
|
|
11
|
+
// file that rides the deck's commits (the review sidecar, the .gitattributes
|
|
12
|
+
// decklight wrote) whole. A plan is a list of commits, each a subject and the
|
|
13
|
+
// units it takes. Commit k's deck is HEAD's deck with the units of commits
|
|
14
|
+
// 1..k applied, so the last commit's deck is the working file, byte for byte.
|
|
15
|
+
//
|
|
16
|
+
// BUILT ASIDE, LANDED ONCE. The commits are made with plumbing in a private
|
|
17
|
+
// index (read-tree HEAD, update-index, write-tree, commit-tree), never the
|
|
18
|
+
// author's: whatever else they have staged stays staged. The branch moves
|
|
19
|
+
// once, from the HEAD the plan was made on to the last commit, with
|
|
20
|
+
// update-ref's old-value check, so a failure halfway leaves nothing behind
|
|
21
|
+
// and a HEAD that moved in the meantime is refused rather than overwritten.
|
|
22
|
+
// commit-tree runs no hooks, which is the same thing the snapshot already
|
|
23
|
+
// does; the one-commit path (gitAutocommit) still runs them.
|
|
24
|
+
//
|
|
25
|
+
// A PLAN IS FOR THE WORK IT WAS MADE ON. Its `base` is a fingerprint of HEAD
|
|
26
|
+
// and of every unit; committing re-reads both and refuses a plan whose work
|
|
27
|
+
// changed under it (an edit landed while the agent was thinking), because
|
|
28
|
+
// unit 3 of one diff is not unit 3 of another.
|
|
29
|
+
|
|
30
|
+
import { execFileSync, execFile } from 'node:child_process';
|
|
31
|
+
import { createHash } from 'node:crypto';
|
|
32
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
33
|
+
import { tmpdir } from 'node:os';
|
|
34
|
+
import { join, resolve as resolvePath } from 'node:path';
|
|
35
|
+
import { agentAsk } from './agents.mjs';
|
|
36
|
+
import { ASK_TIMEOUT_MS, ask, deckOutline, deckTitle, reviewSubject } from './commit-message.mjs';
|
|
37
|
+
import { GIT_MAX_BUFFER, classifyRemoteError, commitSubject, isIdentityError, noPromptEnv, oneline, remoteState } from './git.mjs';
|
|
38
|
+
|
|
39
|
+
/** Past this many hunks, the hunks of one slide travel together. */
|
|
40
|
+
export const MAX_UNITS = 40;
|
|
41
|
+
|
|
42
|
+
/** How long a push may take before it is called offline. */
|
|
43
|
+
export const PUSH_TIMEOUT_MS = 60_000;
|
|
44
|
+
|
|
45
|
+
/** git, untrimmed: a diff's last line may end in the very spaces it changed. */
|
|
46
|
+
const raw = (args, cwd, { input, env } = {}) => execFileSync('git', args, {
|
|
47
|
+
cwd, encoding: 'utf8', maxBuffer: GIT_MAX_BUFFER, input, env,
|
|
48
|
+
stdio: [input === undefined ? 'ignore' : 'pipe', 'pipe', 'pipe'],
|
|
49
|
+
});
|
|
50
|
+
const run = (args, cwd, opts) => raw(args, cwd, opts).trim();
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The `-U0` hunks of one file's diff, each `{ oldStart, oldCount, newStart,
|
|
54
|
+
* newCount, add, text }`. Pure. Null when the diff has a "No newline" marker:
|
|
55
|
+
* those hunks do not splice by line, so the file goes as one unit instead.
|
|
56
|
+
*/
|
|
57
|
+
export function parseZeroHunks(diff) {
|
|
58
|
+
const out = [];
|
|
59
|
+
let cur = null;
|
|
60
|
+
for (const line of String(diff ?? '').split('\n')) {
|
|
61
|
+
const h = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
62
|
+
if (h) {
|
|
63
|
+
cur = {
|
|
64
|
+
oldStart: Number(h[1]), oldCount: h[2] === undefined ? 1 : Number(h[2]),
|
|
65
|
+
newStart: Number(h[3]), newCount: h[4] === undefined ? 1 : Number(h[4]),
|
|
66
|
+
add: [], text: [line],
|
|
67
|
+
};
|
|
68
|
+
out.push(cur);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (!cur) continue;
|
|
72
|
+
if (line.startsWith('\\')) return null;
|
|
73
|
+
if (line.startsWith('+')) { cur.add.push(line.slice(1)); cur.text.push(line); }
|
|
74
|
+
else if (line.startsWith('-')) cur.text.push(line);
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* `text` with the chosen hunks of its `-U0` diff applied. Pure. Every hunk is
|
|
81
|
+
* in the old file's coordinates, so they are spliced from the bottom up and
|
|
82
|
+
* none moves another.
|
|
83
|
+
*/
|
|
84
|
+
export function applyHunks(text, hunks) {
|
|
85
|
+
const lines = String(text).split('\n');
|
|
86
|
+
for (const h of [...hunks].sort((a, b) => b.oldStart - a.oldStart)) {
|
|
87
|
+
// -a,0 is an insertion AFTER line a; -a,b replaces lines a..a+b-1
|
|
88
|
+
const at = h.oldCount === 0 ? h.oldStart : h.oldStart - 1;
|
|
89
|
+
lines.splice(at, h.oldCount, ...h.add);
|
|
90
|
+
}
|
|
91
|
+
return lines.join('\n');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const slidesAt = (outline, from, count) => {
|
|
95
|
+
const last = from + Math.max(count, 1) - 1;
|
|
96
|
+
return outline.filter((s) => s.from <= last && s.to >= from).map((s) => s.index);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** "slide 3", "slides 3-4", "slides 2, 5", or where a change outside any slide is. */
|
|
100
|
+
export function whereLabel(slides) {
|
|
101
|
+
if (!slides.length) return 'theme or metadata';
|
|
102
|
+
if (slides.length === 1) return `slide ${slides[0]}`;
|
|
103
|
+
const run_ = slides.every((s, i) => i === 0 || s === slides[i - 1] + 1);
|
|
104
|
+
return run_ ? `slides ${slides[0]}-${slides[slides.length - 1]}` : `slides ${slides.join(', ')}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The uncommitted work, as units: `{ head, base, units, files }` or null when
|
|
109
|
+
* there is no HEAD to cut against (a first commit is one commit). Each unit is
|
|
110
|
+
* `{ id, file, whole, hunks, slides, where, lines, diff }`; `files` maps each
|
|
111
|
+
* file to `{ head, work, mode }`.
|
|
112
|
+
*/
|
|
113
|
+
export function workingUnits({ cwd, deckRel, also = [] }) {
|
|
114
|
+
let head;
|
|
115
|
+
try { head = run(['rev-parse', '--verify', 'HEAD'], cwd); } catch { return null; }
|
|
116
|
+
const units = [];
|
|
117
|
+
const files = {};
|
|
118
|
+
const hash = createHash('sha1').update(head);
|
|
119
|
+
const headText = (rel) => { try { return raw(['show', `HEAD:./${rel}`], cwd); } catch { return null; } };
|
|
120
|
+
const modeOf = (rel) => {
|
|
121
|
+
try { return run(['ls-tree', 'HEAD', '--', rel], cwd).split(/\s/)[0] || '100644'; } catch { return '100644'; }
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
for (const rel of [deckRel, ...also.filter(Boolean)]) {
|
|
125
|
+
const abs = resolvePath(cwd, rel);
|
|
126
|
+
if (!existsSync(abs)) continue;
|
|
127
|
+
const work = readFileSync(abs, 'utf8');
|
|
128
|
+
const before = headText(rel);
|
|
129
|
+
if (before === work) continue;
|
|
130
|
+
files[rel] = { head: before, work, mode: before === null ? '100644' : modeOf(rel) };
|
|
131
|
+
hash.update(`\0${rel}\0`).update(work);
|
|
132
|
+
let hunks = null;
|
|
133
|
+
let diff = '';
|
|
134
|
+
if (before !== null && rel === deckRel) {
|
|
135
|
+
try { diff = raw(['diff', '--no-color', '--no-ext-diff', '--unified=0', 'HEAD', '--', rel], cwd); } catch { diff = ''; }
|
|
136
|
+
hunks = parseZeroHunks(diff);
|
|
137
|
+
// the hunks must rebuild the file exactly, or the file goes whole
|
|
138
|
+
if (hunks && (!hunks.length || applyHunks(before, hunks) !== work)) hunks = null;
|
|
139
|
+
}
|
|
140
|
+
if (!hunks) {
|
|
141
|
+
const n = work.split('\n').length;
|
|
142
|
+
units.push({ file: rel, whole: true, hunks: [], slides: [], where: rel, lines: n, diff: before === null ? `(new file ${rel})` : `(${rel}, changed)` });
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const outline = deckOutline(work);
|
|
146
|
+
let mine = hunks.map((h) => ({
|
|
147
|
+
file: rel, whole: false, hunks: [h],
|
|
148
|
+
slides: slidesAt(outline, h.newStart, h.newCount),
|
|
149
|
+
}));
|
|
150
|
+
// A deck rewritten wholesale is hundreds of hunks no agent should be
|
|
151
|
+
// asked to sort: past MAX_UNITS, the hunks of one slide go together.
|
|
152
|
+
if (mine.length > MAX_UNITS) {
|
|
153
|
+
const by = new Map();
|
|
154
|
+
for (const u of mine) {
|
|
155
|
+
const key = u.slides.join(',') || '-';
|
|
156
|
+
if (by.has(key)) by.get(key).hunks.push(...u.hunks);
|
|
157
|
+
else by.set(key, { ...u, hunks: [...u.hunks] });
|
|
158
|
+
}
|
|
159
|
+
mine = [...by.values()];
|
|
160
|
+
}
|
|
161
|
+
for (const u of mine) {
|
|
162
|
+
units.push({
|
|
163
|
+
...u,
|
|
164
|
+
where: whereLabel(u.slides),
|
|
165
|
+
lines: u.hunks.reduce((n, h) => n + h.oldCount + h.newCount, 0),
|
|
166
|
+
diff: u.hunks.map((h) => h.text.join('\n')).join('\n'),
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
units.forEach((u, i) => { u.id = i + 1; });
|
|
171
|
+
return { head, base: hash.digest('hex').slice(0, 16), units, files };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* What the agent is asked: these numbered changes, grouped into commits. Pure.
|
|
176
|
+
* The answer is parsed line by line (`parsePlan`), so the format is spelled
|
|
177
|
+
* out and nothing else is asked for.
|
|
178
|
+
*/
|
|
179
|
+
export function planPrompt({ deck, title = null, units, max = 24 * 1024 }) {
|
|
180
|
+
let budget = max;
|
|
181
|
+
const shown = [];
|
|
182
|
+
for (const u of units) {
|
|
183
|
+
const body = u.diff.length > budget ? `${u.diff.slice(0, Math.max(0, budget))}\n(…cut)` : u.diff;
|
|
184
|
+
budget = Math.max(0, budget - body.length);
|
|
185
|
+
shown.push(`[${u.id}] ${u.where}${u.whole ? '' : ` (${u.file})`}\n${body}`);
|
|
186
|
+
}
|
|
187
|
+
return [
|
|
188
|
+
`These are the uncommitted changes to "${deck}", a Decklight deck (a single-file`,
|
|
189
|
+
'HTML presentation; one top-level <section> per slide), numbered.',
|
|
190
|
+
...(title ? [`The deck is titled "${title}".`] : []),
|
|
191
|
+
'',
|
|
192
|
+
'Group them into git commits, one commit per separate idea. Changes that are',
|
|
193
|
+
'one idea go in one commit; if they are all one idea, reply with ONE commit.',
|
|
194
|
+
'Order the commits so each one makes sense after the ones before it.',
|
|
195
|
+
'',
|
|
196
|
+
'Reply with one line per commit and nothing else, in this exact form:',
|
|
197
|
+
' <change numbers, comma-separated> | <subject>',
|
|
198
|
+
'for example:',
|
|
199
|
+
' 1, 3 | shorten the agent slide to one claim',
|
|
200
|
+
' 2 | add a closing slide with the three takeaways',
|
|
201
|
+
'',
|
|
202
|
+
'Rules for each subject: at most 72 characters, no quotes, no trailing period;',
|
|
203
|
+
'say what changed for the talk, naming a slide by its own words, never by its',
|
|
204
|
+
'number. Every change number appears in exactly one commit.',
|
|
205
|
+
'',
|
|
206
|
+
...shown,
|
|
207
|
+
].join('\n');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** `[{ units: [ids], subject }]` from the agent's lines. Pure. */
|
|
211
|
+
export function parsePlan(answer) {
|
|
212
|
+
const out = [];
|
|
213
|
+
for (const line of String(answer ?? '').split('\n')) {
|
|
214
|
+
const m = /^\s*[-*]?\s*\[?((?:\d+\s*,?\s*)+)\]?\s*[|:]\s*(.+?)\s*$/.exec(line);
|
|
215
|
+
if (!m) continue;
|
|
216
|
+
const ids = m[1].split(/[\s,]+/).filter(Boolean).map(Number).filter(Number.isInteger);
|
|
217
|
+
if (ids.length && m[2]) out.push({ units: ids, subject: m[2].replace(/^["'`]|["'`]$/g, '') });
|
|
218
|
+
}
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* A plan every unit is in exactly once. Pure. A unit the agent named twice
|
|
224
|
+
* stays with the first commit that named it; one it forgot joins the commit
|
|
225
|
+
* of the unit before it (the change it sits beside), or the first commit.
|
|
226
|
+
* Commits left with nothing are dropped.
|
|
227
|
+
*/
|
|
228
|
+
export function normalizePlan(groups, units) {
|
|
229
|
+
const ids = units.map((u) => u.id);
|
|
230
|
+
const owner = new Map();
|
|
231
|
+
const plan = groups.map((g) => ({ subject: g.subject, units: [] }));
|
|
232
|
+
plan.forEach((g, i) => {
|
|
233
|
+
for (const id of groups[i].units) {
|
|
234
|
+
if (ids.includes(id) && !owner.has(id)) { owner.set(id, i); g.units.push(id); }
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
if (!plan.length) plan.push({ subject: '', units: [] });
|
|
238
|
+
let last = 0;
|
|
239
|
+
for (const id of ids) {
|
|
240
|
+
if (owner.has(id)) { last = owner.get(id); continue; }
|
|
241
|
+
owner.set(id, last);
|
|
242
|
+
plan[last].units.push(id);
|
|
243
|
+
}
|
|
244
|
+
for (const g of plan) g.units.sort((a, b) => a - b);
|
|
245
|
+
return plan.filter((g) => g.units.length);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** The added lines of a file that rides the deck (an append-only sidecar), as a diff. */
|
|
249
|
+
const addedLines = (f) => {
|
|
250
|
+
const fresh = f.head !== null && f.work.startsWith(f.head) ? f.work.slice(f.head.length) : f.work;
|
|
251
|
+
return fresh.split('\n').filter(Boolean).map((l) => `+${l}`).join('\n');
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The commits "write it for me" proposes: `{ work, commits }`, where
|
|
256
|
+
* `commits` is `[{ subject, units }]`, or `{ work, commits: null }` when there
|
|
257
|
+
* is nothing to cut (no HEAD: a first commit is one commit; nothing changed).
|
|
258
|
+
*
|
|
259
|
+
* The files that ride the deck are not the agent's to describe: the review
|
|
260
|
+
* sidecar's records say what they are (reviewSubject), so they are one commit
|
|
261
|
+
* of their own with that subject, and a .gitattributes decklight wrote goes
|
|
262
|
+
* with them. Only the deck's hunks are sent to the agent, and only on the
|
|
263
|
+
* click that asked for it. An agent that answers nothing usable leaves the
|
|
264
|
+
* deck as one commit with an empty subject, for the author to write.
|
|
265
|
+
*/
|
|
266
|
+
export async function planWorking({
|
|
267
|
+
cwd, deckPath, deckRel, also = [], agent = null, env = process.env,
|
|
268
|
+
timeoutMs = ASK_TIMEOUT_MS, exec = ask, resolve = agentAsk, read = readFileSync,
|
|
269
|
+
}) {
|
|
270
|
+
const work = workingUnits({ cwd, deckRel, also });
|
|
271
|
+
if (!work || !work.units.length) return { work, commits: null };
|
|
272
|
+
const deckUnits = work.units.filter((u) => u.file === deckRel);
|
|
273
|
+
const rest = work.units.filter((u) => u.file !== deckRel);
|
|
274
|
+
const commits = [];
|
|
275
|
+
if (deckUnits.length) {
|
|
276
|
+
let groups = [];
|
|
277
|
+
let html = '';
|
|
278
|
+
try { html = read(deckPath, 'utf8'); } catch { /* the title is optional */ }
|
|
279
|
+
const spawned = resolve(agent, planPrompt({ deck: deckRel, title: deckTitle(html), units: deckUnits }), { env });
|
|
280
|
+
if (spawned) {
|
|
281
|
+
let answer = null;
|
|
282
|
+
try { answer = await exec(spawned, cwd, timeoutMs); } catch { answer = null; }
|
|
283
|
+
groups = parsePlan(answer);
|
|
284
|
+
}
|
|
285
|
+
commits.push(...normalizePlan(groups, deckUnits).map((g) => ({ subject: g.subject, units: g.units })));
|
|
286
|
+
}
|
|
287
|
+
if (rest.length) {
|
|
288
|
+
const side = rest.map((u) => reviewSubject(addedLines(work.files[u.file]))).find(Boolean);
|
|
289
|
+
commits.push({ subject: side ?? '', units: rest.map((u) => u.id) });
|
|
290
|
+
}
|
|
291
|
+
return { work, commits };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** A unit as the window shows it: no hunk bodies, which can be large. */
|
|
295
|
+
export const unitSummary = (u) => ({ id: u.id, file: u.file, where: u.where, lines: u.lines, slides: u.slides });
|
|
296
|
+
|
|
297
|
+
/** Each top-level slide's source, whitespace at line ends ignored. Pure. */
|
|
298
|
+
const slideSources = (html) => {
|
|
299
|
+
const lines = String(html ?? '').split('\n');
|
|
300
|
+
return deckOutline(html).map((sl) => lines.slice(sl.from - 1, sl.to).map((l) => l.trim()).join('\n'));
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Whether two slides are one slide edited rather than one out and another in.
|
|
305
|
+
* Pure. The same heading says so; otherwise half their lines in common does
|
|
306
|
+
* (two slides share little more than their <section> tags).
|
|
307
|
+
*/
|
|
308
|
+
export function sameSlide(a, b) {
|
|
309
|
+
const heading = (src) => {
|
|
310
|
+
const m = /<h[1-6][^>]*>([\s\S]*?)<\/h[1-6]>/i.exec(src);
|
|
311
|
+
return m ? m[1].replace(/<[^>]*>/g, '').replace(/\s+/g, ' ').trim().toLowerCase() : '';
|
|
312
|
+
};
|
|
313
|
+
const ha = heading(a);
|
|
314
|
+
if (ha && ha === heading(b)) return true;
|
|
315
|
+
const la = new Set(a.split('\n').filter(Boolean));
|
|
316
|
+
const lb = new Set(b.split('\n').filter(Boolean));
|
|
317
|
+
let shared = 0;
|
|
318
|
+
for (const l of la) if (lb.has(l)) shared++;
|
|
319
|
+
const union = la.size + lb.size - shared;
|
|
320
|
+
return union > 0 && shared / union >= 0.5;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* How many slides the work changed, added and removed against HEAD. Pure.
|
|
325
|
+
*
|
|
326
|
+
* Slides are matched the way a diff matches lines: the longest run of
|
|
327
|
+
* identical slides in order is kept. Between two kept slides, an old and a
|
|
328
|
+
* new one that are the same slide edited (sameSlide) pair up as CHANGED, in
|
|
329
|
+
* order; what is left of the old is REMOVED and of the new ADDED. So an edit
|
|
330
|
+
* is one changed, a slide replaced by a different one is one removed and one
|
|
331
|
+
* added, a slide moved is one removed and one added, and a deck whose slides
|
|
332
|
+
* are all edited is all changed, never all replaced.
|
|
333
|
+
*/
|
|
334
|
+
export function slideChanges(before, after) {
|
|
335
|
+
const { changed, added, removed } = slideDiff(before, after);
|
|
336
|
+
return { changed, added, removed };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* slideChanges, slide by slide: the counts, and `stops`, every slide the
|
|
341
|
+
* change touched in the order a reader meets them. `{ kind: 'changed', slide,
|
|
342
|
+
* was }` and `{ kind: 'added', slide }` number slides in `after`;
|
|
343
|
+
* `{ kind: 'removed', was }` numbers one in `before`, which is the only
|
|
344
|
+
* version that still has it. The History overlay walks these (PRESENTING).
|
|
345
|
+
* Pure; 1-based like `state.slide`.
|
|
346
|
+
*/
|
|
347
|
+
export function slideDiff(before, after) {
|
|
348
|
+
const a = slideSources(before);
|
|
349
|
+
const b = slideSources(after);
|
|
350
|
+
// LCS over whole slides; decks are tens of slides, so n×m is nothing
|
|
351
|
+
const n = a.length, m = b.length;
|
|
352
|
+
const L = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
|
|
353
|
+
for (let i = n - 1; i >= 0; i--) for (let j = m - 1; j >= 0; j--) {
|
|
354
|
+
L[i][j] = a[i] === b[j] ? L[i + 1][j + 1] + 1 : Math.max(L[i + 1][j], L[i][j + 1]);
|
|
355
|
+
}
|
|
356
|
+
const stops = [];
|
|
357
|
+
// gaps hold INDEXES, so each stop can say which slide it is
|
|
358
|
+
let gapOld = [], gapNew = [];
|
|
359
|
+
const close = () => {
|
|
360
|
+
let x = 0, y = 0;
|
|
361
|
+
while (x < gapOld.length && y < gapNew.length) {
|
|
362
|
+
if (sameSlide(a[gapOld[x]], b[gapNew[y]])) {
|
|
363
|
+
stops.push({ kind: 'changed', slide: gapNew[y] + 1, was: gapOld[x] + 1 }); x++; y++;
|
|
364
|
+
// the old one is edited further on: this new one was put in
|
|
365
|
+
} else if (gapNew.slice(y + 1).some((k) => sameSlide(a[gapOld[x]], b[k]))) {
|
|
366
|
+
stops.push({ kind: 'added', slide: gapNew[y] + 1 }); y++;
|
|
367
|
+
} else { stops.push({ kind: 'removed', was: gapOld[x] + 1 }); x++; }
|
|
368
|
+
}
|
|
369
|
+
for (; x < gapOld.length; x++) stops.push({ kind: 'removed', was: gapOld[x] + 1 });
|
|
370
|
+
for (; y < gapNew.length; y++) stops.push({ kind: 'added', slide: gapNew[y] + 1 });
|
|
371
|
+
gapOld = []; gapNew = [];
|
|
372
|
+
};
|
|
373
|
+
let i = 0, j = 0;
|
|
374
|
+
while (i < n && j < m) {
|
|
375
|
+
if (a[i] === b[j]) { close(); i++; j++; }
|
|
376
|
+
else if (L[i + 1][j] >= L[i][j + 1]) gapOld.push(i++);
|
|
377
|
+
else gapNew.push(j++);
|
|
378
|
+
}
|
|
379
|
+
while (i < n) gapOld.push(i++);
|
|
380
|
+
while (j < m) gapNew.push(j++);
|
|
381
|
+
close();
|
|
382
|
+
const count = (k) => stops.filter((s) => s.kind === k).length;
|
|
383
|
+
return { changed: count('changed'), added: count('added'), removed: count('removed'), stops };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* The deck's slides against HEAD, `{ changed, added, removed }`; with no HEAD
|
|
388
|
+
* every slide is added. Null when the deck cannot be read.
|
|
389
|
+
*/
|
|
390
|
+
export function slidesSinceHead(cwd, deckRel) {
|
|
391
|
+
let work;
|
|
392
|
+
try { work = readFileSync(resolvePath(cwd, deckRel), 'utf8'); } catch { return null; }
|
|
393
|
+
let before = '';
|
|
394
|
+
try { before = raw(['show', `HEAD:./${deckRel}`], cwd); } catch { before = ''; }
|
|
395
|
+
return slideChanges(before, work);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* The commits the author left out, re-cut against the HEAD that now holds the
|
|
400
|
+
* others: `{ base, units, commits: [{ subject, units }] }` with the new unit
|
|
401
|
+
* ids, or null when they cannot be matched (the window then offers to write
|
|
402
|
+
* them again). A -U0 hunk carries its own lines, so a hunk left out reads the
|
|
403
|
+
* same against the new HEAD, only at other line numbers: it is found by its
|
|
404
|
+
* text, in order. A file that went whole is matched by its name.
|
|
405
|
+
*/
|
|
406
|
+
export function leftAfter({ cwd, deckRel, also = [], before, skipped }) {
|
|
407
|
+
const after = workingUnits({ cwd, deckRel, also });
|
|
408
|
+
if (!after) return null;
|
|
409
|
+
const byId = new Map(before.map((u) => [u.id, u]));
|
|
410
|
+
const body = (h) => h.text.slice(1).join('\n');
|
|
411
|
+
// every left-out hunk (or whole file), in order, with the commit it is in
|
|
412
|
+
const want = [];
|
|
413
|
+
skipped.forEach((g, k) => {
|
|
414
|
+
for (const id of g.units) {
|
|
415
|
+
const u = byId.get(id);
|
|
416
|
+
if (!u) continue;
|
|
417
|
+
if (u.whole) want.push({ k, file: u.file, whole: true });
|
|
418
|
+
else for (const h of u.hunks) want.push({ k, file: u.file, body: body(h) });
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
const commits = skipped.map((g) => ({ subject: g.subject, units: [] }));
|
|
422
|
+
const used = new Set();
|
|
423
|
+
for (const u of after.units) {
|
|
424
|
+
let k = -1;
|
|
425
|
+
const pieces = u.whole ? [{ whole: true }] : u.hunks.map((h) => ({ body: body(h) }));
|
|
426
|
+
for (const piece of pieces) {
|
|
427
|
+
const i = want.findIndex((w, j) => !used.has(j) && w.file === u.file
|
|
428
|
+
&& (piece.whole ? w.whole : !w.whole && w.body === piece.body));
|
|
429
|
+
if (i < 0) return null;
|
|
430
|
+
used.add(i);
|
|
431
|
+
if (k < 0) k = want[i].k;
|
|
432
|
+
}
|
|
433
|
+
if (k < 0) return null;
|
|
434
|
+
commits[k].units.push(u.id);
|
|
435
|
+
}
|
|
436
|
+
if (used.size !== want.length) return null;
|
|
437
|
+
const kept = commits.filter((c) => c.units.length);
|
|
438
|
+
return kept.length ? { base: after.base, units: after.units.map(unitSummary), commits: kept } : null;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** A commit-tree that falls back to a stand-in identity, like gitAutocommit. */
|
|
442
|
+
function commitTree(cwd, tree, parent, message, env) {
|
|
443
|
+
const args = ['commit-tree', tree, '-p', parent, '-m', message];
|
|
444
|
+
try { return run(args, cwd, { env }); } catch (e) {
|
|
445
|
+
if (!isIdentityError(e)) throw e;
|
|
446
|
+
return run(['-c', 'user.name=decklight', '-c', 'user.email=decklight@localhost', ...args], cwd, { env });
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Make the plan's commits on top of HEAD. `commits` is `[{ message, units,
|
|
452
|
+
* skip }]`; `work` is what `workingUnits` returned when the plan was made.
|
|
453
|
+
* Resolves to `{ ok: true, shas, subjects, files, left }`, or `{ ok: false,
|
|
454
|
+
* code, error }` with code STALE when the work or HEAD moved since.
|
|
455
|
+
*
|
|
456
|
+
* A commit marked `skip` is left out: its units stay uncommitted in the
|
|
457
|
+
* working file, and the ones taken are applied to HEAD's text without them.
|
|
458
|
+
* The plan is normalised with the skipped commits still in it, so a unit
|
|
459
|
+
* the plan forgot joins its neighbour whether or not that one is taken, and
|
|
460
|
+
* never slips into a commit the author ticked. `left` is what remains,
|
|
461
|
+
* re-cut against the new HEAD (see leftAfter), or null when nothing was
|
|
462
|
+
* skipped or it could not be matched.
|
|
463
|
+
*/
|
|
464
|
+
export function commitPlan({ cwd, deckRel, also = [], base, commits, template }) {
|
|
465
|
+
const now = workingUnits({ cwd, deckRel, also });
|
|
466
|
+
if (!now || now.base !== base) {
|
|
467
|
+
return { ok: false, code: 'STALE', error: 'the deck changed since these commits were proposed — write it again' };
|
|
468
|
+
}
|
|
469
|
+
const byId = new Map(now.units.map((u) => [u.id, u]));
|
|
470
|
+
const asked = (commits ?? []).map((c) => ({ subject: String(c?.message ?? ''), units: (c?.units ?? []).map(Number), skip: !!c?.skip }));
|
|
471
|
+
// normalised whole, so every unit has the owner it would have if all were taken
|
|
472
|
+
const owned = normalizePlan(asked.map(({ subject, units }) => ({ subject, units })), now.units);
|
|
473
|
+
const skipOf = (g) => asked.find((a) => a.units.some((id) => g.units.includes(id)))?.skip ?? false;
|
|
474
|
+
const plan = owned.filter((g) => !skipOf(g));
|
|
475
|
+
const skipped = owned.filter((g) => skipOf(g));
|
|
476
|
+
if (!plan.length) return { ok: false, code: 'EMPTY', error: 'nothing to commit' };
|
|
477
|
+
const subjects = plan.map((g) => commitSubject(g.subject.trim() || template, template));
|
|
478
|
+
|
|
479
|
+
const dir = mkdtempSync(join(tmpdir(), 'decklight-split-'));
|
|
480
|
+
const env = { ...process.env, GIT_INDEX_FILE: join(dir, 'index') };
|
|
481
|
+
try {
|
|
482
|
+
run(['read-tree', now.head], cwd, { env });
|
|
483
|
+
const taken = new Set();
|
|
484
|
+
let parent = now.head;
|
|
485
|
+
const shas = [];
|
|
486
|
+
plan.forEach((g, k) => {
|
|
487
|
+
for (const id of g.units) taken.add(id);
|
|
488
|
+
// every file this commit touches, at the state of commits 1..k
|
|
489
|
+
for (const [rel, f] of Object.entries(now.files)) {
|
|
490
|
+
const mine = now.units.filter((u) => u.file === rel && taken.has(u.id));
|
|
491
|
+
if (!g.units.some((id) => byId.get(id).file === rel)) continue;
|
|
492
|
+
const text = mine.some((u) => u.whole) ? f.work : applyHunks(f.head ?? '', mine.flatMap((u) => u.hunks));
|
|
493
|
+
const blob = run(['hash-object', '-w', '--stdin', `--path=${rel}`], cwd, { input: text });
|
|
494
|
+
run(['update-index', '--add', '--cacheinfo', `${f.mode},${blob},${rel}`], cwd, { env });
|
|
495
|
+
}
|
|
496
|
+
const tree = run(['write-tree'], cwd, { env });
|
|
497
|
+
parent = commitTree(cwd, tree, parent, subjects[k], env);
|
|
498
|
+
shas.push(parent);
|
|
499
|
+
});
|
|
500
|
+
// the one move, and only from the HEAD the plan was made on
|
|
501
|
+
run(['update-ref', '-m', `decklight: ${plan.length} commits`, 'HEAD', parent, now.head], cwd);
|
|
502
|
+
// the author's index now agrees with the new HEAD for these files, and
|
|
503
|
+
// keeps everything else it had
|
|
504
|
+
try { run(['reset', '-q', 'HEAD', '--', ...Object.keys(now.files)], cwd); } catch { /* status shows it; nothing is lost */ }
|
|
505
|
+
const files = Object.keys(now.files).filter((rel) => now.units.some((u) => u.file === rel && taken.has(u.id)));
|
|
506
|
+
const left = skipped.length ? leftAfter({ cwd, deckRel, also, before: now.units, skipped }) : null;
|
|
507
|
+
return { ok: true, shas, subjects, files, left };
|
|
508
|
+
} catch (e) {
|
|
509
|
+
return { ok: false, code: 'GIT', error: oneline(e) };
|
|
510
|
+
} finally {
|
|
511
|
+
rmSync(dir, { recursive: true, force: true });
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** A tag name git accepts and that cannot be read as an option. */
|
|
516
|
+
export function tagNameProblem(name, cwd) {
|
|
517
|
+
const v = String(name ?? '').trim();
|
|
518
|
+
if (!v) return 'a tag needs a name';
|
|
519
|
+
if (v.startsWith('-')) return 'a tag name cannot start with -';
|
|
520
|
+
try { run(['check-ref-format', `refs/tags/${v}`], cwd); } catch { return `"${v}" is not a name git takes for a tag`; }
|
|
521
|
+
return null;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/** An annotated tag on HEAD: `{ ok, tag, sha }` or `{ ok: false, code, error }`. */
|
|
525
|
+
export function tagHead(cwd, name) {
|
|
526
|
+
const v = String(name ?? '').trim();
|
|
527
|
+
const bad = tagNameProblem(v, cwd);
|
|
528
|
+
if (bad) return { ok: false, code: 'BAD', error: bad };
|
|
529
|
+
let sha;
|
|
530
|
+
try { sha = run(['rev-parse', '--verify', 'HEAD'], cwd); } catch { return { ok: false, code: 'UNBORN', error: 'nothing is committed yet — commit first, then tag it' }; }
|
|
531
|
+
try { run(['rev-parse', '--verify', '--quiet', `refs/tags/${v}`], cwd); return { ok: false, code: 'EXISTS', error: `the tag ${v} already exists` }; } catch { /* free */ }
|
|
532
|
+
const args = ['tag', '-a', v, '-m', v, sha];
|
|
533
|
+
try { run(args, cwd); } catch (e) {
|
|
534
|
+
if (!isIdentityError(e)) return { ok: false, code: 'GIT', error: oneline(e) };
|
|
535
|
+
try { run(['-c', 'user.name=decklight', '-c', 'user.email=decklight@localhost', ...args], cwd); }
|
|
536
|
+
catch (e2) { return { ok: false, code: 'GIT', error: oneline(e2) }; }
|
|
537
|
+
}
|
|
538
|
+
return { ok: true, tag: v, sha };
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** Tags pointing at HEAD, for the window to show what is already tagged. */
|
|
542
|
+
export function headTags(cwd) {
|
|
543
|
+
try { return run(['tag', '--points-at', 'HEAD'], cwd).split('\n').filter(Boolean); } catch { return []; }
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Why a push was refused, in words a presenter can act on. Pure. The remote
|
|
548
|
+
* having commits this branch does not is its own sentence, since it is the
|
|
549
|
+
* common one and "unknown" helps nobody.
|
|
550
|
+
*/
|
|
551
|
+
export function pushError(e, s) {
|
|
552
|
+
const text = String(e?.stderr || e?.message || e || '');
|
|
553
|
+
if (/\[rejected\]|non-fast-forward|fetch first/i.test(text)) {
|
|
554
|
+
return `${s.remote} has commits this branch does not — pull them first (git pull --ff-only), then push`;
|
|
555
|
+
}
|
|
556
|
+
switch (classifyRemoteError(e)) {
|
|
557
|
+
case 'offline': return `${s.remote} did not answer — offline, or it took longer than a minute`;
|
|
558
|
+
case 'denied': return `${s.remote} refused the credentials on this machine — push once from a terminal to set them up`;
|
|
559
|
+
case 'gone': return `${s.remote} is not there any more (${s.url ?? 'its url'})`;
|
|
560
|
+
default: return oneline(e);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Why there is nothing a push can do here, or null when it can go. Pure over
|
|
566
|
+
* a `remoteState`.
|
|
567
|
+
*/
|
|
568
|
+
export function pushBlocked(s) {
|
|
569
|
+
switch (s?.state) {
|
|
570
|
+
case 'ok': case 'no-upstream': return null;
|
|
571
|
+
case 'no-remote': return 'no remote to push to — git remote add origin <url>';
|
|
572
|
+
case 'ambiguous-remote': return 'several remotes and none is called origin — push from a terminal to choose';
|
|
573
|
+
case 'detached': return 'detached HEAD — there is no branch to push';
|
|
574
|
+
case 'unborn': return 'nothing is committed yet';
|
|
575
|
+
default: return 'git cannot say where this branch stands';
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Push the branch, and (unless `tags` is false) the annotated tags on what it
|
|
581
|
+
* pushes (--follow-tags),
|
|
582
|
+
* on a click and only then (git.mjs: never a push you did not ask for). No
|
|
583
|
+
* prompt can hang it (noPromptEnv) and a minute is the bound. Resolves to
|
|
584
|
+
* `{ ok, pushed, remote, branch }` or `{ ok: false, code, error }`; never rejects.
|
|
585
|
+
*/
|
|
586
|
+
export function pushBranch(cwd, { state = remoteState(cwd), tags = true, timeoutMs = PUSH_TIMEOUT_MS, exec = execFile } = {}) {
|
|
587
|
+
const blocked = pushBlocked(state);
|
|
588
|
+
if (blocked) return Promise.resolve({ ok: false, code: 'BLOCKED', error: blocked });
|
|
589
|
+
// `tags: false` is the setting that keeps tags on this machine (Settings…,
|
|
590
|
+
// PRESENTING): the branch goes, the tags wait for a push you make yourself
|
|
591
|
+
const follow = tags ? ['--follow-tags'] : [];
|
|
592
|
+
const args = state.state === 'no-upstream'
|
|
593
|
+
? ['push', ...follow, '-u', state.remote, state.branch]
|
|
594
|
+
: ['push', ...follow];
|
|
595
|
+
return new Promise((resolve) => {
|
|
596
|
+
exec('git', args, { cwd, env: noPromptEnv(), timeout: timeoutMs, encoding: 'utf8' }, (err, _out, stderr) => {
|
|
597
|
+
if (err) { err.stderr = err.stderr || stderr; resolve({ ok: false, code: 'REMOTE', error: pushError(err, state) }); return; }
|
|
598
|
+
resolve({ ok: true, pushed: true, remote: state.remote, branch: state.branch, pushedTags: !!tags, args: args.join(' ') });
|
|
599
|
+
});
|
|
600
|
+
});
|
|
601
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a deck can REFERENCE from a marketplace, as one table.
|
|
6
|
+
*
|
|
7
|
+
* A theme, a design system and a font are each named in the deck's
|
|
8
|
+
* configuration block and served into it, never copied (SPEC
|
|
9
|
+
* THEME_DISTRIBUTION, DESIGN_SYSTEMS, FONTS). Each kind keeps its own module
|
|
10
|
+
* for what is different about it (theme-refs.mjs, design-system-refs.mjs,
|
|
11
|
+
* font-refs.mjs: how a reference resolves, what a package holds). What is the
|
|
12
|
+
* SAME was written three times, once at each place a deck is served: the
|
|
13
|
+
* chain of three link calls (the deck server, the edit server's audited deck,
|
|
14
|
+
* a history preview) and the three asset lookups in the static file server.
|
|
15
|
+
* A fourth kind would have been a fourth edit in each (ROADMAP section 4).
|
|
16
|
+
* This table is that wiring, written once, like UNIT_TYPES is for the units.
|
|
17
|
+
*
|
|
18
|
+
* ORDER IS THE CHAIN: themes, then design systems, then fonts, the order the
|
|
19
|
+
* three calls were nested in, so the served bytes are unchanged.
|
|
20
|
+
*
|
|
21
|
+
* `namespace` says whose URLs a kind owns. A design system's and a font's
|
|
22
|
+
* files live under their own prefix, TERMINAL: answered from the package or a
|
|
23
|
+
* plain 404, never from the deck's directory. A referenced theme has no
|
|
24
|
+
* prefix of its own and is the FALLBACK for a path the deck's directory does
|
|
25
|
+
* not have.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { linkAddedThemes, themeRefAsset } from './theme-refs.mjs';
|
|
29
|
+
import { linkDesignSystems, designSystemAsset, inDesignSystemNamespace } from './design-system-refs.mjs';
|
|
30
|
+
import { linkFonts, fontAsset, inFontNamespace } from './font-refs.mjs';
|
|
31
|
+
|
|
32
|
+
export const REFERENCE_KINDS = [
|
|
33
|
+
{ kind: 'theme', link: linkAddedThemes, asset: themeRefAsset, namespace: null },
|
|
34
|
+
{ kind: 'design-system', link: linkDesignSystems, asset: designSystemAsset, namespace: inDesignSystemNamespace },
|
|
35
|
+
{ kind: 'font', link: linkFonts, asset: fontAsset, namespace: inFontNamespace },
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Every reference kind linked into a served page, in the chain's order.
|
|
40
|
+
* `dir` is the deck's folder and `deck` its file name, for what a kind reads
|
|
41
|
+
* beside it and says about it (a design system's behind check reads the
|
|
42
|
+
* stylesheets the deck links, and names the deck in its command).
|
|
43
|
+
*/
|
|
44
|
+
export function linkReferences(html, { dir = null, deck = undefined, kinds = REFERENCE_KINDS } = {}) {
|
|
45
|
+
return kinds.reduce((out, k) => k.link(out, undefined, { dir, deck }), html);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The kind whose namespace a path is in, or null: such a path is that kind's
|
|
50
|
+
* alone (terminal), and the static server answers it from the package or 404s.
|
|
51
|
+
*/
|
|
52
|
+
export function namespacedKind(rel, kinds = REFERENCE_KINDS) {
|
|
53
|
+
return kinds.find((k) => k.namespace?.(rel)) ?? null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The fallback kinds' asset for a path the deck's directory lacks, or null. */
|
|
57
|
+
export function fallbackReferenceAsset(rel, kinds = REFERENCE_KINDS) {
|
|
58
|
+
for (const k of kinds) {
|
|
59
|
+
if (k.namespace) continue;
|
|
60
|
+
const asset = k.asset(rel);
|
|
61
|
+
if (asset) return asset;
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|