@bangerpage/cli 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/NOTICE +16 -0
- package/README.md +17 -2
- package/dist/args.d.ts +8 -0
- package/dist/bin.js +943 -0
- package/dist/bin.js.map +7 -0
- package/dist/browser.d.ts +14 -0
- package/dist/capture.d.ts +49 -0
- package/dist/commands/check.d.ts +9 -0
- package/dist/commands/export.d.ts +1 -0
- package/dist/commands/init.d.ts +7 -0
- package/dist/commands/preview.d.ts +1 -0
- package/dist/commands/skill.d.ts +11 -0
- package/dist/commands/snap.d.ts +1 -0
- package/dist/commands/theme.d.ts +1 -0
- package/dist/core-package.d.ts +9 -0
- package/dist/errors.d.ts +11 -0
- package/dist/export/pdf.d.ts +19 -0
- package/dist/html-transform.d.ts +41 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +383 -0
- package/dist/index.js.map +7 -0
- package/dist/server.d.ts +13 -0
- package/dist/skill/SKILL.md +55 -0
- package/dist/skill/meta.json +4 -0
- package/dist/skill/reference/components.md +380 -0
- package/dist/skill/reference/patterns.md +377 -0
- package/dist/skill/reference/themes.md +263 -0
- package/dist/skill/templates/starter.html +20 -0
- package/dist/update-check.d.ts +17 -0
- package/package.json +58 -3
package/dist/bin.js
ADDED
|
@@ -0,0 +1,943 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/bin.ts
|
|
4
|
+
import { readFileSync as readFileSync6, realpathSync } from "node:fs";
|
|
5
|
+
import { fileURLToPath as fileURLToPath4, pathToFileURL } from "node:url";
|
|
6
|
+
|
|
7
|
+
// src/browser.ts
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
|
+
import { chromium } from "playwright-core";
|
|
10
|
+
var BrowserNotFoundError = class extends Error {
|
|
11
|
+
constructor() {
|
|
12
|
+
super("no browser found (tried Google Chrome and the bundled Chromium)");
|
|
13
|
+
this.name = "BrowserNotFoundError";
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function installHint() {
|
|
17
|
+
const { version: version2 } = createRequire(import.meta.url)("playwright-core/package.json");
|
|
18
|
+
return `install a browser with: npx playwright-core@${version2} install chromium`;
|
|
19
|
+
}
|
|
20
|
+
async function launchBrowser(opts = {}) {
|
|
21
|
+
const headless = opts.headless ?? true;
|
|
22
|
+
const launchers = opts.launchers ?? {
|
|
23
|
+
chrome: () => chromium.launch({ channel: "chrome", headless }),
|
|
24
|
+
chromium: () => chromium.launch({ headless })
|
|
25
|
+
};
|
|
26
|
+
for (const launch of [launchers.chrome, launchers.chromium]) {
|
|
27
|
+
try {
|
|
28
|
+
return await launch();
|
|
29
|
+
} catch {
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
throw new BrowserNotFoundError();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/args.ts
|
|
36
|
+
import { parseArgs } from "node:util";
|
|
37
|
+
|
|
38
|
+
// src/errors.ts
|
|
39
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
40
|
+
var CliError = class extends Error {
|
|
41
|
+
constructor(exitCode, message) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.exitCode = exitCode;
|
|
44
|
+
this.name = "CliError";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function readInput(file) {
|
|
48
|
+
if (!existsSync(file) || !statSync(file).isFile()) throw new CliError(2, `file not found: ${file}`);
|
|
49
|
+
return readFileSync(file, "utf8");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/args.ts
|
|
53
|
+
function parseArgsStrict(argv, options) {
|
|
54
|
+
try {
|
|
55
|
+
return parseArgs({ args: argv, options, allowPositionals: true, strict: true });
|
|
56
|
+
} catch (error) {
|
|
57
|
+
throw new CliError(2, error.message);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function deckFile(positionals, usage) {
|
|
61
|
+
if (positionals.length > 1) throw new CliError(2, usage);
|
|
62
|
+
return positionals[0] ?? "index.html";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/capture.ts
|
|
66
|
+
import { readdir, rm } from "node:fs/promises";
|
|
67
|
+
import { join as join3 } from "node:path";
|
|
68
|
+
|
|
69
|
+
// src/core-package.ts
|
|
70
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
71
|
+
import { dirname, join } from "node:path";
|
|
72
|
+
import { fileURLToPath } from "node:url";
|
|
73
|
+
import { PRESET_NAMES, isPresetName, parseThemeJson, presetTheme, resolveTheme, serializeTheme } from "@bangerpage/core/theme";
|
|
74
|
+
var CORE_PACKAGE = "@bangerpage/core";
|
|
75
|
+
var CDN_HOSTS = ["cdn.jsdelivr.net/npm", "unpkg.com"];
|
|
76
|
+
function corePackageDir() {
|
|
77
|
+
return dirname(fileURLToPath(import.meta.resolve(`${CORE_PACKAGE}/package.json`)));
|
|
78
|
+
}
|
|
79
|
+
function coreDistDir() {
|
|
80
|
+
return join(corePackageDir(), "dist");
|
|
81
|
+
}
|
|
82
|
+
function coreVersion() {
|
|
83
|
+
return JSON.parse(readFileSync2(join(corePackageDir(), "package.json"), "utf8")).version;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/html-transform.ts
|
|
87
|
+
import { parse } from "parse5";
|
|
88
|
+
function escapeRegExp(value) {
|
|
89
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
90
|
+
}
|
|
91
|
+
function runtimeUrlPattern(corePackage) {
|
|
92
|
+
const hosts = CDN_HOSTS.map(escapeRegExp).join("|");
|
|
93
|
+
return new RegExp(`^https?://(${hosts})/${escapeRegExp(corePackage)}(@[^/]+)?/dist/index\\.js$`);
|
|
94
|
+
}
|
|
95
|
+
function moduleScripts(html) {
|
|
96
|
+
const found = [];
|
|
97
|
+
const visit = (node) => {
|
|
98
|
+
for (const child of node.childNodes) {
|
|
99
|
+
if (!("tagName" in child)) continue;
|
|
100
|
+
const element = child;
|
|
101
|
+
const loc = element.sourceCodeLocation;
|
|
102
|
+
if (element.tagName === "script" && loc) {
|
|
103
|
+
const attr = (name) => element.attrs.find((a) => a.name === name)?.value;
|
|
104
|
+
const src = attr("src");
|
|
105
|
+
const srcLoc = loc.attrs?.src;
|
|
106
|
+
if (attr("type") === "module" && src !== void 0 && srcLoc) {
|
|
107
|
+
found.push({
|
|
108
|
+
src,
|
|
109
|
+
line: loc.startLine,
|
|
110
|
+
element: { start: loc.startOffset, end: loc.endOffset },
|
|
111
|
+
srcAttr: { start: srcLoc.startOffset, end: srcLoc.endOffset }
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
visit(element);
|
|
116
|
+
if (element.tagName === "template") visit(element.content);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
visit(parse(html, { sourceCodeLocationInfo: true }));
|
|
120
|
+
return found;
|
|
121
|
+
}
|
|
122
|
+
function findRuntimeScript(html, corePackage) {
|
|
123
|
+
const pattern = runtimeUrlPattern(corePackage);
|
|
124
|
+
for (const script of moduleScripts(html)) {
|
|
125
|
+
const match = pattern.exec(script.src);
|
|
126
|
+
if (match) return { ...script, version: match[2] ? match[2].slice(1) : null };
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
function runtimeVersionWarning(html, corePackage, localVersion) {
|
|
131
|
+
const ref = findRuntimeScript(html, corePackage);
|
|
132
|
+
if (ref) {
|
|
133
|
+
if (ref.version === null) {
|
|
134
|
+
return { message: `the runtime URL has no version; pin it to ${corePackage}@${localVersion}`, line: ref.line };
|
|
135
|
+
}
|
|
136
|
+
if (ref.version !== localVersion) {
|
|
137
|
+
return {
|
|
138
|
+
message: `the deck loads ${corePackage}@${ref.version} but this CLI uses ${corePackage}@${localVersion}; the published deck will run ${ref.version}`,
|
|
139
|
+
line: ref.line
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const other = moduleScripts(html).find((script) => script.src.includes(corePackage));
|
|
145
|
+
if (other) {
|
|
146
|
+
return {
|
|
147
|
+
message: `runtime script ${other.src} is not a recognized CDN URL and is not replaced with the local core`,
|
|
148
|
+
line: other.line
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
function rewriteRuntimeScript(html, corePackage, newSrc) {
|
|
154
|
+
const ref = findRuntimeScript(html, corePackage);
|
|
155
|
+
if (!ref) return html;
|
|
156
|
+
const attrText = html.slice(ref.srcAttr.start, ref.srcAttr.end);
|
|
157
|
+
const valueStart = attrText.indexOf(ref.src);
|
|
158
|
+
if (valueStart < 0) return html;
|
|
159
|
+
const start = ref.srcAttr.start + valueStart;
|
|
160
|
+
return html.slice(0, start) + newSrc + html.slice(start + ref.src.length);
|
|
161
|
+
}
|
|
162
|
+
function injectBeforeBodyEnd(html, snippet) {
|
|
163
|
+
const index = html.toLowerCase().lastIndexOf("</body>");
|
|
164
|
+
if (index < 0) return html + snippet;
|
|
165
|
+
return html.slice(0, index) + snippet + html.slice(index);
|
|
166
|
+
}
|
|
167
|
+
function findFirstElement(node, tagName) {
|
|
168
|
+
for (const child of node.childNodes) {
|
|
169
|
+
if (!("tagName" in child)) continue;
|
|
170
|
+
const element = child;
|
|
171
|
+
if (element.tagName === tagName) return element;
|
|
172
|
+
const inner = findFirstElement(element, tagName);
|
|
173
|
+
if (inner) return inner;
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
function lineIndent(html, offset) {
|
|
178
|
+
let start = offset;
|
|
179
|
+
while (start > 0 && (html[start - 1] === " " || html[start - 1] === " ")) start--;
|
|
180
|
+
if (start === 0 || html[start - 1] === "\n" || html[start - 1] === "\r") return html.slice(start, offset);
|
|
181
|
+
return "";
|
|
182
|
+
}
|
|
183
|
+
function hasOnlyJsonScript(block) {
|
|
184
|
+
let scripts = 0;
|
|
185
|
+
for (const child of block.childNodes) {
|
|
186
|
+
if (child.nodeName === "#text") {
|
|
187
|
+
if (child.value.trim() !== "") return false;
|
|
188
|
+
} else if (!("tagName" in child)) {
|
|
189
|
+
return false;
|
|
190
|
+
} else if (++scripts > 1 || child.tagName !== "script" || !child.attrs.some((a) => a.name === "type" && a.value === "application/json")) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
function applyThemeBlock(html, json) {
|
|
197
|
+
const root = findFirstElement(parse(html, { sourceCodeLocationInfo: true }), "bang-root");
|
|
198
|
+
const rootLoc = root?.sourceCodeLocation;
|
|
199
|
+
if (!root || !rootLoc?.startTag) return null;
|
|
200
|
+
const blocks = root.childNodes.filter((child) => "tagName" in child && child.tagName === "bang-theme");
|
|
201
|
+
const eol = html.includes("\r\n") ? "\r\n" : "\n";
|
|
202
|
+
const makeBlock = (indent) => [
|
|
203
|
+
"<bang-theme>",
|
|
204
|
+
`${indent} <script type="application/json">`,
|
|
205
|
+
...json.split("\n").map((line) => `${indent} ${line}`),
|
|
206
|
+
`${indent} </script>`,
|
|
207
|
+
`${indent}</bang-theme>`
|
|
208
|
+
].join(eol);
|
|
209
|
+
const edits = [];
|
|
210
|
+
const first = blocks[0];
|
|
211
|
+
if (first) {
|
|
212
|
+
const loc = first.sourceCodeLocation;
|
|
213
|
+
if (!loc?.endTag) return { error: "unclosed-theme" };
|
|
214
|
+
if (!hasOnlyJsonScript(first)) return { error: "foreign-children" };
|
|
215
|
+
edits.push({ start: loc.startOffset, end: loc.endOffset, text: makeBlock(lineIndent(html, loc.startOffset)) });
|
|
216
|
+
} else {
|
|
217
|
+
const at = rootLoc.startTag.endOffset;
|
|
218
|
+
const indent = `${lineIndent(html, rootLoc.startOffset)} `;
|
|
219
|
+
edits.push({ start: at, end: at, text: `${eol}${indent}${makeBlock(indent)}` });
|
|
220
|
+
}
|
|
221
|
+
const attr = root.attrs.find((a) => a.name === "theme");
|
|
222
|
+
if (!attr) {
|
|
223
|
+
const at = rootLoc.startOffset + 1 + "bang-root".length;
|
|
224
|
+
edits.push({ start: at, end: at, text: ' theme="custom"' });
|
|
225
|
+
} else if (attr.value !== "custom") {
|
|
226
|
+
const attrLoc = rootLoc.attrs?.theme;
|
|
227
|
+
if (attrLoc) edits.push({ start: attrLoc.startOffset, end: attrLoc.endOffset, text: 'theme="custom"' });
|
|
228
|
+
}
|
|
229
|
+
let out = html;
|
|
230
|
+
for (const edit of edits.sort((a, b) => b.start - a.start)) out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
231
|
+
return { html: out, extraBlocks: Math.max(0, blocks.length - 1) };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// src/server.ts
|
|
235
|
+
import { createReadStream, existsSync as existsSync2, readFileSync as readFileSync3, statSync as statSync2, watch } from "node:fs";
|
|
236
|
+
import { createServer } from "node:http";
|
|
237
|
+
import { basename, dirname as dirname2, extname, join as join2, normalize, resolve, sep } from "node:path";
|
|
238
|
+
var CONTENT_TYPES = {
|
|
239
|
+
".html": "text/html; charset=utf-8",
|
|
240
|
+
".js": "text/javascript; charset=utf-8",
|
|
241
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
242
|
+
".css": "text/css; charset=utf-8",
|
|
243
|
+
".json": "application/json; charset=utf-8",
|
|
244
|
+
".map": "application/json; charset=utf-8",
|
|
245
|
+
".svg": "image/svg+xml",
|
|
246
|
+
".png": "image/png",
|
|
247
|
+
".jpg": "image/jpeg",
|
|
248
|
+
".jpeg": "image/jpeg",
|
|
249
|
+
".gif": "image/gif",
|
|
250
|
+
".webp": "image/webp",
|
|
251
|
+
".avif": "image/avif",
|
|
252
|
+
".woff2": "font/woff2",
|
|
253
|
+
".txt": "text/plain; charset=utf-8"
|
|
254
|
+
};
|
|
255
|
+
function contentType(file) {
|
|
256
|
+
return CONTENT_TYPES[extname(file).toLowerCase()] ?? "application/octet-stream";
|
|
257
|
+
}
|
|
258
|
+
function resolveWithin(root, urlPath) {
|
|
259
|
+
let decoded;
|
|
260
|
+
try {
|
|
261
|
+
decoded = decodeURIComponent(urlPath);
|
|
262
|
+
} catch {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
if (decoded.includes("\0")) return null;
|
|
266
|
+
const base = resolve(root);
|
|
267
|
+
const full = normalize(join2(base, decoded));
|
|
268
|
+
return full === base || full.startsWith(base + sep) ? full : null;
|
|
269
|
+
}
|
|
270
|
+
var RELOAD_SNIPPET = `<script>new EventSource("/__banger/events").addEventListener("reload",()=>location.reload())</script>`;
|
|
271
|
+
var EVENTS_PATH = "/__banger/events";
|
|
272
|
+
var CORE_PREFIX = "/__banger/core/";
|
|
273
|
+
async function startServer(opts) {
|
|
274
|
+
readInput(opts.file);
|
|
275
|
+
const file = resolve(opts.file);
|
|
276
|
+
const root = dirname2(file);
|
|
277
|
+
const served = /* @__PURE__ */ new Set();
|
|
278
|
+
const clients = /* @__PURE__ */ new Set();
|
|
279
|
+
const send = (res, status, body, type = "text/plain; charset=utf-8") => {
|
|
280
|
+
res.writeHead(status, { "content-type": type, "cache-control": "no-store" });
|
|
281
|
+
res.end(body);
|
|
282
|
+
};
|
|
283
|
+
const server = createServer((req, res) => {
|
|
284
|
+
if (req.method !== "GET" && req.method !== "HEAD") return send(res, 405, "method not allowed");
|
|
285
|
+
const urlPath = (req.url ?? "/").split("?")[0] ?? "/";
|
|
286
|
+
if (urlPath === EVENTS_PATH && opts.watch) {
|
|
287
|
+
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-store", connection: "keep-alive" });
|
|
288
|
+
res.write(": connected\n\n");
|
|
289
|
+
clients.add(res);
|
|
290
|
+
req.on("close", () => clients.delete(res));
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const isCore = urlPath.startsWith(CORE_PREFIX);
|
|
294
|
+
const target = isCore ? resolveWithin(coreDistDir(), urlPath.slice(CORE_PREFIX.length)) : resolveWithin(root, urlPath === "/" ? basename(file) : urlPath);
|
|
295
|
+
if (!target || !existsSync2(target) || !statSync2(target).isFile()) return send(res, 404, "not found");
|
|
296
|
+
if (!isCore) served.add(target);
|
|
297
|
+
const type = contentType(target);
|
|
298
|
+
if (!isCore && extname(target).toLowerCase() === ".html") {
|
|
299
|
+
let html = rewriteRuntimeScript(readFileSync3(target, "utf8"), CORE_PACKAGE, `${CORE_PREFIX}index.js`);
|
|
300
|
+
if (opts.watch) html = injectBeforeBodyEnd(html, RELOAD_SNIPPET);
|
|
301
|
+
return send(res, 200, req.method === "HEAD" ? "" : html, type);
|
|
302
|
+
}
|
|
303
|
+
res.writeHead(200, { "content-type": type, "cache-control": "no-store" });
|
|
304
|
+
if (req.method === "HEAD") return res.end();
|
|
305
|
+
createReadStream(target).pipe(res);
|
|
306
|
+
});
|
|
307
|
+
await new Promise((resolveListen, rejectListen) => {
|
|
308
|
+
server.once("error", (error) => {
|
|
309
|
+
rejectListen(
|
|
310
|
+
error.code === "EADDRINUSE" ? new CliError(2, `port ${opts.port} is already in use`) : new CliError(2, `cannot start the server: ${error.message}`)
|
|
311
|
+
);
|
|
312
|
+
});
|
|
313
|
+
server.listen(opts.port ?? 0, "127.0.0.1", resolveListen);
|
|
314
|
+
});
|
|
315
|
+
let watcher;
|
|
316
|
+
let timer;
|
|
317
|
+
if (opts.watch) {
|
|
318
|
+
watcher = watch(root, { recursive: true }, (_event, name) => {
|
|
319
|
+
if (!name || !served.has(join2(root, name.toString()))) return;
|
|
320
|
+
clearTimeout(timer);
|
|
321
|
+
timer = setTimeout(() => {
|
|
322
|
+
for (const client of clients) client.write("event: reload\ndata: {}\n\n");
|
|
323
|
+
}, 100);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
const origin = `http://127.0.0.1:${server.address().port}`;
|
|
327
|
+
const entry2 = `${origin}/${encodeURIComponent(basename(file))}`;
|
|
328
|
+
return {
|
|
329
|
+
url: entry2,
|
|
330
|
+
urlFor: (query) => query ? `${entry2}?${query}` : entry2,
|
|
331
|
+
close: async () => {
|
|
332
|
+
clearTimeout(timer);
|
|
333
|
+
watcher?.close();
|
|
334
|
+
for (const client of clients) client.end();
|
|
335
|
+
server.closeAllConnections();
|
|
336
|
+
await new Promise((resolveClose) => server.close(() => resolveClose()));
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// src/capture.ts
|
|
342
|
+
var RUNTIME_LOAD_TIMEOUT_MS = 15e3;
|
|
343
|
+
var STOP_FILE_PATTERN = /^stop-\d{3}-step-\d{3}\.png$/;
|
|
344
|
+
var pad = (n) => String(n).padStart(3, "0");
|
|
345
|
+
function stopFileName(stop, step) {
|
|
346
|
+
return `stop-${pad(stop)}-step-${pad(step)}.png`;
|
|
347
|
+
}
|
|
348
|
+
function expandStates(path, sel, only) {
|
|
349
|
+
if (only) return [only];
|
|
350
|
+
return path.flatMap(
|
|
351
|
+
(stop, index) => sel === "last" ? [{ stop: index, step: stop.steps }] : Array.from({ length: stop.steps + 1 }, (_, step) => ({ stop: index, step }))
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
async function clearStopFiles(dir) {
|
|
355
|
+
let entries;
|
|
356
|
+
try {
|
|
357
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
358
|
+
} catch (error) {
|
|
359
|
+
if (error.code === "ENOENT") return [];
|
|
360
|
+
throw error;
|
|
361
|
+
}
|
|
362
|
+
const removed = [];
|
|
363
|
+
for (const entry2 of entries) {
|
|
364
|
+
if (entry2.isFile() && STOP_FILE_PATTERN.test(entry2.name)) {
|
|
365
|
+
await rm(join3(dir, entry2.name));
|
|
366
|
+
removed.push(entry2.name);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return removed;
|
|
370
|
+
}
|
|
371
|
+
var defaultOnWarning = (message) => {
|
|
372
|
+
process.stderr.write(`warning: ${message}
|
|
373
|
+
`);
|
|
374
|
+
};
|
|
375
|
+
async function captureState(session, ref, clip = { x: 0, y: 0, width: 1920, height: 1080 }) {
|
|
376
|
+
return session.page.screenshot({ type: "png", clip });
|
|
377
|
+
}
|
|
378
|
+
async function withCapture(file, opts, fn) {
|
|
379
|
+
const seen = /* @__PURE__ */ new Set();
|
|
380
|
+
const sink = opts.onWarning ?? defaultOnWarning;
|
|
381
|
+
const warn = (message) => {
|
|
382
|
+
if (seen.has(message)) return;
|
|
383
|
+
seen.add(message);
|
|
384
|
+
sink(message);
|
|
385
|
+
};
|
|
386
|
+
const html = readInput(file);
|
|
387
|
+
if (opts.runtimeVersionCheck !== false) {
|
|
388
|
+
const versionWarning = runtimeVersionWarning(html, CORE_PACKAGE, coreVersion());
|
|
389
|
+
if (versionWarning) warn(versionWarning.message);
|
|
390
|
+
}
|
|
391
|
+
const server = await startServer({ file, watch: false, port: 0 });
|
|
392
|
+
try {
|
|
393
|
+
const browser = await launchBrowser({ launchers: opts.launchers }).catch((error) => {
|
|
394
|
+
if (error instanceof BrowserNotFoundError) throw new CliError(2, `${error.message}; ${installHint()}`);
|
|
395
|
+
throw error;
|
|
396
|
+
});
|
|
397
|
+
try {
|
|
398
|
+
const scale = opts.scale ?? 1;
|
|
399
|
+
const context = await browser.newContext({ viewport: { width: 1920, height: 1080 }, deviceScaleFactor: scale });
|
|
400
|
+
const page = await context.newPage();
|
|
401
|
+
const failedRequests = [];
|
|
402
|
+
page.on("requestfailed", (request) => failedRequests.push(request.url()));
|
|
403
|
+
page.on("response", (response) => {
|
|
404
|
+
if (response.status() >= 400) failedRequests.push(`${response.url()} (${response.status()})`);
|
|
405
|
+
});
|
|
406
|
+
await page.goto(server.urlFor("mode=export#stop=0&step=last"), { waitUntil: "commit" });
|
|
407
|
+
const timeoutMs = opts.runtimeTimeoutMs ?? RUNTIME_LOAD_TIMEOUT_MS;
|
|
408
|
+
const loaded = await page.evaluate(
|
|
409
|
+
(ms) => Promise.race([
|
|
410
|
+
customElements.whenDefined("bang-root").then(() => true),
|
|
411
|
+
new Promise((resolveTimeout) => setTimeout(() => resolveTimeout(false), ms))
|
|
412
|
+
]),
|
|
413
|
+
timeoutMs
|
|
414
|
+
);
|
|
415
|
+
if (!loaded) {
|
|
416
|
+
const detail = failedRequests.length > 0 ? ` Failed requests: ${failedRequests.join(", ")}.` : "";
|
|
417
|
+
throw new CliError(
|
|
418
|
+
1,
|
|
419
|
+
`the runtime script did not load (bang-root was not defined within ${timeoutMs}ms).${detail} Check the <script type="module" src> URL of the runtime.`
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
await page.waitForFunction(
|
|
423
|
+
() => typeof document.querySelector("bang-root")?.getPath === "function",
|
|
424
|
+
void 0,
|
|
425
|
+
{ timeout: timeoutMs }
|
|
426
|
+
).catch(() => {
|
|
427
|
+
throw new CliError(1, "the deck has no <bang-root> element; wrap the slides in <bang-root>.");
|
|
428
|
+
});
|
|
429
|
+
const path = await page.evaluate(
|
|
430
|
+
() => document.querySelector("bang-root").getPath()
|
|
431
|
+
);
|
|
432
|
+
const session = {
|
|
433
|
+
page,
|
|
434
|
+
path,
|
|
435
|
+
title: await page.title(),
|
|
436
|
+
async goto(ref) {
|
|
437
|
+
const report = await page.evaluate(async ({ stop, step }) => {
|
|
438
|
+
const root = document.querySelector("bang-root");
|
|
439
|
+
await root.goto(stop, step);
|
|
440
|
+
return root.ready();
|
|
441
|
+
}, ref);
|
|
442
|
+
for (const message of report.warnings) warn(message);
|
|
443
|
+
return report;
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
return await fn(session);
|
|
447
|
+
} finally {
|
|
448
|
+
await browser.close();
|
|
449
|
+
}
|
|
450
|
+
} finally {
|
|
451
|
+
await server.close();
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
function formatReadyFailure(failure) {
|
|
455
|
+
return `ready failed: ${failure.tag} ${failure.selector}: ${failure.reason}`;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// src/commands/check.ts
|
|
459
|
+
var USAGE = "usage: banger check [file] [--json]";
|
|
460
|
+
async function checkDeck(file) {
|
|
461
|
+
const warnings = [];
|
|
462
|
+
const failed = /* @__PURE__ */ new Set();
|
|
463
|
+
const stops = await withCapture(file, { onWarning: (message) => warnings.push(message) }, async (session) => {
|
|
464
|
+
for (const ref of expandStates(session.path, "last")) {
|
|
465
|
+
const report = await session.goto(ref);
|
|
466
|
+
for (const failure of report.failed) failed.add(formatReadyFailure(failure));
|
|
467
|
+
}
|
|
468
|
+
return session.path.length;
|
|
469
|
+
});
|
|
470
|
+
return { ok: warnings.length === 0 && failed.size === 0, stops, warnings, failed: [...failed] };
|
|
471
|
+
}
|
|
472
|
+
async function runCheck(argv) {
|
|
473
|
+
if (argv.includes("--help")) {
|
|
474
|
+
process.stdout.write(`${USAGE}
|
|
475
|
+
`);
|
|
476
|
+
return 0;
|
|
477
|
+
}
|
|
478
|
+
const { values, positionals } = parseArgsStrict(argv, { json: { type: "boolean" } });
|
|
479
|
+
const result = await checkDeck(deckFile(positionals, USAGE));
|
|
480
|
+
if (values.json) {
|
|
481
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}
|
|
482
|
+
`);
|
|
483
|
+
} else {
|
|
484
|
+
for (const line of result.failed) process.stdout.write(`${line}
|
|
485
|
+
`);
|
|
486
|
+
for (const line of result.warnings) process.stdout.write(`warning: ${line}
|
|
487
|
+
`);
|
|
488
|
+
const problems = result.failed.length + result.warnings.length;
|
|
489
|
+
process.stdout.write(
|
|
490
|
+
problems === 0 ? `ok: ${result.stops} stops, no problems
|
|
491
|
+
` : `${problems} problem${problems === 1 ? "" : "s"} in ${result.stops} stops
|
|
492
|
+
`
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
return result.ok ? 0 : 1;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// src/export/pdf.ts
|
|
499
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
500
|
+
import { basename as basename2, dirname as dirname3, extname as extname2, resolve as resolve2 } from "node:path";
|
|
501
|
+
import { PDFDocument } from "@cantoo/pdf-lib";
|
|
502
|
+
async function exportPdf(file, opts = {}) {
|
|
503
|
+
const out = resolve2(opts.out ?? resolve2(dirname3(file), `${basename2(file, extname2(file))}.pdf`));
|
|
504
|
+
const failures = /* @__PURE__ */ new Map();
|
|
505
|
+
const pages = [];
|
|
506
|
+
const title = await withCapture(
|
|
507
|
+
file,
|
|
508
|
+
{ launchers: opts.launchers, onWarning: opts.onWarning },
|
|
509
|
+
async (session) => {
|
|
510
|
+
for (const ref of expandStates(session.path, opts.steps ?? "last")) {
|
|
511
|
+
const report = await session.goto(ref);
|
|
512
|
+
for (const failure of report.failed) failures.set(formatReadyFailure(failure), failure);
|
|
513
|
+
pages.push(
|
|
514
|
+
await session.page.pdf({ width: "1920px", height: "1080px", printBackground: true, pageRanges: "1" })
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
return session.title;
|
|
518
|
+
}
|
|
519
|
+
);
|
|
520
|
+
if (failures.size > 0) {
|
|
521
|
+
throw new CliError(1, `export failed; nothing was written
|
|
522
|
+
${[...failures.keys()].join("\n")}`);
|
|
523
|
+
}
|
|
524
|
+
const merged = await PDFDocument.create();
|
|
525
|
+
for (const page of pages) {
|
|
526
|
+
const doc = await PDFDocument.load(page);
|
|
527
|
+
for (const copied of await merged.copyPages(doc, doc.getPageIndices())) merged.addPage(copied);
|
|
528
|
+
}
|
|
529
|
+
merged.setTitle(title);
|
|
530
|
+
await mkdir(dirname3(out), { recursive: true });
|
|
531
|
+
await writeFile(out, await merged.save());
|
|
532
|
+
return out;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// src/commands/export.ts
|
|
536
|
+
var USAGE2 = "usage: banger export [file] --pdf [--out path] [--steps last|all]";
|
|
537
|
+
async function runExport(argv) {
|
|
538
|
+
if (argv.includes("--help")) {
|
|
539
|
+
process.stdout.write(`${USAGE2}
|
|
540
|
+
`);
|
|
541
|
+
return 0;
|
|
542
|
+
}
|
|
543
|
+
const { values, positionals } = parseArgsStrict(argv, {
|
|
544
|
+
pdf: { type: "boolean" },
|
|
545
|
+
out: { type: "string" },
|
|
546
|
+
steps: { type: "string" }
|
|
547
|
+
});
|
|
548
|
+
const file = deckFile(positionals, USAGE2);
|
|
549
|
+
if (!values.pdf) throw new CliError(2, `choose exactly one export format
|
|
550
|
+
${USAGE2}`);
|
|
551
|
+
if (values.steps !== void 0 && values.steps !== "last" && values.steps !== "all") {
|
|
552
|
+
throw new CliError(2, `invalid --steps: ${values.steps} (expected last or all)`);
|
|
553
|
+
}
|
|
554
|
+
const out = await exportPdf(file, { out: values.out, steps: values.steps });
|
|
555
|
+
process.stdout.write(`${out}
|
|
556
|
+
`);
|
|
557
|
+
return 0;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// src/commands/init.ts
|
|
561
|
+
import { copyFileSync, existsSync as existsSync3, mkdirSync } from "node:fs";
|
|
562
|
+
import { join as join4, resolve as resolve3 } from "node:path";
|
|
563
|
+
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
564
|
+
var USAGE3 = "usage: banger init [dir] [--force]";
|
|
565
|
+
function initDeck(opts) {
|
|
566
|
+
const target = join4(opts.dir, "index.html");
|
|
567
|
+
if (existsSync3(target) && !opts.force) throw new CliError(2, `${target} already exists; pass --force to overwrite`);
|
|
568
|
+
if (!existsSync3(opts.starter)) throw new CliError(2, `the bundled starter is missing: ${opts.starter}`);
|
|
569
|
+
mkdirSync(opts.dir, { recursive: true });
|
|
570
|
+
copyFileSync(opts.starter, target);
|
|
571
|
+
return target;
|
|
572
|
+
}
|
|
573
|
+
function runInit(argv) {
|
|
574
|
+
const { values, positionals } = parseArgsStrict(argv, { force: { type: "boolean" } });
|
|
575
|
+
if (positionals.length > 1) throw new CliError(2, USAGE3);
|
|
576
|
+
const starter = fileURLToPath2(new URL("./skill/templates/starter.html", import.meta.url));
|
|
577
|
+
const target = initDeck({ starter, dir: resolve3(positionals[0] ?? "."), force: values.force });
|
|
578
|
+
process.stdout.write(`${target}
|
|
579
|
+
`);
|
|
580
|
+
return 0;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// src/commands/preview.ts
|
|
584
|
+
var USAGE4 = "usage: banger preview [file] [--port N]";
|
|
585
|
+
async function runPreview(argv) {
|
|
586
|
+
if (argv.includes("--help")) {
|
|
587
|
+
process.stdout.write(`${USAGE4}
|
|
588
|
+
`);
|
|
589
|
+
return 0;
|
|
590
|
+
}
|
|
591
|
+
const { values, positionals } = parseArgsStrict(argv, { port: { type: "string" } });
|
|
592
|
+
const file = deckFile(positionals, USAGE4);
|
|
593
|
+
const port = values.port === void 0 ? 4321 : Number(values.port);
|
|
594
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new CliError(2, `invalid --port: ${values.port}`);
|
|
595
|
+
const warning = runtimeVersionWarning(readInput(file), CORE_PACKAGE, coreVersion());
|
|
596
|
+
if (warning) process.stderr.write(`warning: ${warning.message}
|
|
597
|
+
`);
|
|
598
|
+
const stopped = new Promise((resolveStop) => process.once("SIGINT", resolveStop));
|
|
599
|
+
const server = await startServer({ file, watch: true, port });
|
|
600
|
+
process.stdout.write(`${server.url}
|
|
601
|
+
`);
|
|
602
|
+
await stopped;
|
|
603
|
+
await server.close();
|
|
604
|
+
return 0;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// src/commands/skill.ts
|
|
608
|
+
import { cpSync, existsSync as existsSync4, readFileSync as readFileSync4, rmSync, writeFileSync } from "node:fs";
|
|
609
|
+
import { homedir } from "node:os";
|
|
610
|
+
import { join as join5, resolve as resolve4 } from "node:path";
|
|
611
|
+
import { fileURLToPath as fileURLToPath3 } from "node:url";
|
|
612
|
+
var USAGE5 = "usage: banger skill install [--project | --dir <path>] [--force]";
|
|
613
|
+
var MARKER = ".banger-skill";
|
|
614
|
+
function installSkill(opts) {
|
|
615
|
+
if (!existsSync4(join5(opts.source, "SKILL.md"))) throw new CliError(2, `the bundled skill is missing: ${opts.source}`);
|
|
616
|
+
let outcome = "installed";
|
|
617
|
+
if (existsSync4(opts.dest)) {
|
|
618
|
+
if (!existsSync4(join5(opts.dest, MARKER)) && !opts.force) {
|
|
619
|
+
throw new CliError(2, `${opts.dest} already exists and was not written by banger; pass --force to replace it`);
|
|
620
|
+
}
|
|
621
|
+
rmSync(opts.dest, { recursive: true, force: true });
|
|
622
|
+
outcome = "updated";
|
|
623
|
+
}
|
|
624
|
+
cpSync(opts.source, opts.dest, { recursive: true });
|
|
625
|
+
writeFileSync(join5(opts.dest, MARKER), `${opts.version}
|
|
626
|
+
`);
|
|
627
|
+
return outcome;
|
|
628
|
+
}
|
|
629
|
+
function bundledSkill() {
|
|
630
|
+
return fileURLToPath3(new URL("./skill", import.meta.url));
|
|
631
|
+
}
|
|
632
|
+
function cliVersion() {
|
|
633
|
+
const manifest = fileURLToPath3(new URL("../package.json", import.meta.url));
|
|
634
|
+
return JSON.parse(readFileSync4(manifest, "utf8")).version;
|
|
635
|
+
}
|
|
636
|
+
function runSkill(argv) {
|
|
637
|
+
const [sub, ...rest] = argv;
|
|
638
|
+
if (sub !== "install") throw new CliError(2, USAGE5);
|
|
639
|
+
const { values, positionals } = parseArgsStrict(rest, {
|
|
640
|
+
project: { type: "boolean" },
|
|
641
|
+
dir: { type: "string" },
|
|
642
|
+
force: { type: "boolean" }
|
|
643
|
+
});
|
|
644
|
+
if (positionals.length > 0 || values.project && values.dir !== void 0) throw new CliError(2, USAGE5);
|
|
645
|
+
const dest = values.dir !== void 0 ? resolve4(values.dir) : values.project ? resolve4(".claude", "skills", "banger") : join5(homedir(), ".claude", "skills", "banger");
|
|
646
|
+
const outcome = installSkill({ source: bundledSkill(), dest, version: cliVersion(), force: values.force });
|
|
647
|
+
process.stdout.write(`${outcome}: ${dest}
|
|
648
|
+
`);
|
|
649
|
+
return 0;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// src/commands/snap.ts
|
|
653
|
+
import { mkdir as mkdir2, writeFile as writeFile2 } from "node:fs/promises";
|
|
654
|
+
import { dirname as dirname4, join as join6 } from "node:path";
|
|
655
|
+
var USAGE6 = "usage: banger snap [file] [--stop N] [--step M|last] [--steps last|all] [--out path] [--out-dir dir] [--scale 1|2]";
|
|
656
|
+
function parseCount(name, value) {
|
|
657
|
+
if (!/^\d+$/.test(value)) throw new CliError(2, `invalid --${name}: ${value}`);
|
|
658
|
+
return Number(value);
|
|
659
|
+
}
|
|
660
|
+
async function runSnap(argv) {
|
|
661
|
+
if (argv.includes("--help")) {
|
|
662
|
+
process.stdout.write(`${USAGE6}
|
|
663
|
+
`);
|
|
664
|
+
return 0;
|
|
665
|
+
}
|
|
666
|
+
const { values, positionals } = parseArgsStrict(argv, {
|
|
667
|
+
stop: { type: "string" },
|
|
668
|
+
step: { type: "string" },
|
|
669
|
+
steps: { type: "string" },
|
|
670
|
+
out: { type: "string" },
|
|
671
|
+
"out-dir": { type: "string" },
|
|
672
|
+
scale: { type: "string" }
|
|
673
|
+
});
|
|
674
|
+
const file = deckFile(positionals, USAGE6);
|
|
675
|
+
if (values.scale !== void 0 && values.scale !== "1" && values.scale !== "2") {
|
|
676
|
+
throw new CliError(2, `invalid --scale: ${values.scale} (expected 1 or 2)`);
|
|
677
|
+
}
|
|
678
|
+
const scale = values.scale === "2" ? 2 : 1;
|
|
679
|
+
if (values.stop === void 0 && values.step !== void 0) throw new CliError(2, "--step requires --stop");
|
|
680
|
+
if (values.steps !== void 0 && values.steps !== "last" && values.steps !== "all") {
|
|
681
|
+
throw new CliError(2, `invalid --steps: ${values.steps} (expected last or all)`);
|
|
682
|
+
}
|
|
683
|
+
if (values.steps !== void 0 && values.stop !== void 0) throw new CliError(2, "--steps cannot be used with --stop");
|
|
684
|
+
const selection = values.steps === "all" ? "all" : "last";
|
|
685
|
+
if (values.stop === void 0 && values.out !== void 0) throw new CliError(2, "--out requires --stop; use --out-dir");
|
|
686
|
+
const stop = values.stop === void 0 ? void 0 : parseCount("stop", values.stop);
|
|
687
|
+
const stepArg = values.step === void 0 || values.step === "last" ? "last" : parseCount("step", values.step);
|
|
688
|
+
const outDir = values["out-dir"] ?? "snap";
|
|
689
|
+
const failures = /* @__PURE__ */ new Set();
|
|
690
|
+
await withCapture(file, { scale }, async (session) => {
|
|
691
|
+
let only;
|
|
692
|
+
if (stop !== void 0) {
|
|
693
|
+
const info = session.path[stop];
|
|
694
|
+
if (!info) throw new CliError(2, `--stop ${stop} is out of range (the deck has ${session.path.length} stops)`);
|
|
695
|
+
const step = stepArg === "last" ? info.steps : stepArg;
|
|
696
|
+
if (step > info.steps) throw new CliError(2, `--step ${step} is out of range (stop ${stop} has ${info.steps} steps)`);
|
|
697
|
+
only = { stop, step };
|
|
698
|
+
}
|
|
699
|
+
const states = expandStates(session.path, selection, only);
|
|
700
|
+
const single = only !== void 0;
|
|
701
|
+
if (!single) await clearStopFiles(outDir);
|
|
702
|
+
for (const ref of states) {
|
|
703
|
+
const report = await session.goto(ref);
|
|
704
|
+
for (const failure of report.failed) failures.add(formatReadyFailure(failure));
|
|
705
|
+
const png = await captureState(session, ref);
|
|
706
|
+
const target = single && values.out || join6(outDir, stopFileName(ref.stop, ref.step));
|
|
707
|
+
await mkdir2(dirname4(target), { recursive: true });
|
|
708
|
+
await writeFile2(target, png);
|
|
709
|
+
}
|
|
710
|
+
return void 0;
|
|
711
|
+
});
|
|
712
|
+
for (const line of failures) process.stderr.write(`${line}
|
|
713
|
+
`);
|
|
714
|
+
return failures.size > 0 ? 1 : 0;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// src/commands/theme.ts
|
|
718
|
+
import { existsSync as existsSync5, writeFileSync as writeFileSync2 } from "node:fs";
|
|
719
|
+
var USAGE7 = `usage: banger theme init [file] [--from <preset>] [--force]
|
|
720
|
+
banger theme apply <theme.json> <deck.html>`;
|
|
721
|
+
function runThemeInit(argv) {
|
|
722
|
+
const { values, positionals } = parseArgsStrict(argv, { from: { type: "string" }, force: { type: "boolean" } });
|
|
723
|
+
if (positionals.length > 1) throw new CliError(2, USAGE7);
|
|
724
|
+
const file = positionals[0] ?? "theme.json";
|
|
725
|
+
const name = values.from ?? "signal";
|
|
726
|
+
if (!isPresetName(name)) throw new CliError(2, `unknown preset: ${name} (choose one of: ${PRESET_NAMES.join(", ")})`);
|
|
727
|
+
if (existsSync5(file) && !values.force) throw new CliError(2, `${file} already exists; pass --force to overwrite`);
|
|
728
|
+
try {
|
|
729
|
+
writeFileSync2(file, `${serializeTheme(presetTheme(name))}
|
|
730
|
+
`);
|
|
731
|
+
} catch (error) {
|
|
732
|
+
throw new CliError(2, `cannot write ${file}: ${error.code ?? "error"}`);
|
|
733
|
+
}
|
|
734
|
+
process.stdout.write(`${file}
|
|
735
|
+
`);
|
|
736
|
+
return 0;
|
|
737
|
+
}
|
|
738
|
+
function runThemeApply(argv) {
|
|
739
|
+
const { positionals } = parseArgsStrict(argv, {});
|
|
740
|
+
if (positionals.length !== 2) throw new CliError(2, USAGE7);
|
|
741
|
+
const [themeFile, deckFile2] = positionals;
|
|
742
|
+
const parsed = parseThemeJson(readInput(themeFile));
|
|
743
|
+
if (!parsed.definition) {
|
|
744
|
+
throw new CliError(1, `${themeFile}: ${(parsed.warnings[0] ?? "theme: invalid theme").replace(/^theme: /, "")}`);
|
|
745
|
+
}
|
|
746
|
+
for (const message of [...parsed.warnings, ...resolveTheme(parsed.definition).warnings]) {
|
|
747
|
+
process.stderr.write(`warning: ${message}
|
|
748
|
+
`);
|
|
749
|
+
}
|
|
750
|
+
const html = readInput(deckFile2);
|
|
751
|
+
const result = applyThemeBlock(html, serializeTheme(parsed.definition));
|
|
752
|
+
if (result === null) throw new CliError(1, `${deckFile2}: no <bang-root> element to apply the theme to`);
|
|
753
|
+
if ("error" in result) {
|
|
754
|
+
throw new CliError(
|
|
755
|
+
1,
|
|
756
|
+
result.error === "unclosed-theme" ? `${deckFile2}: <bang-theme> has no closing tag; fix it before applying` : `${deckFile2}: <bang-theme> has children other than the JSON script; remove them before applying`
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
if (result.extraBlocks > 0) {
|
|
760
|
+
process.stderr.write(
|
|
761
|
+
`warning: ${deckFile2} has ${result.extraBlocks} more <bang-theme> elements; the runtime uses only the first
|
|
762
|
+
`
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
if (result.html !== html) {
|
|
766
|
+
writeFileSync2(deckFile2, result.html);
|
|
767
|
+
process.stdout.write(`applied: ${deckFile2}
|
|
768
|
+
`);
|
|
769
|
+
} else {
|
|
770
|
+
process.stdout.write(`unchanged: ${deckFile2}
|
|
771
|
+
`);
|
|
772
|
+
}
|
|
773
|
+
return 0;
|
|
774
|
+
}
|
|
775
|
+
async function runTheme(argv) {
|
|
776
|
+
const [sub, ...rest] = argv;
|
|
777
|
+
if (sub === void 0) throw new CliError(2, USAGE7);
|
|
778
|
+
if (sub === "--help" || sub === "-h" || rest.includes("--help")) {
|
|
779
|
+
process.stdout.write(`${USAGE7}
|
|
780
|
+
`);
|
|
781
|
+
return 0;
|
|
782
|
+
}
|
|
783
|
+
if (sub === "init") return runThemeInit(rest);
|
|
784
|
+
if (sub === "apply") return runThemeApply(rest);
|
|
785
|
+
throw new CliError(2, `unknown theme subcommand: ${sub}
|
|
786
|
+
${USAGE7}`);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// src/update-check.ts
|
|
790
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync5, writeFileSync as writeFileSync3 } from "node:fs";
|
|
791
|
+
import { homedir as homedir2 } from "node:os";
|
|
792
|
+
import { dirname as dirname5, join as join7 } from "node:path";
|
|
793
|
+
var PACKAGE = "@bangerpage/cli";
|
|
794
|
+
var LATEST_URL = "https://registry.npmjs.org/@bangerpage%2fcli/latest";
|
|
795
|
+
var INTERVAL_MS = 24 * 60 * 60 * 1e3;
|
|
796
|
+
var TIMEOUT_MS = 2e3;
|
|
797
|
+
function updateCacheFile(env = process.env) {
|
|
798
|
+
return join7(env.XDG_CACHE_HOME || join7(homedir2(), ".cache"), "banger", "update-check.json");
|
|
799
|
+
}
|
|
800
|
+
function enabled(env, isTTY) {
|
|
801
|
+
return isTTY && !env.CI && !env.BANGER_NO_UPDATE_CHECK && !env.NO_UPDATE_NOTIFIER;
|
|
802
|
+
}
|
|
803
|
+
function parse2(version2) {
|
|
804
|
+
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version2);
|
|
805
|
+
return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : void 0;
|
|
806
|
+
}
|
|
807
|
+
function isNewer(latest, current) {
|
|
808
|
+
const a = parse2(latest);
|
|
809
|
+
const b = parse2(current);
|
|
810
|
+
if (!a || !b) return false;
|
|
811
|
+
for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] > b[i];
|
|
812
|
+
return false;
|
|
813
|
+
}
|
|
814
|
+
function readCache(file) {
|
|
815
|
+
try {
|
|
816
|
+
const data = JSON.parse(readFileSync5(file, "utf8"));
|
|
817
|
+
return typeof data.checkedAt === "number" && typeof data.latest === "string" ? { checkedAt: data.checkedAt, latest: data.latest } : void 0;
|
|
818
|
+
} catch {
|
|
819
|
+
return void 0;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
async function fetchFromRegistry() {
|
|
823
|
+
const response = await fetch(LATEST_URL, { signal: AbortSignal.timeout(TIMEOUT_MS) });
|
|
824
|
+
if (!response.ok) return void 0;
|
|
825
|
+
const body = await response.json();
|
|
826
|
+
return typeof body.version === "string" ? body.version : void 0;
|
|
827
|
+
}
|
|
828
|
+
function checkForUpdate(opts) {
|
|
829
|
+
const env = opts.env ?? process.env;
|
|
830
|
+
if (!enabled(env, opts.isTTY ?? process.stderr.isTTY === true)) return Promise.resolve();
|
|
831
|
+
const file = opts.cacheFile ?? updateCacheFile(env);
|
|
832
|
+
const now = opts.now ?? Date.now();
|
|
833
|
+
const write = opts.write ?? ((text) => process.stderr.write(text));
|
|
834
|
+
const cache = readCache(file);
|
|
835
|
+
if (cache && isNewer(cache.latest, opts.current)) {
|
|
836
|
+
write(`banger: ${cache.latest} is available (you have ${opts.current}). Update with: npm install -g ${PACKAGE}@latest
|
|
837
|
+
`);
|
|
838
|
+
}
|
|
839
|
+
if (cache && now - cache.checkedAt < INTERVAL_MS) return Promise.resolve();
|
|
840
|
+
return (opts.fetchLatest ?? fetchFromRegistry)().then((latest) => {
|
|
841
|
+
if (!latest) return;
|
|
842
|
+
mkdirSync2(dirname5(file), { recursive: true });
|
|
843
|
+
writeFileSync3(file, `${JSON.stringify({ checkedAt: now, latest })}
|
|
844
|
+
`);
|
|
845
|
+
}).catch(() => void 0);
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// src/bin.ts
|
|
849
|
+
var USAGE8 = `usage: banger <command> [options]
|
|
850
|
+
|
|
851
|
+
commands:
|
|
852
|
+
init [dir] [--force] start a deck: write index.html from the starter
|
|
853
|
+
preview [file] [--port N] preview with live reload
|
|
854
|
+
snap [file] [--stop N] [--step M|last] [--steps last|all] [--out path] [--out-dir dir] [--scale 1|2]
|
|
855
|
+
render states as 1920x1080 PNGs
|
|
856
|
+
check [file] [--json] render every stop and list the problems the runtime finds;
|
|
857
|
+
exits 1 when there is any
|
|
858
|
+
export [file] --pdf [--out path] [--steps last|all]
|
|
859
|
+
write a PDF, one page per stop
|
|
860
|
+
theme init [file] [--from <preset>] [--force]
|
|
861
|
+
write a theme.json from a bundled preset
|
|
862
|
+
theme apply <theme.json> <deck.html>
|
|
863
|
+
embed the theme in a deck, idempotently
|
|
864
|
+
skill install [--project | --dir <path>] [--force]
|
|
865
|
+
copy the agent skill to ~/.claude/skills/banger
|
|
866
|
+
(--project: ./.claude/skills/banger)
|
|
867
|
+
|
|
868
|
+
[file] defaults to index.html in the working directory.
|
|
869
|
+
|
|
870
|
+
In a terminal, banger checks npm for a newer release once a day and prints a notice.
|
|
871
|
+
Set BANGER_NO_UPDATE_CHECK=1 to turn it off; it is also off when CI is set.
|
|
872
|
+
|
|
873
|
+
options:
|
|
874
|
+
--help show this help
|
|
875
|
+
--version show the version
|
|
876
|
+
`;
|
|
877
|
+
var COMMANDS = /* @__PURE__ */ new Set(["init", "preview", "snap", "check", "export", "theme", "skill"]);
|
|
878
|
+
function version() {
|
|
879
|
+
const manifest = fileURLToPath4(new URL("../package.json", import.meta.url));
|
|
880
|
+
return JSON.parse(readFileSync6(manifest, "utf8")).version;
|
|
881
|
+
}
|
|
882
|
+
async function dispatch(argv) {
|
|
883
|
+
const [command, ...rest] = argv;
|
|
884
|
+
if (command === void 0) throw new CliError(2, `missing command
|
|
885
|
+
${USAGE8}`);
|
|
886
|
+
if (command === "--help" || command === "-h" || command === "help") {
|
|
887
|
+
process.stdout.write(USAGE8);
|
|
888
|
+
return 0;
|
|
889
|
+
}
|
|
890
|
+
if (command === "--version" || command === "-v") {
|
|
891
|
+
process.stdout.write(`${version()}
|
|
892
|
+
`);
|
|
893
|
+
return 0;
|
|
894
|
+
}
|
|
895
|
+
if (COMMANDS.has(command)) void checkForUpdate({ current: version() });
|
|
896
|
+
switch (command) {
|
|
897
|
+
case "init":
|
|
898
|
+
return runInit(rest);
|
|
899
|
+
case "preview":
|
|
900
|
+
return runPreview(rest);
|
|
901
|
+
case "snap":
|
|
902
|
+
return runSnap(rest);
|
|
903
|
+
case "check":
|
|
904
|
+
return runCheck(rest);
|
|
905
|
+
case "export":
|
|
906
|
+
return runExport(rest);
|
|
907
|
+
case "theme":
|
|
908
|
+
return runTheme(rest);
|
|
909
|
+
case "skill":
|
|
910
|
+
return runSkill(rest);
|
|
911
|
+
default:
|
|
912
|
+
throw new CliError(2, `unknown command: ${command}
|
|
913
|
+
${USAGE8}`);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
async function main(argv) {
|
|
917
|
+
try {
|
|
918
|
+
return await dispatch(argv);
|
|
919
|
+
} catch (error) {
|
|
920
|
+
if (error instanceof BrowserNotFoundError) {
|
|
921
|
+
process.stderr.write(`banger: ${error.message}; ${installHint()}
|
|
922
|
+
`);
|
|
923
|
+
return 2;
|
|
924
|
+
}
|
|
925
|
+
if (error instanceof CliError) {
|
|
926
|
+
process.stderr.write(`banger: ${error.message}
|
|
927
|
+
`);
|
|
928
|
+
return error.exitCode;
|
|
929
|
+
}
|
|
930
|
+
process.stderr.write(`${error instanceof Error ? error.stack ?? error.message : String(error)}
|
|
931
|
+
`);
|
|
932
|
+
return 1;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
var entry = process.argv[1];
|
|
936
|
+
if (entry && import.meta.url === pathToFileURL(realpathSync(entry)).href) {
|
|
937
|
+
process.exitCode = await main(process.argv.slice(2));
|
|
938
|
+
}
|
|
939
|
+
export {
|
|
940
|
+
USAGE8 as USAGE,
|
|
941
|
+
main
|
|
942
|
+
};
|
|
943
|
+
//# sourceMappingURL=bin.js.map
|