decklight 0.2.0 → 0.3.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/README.md +69 -31
- package/SPEC.md +855 -80
- package/cli/agents.mjs +113 -9
- package/cli/associate.mjs +279 -0
- package/cli/audit.mjs +424 -0
- package/cli/bundle.mjs +174 -46
- package/cli/deckfile.mjs +233 -0
- package/cli/decklight.mjs +201 -8
- package/cli/dev.mjs +244 -55
- package/cli/edit.mjs +564 -107
- package/cli/extension.mjs +108 -0
- package/cli/git.mjs +141 -0
- package/cli/import.mjs +398 -0
- package/cli/init.mjs +421 -116
- package/cli/loader.mjs +263 -0
- package/cli/marketplace.mjs +730 -0
- package/cli/pdf.mjs +156 -0
- package/cli/pkg.mjs +69 -0
- package/cli/plugin.mjs +646 -0
- package/cli/port-conflict.mjs +94 -0
- package/cli/present.mjs +560 -0
- package/cli/publish.mjs +342 -0
- package/cli/qr.mjs +334 -0
- package/cli/rec.mjs +5 -8
- package/cli/remote.mjs +149 -0
- package/cli/report-bug.mjs +97 -0
- package/cli/restore.mjs +174 -0
- package/cli/serve.mjs +247 -0
- package/cli/sign.mjs +216 -0
- package/cli/skill-content.mjs +222 -0
- package/cli/skills.mjs +338 -0
- package/cli/supervise.mjs +57 -0
- package/cli/theme.mjs +201 -0
- package/cli/units.mjs +488 -0
- package/cli/upgrade.mjs +250 -0
- package/cli/util.mjs +90 -0
- package/cli/wizard.mjs +432 -0
- package/cli/zip.mjs +121 -0
- package/dist/decklight.css +716 -29
- package/dist/decklight.js +198 -125
- package/dist/decklight.js.map +4 -4
- package/docs/architecture.svg +1 -1
- package/package.json +5 -4
- package/themes/README.md +6 -4
- package/themes/aurora.css +1 -1
- package/themes/berry.css +1 -1
- package/themes/carbon.css +1 -1
- package/themes/citrus.css +1 -1
- package/themes/coastal.css +1 -1
- package/themes/cosmos.css +1 -1
- package/themes/dune.css +1 -1
- package/themes/eclipse.css +1 -1
- package/themes/ember.css +1 -1
- package/themes/fjord.css +1 -1
- package/themes/gallery.html +3 -11
- package/themes/glacier.css +1 -1
- package/themes/graphite.css +1 -1
- package/themes/harvest.css +1 -1
- package/themes/ibm-modern.css +1 -1
- package/themes/ink.css +1 -1
- package/themes/latte.css +1 -1
- package/themes/linen.css +1 -1
- package/themes/meadow.css +1 -1
- package/themes/metropolis.css +1 -1
- package/themes/midnight.css +1 -1
- package/themes/mint.css +1 -1
- package/themes/moss.css +1 -1
- package/themes/obsidian.css +1 -1
- package/themes/orchid.css +1 -1
- package/themes/packs.json +2 -30
- package/themes/paper.css +1 -1
- package/themes/peony.css +1 -1
- package/themes/porcelain.css +1 -1
- package/themes/reveal-beige.css +1 -1
- package/themes/reveal-black.css +1 -1
- package/themes/reveal-blood.css +1 -1
- package/themes/reveal-dracula.css +1 -1
- package/themes/reveal-league.css +1 -1
- package/themes/reveal-moon.css +1 -1
- package/themes/reveal-night.css +1 -1
- package/themes/reveal-serif.css +1 -1
- package/themes/reveal-simple.css +1 -1
- package/themes/reveal-sky.css +1 -1
- package/themes/reveal-solarized.css +1 -1
- package/themes/reveal-white.css +1 -1
- package/themes/sepia.css +1 -1
- package/themes/seriph.css +1 -1
- package/themes/slate.css +1 -1
- package/themes/storm.css +1 -1
- package/themes/synthwave.css +1 -1
- package/themes/velvet.css +1 -1
- package/themes/yamabuki.css +1 -1
- package/tools/args.mjs +32 -0
- package/tools/bridge.mjs +30 -0
- package/tools/chrome.mjs +11 -2
- package/tools/color.mjs +110 -0
- package/tools/deck-html.mjs +138 -0
- package/tools/elevenlabs-tts.mjs +248 -0
- package/tools/escape.mjs +40 -0
- package/tools/extension-check-runner.mjs +41 -0
- package/tools/extension-check.mjs +266 -0
- package/tools/gemini-tts.mjs +25 -3
- package/tools/lipsync-engines.mjs +81 -0
- package/tools/lipsync-server.mjs +78 -37
- package/tools/lipsync.mjs +36 -21
- package/tools/local-voice.mjs +209 -0
- package/tools/ooxml.mjs +151 -0
- package/tools/pptx.mjs +320 -0
- package/tools/publish-site-voices.mjs +2 -1
- package/tools/publish-targets.mjs +143 -0
- package/tools/publish-voices.mjs +230 -0
- package/tools/shot.mjs +91 -47
- package/tools/theme-check.mjs +139 -0
- package/tools/tts-engines.mjs +270 -41
- package/tools/tts-setup.mjs +257 -0
- package/tools/veo.mjs +204 -0
- package/tools/video.mjs +316 -0
- package/tools/voiceover-server.mjs +163 -54
- package/tools/voiceover.mjs +24 -7
- package/tools/zip.mjs +143 -0
- package/docs/demo.svg +0 -124
- package/themes/aliens.css +0 -87
- package/themes/apple2.css +0 -129
- package/themes/blade-runner.css +0 -86
- package/themes/c64.css +0 -88
- package/themes/friends.css +0 -85
- package/themes/gameboy.css +0 -121
- package/themes/genesis.css +0 -87
- package/themes/godfather.css +0 -85
- package/themes/ibm-oldschool.css +0 -87
- package/themes/miami-vice.css +0 -87
- package/themes/pulp-fiction.css +0 -87
- package/themes/severance.css +0 -85
- package/themes/snes.css +0 -85
- package/themes/star-wars.css +0 -88
- package/themes/stranger-things.css +0 -87
- package/themes/terminator.css +0 -87
package/cli/upgrade.mjs
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Copyright 2026 Gilles Philippart
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* decklight upgrade — bring a self-contained deck's inlined runtime up to the
|
|
7
|
+
* installed version, touching nothing the author wrote.
|
|
8
|
+
*
|
|
9
|
+
* decklight upgrade <deck.html> [--dry-run]
|
|
10
|
+
*
|
|
11
|
+
* What gets swapped (by exact string surgery — the author's sections, notes,
|
|
12
|
+
* Decklight.init config, inlined casts and whitespace survive byte-for-byte):
|
|
13
|
+
* - the runtime <style> + <script> blocks → the installed dist/ builds,
|
|
14
|
+
* re-marked data-decklight-runtime="css|js" so the next upgrade finds
|
|
15
|
+
* them trivially. Unmarked decks (everything init and bundle wrote before
|
|
16
|
+
* the marker existed) are recognized too: the runtime style is the first
|
|
17
|
+
* head <style> carrying the structural css, the runtime script is the one
|
|
18
|
+
* defining Decklight before the <script>Decklight.init call.
|
|
19
|
+
* - <style data-theme="name"> blocks → the installed themes/<name>.css,
|
|
20
|
+
* preserving which one is active (the media="not all" pattern). A theme
|
|
21
|
+
* that no longer ships upstream is kept as-is with a warning.
|
|
22
|
+
*
|
|
23
|
+
* In place, with <deck>.html.bak written first; --dry-run prints what would
|
|
24
|
+
* change and touches nothing; a second run reports "already current". A file
|
|
25
|
+
* with no Decklight.init call, and a merged multi-module bundle
|
|
26
|
+
* (bundle --all), are refused.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import fs from 'node:fs';
|
|
30
|
+
import path from 'node:path';
|
|
31
|
+
import { makeFail, runMain } from './util.mjs';
|
|
32
|
+
import { PKG, PKG_ROOT, runtimeCss, runtimeJs } from './pkg.mjs';
|
|
33
|
+
import { isMain } from '../tools/args.mjs';
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const fail = makeFail('upgrade');
|
|
37
|
+
|
|
38
|
+
const kb = (s) => `${(Buffer.byteLength(s) / 1024).toFixed(1)} KB`;
|
|
39
|
+
|
|
40
|
+
/** Every <style> block inside <head>, with its offsets. Head-bounded on
|
|
41
|
+
* purpose: the inlined runtime SCRIPT in the body contains "<style" and
|
|
42
|
+
* "</style" as strings, which would corrupt a whole-document scan. (The
|
|
43
|
+
* runtime also contains "</head" — but the real head end comes first.) */
|
|
44
|
+
/**
|
|
45
|
+
* Blank the inside of every HTML comment, preserving length and line breaks.
|
|
46
|
+
*
|
|
47
|
+
* A comment that *talks about* markup is still markup to a regex: smoke.html's
|
|
48
|
+
* slide-15 comment says "its text lives inside a <script>", and a raw scan
|
|
49
|
+
* pairs that mention with the real `</script>` several lines below, inventing a
|
|
50
|
+
* block that spans the gap. Masking rather than deleting keeps every offset
|
|
51
|
+
* exact, so the ranges these scanners return still index the original file —
|
|
52
|
+
* and since the mask only ever rewrites characters inside comments, any match
|
|
53
|
+
* found outside one is byte-identical in both strings.
|
|
54
|
+
*/
|
|
55
|
+
const maskComments = (html) => html.replace(/<!--[\s\S]*?-->/g, (m) => m.replace(/[^\n]/g, ' '));
|
|
56
|
+
|
|
57
|
+
// Exported because the ingredients label (cli/audit.mjs, PRESENT#AUDIT) has to
|
|
58
|
+
// enumerate exactly the blocks this command knows how to find. Two scanners
|
|
59
|
+
// that drift apart would mean upgrade rewriting a block the audit calls
|
|
60
|
+
// unaccounted, or the reverse — so there is one of each.
|
|
61
|
+
export function headStyles(html) {
|
|
62
|
+
const headEnd = html.search(/<\/head>/i);
|
|
63
|
+
const out = [];
|
|
64
|
+
const re = /<style\b([^>]*)>([\s\S]*?)<\/style>/gi;
|
|
65
|
+
const masked = maskComments(html);
|
|
66
|
+
let m;
|
|
67
|
+
while ((m = re.exec(masked))) {
|
|
68
|
+
if (headEnd !== -1 && m.index >= headEnd) break;
|
|
69
|
+
out.push({ start: m.index, end: m.index + m[0].length, tag: m[0], attrs: m[1], inner: m[2] });
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Every <script> block, with its offsets. Safe to scan whole-document: the
|
|
75
|
+
* runtime payload is scriptSafe-escaped (init, bundle, and this command all
|
|
76
|
+
* guarantee it), so the first literal "</script>" after an opening tag is
|
|
77
|
+
* that tag's own closer. */
|
|
78
|
+
export function scripts(html) {
|
|
79
|
+
return [...maskComments(html).matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script>/gi)]
|
|
80
|
+
.map((m) => ({ start: m.index, end: m.index + m[0].length, tag: m[0], attrs: m[1], inner: m[2] }));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function upgradeMain(argv = process.argv.slice(2)) {
|
|
84
|
+
if (!argv.length || argv.includes('--help') || argv.includes('-h')) {
|
|
85
|
+
process.stdout.write(`decklight upgrade — bring a self-contained deck's runtime up to the installed version
|
|
86
|
+
|
|
87
|
+
Usage:
|
|
88
|
+
decklight upgrade <deck.html> [--dry-run]
|
|
89
|
+
|
|
90
|
+
Replaces the deck's inlined runtime css + js blocks with the installed
|
|
91
|
+
package's dist/ builds and refreshes its embedded <style data-theme> blocks
|
|
92
|
+
from the installed themes/ (the active theme stays active; a theme that no
|
|
93
|
+
longer ships is kept as-is with a warning). Everything the author wrote —
|
|
94
|
+
sections, notes, the Decklight.init config, inlined casts, custom styles —
|
|
95
|
+
survives byte-for-byte.
|
|
96
|
+
|
|
97
|
+
In place, with <deck>.html.bak written first. Both the marked blocks this
|
|
98
|
+
tool (and init) writes and older unmarked init/bundle output are recognized.
|
|
99
|
+
|
|
100
|
+
Options:
|
|
101
|
+
--dry-run print what would change; write nothing
|
|
102
|
+
`);
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let file = null, dryRun = false;
|
|
107
|
+
for (const a of argv) {
|
|
108
|
+
if (a === '--dry-run') dryRun = true;
|
|
109
|
+
else if (a === '--all') fail('--all is bundle\'s merge flag — upgrade works on single decks; upgrade the source modules, then re-merge');
|
|
110
|
+
else if (!a.startsWith('-')) file = file ?? a;
|
|
111
|
+
else fail(`unknown argument: ${a}`);
|
|
112
|
+
}
|
|
113
|
+
if (!file) fail('no deck given');
|
|
114
|
+
const deckPath = path.resolve(file);
|
|
115
|
+
if (!fs.existsSync(deckPath)) fail(`deck not found: ${deckPath}`);
|
|
116
|
+
const html = fs.readFileSync(deckPath, 'utf8');
|
|
117
|
+
const rel = path.relative('.', deckPath) || file;
|
|
118
|
+
|
|
119
|
+
if (!/Decklight\.init\s*\(/.test(html)) {
|
|
120
|
+
fail(`${rel} is not a Decklight deck (no Decklight.init call found) — nothing to upgrade`);
|
|
121
|
+
}
|
|
122
|
+
// "data-module" alone also appears inside the runtime js; requiring the
|
|
123
|
+
// literal "<section" prefix (absent from the runtime) keeps this precise.
|
|
124
|
+
if (/<section\b[^>]*\bdata-module\s*=/i.test(html)) {
|
|
125
|
+
fail(`${rel} is a merged multi-module bundle (bundle --all) — upgrade works on single decks; upgrade the source modules, then re-merge`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ------------------------------------------------------ runtime js block
|
|
129
|
+
|
|
130
|
+
const allScripts = scripts(html);
|
|
131
|
+
let jsBlock = allScripts.find((s) => /\bdata-decklight-runtime\b/i.test(s.attrs)) ?? null;
|
|
132
|
+
if (!jsBlock) {
|
|
133
|
+
// Unmarked deck: the runtime is the <script> defining Decklight, before
|
|
134
|
+
// the <script>Decklight.init(...) call (init and bundle both write them
|
|
135
|
+
// adjacent; scanning back tolerates an author script slipped between).
|
|
136
|
+
const definesRuntime = (s) => /(?:\bvar\s+|\bwindow\.)Decklight\s*=/.test(s.inner);
|
|
137
|
+
const initAt = allScripts.findIndex((s) =>
|
|
138
|
+
!/\bsrc\s*=/i.test(s.attrs) && /Decklight\.init\s*\(/.test(s.inner) && !definesRuntime(s));
|
|
139
|
+
for (let i = initAt - 1; i >= 0; i--) {
|
|
140
|
+
const s = allScripts[i];
|
|
141
|
+
if (!/\bsrc\s*=/i.test(s.attrs) && definesRuntime(s)) { jsBlock = s; break; }
|
|
142
|
+
}
|
|
143
|
+
if (!jsBlock) {
|
|
144
|
+
if (allScripts.some((s) => /\bsrc\s*=\s*["'][^"']*decklight[^"']*\.js["']/i.test(s.attrs))) {
|
|
145
|
+
fail(`${rel} references the runtime by src= — it is not self-contained, so there is nothing inlined to upgrade (its decklight.{js,css} files are the thing to update; decklight bundle produces a self-contained file)`);
|
|
146
|
+
}
|
|
147
|
+
fail(`${rel}: could not find the inlined runtime (no <script> defining Decklight before the Decklight.init call)`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ----------------------------------------------------- runtime css block
|
|
152
|
+
|
|
153
|
+
const styles = headStyles(html);
|
|
154
|
+
const cssBlock =
|
|
155
|
+
styles.find((s) => /\bdata-decklight-runtime\b/i.test(s.attrs))
|
|
156
|
+
// unmarked: the first head style that is not a theme block and carries
|
|
157
|
+
// the structural css (every version of it styles .decklight itself)
|
|
158
|
+
?? styles.find((s) => !/\bdata-theme\b/i.test(s.attrs) && /\.decklight\b/.test(s.inner))
|
|
159
|
+
?? null;
|
|
160
|
+
|
|
161
|
+
// ------------------------------------------------------------ new blocks
|
|
162
|
+
|
|
163
|
+
const distCss = runtimeCss();
|
|
164
|
+
const distJs = runtimeJs();
|
|
165
|
+
|
|
166
|
+
const edits = []; // { start, end, text }
|
|
167
|
+
const changed = [];
|
|
168
|
+
const warnings = [];
|
|
169
|
+
|
|
170
|
+
edits.push({ start: jsBlock.start, end: jsBlock.end,
|
|
171
|
+
text: `<script data-decklight-runtime="js">${distJs}</script>` });
|
|
172
|
+
if (edits[0].text !== jsBlock.tag) changed.push(`runtime js (${kb(jsBlock.inner)} → ${kb(distJs)})`);
|
|
173
|
+
|
|
174
|
+
if (cssBlock) {
|
|
175
|
+
// Keep the closing tag's own indentation so a marked, current deck
|
|
176
|
+
// round-trips byte-identical (init writes "\n<css>\n </style>").
|
|
177
|
+
const closeIndent = (cssBlock.inner.match(/\n([ \t]*)$/) || [, ''])[1];
|
|
178
|
+
const text = `<style data-decklight-runtime="css">\n${distCss}\n${closeIndent}</style>`;
|
|
179
|
+
edits.push({ start: cssBlock.start, end: cssBlock.end, text });
|
|
180
|
+
if (text !== cssBlock.tag) changed.push(`runtime css (${kb(cssBlock.inner)} → ${kb(distCss)})`);
|
|
181
|
+
} else {
|
|
182
|
+
warnings.push('no runtime <style> block found in <head> — css left alone');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ----------------------------------------------------------- theme blocks
|
|
186
|
+
|
|
187
|
+
let themesRefreshed = 0, themesCurrent = 0;
|
|
188
|
+
for (const s of styles) {
|
|
189
|
+
if (s === cssBlock) continue;
|
|
190
|
+
const nameM = s.attrs.match(/\bdata-theme\s*=\s*["']([\w-]+)["']/i);
|
|
191
|
+
if (!nameM) continue; // generated blocks (valueless data-theme) stay the author's
|
|
192
|
+
const name = nameM[1];
|
|
193
|
+
const cssPath = path.join(PKG_ROOT, 'themes', `${name}.css`);
|
|
194
|
+
if (!fs.existsSync(cssPath)) {
|
|
195
|
+
// Two different situations look identical from here (neither block is
|
|
196
|
+
// in themes/), and MARKETPLACE.md OPEN 1 says they should read
|
|
197
|
+
// differently: `data-theme-added` is theme add/Browse's own marker
|
|
198
|
+
// (SPEC THEME_DISTRIBUTION), so a theme carrying it was never
|
|
199
|
+
// decklight's to begin with — upgrade has nothing "upstream" to refresh
|
|
200
|
+
// it FROM, and never fetches one (MARKETPLACE_REGISTRY's registered-
|
|
201
|
+
// not-fetched invariant extends here too; a theme also carries no
|
|
202
|
+
// version to pin or refuse a stale one against, unlike a transform's
|
|
203
|
+
// apiVersion — THEMING is deliberate about that). Absent the marker,
|
|
204
|
+
// this is a theme decklight itself used to ship and has since dropped.
|
|
205
|
+
const added = /\bdata-theme-added\b/i.test(s.attrs);
|
|
206
|
+
warnings.push(added
|
|
207
|
+
? `theme "${name}" was added from outside decklight (theme add/Browse) — not decklight's to refresh; kept as-is`
|
|
208
|
+
: `theme "${name}" no longer ships upstream — kept as-is`);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const fresh = fs.readFileSync(cssPath, 'utf8');
|
|
212
|
+
if (s.inner.trim() === fresh.trim()) { themesCurrent++; continue; }
|
|
213
|
+
// Swap only the css between the tags: the opening tag (media="not all"
|
|
214
|
+
// or active, plus any author attrs) and surrounding whitespace survive.
|
|
215
|
+
const lead = s.inner.match(/^\s*/)[0];
|
|
216
|
+
const trail = s.inner.match(/\s*$/)[0];
|
|
217
|
+
edits.push({ start: s.start, end: s.end,
|
|
218
|
+
text: `<style${s.attrs}>${lead}${fresh.trim()}${trail}</style>` });
|
|
219
|
+
themesRefreshed++;
|
|
220
|
+
}
|
|
221
|
+
if (themesRefreshed) changed.push(`${themesRefreshed} theme${themesRefreshed > 1 ? 's' : ''} refreshed`);
|
|
222
|
+
|
|
223
|
+
// --------------------------------------------------------------- assemble
|
|
224
|
+
|
|
225
|
+
let next = html;
|
|
226
|
+
for (const e of edits.sort((a, b) => b.start - a.start)) {
|
|
227
|
+
next = next.slice(0, e.start) + e.text + next.slice(e.end);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
for (const w of warnings) process.stdout.write(`warning: ${w}\n`);
|
|
231
|
+
|
|
232
|
+
if (next === html) {
|
|
233
|
+
process.stdout.write(`${rel} is already current (decklight ${PKG.version})\n`);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (dryRun) {
|
|
238
|
+
process.stdout.write(`${rel} → decklight ${PKG.version} (dry run — nothing written):\n`);
|
|
239
|
+
for (const c of changed) process.stdout.write(` would update ${c.replace(' refreshed', '')}\n`);
|
|
240
|
+
if (themesCurrent) process.stdout.write(` ${themesCurrent} theme${themesCurrent > 1 ? 's' : ''} already current\n`);
|
|
241
|
+
process.stdout.write(` would back up first: ${rel}.bak\n`);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
fs.writeFileSync(`${deckPath}.bak`, html);
|
|
246
|
+
fs.writeFileSync(deckPath, next);
|
|
247
|
+
process.stdout.write(`upgraded ${rel} to decklight ${PKG.version} (${changed.join(', ')}; backup: ${rel}.bak)\n`);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (isMain(import.meta.url)) process.exitCode = await runMain('upgrade', upgradeMain);
|
package/cli/util.mjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Helpers shared by the CLI commands. Generic Node-CLI plumbing (argv reading,
|
|
5
|
+
// entry-point detection) lives in tools/args.mjs; this file is the pieces that
|
|
6
|
+
// are specific to *these* commands — the failure convention and the one HTML
|
|
7
|
+
// escape whose exact form is load-bearing.
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A command's refusal, with the message a human reads. Thrown rather than
|
|
11
|
+
* printed, so that the same failure can be reported by whoever is running the
|
|
12
|
+
* command — the dispatcher, a direct `node cli/bundle.mjs` run, or a test
|
|
13
|
+
* calling the main function in-process.
|
|
14
|
+
*/
|
|
15
|
+
export class CommandError extends Error {
|
|
16
|
+
constructor(message, command) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = 'CommandError';
|
|
19
|
+
this.command = command;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A command's failure. `makeFail('bundle')` returns a `fail` that raises
|
|
25
|
+
* `decklight bundle: <msg>`.
|
|
26
|
+
*
|
|
27
|
+
* WHY IT THROWS INSTEAD OF EXITING. It used to call `process.exit(1)` right
|
|
28
|
+
* here, which meant a command that used it could not be called in-process at
|
|
29
|
+
* all: any test touching a failure path took the test runner down with it. So
|
|
30
|
+
* six commands (init, skills, rec, upgrade, publish, bundle) were reachable
|
|
31
|
+
* only by spawning a subprocess, while eleven newer ones returned an exit code
|
|
32
|
+
* and were testable directly — two conventions, and the older one taxed every
|
|
33
|
+
* test that came near it.
|
|
34
|
+
*
|
|
35
|
+
* Throwing keeps the property callers actually depend on — `fail()` never
|
|
36
|
+
* returns, so the code after it is unreachable — and hands the decision about
|
|
37
|
+
* exit codes to the boundary, where it belongs. Every existing call site sits
|
|
38
|
+
* either at the top level of a main or inside a `catch`, so nothing that used
|
|
39
|
+
* to exit now gets swallowed by an intervening `try`.
|
|
40
|
+
*/
|
|
41
|
+
export const makeFail = (cmd) => (msg) => { throw new CommandError(msg, cmd); };
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Print a failure the way a command does: `decklight <cmd>: <message>`.
|
|
45
|
+
*
|
|
46
|
+
* A `CommandError` is a refusal the command wrote for a human, so it prints as
|
|
47
|
+
* itself. Anything else is a bug — a stack is the useful thing there, but not
|
|
48
|
+
* as the first thing the user sees, so the message leads and the stack follows
|
|
49
|
+
* only under DECKLIGHT_DEBUG. Before this, an unexpected throw reached the
|
|
50
|
+
* terminal as a raw Node stack: `decklight import` greeted a Windows user with
|
|
51
|
+
* `Error: ENOENT … scandir '…/space%20dir/decklight/themes'` (#275).
|
|
52
|
+
*/
|
|
53
|
+
export function reportFailure(e, cmd) {
|
|
54
|
+
const name = e instanceof CommandError ? (e.command ?? cmd) : cmd;
|
|
55
|
+
const where = name ? `decklight ${name}: ` : 'decklight: ';
|
|
56
|
+
if (e instanceof CommandError) {
|
|
57
|
+
process.stderr.write(`${where}${e.message}\n`);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
process.stderr.write(`${where}${e?.message ?? e}\n`);
|
|
61
|
+
if (process.env.DECKLIGHT_DEBUG) process.stderr.write(`${e?.stack ?? ''}\n`);
|
|
62
|
+
else process.stderr.write(' (set DECKLIGHT_DEBUG=1 for the stack — this one is a bug, please report it)\n');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Run a command main as a program: await it, turn a failure into the printed
|
|
67
|
+
* message plus exit 1, and pass a returned exit code straight through. The one
|
|
68
|
+
* boundary every entry point uses — `cli/decklight.mjs` for a dispatched
|
|
69
|
+
* command, and each module's own `isMain` block for a direct run.
|
|
70
|
+
*/
|
|
71
|
+
export async function runMain(cmd, fn) {
|
|
72
|
+
try {
|
|
73
|
+
return (await fn()) ?? 0;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
reportFailure(e, cmd);
|
|
76
|
+
return 1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Inline <script> content must never contain "</script" (terminates the tag)
|
|
81
|
+
// NOR "<!--" (flips the HTML tokenizer into script-data-escaped mode, after
|
|
82
|
+
// which closers mis-parse — marked's comment regexes contain it). "\/" is an
|
|
83
|
+
// identity escape everywhere. "<!--" is broken by rewriting the bang as a
|
|
84
|
+
// backslash-u0021 unicode escape, NOT as backslash-bang: the latter is fine
|
|
85
|
+
// in strings and flagless regexes but an INVALID escape inside u-flagged
|
|
86
|
+
// regexes — highlight.js composes its XML grammar's comment regex with /imu
|
|
87
|
+
// the first time a deck highlights language-html, which turned the old
|
|
88
|
+
// escape into a lazy SyntaxError. The unicode escape is valid in strings,
|
|
89
|
+
// templates, JSON, and regexes with or without the u flag.
|
|
90
|
+
export const scriptSafe = (s) => s.replace(/<\/script/gi, '<\\/script').replace(/<!--/g, '<\\u0021--');
|