@tobos/cli 0.4.18 → 0.4.20

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.
@@ -0,0 +1,189 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire as __tobosRequire } from 'node:module';
3
+ const require = __tobosRequire(import.meta.url);
4
+ import {
5
+ CliError
6
+ } from "./chunk-4CLH25WE.js";
7
+ import {
8
+ init_define_TOBOS_PRESENT_TYPES,
9
+ init_define_TOBOS_SKILL,
10
+ init_define_TOBOS_TOOLCHAIN
11
+ } from "./chunk-O52ON2LV.js";
12
+
13
+ // src/render.ts
14
+ init_define_TOBOS_PRESENT_TYPES();
15
+ init_define_TOBOS_SKILL();
16
+ init_define_TOBOS_TOOLCHAIN();
17
+ import { execFile } from "node:child_process";
18
+ import { mkdirSync, readFileSync } from "node:fs";
19
+ import { createRequire } from "node:module";
20
+ import { homedir } from "node:os";
21
+ import { dirname, join } from "node:path";
22
+ async function launch() {
23
+ let playwright;
24
+ try {
25
+ playwright = await import("playwright-core");
26
+ } catch {
27
+ throw new CliError(
28
+ "Rendering needs playwright-core. Reinstall the CLI: `npm i -g @tobos/cli`."
29
+ );
30
+ }
31
+ const tmp = join(process.env.XDG_CACHE_HOME || join(homedir(), ".cache"), "tobos", "chromium");
32
+ mkdirSync(tmp, { recursive: true });
33
+ const env = { ...process.env, TMPDIR: tmp };
34
+ const executablePath = process.env.TOBOS_BROWSER;
35
+ if (executablePath) {
36
+ const named = await firstToLaunch([() => playwright.chromium.launch({ env, executablePath })]);
37
+ if ("browser" in named) return named.browser;
38
+ throw new CliError(`TOBOS_BROWSER (${executablePath}) didn't start: ${named.error}`);
39
+ }
40
+ const found = await firstToLaunch([
41
+ () => playwright.chromium.launch({ env }),
42
+ () => playwright.chromium.launch({ env, channel: "chrome" }),
43
+ () => playwright.chromium.launch({ env, channel: "msedge" })
44
+ ]);
45
+ if ("browser" in found) return found.browser;
46
+ await installChromium();
47
+ const installed = await firstToLaunch([() => playwright.chromium.launch({ env })]);
48
+ if ("browser" in installed) return installed.browser;
49
+ throw new CliError(
50
+ `Chromium is installed but didn't start: ${installed.error}
51
+ On Linux it may need system libraries: \`sudo npx playwright-core install-deps chromium\`. Or point TOBOS_BROWSER at a Chrome or Chromium executable.`
52
+ );
53
+ }
54
+ async function firstToLaunch(attempts) {
55
+ let error = "";
56
+ for (const attempt of attempts) {
57
+ try {
58
+ return { browser: await attempt() };
59
+ } catch (e) {
60
+ error = e.message.split("\n")[0] ?? "";
61
+ }
62
+ }
63
+ return { error };
64
+ }
65
+ async function installChromium() {
66
+ const pkg = createRequire(import.meta.url).resolve("playwright-core/package.json");
67
+ const args = [join(dirname(pkg), "cli.js"), "install", "--only-shell", "--no-remove", "chromium"];
68
+ process.stderr.write(
69
+ "No Chromium on this machine: installing one for rendering (once, ~100 MB)\u2026\n"
70
+ );
71
+ await new Promise(
72
+ (done, fail) => execFile(process.execPath, args, { maxBuffer: 16 * 1024 * 1024 }, (error, stdout, stderr) => {
73
+ if (!error) return done();
74
+ const why = `${stderr}
75
+ ${stdout}`.trim().split("\n").filter(Boolean).at(-1) ?? error.message;
76
+ fail(
77
+ new CliError(
78
+ `No Chromium to render with, and installing one failed: ${why}
79
+ It downloads from cdn.playwright.dev; a sandbox has to allow that host. Or point TOBOS_BROWSER at a Chrome or Chromium executable.`
80
+ )
81
+ );
82
+ })
83
+ );
84
+ process.stderr.write("Chromium installed.\n");
85
+ }
86
+ async function openViewer(browser, url, scale, errors = []) {
87
+ const page = await browser.newPage({
88
+ viewport: { width: 1920, height: 1080 },
89
+ deviceScaleFactor: scale
90
+ });
91
+ page.setDefaultTimeout(3e4);
92
+ page.on("pageerror", (error) => errors.push(error.message));
93
+ page.on("console", (message) => {
94
+ if (message.type() === "error") errors.push(message.text());
95
+ });
96
+ await page.goto(url, { waitUntil: "domcontentloaded" });
97
+ try {
98
+ await page.waitForFunction(() => "__tobos" in window, null, { timeout: 3e4 });
99
+ const ready = await page.evaluate(
100
+ () => window.__tobos.ready
101
+ );
102
+ return { page, ready };
103
+ } catch (error) {
104
+ throw new CliError(`The viewer didn't start: ${errors[0] ?? error.message}`);
105
+ }
106
+ }
107
+ var CRASHED = /crashed|Target closed|has been closed/i;
108
+ async function renderBeats(url, positions, options) {
109
+ mkdirSync(options.dir, { recursive: true });
110
+ const errors = [];
111
+ let browser = await launch();
112
+ let { page } = await openViewer(browser, url, options.scale, errors);
113
+ const files = [];
114
+ const renderOne = async (position) => {
115
+ const hash = `#${position}`;
116
+ const base = position.replace("/", "--");
117
+ if (!options.film) {
118
+ const drawn2 = await page.evaluate(
119
+ (h) => window.__tobos.goto(h),
120
+ hash
121
+ );
122
+ const path = join(options.dir, `${base}.png`);
123
+ await page.screenshot({ path });
124
+ return { position, path, ...drawn2 ? {} : { late: true } };
125
+ }
126
+ const count = Math.max(2, Math.min(options.frames, 12));
127
+ const frames = [];
128
+ let drawn = true;
129
+ for (let i = 0; i < count; i++) {
130
+ const sampled = await page.evaluate(
131
+ ([h, p]) => window.__tobos.sample(h, p),
132
+ [hash, i / (count - 1)]
133
+ );
134
+ drawn &&= sampled;
135
+ const path = join(options.dir, `${base}--${i}.png`);
136
+ await page.screenshot({ path });
137
+ frames.push(path);
138
+ }
139
+ const strip = join(options.dir, `${base}--film.png`);
140
+ await stripOf(browser, frames, strip);
141
+ return { position, path: strip, frames, ...drawn ? {} : { late: true } };
142
+ };
143
+ try {
144
+ for (const position of positions) {
145
+ let rendered;
146
+ try {
147
+ rendered = await renderOne(position);
148
+ } catch (error) {
149
+ if (!CRASHED.test(error.message)) throw error;
150
+ options.note(`the browser crashed on ${position}; retrying`);
151
+ await browser.close().catch(() => void 0);
152
+ browser = await launch();
153
+ ({ page } = await openViewer(browser, url, options.scale, errors));
154
+ try {
155
+ rendered = await renderOne(position);
156
+ } catch (again) {
157
+ if (!CRASHED.test(again.message)) throw again;
158
+ throw new CliError(
159
+ `Chromium crashed twice rendering ${position}. It usually runs out of memory: check that the scene shows media at the size it is drawn, and that the machine has memory free.`
160
+ );
161
+ }
162
+ }
163
+ if (rendered.late)
164
+ options.note(
165
+ `${position} was taken before everything on it was drawn: fonts, images or video frames still loading`
166
+ );
167
+ files.push(rendered);
168
+ options.note(
169
+ `rendered ${position}${rendered.frames ? ` (${rendered.frames.length} frames)` : ""}`
170
+ );
171
+ }
172
+ if (errors.length) options.note(`the deck threw while rendering: ${errors[0]}`);
173
+ return { files, errors };
174
+ } finally {
175
+ await browser.close();
176
+ }
177
+ }
178
+ async function stripOf(browser, frames, target) {
179
+ const page = await browser.newPage({ viewport: { width: 488 * frames.length + 8, height: 286 } });
180
+ const images = frames.map((f) => `<img src="data:image/png;base64,${readFileSync(f).toString("base64")}">`).join("");
181
+ await page.setContent(
182
+ `<body style="margin:0;padding:8px;display:flex;gap:8px">${images}<style>img{width:480px;height:270px}</style></body>`
183
+ );
184
+ await page.screenshot({ path: target });
185
+ await page.close();
186
+ }
187
+ export {
188
+ renderBeats
189
+ };
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire as __tobosRequire } from 'node:module';
3
+ const require = __tobosRequire(import.meta.url);
4
+ import {
5
+ ID,
6
+ TEXT_MAX,
7
+ activitiesOf,
8
+ buildDeck,
9
+ createCache,
10
+ formatMediaJson,
11
+ formatProblems,
12
+ isSafePath,
13
+ masterOf,
14
+ mediaTypeOf,
15
+ memoryTree,
16
+ openActivity,
17
+ parseDeckJson,
18
+ parseMediaJson,
19
+ readText,
20
+ servesModule,
21
+ sha256,
22
+ sharedManifest
23
+ } from "./chunk-L3I6AVL4.js";
24
+ import "./chunk-O52ON2LV.js";
25
+ export {
26
+ ID,
27
+ TEXT_MAX,
28
+ activitiesOf,
29
+ buildDeck,
30
+ createCache,
31
+ formatMediaJson,
32
+ formatProblems,
33
+ isSafePath,
34
+ masterOf,
35
+ mediaTypeOf,
36
+ memoryTree,
37
+ openActivity,
38
+ parseDeckJson,
39
+ parseMediaJson,
40
+ readText,
41
+ servesModule,
42
+ sha256,
43
+ sharedManifest
44
+ };