beatsheet 0.0.0-stage → 0.2.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/README.md +63 -2
- package/bin/beatsheet.js +14 -0
- package/bin/register-fallback.mjs +23 -0
- package/bin/resolve-fallback.mjs +83 -0
- package/package.json +45 -4
- package/player/env.d.ts +5 -0
- package/player/gallery-audio.ts +147 -0
- package/player/gallery-css.ts +144 -0
- package/player/gallery-detail.ts +385 -0
- package/player/gallery-music.ts +257 -0
- package/player/gallery-pick-state.ts +167 -0
- package/player/gallery-pick.ts +749 -0
- package/player/gallery-sounds.ts +137 -0
- package/player/gallery-state.ts +457 -0
- package/player/gallery.ts +612 -0
- package/player/index.html +49 -0
- package/player/main.ts +83 -0
- package/skill/SKILL.md +658 -0
- package/skill/examples/library/assets/tour/avatar-mk.svg +1 -0
- package/skill/examples/library/assets/tour/dashboard-insights.svg +1 -0
- package/skill/examples/library/assets/tour/dashboard.svg +1 -0
- package/skill/examples/library/assets/tour/settings.svg +1 -0
- package/skill/examples/library/library-tour.plan.md +32 -0
- package/skill/examples/library/library-tour.video.json +268 -0
- package/skill/reference/scene-techniques.md +306 -0
- package/skill/scripts/beats.ts +34 -0
- package/skill/scripts/catalogue.ts +120 -0
- package/skill/scripts/frames.ts +105 -0
- package/src/args.ts +344 -0
- package/src/cli.ts +95 -0
- package/src/commands.ts +812 -0
- package/src/default-project.config.ts +9 -0
- package/src/index.ts +47 -0
- package/src/packages.ts +17 -0
- package/src/paths.ts +32 -0
- package/src/project.ts +200 -0
- package/src/render/browser.ts +185 -0
- package/src/render/capture.ts +320 -0
- package/src/render/ffmpeg.ts +229 -0
- package/src/render/gif-info.ts +61 -0
- package/src/render/render.ts +220 -0
- package/src/samples.ts +557 -0
- package/src/server/api.ts +494 -0
- package/src/server/jobs.ts +210 -0
- package/src/server/security.ts +47 -0
- package/src/server/server.ts +191 -0
- package/src/server/sse.ts +76 -0
- package/src/server/static.ts +127 -0
- package/src/server/vite.ts +100 -0
- package/src/skill.ts +111 -0
- package/src/snap.ts +212 -0
- package/src/speech.ts +689 -0
- package/src/svg-text.ts +136 -0
- package/src/voicefx.ts +311 -0
- package/studio/3rdpartylicenses.txt +287 -0
- package/studio/favicon.ico +0 -0
- package/studio/icons/apple-touch-icon.png +0 -0
- package/studio/icons/icon-192.png +0 -0
- package/studio/icons/icon-512.png +0 -0
- package/studio/icons/icon.svg +1 -0
- package/studio/icons/maskable-512.png +0 -0
- package/studio/index.html +18 -0
- package/studio/main-KMYDBWZH.js +15 -0
- package/studio/manifest.webmanifest +17 -0
- package/studio/offline.html +92 -0
- package/studio/styles-NJ2CULOW.css +1 -0
- package/studio/sw.js +71 -0
package/src/samples.ts
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `beatsheet check --samples` and `stills --samples`: the samples of the project's scene types as
|
|
3
|
+
* one video (samplesDoc), written to a temp folder and checked or captured like a video file, then
|
|
4
|
+
* reported per sample ("counter · Long label (counter-long) @ 4:5 1.40 s ...").
|
|
5
|
+
*/
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
import fs from 'node:fs/promises';
|
|
8
|
+
import os from 'node:os';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { pathToFileURL } from 'node:url';
|
|
11
|
+
import type { Browser } from 'playwright';
|
|
12
|
+
import {
|
|
13
|
+
layoutWarningId,
|
|
14
|
+
layoutWarnings,
|
|
15
|
+
samplesDoc,
|
|
16
|
+
sceneSamples,
|
|
17
|
+
type Format,
|
|
18
|
+
type FormatId,
|
|
19
|
+
type LayoutFinding,
|
|
20
|
+
type LayoutIssue,
|
|
21
|
+
type SampleEntry,
|
|
22
|
+
type ValidationIssue,
|
|
23
|
+
type VideoDoc,
|
|
24
|
+
} from '@beatsheet/core';
|
|
25
|
+
import type { Command } from './args';
|
|
26
|
+
import { CheckFailed, clearStills, formatPath, validateVideo, videoServer, type Log, type StillsResult } from './commands';
|
|
27
|
+
import { describeProject, discoverProject, type ProjectContext } from './project';
|
|
28
|
+
import { checkDoc } from './server/api';
|
|
29
|
+
import { launchBrowser } from './render/browser';
|
|
30
|
+
import type { RunningServer } from './server/server';
|
|
31
|
+
import { collectLayoutIssues, type BridgePage } from './render/capture';
|
|
32
|
+
import { checkTimes, openPlayer, settledTime } from './render/render';
|
|
33
|
+
|
|
34
|
+
/** File name of the samples video inside its temp folder. */
|
|
35
|
+
const SAMPLES_FILE = 'samples.video.json';
|
|
36
|
+
|
|
37
|
+
/** A sample entry plus the sample's own title ('Long label'; `title` is 'Counter · Long label'). */
|
|
38
|
+
export interface SampleInfo extends SampleEntry {
|
|
39
|
+
sampleTitle: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface SampleSelection {
|
|
43
|
+
/** Scene type ids (default: all of the project's). */
|
|
44
|
+
types?: string[];
|
|
45
|
+
/** Sample ids (default: all). */
|
|
46
|
+
samples?: string[];
|
|
47
|
+
/** Default: all four. */
|
|
48
|
+
formats?: FormatId[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The samples video of a project, written to a temp folder that dispose() removes. */
|
|
52
|
+
export interface SamplesVideo {
|
|
53
|
+
path: string;
|
|
54
|
+
file: string;
|
|
55
|
+
/** Output base name: 'samples', or 'samples-<types>-<samples>' for a selection. */
|
|
56
|
+
name: string;
|
|
57
|
+
doc: VideoDoc;
|
|
58
|
+
json: unknown;
|
|
59
|
+
project: ProjectContext;
|
|
60
|
+
samples: SampleInfo[];
|
|
61
|
+
dispose(): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Builds samplesDoc(project) for the project at `cwd` and writes it to a temp folder. */
|
|
65
|
+
export async function loadSamples(sel: SampleSelection, cwd: string = process.cwd()): Promise<SamplesVideo> {
|
|
66
|
+
const project = await discoverProject(cwd, cwd);
|
|
67
|
+
const config = project.config;
|
|
68
|
+
const known = config.scenes.map((t) => t.id);
|
|
69
|
+
const unknownTypes = (sel.types ?? []).filter((t) => !known.includes(t));
|
|
70
|
+
if (unknownTypes.length) {
|
|
71
|
+
throw new Error(`Unknown scene type${unknownTypes.length === 1 ? '' : 's'} ${unknownTypes.join(', ')} (known: ${known.join(', ')})`);
|
|
72
|
+
}
|
|
73
|
+
const { doc, entries } = samplesDoc(config, { types: sel.types, samples: sel.samples, formats: sel.formats });
|
|
74
|
+
const missing = (sel.samples ?? []).filter((id) => !entries.some((e) => e.sampleId === id));
|
|
75
|
+
if (missing.length) throw new Error(`No sample ${missing.join(', ')} in ${sel.types ? sel.types.join(', ') : 'any scene type'}`);
|
|
76
|
+
if (entries.length === 0) throw new Error(`${project.name} has no scene types to sample`);
|
|
77
|
+
|
|
78
|
+
const titles = new Map(config.scenes.map((t) => [t.id, new Map(sceneSamples(t).map((s) => [s.id, s.title]))]));
|
|
79
|
+
const samples = entries.map((e) => ({ ...e, sampleTitle: titles.get(e.typeId)?.get(e.sampleId) ?? e.sampleId }));
|
|
80
|
+
// The JSON round trip is what a video file goes through (undefined props dropped).
|
|
81
|
+
const json = JSON.parse(JSON.stringify(doc)) as unknown;
|
|
82
|
+
const checked = checkDoc(json);
|
|
83
|
+
if (!checked.ok) throw new CheckFailed(checked.issues);
|
|
84
|
+
|
|
85
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'beatsheet-samples-'));
|
|
86
|
+
const file = path.join(dir, SAMPLES_FILE);
|
|
87
|
+
await fs.writeFile(file, `${JSON.stringify(json, null, 2)}\n`);
|
|
88
|
+
return {
|
|
89
|
+
path: file,
|
|
90
|
+
file: SAMPLES_FILE,
|
|
91
|
+
name: samplesName(sel),
|
|
92
|
+
doc: checked.doc,
|
|
93
|
+
json,
|
|
94
|
+
project,
|
|
95
|
+
samples,
|
|
96
|
+
dispose: () => fs.rm(dir, { recursive: true, force: true }),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 'samples' for all, 'samples-cta-terminal' for --type cta,terminal (hashed when long), so parallel runs don't share folders. */
|
|
101
|
+
export function samplesName(sel: Pick<SampleSelection, 'types' | 'samples'>): string {
|
|
102
|
+
const parts = [...(sel.types ?? []), ...(sel.samples ?? [])];
|
|
103
|
+
if (parts.length === 0) return 'samples';
|
|
104
|
+
const name = `samples-${parts.join('-')}`.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/-+$/, '');
|
|
105
|
+
return name.length <= 60 ? name : `samples-${createHash('sha1').update(parts.join(',')).digest('hex').slice(0, 10)}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
// check --samples
|
|
110
|
+
|
|
111
|
+
export interface SampleLayoutIssue {
|
|
112
|
+
sample: SampleInfo;
|
|
113
|
+
format: FormatId;
|
|
114
|
+
/** Seconds into the sample (first moment the issue shows). */
|
|
115
|
+
t: number;
|
|
116
|
+
issue: LayoutIssue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface SampleValidationIssue {
|
|
120
|
+
/** null for document-level issues. */
|
|
121
|
+
sample: SampleInfo | null;
|
|
122
|
+
/** Path inside the sample's scene ('props.label'), 'timeline', 'player' or 'console'. */
|
|
123
|
+
where: string;
|
|
124
|
+
message: string;
|
|
125
|
+
severity: 'error' | 'warning';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface SamplesCheckReport {
|
|
129
|
+
project: string;
|
|
130
|
+
samples: SampleInfo[];
|
|
131
|
+
formats: FormatId[];
|
|
132
|
+
/** Validation issues, timeline warnings and player errors (failed mount/update), per sample. */
|
|
133
|
+
validation: SampleValidationIssue[];
|
|
134
|
+
/** Deduplicated per sample, format, element and kind (first moment it shows). */
|
|
135
|
+
layout: SampleLayoutIssue[];
|
|
136
|
+
/** Errors (validation, player) + layout issues: the exit code is 1 when > 0. */
|
|
137
|
+
errors: number;
|
|
138
|
+
warnings: number;
|
|
139
|
+
/** Moments inspected per format (every beat + a settled moment per sample). */
|
|
140
|
+
moments: number;
|
|
141
|
+
seconds: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Runs `fn` with a server for the samples video and a browser. Both are closed afterwards, the
|
|
146
|
+
* server also when the browser fails to launch.
|
|
147
|
+
*/
|
|
148
|
+
async function withServerAndBrowser<T>(s: SamplesVideo, fn: (server: RunningServer, browser: Browser) => Promise<T>): Promise<T> {
|
|
149
|
+
const server = await videoServer(s);
|
|
150
|
+
try {
|
|
151
|
+
const browser = await launchBrowser();
|
|
152
|
+
try {
|
|
153
|
+
return await fn(server, browser);
|
|
154
|
+
} finally {
|
|
155
|
+
await browser.close();
|
|
156
|
+
}
|
|
157
|
+
} finally {
|
|
158
|
+
await server.close();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Validation + layout issues at every beat and a settled moment of every sample, all formats in parallel. */
|
|
163
|
+
export async function runCheckSamples(
|
|
164
|
+
cmd: Omit<Extract<Command, { cmd: 'check-samples' }>, 'cmd'>,
|
|
165
|
+
log: Log,
|
|
166
|
+
cwd: string = process.cwd(),
|
|
167
|
+
): Promise<SamplesCheckReport> {
|
|
168
|
+
const started = Date.now();
|
|
169
|
+
const s = await loadSamples(cmd, cwd);
|
|
170
|
+
try {
|
|
171
|
+
const byScene = new Map(s.samples.map((x) => [x.sceneId, x]));
|
|
172
|
+
// Samples check scene types, not a video: the project's brand rules (allowed types, music, outro...) don't apply.
|
|
173
|
+
const { validation, timelineWarnings } = validateVideo(s.json, s.doc, { ...s.project.config, rules: undefined });
|
|
174
|
+
const issues: SampleValidationIssue[] = [
|
|
175
|
+
...validation.issues.map((i) => sampleValidationIssue(i, s.samples)),
|
|
176
|
+
...timelineWarnings.map((w): SampleValidationIssue => ({
|
|
177
|
+
sample: (w.sceneId && byScene.get(w.sceneId)) || null,
|
|
178
|
+
where: 'timeline',
|
|
179
|
+
message: w.message,
|
|
180
|
+
severity: 'warning',
|
|
181
|
+
})),
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
const formats = s.doc.formats;
|
|
185
|
+
const layout: SampleLayoutIssue[] = [];
|
|
186
|
+
const layoutSoft: LayoutFinding[] = [];
|
|
187
|
+
const pageErrors: { format: FormatId; errors: BridgePage['errors'] }[] = [];
|
|
188
|
+
let moments = 0;
|
|
189
|
+
await withServerAndBrowser(s, async (server, browser) => {
|
|
190
|
+
await Promise.all(
|
|
191
|
+
formats.map(async (format) => {
|
|
192
|
+
const bp = await openPlayer(browser, server.url, s.file, format);
|
|
193
|
+
try {
|
|
194
|
+
const starts = new Map(bp.timeline.scenes.map((scene) => [scene.id, scene.start]));
|
|
195
|
+
const times = checkTimes(bp.timeline, { settled: true });
|
|
196
|
+
moments = times.length;
|
|
197
|
+
const seen = new Set<string>();
|
|
198
|
+
for (const { t, issues: found } of await collectLayoutIssues(bp, times)) {
|
|
199
|
+
for (const issue of found) {
|
|
200
|
+
const soft = layoutWarningId(issue);
|
|
201
|
+
if (soft) {
|
|
202
|
+
// Samples show what a type does with long code, wrapping included: code-wrap is a video's warning.
|
|
203
|
+
if (soft !== 'code-wrap') layoutSoft.push({ format, t, issue });
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const key = `${issue.sceneId}|${issue.element}|${issue.kind}`;
|
|
207
|
+
if (seen.has(key)) continue;
|
|
208
|
+
seen.add(key);
|
|
209
|
+
const sample = byScene.get(issue.sceneId) ?? unknownSample(issue.sceneId);
|
|
210
|
+
layout.push({ sample, format, t: Math.round((t - (starts.get(issue.sceneId) ?? 0)) * 100) / 100, issue });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
pageErrors.push({ format, errors: [...bp.errors] });
|
|
214
|
+
} finally {
|
|
215
|
+
await bp.close();
|
|
216
|
+
}
|
|
217
|
+
}),
|
|
218
|
+
);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
issues.push(...playerIssues(pageErrors, formats, byScene));
|
|
222
|
+
// Labels that cover each other and code lines that wrap, as warnings (layout-overlap, code-wrap).
|
|
223
|
+
issues.push(...layoutWarnings(layoutSoft, s.doc.scenes).map((w) => sampleValidationIssue({ ...w, message: `${w.message} [${w.code}]` }, s.samples)));
|
|
224
|
+
|
|
225
|
+
const order = new Map(s.samples.map((x, i) => [x.sceneId, i]));
|
|
226
|
+
const rank = (l: SampleLayoutIssue): number => order.get(l.sample.sceneId) ?? order.size;
|
|
227
|
+
layout.sort(
|
|
228
|
+
(a, b) =>
|
|
229
|
+
rank(a) - rank(b) ||
|
|
230
|
+
formats.indexOf(a.format) - formats.indexOf(b.format) ||
|
|
231
|
+
a.t - b.t ||
|
|
232
|
+
a.issue.element.localeCompare(b.issue.element, 'en', { numeric: true }),
|
|
233
|
+
);
|
|
234
|
+
const report: SamplesCheckReport = {
|
|
235
|
+
project: s.project.name,
|
|
236
|
+
samples: s.samples,
|
|
237
|
+
formats,
|
|
238
|
+
validation: issues,
|
|
239
|
+
layout,
|
|
240
|
+
errors: issues.filter((i) => i.severity === 'error').length + layout.length,
|
|
241
|
+
warnings: issues.filter((i) => i.severity === 'warning').length,
|
|
242
|
+
moments,
|
|
243
|
+
seconds: (Date.now() - started) / 1000,
|
|
244
|
+
};
|
|
245
|
+
for (const line of formatSamplesCheck(report, describeProject(s.project))) log(line);
|
|
246
|
+
return report;
|
|
247
|
+
} finally {
|
|
248
|
+
await s.dispose();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const SCENE_ERROR = /^beatsheet: scene "([^"]*)"(?: \([^)]*\))? (.*)$/;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Errors the player logged while mounting/updating (it shows a placeholder and goes on, so the
|
|
256
|
+
* layout looks fine): errors of the sample's scene; uncaught page errors as document errors; other
|
|
257
|
+
* console errors (a resource that failed to load...) as warnings. One issue per message, with the
|
|
258
|
+
* formats it showed in when not all.
|
|
259
|
+
*/
|
|
260
|
+
export function playerIssues(
|
|
261
|
+
pages: { format: FormatId; errors: BridgePage['errors'] }[],
|
|
262
|
+
formats: FormatId[],
|
|
263
|
+
byScene: Map<string, SampleInfo>,
|
|
264
|
+
): SampleValidationIssue[] {
|
|
265
|
+
const seen = new Map<string, { issue: SampleValidationIssue; formats: Set<FormatId> }>();
|
|
266
|
+
for (const { format, errors } of pages) {
|
|
267
|
+
for (const e of errors) {
|
|
268
|
+
const m = e.kind === 'console' ? SCENE_ERROR.exec(e.text) : null;
|
|
269
|
+
const sample = m ? byScene.get(m[1] as string) ?? unknownSample(m[1] as string) : null;
|
|
270
|
+
const issue: SampleValidationIssue = sample
|
|
271
|
+
? { sample, where: 'player', message: m![2] as string, severity: 'error' }
|
|
272
|
+
: e.kind === 'page'
|
|
273
|
+
? { sample: null, where: 'player', message: `uncaught: ${e.text}`, severity: 'error' }
|
|
274
|
+
: { sample: null, where: 'console', message: e.text, severity: 'warning' };
|
|
275
|
+
const key = `${issue.sample?.sceneId ?? ''}|${issue.where}|${issue.message}`;
|
|
276
|
+
const known = seen.get(key) ?? { issue, formats: new Set<FormatId>() };
|
|
277
|
+
known.formats.add(format);
|
|
278
|
+
seen.set(key, known);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return [...seen.values()].map(({ issue, formats: shown }) =>
|
|
282
|
+
shown.size === formats.length ? issue : { ...issue, message: `${issue.message} [${formats.filter((f) => shown.has(f)).join(', ')}]` },
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function sampleValidationIssue(i: ValidationIssue, samples: SampleInfo[]): SampleValidationIssue {
|
|
287
|
+
const [head, index, ...rest] = i.path;
|
|
288
|
+
const sample = head === 'scenes' && typeof index === 'number' ? samples[index] : undefined;
|
|
289
|
+
if (sample) return { sample, where: rest.length ? formatPath(rest) : '(scene)', message: i.message, severity: i.severity };
|
|
290
|
+
return { sample: null, where: formatPath(i.path), message: i.message, severity: i.severity };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function unknownSample(sceneId: string): SampleInfo {
|
|
294
|
+
return { sceneId, typeId: sceneId || '?', sampleId: '?', title: sceneId, sampleTitle: '?', category: 'other', stress: false };
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const KIND_TEXT: Record<LayoutIssue['kind'], string> = {
|
|
298
|
+
'overflows-parent': 'overflows its box',
|
|
299
|
+
'outside-stage': 'sticks out of the stage',
|
|
300
|
+
// Warnings (layoutWarnings words them); here only for a caller that formats one as a layout line.
|
|
301
|
+
overlaps: 'overlaps another element',
|
|
302
|
+
wraps: 'wraps',
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
/** 'counter · Long label (counter-long)'. */
|
|
306
|
+
export function sampleLabel(s: Pick<SampleInfo, 'typeId' | 'sampleTitle' | 'sceneId'>): string {
|
|
307
|
+
return `${s.typeId} · ${s.sampleTitle} (${s.sceneId})`;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** 'counter · Long label (counter-long) @ 4:5 1.40 s div.bs-counter-label overflows its box by 12 px'. */
|
|
311
|
+
export function formatSampleLayoutIssue(l: SampleLayoutIssue): string {
|
|
312
|
+
const kind = KIND_TEXT[l.issue.kind] ?? l.issue.kind;
|
|
313
|
+
return `${sampleLabel(l.sample)} @ ${l.format} ${l.t.toFixed(2)} s ${l.issue.element} ${kind} by ${Math.round(l.issue.amount)} px`;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const LAST_NTH = /:nth-child\((\d+)\)$/;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* One line per layout issue, except that 3+ siblings with the same issue at the same moment (the
|
|
320
|
+
* words of a headline...) share one: 'span.bs-w:nth-child(4-9, 12) sticks out of the stage by up
|
|
321
|
+
* to 242 px (7 elements)'. Expects the report's order (sample, format, time, element).
|
|
322
|
+
*/
|
|
323
|
+
export function formatLayoutLines(layout: SampleLayoutIssue[]): string[] {
|
|
324
|
+
const out: string[] = [];
|
|
325
|
+
for (let i = 0; i < layout.length; ) {
|
|
326
|
+
const a = layout[i] as SampleLayoutIssue;
|
|
327
|
+
const base = a.issue.element.replace(LAST_NTH, '');
|
|
328
|
+
let j = i + 1;
|
|
329
|
+
if (base !== a.issue.element) {
|
|
330
|
+
while (j < layout.length) {
|
|
331
|
+
const b = layout[j] as SampleLayoutIssue;
|
|
332
|
+
const same = b.sample.sceneId === a.sample.sceneId && b.format === a.format && b.t === a.t && b.issue.kind === a.issue.kind;
|
|
333
|
+
if (!same || !LAST_NTH.test(b.issue.element) || b.issue.element.replace(LAST_NTH, '') !== base) break;
|
|
334
|
+
j++;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const run = layout.slice(i, j);
|
|
338
|
+
if (run.length < 3) {
|
|
339
|
+
out.push(...run.map(formatSampleLayoutIssue));
|
|
340
|
+
} else {
|
|
341
|
+
const nths = run.map((l) => Number(LAST_NTH.exec(l.issue.element)?.[1])).sort((x, y) => x - y);
|
|
342
|
+
const max = Math.max(...run.map((l) => l.issue.amount));
|
|
343
|
+
const kind = KIND_TEXT[a.issue.kind] ?? a.issue.kind;
|
|
344
|
+
out.push(
|
|
345
|
+
`${sampleLabel(a.sample)} @ ${a.format} ${a.t.toFixed(2)} s ${base}:nth-child(${ranges(nths)}) ${kind} by up to ${Math.round(max)} px (${run.length} elements)`,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
i = j;
|
|
349
|
+
}
|
|
350
|
+
return out;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** [3, 4, 5, 9] -> '3-5, 9'. */
|
|
354
|
+
function ranges(ns: number[]): string {
|
|
355
|
+
const parts: string[] = [];
|
|
356
|
+
for (let i = 0; i < ns.length; ) {
|
|
357
|
+
let j = i;
|
|
358
|
+
while (j + 1 < ns.length && (ns[j + 1] as number) === (ns[j] as number) + 1) j++;
|
|
359
|
+
parts.push(j > i ? `${ns[i]}-${ns[j]}` : String(ns[i]));
|
|
360
|
+
i = j + 1;
|
|
361
|
+
}
|
|
362
|
+
return parts.join(', ');
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** 'counter · Long label (counter-long) props.label Too long' (document-level: 'formats ...'). */
|
|
366
|
+
export function formatSampleValidationIssue(v: SampleValidationIssue): string {
|
|
367
|
+
return v.sample ? `${sampleLabel(v.sample)} ${v.where} ${v.message}` : `${v.where} ${v.message}`;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** The report lines: header (`project`: describeProject, else the report's name), layout issues, errors, warnings, a per-type summary, the verdict. */
|
|
371
|
+
export function formatSamplesCheck(r: SamplesCheckReport, project?: string): string[] {
|
|
372
|
+
const out: string[] = [];
|
|
373
|
+
const types = [...new Set(r.samples.map((s) => s.typeId))];
|
|
374
|
+
const plural = (n: number, word: string): string => `${n} ${word}${n === 1 ? '' : 's'}`;
|
|
375
|
+
out.push(`check --samples · ${project ?? r.project}`);
|
|
376
|
+
out.push(` ${plural(r.samples.length, 'sample')} of ${plural(types.length, 'scene type')} · ${r.formats.join(', ')} · ${r.moments} moments per format`);
|
|
377
|
+
|
|
378
|
+
const errors = r.validation.filter((v) => v.severity === 'error');
|
|
379
|
+
const warnings = r.validation.filter((v) => v.severity === 'warning');
|
|
380
|
+
if (r.layout.length) {
|
|
381
|
+
out.push('', `layout issues (${r.layout.length})`);
|
|
382
|
+
for (const line of formatLayoutLines(r.layout)) out.push(` ${line}`);
|
|
383
|
+
}
|
|
384
|
+
if (errors.length) {
|
|
385
|
+
out.push('', `errors (${errors.length})`);
|
|
386
|
+
for (const v of errors) out.push(` ${formatSampleValidationIssue(v)}`);
|
|
387
|
+
}
|
|
388
|
+
if (warnings.length) {
|
|
389
|
+
out.push('', `warnings (${warnings.length})`);
|
|
390
|
+
for (const v of warnings) out.push(` ${formatSampleValidationIssue(v)}`);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
out.push('', 'summary');
|
|
394
|
+
const width = Math.max(...types.map((t) => t.length));
|
|
395
|
+
for (const type of types) {
|
|
396
|
+
const samples = r.samples.filter((s) => s.typeId === type);
|
|
397
|
+
const failing = samples
|
|
398
|
+
.map((s) => {
|
|
399
|
+
const formats = [...new Set(r.layout.filter((l) => l.sample.sceneId === s.sceneId).map((l) => l.format))];
|
|
400
|
+
const failed = errors.some((v) => v.sample?.sceneId === s.sceneId);
|
|
401
|
+
const where = [...formats, ...(failed ? ['error'] : [])];
|
|
402
|
+
return where.length ? `${s.sampleId} [${where.join(', ')}]` : null;
|
|
403
|
+
})
|
|
404
|
+
.filter((x): x is string => x !== null);
|
|
405
|
+
const status = failing.length ? `${failing.length} failing: ${failing.join(', ')}` : 'ok';
|
|
406
|
+
out.push(` ${type.padEnd(width)} ${plural(samples.length, 'sample').padEnd(10)} ${status}`);
|
|
407
|
+
}
|
|
408
|
+
const docErrors = errors.filter((v) => !v.sample).length;
|
|
409
|
+
if (docErrors) out.push(` (document) ${plural(docErrors, 'error')}`);
|
|
410
|
+
|
|
411
|
+
const failingSamples = new Set(r.layout.map((l) => l.sample.sceneId)).size;
|
|
412
|
+
const took = `${r.seconds.toFixed(1)} s`;
|
|
413
|
+
out.push(
|
|
414
|
+
'',
|
|
415
|
+
r.errors === 0
|
|
416
|
+
? `ok: ${plural(r.samples.length, 'sample')} in ${plural(r.formats.length, 'format')}, no layout issues, no errors, ${plural(r.warnings, 'warning')} (${took})`
|
|
417
|
+
: `failed: ${plural(r.layout.length, 'layout issue')} in ${plural(failingSamples, 'sample')}, ${plural(errors.length, 'error')}, ${plural(r.warnings, 'warning')} (${took})`,
|
|
418
|
+
);
|
|
419
|
+
return out;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// ---------------------------------------------------------------------------
|
|
423
|
+
// stills --samples
|
|
424
|
+
|
|
425
|
+
export interface SampleStillsResult extends StillsResult {
|
|
426
|
+
/** Contact sheet pages (--sheet); `sheet` is the first. */
|
|
427
|
+
sheets: string[];
|
|
428
|
+
samples: SampleInfo[];
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** One settled still per sample (<sceneId>.png) per format, plus labelled contact sheets with --sheet. */
|
|
432
|
+
export async function runStillsSamples(
|
|
433
|
+
cmd: Omit<Extract<Command, { cmd: 'stills-samples' }>, 'cmd'>,
|
|
434
|
+
log: Log,
|
|
435
|
+
cwd: string = process.cwd(),
|
|
436
|
+
): Promise<SampleStillsResult[]> {
|
|
437
|
+
const s = await loadSamples(cmd, cwd);
|
|
438
|
+
try {
|
|
439
|
+
const outRoot = path.join(cmd.out ? path.resolve(cmd.out) : s.project.outDir, 'stills');
|
|
440
|
+
const byScene = new Map(s.samples.map((x) => [x.sceneId, x]));
|
|
441
|
+
return await withServerAndBrowser(s, (server, browser) =>
|
|
442
|
+
Promise.all(
|
|
443
|
+
s.doc.formats.map(async (format): Promise<SampleStillsResult> => {
|
|
444
|
+
const bp = await openPlayer(browser, server.url, s.file, format);
|
|
445
|
+
const dir = path.join(outRoot, `${s.name}-${bp.format.slug}`);
|
|
446
|
+
const files: string[] = [];
|
|
447
|
+
try {
|
|
448
|
+
// Earlier runs stay unless --clean asks: a --type run keeps the stills of the other types.
|
|
449
|
+
if (cmd.clean) await clearStills(dir, /\.png$/);
|
|
450
|
+
await fs.mkdir(dir, { recursive: true });
|
|
451
|
+
for (const scene of bp.timeline.scenes) {
|
|
452
|
+
await bp.seek(settledTime(scene, bp.timeline.fps));
|
|
453
|
+
const file = path.join(dir, `${scene.id}.png`);
|
|
454
|
+
await bp.page.screenshot({ type: 'png', path: file });
|
|
455
|
+
files.push(file);
|
|
456
|
+
}
|
|
457
|
+
} finally {
|
|
458
|
+
await bp.close();
|
|
459
|
+
}
|
|
460
|
+
log(`[${format}] ${files.length} stills -> ${dir}`);
|
|
461
|
+
const result: SampleStillsResult = { format, dir, files, sheets: [], samples: s.samples };
|
|
462
|
+
if (cmd.sheet && files.length > 0) {
|
|
463
|
+
const base = `${s.name}-${bp.format.slug}-sheet`;
|
|
464
|
+
await clearStills(outRoot, new RegExp(`^${base}(-\\d+)?\\.png$`));
|
|
465
|
+
const tiles = bp.timeline.scenes.map((scene, i): SheetTile => {
|
|
466
|
+
const sample = byScene.get(scene.id) ?? unknownSample(scene.id);
|
|
467
|
+
return { file: files[i] as string, title: sample.title, detail: `${format} · ${scene.id}${sample.stress ? ' · stress' : ''}` };
|
|
468
|
+
});
|
|
469
|
+
result.sheets = await buildLabelledSheets(browser, tiles, {
|
|
470
|
+
format: bp.format,
|
|
471
|
+
heading: `${s.project.name} · samples · ${format}`,
|
|
472
|
+
out: path.join(outRoot, base),
|
|
473
|
+
});
|
|
474
|
+
result.sheet = result.sheets[0];
|
|
475
|
+
for (const sheet of result.sheets) log(`[${format}] contact sheet ${sheet}`);
|
|
476
|
+
}
|
|
477
|
+
return result;
|
|
478
|
+
}),
|
|
479
|
+
),
|
|
480
|
+
);
|
|
481
|
+
} finally {
|
|
482
|
+
await s.dispose();
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
export interface SheetTile {
|
|
487
|
+
file: string;
|
|
488
|
+
/** First caption line, e.g. 'Counter · Long label'. */
|
|
489
|
+
title: string;
|
|
490
|
+
/** Second caption line, e.g. '4:5 · counter-long · stress'. */
|
|
491
|
+
detail: string;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const SHEET_COLUMNS = 4;
|
|
495
|
+
const TILE_WIDTH = 460;
|
|
496
|
+
const SHEET_GAP = 20;
|
|
497
|
+
const SHEET_PAD = 24;
|
|
498
|
+
|
|
499
|
+
/** Rows per sheet page: 3 for landscape and square formats, 2 for portrait (pages stay under ~1900 px). */
|
|
500
|
+
export function sheetRows(format: Pick<Format, 'width' | 'height'>): number {
|
|
501
|
+
return format.width >= format.height ? 3 : 2;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Contact sheets with a caption above every tile (title + detail), drawn as an HTML page in the
|
|
506
|
+
* browser and screenshot: `<out>.png`, or `<out>-1.png`, `<out>-2.png`... when the tiles need more
|
|
507
|
+
* than one page. Returns the files written.
|
|
508
|
+
*/
|
|
509
|
+
export async function buildLabelledSheets(
|
|
510
|
+
browser: Browser,
|
|
511
|
+
tiles: SheetTile[],
|
|
512
|
+
o: { format: Pick<Format, 'width' | 'height'>; heading: string; out: string },
|
|
513
|
+
): Promise<string[]> {
|
|
514
|
+
const perPage = SHEET_COLUMNS * sheetRows(o.format);
|
|
515
|
+
const pages: SheetTile[][] = [];
|
|
516
|
+
for (let i = 0; i < tiles.length; i += perPage) pages.push(tiles.slice(i, i + perPage));
|
|
517
|
+
const written: string[] = [];
|
|
518
|
+
for (const [i, pageTiles] of pages.entries()) {
|
|
519
|
+
const file = pages.length === 1 ? `${o.out}.png` : `${o.out}-${i + 1}.png`;
|
|
520
|
+
const cols = Math.min(SHEET_COLUMNS, pageTiles.length);
|
|
521
|
+
const width = SHEET_PAD * 2 + cols * TILE_WIDTH + (cols - 1) * SHEET_GAP;
|
|
522
|
+
const heading = pages.length === 1 ? o.heading : `${o.heading} · sheet ${i + 1}/${pages.length}`;
|
|
523
|
+
const htmlFile = `${file}.html`;
|
|
524
|
+
await fs.writeFile(htmlFile, sheetHtml(pageTiles, heading, cols, path.dirname(htmlFile)));
|
|
525
|
+
const page = await browser.newPage({ viewport: { width, height: 400 }, deviceScaleFactor: 1 });
|
|
526
|
+
try {
|
|
527
|
+
await page.goto(pathToFileURL(htmlFile).href, { waitUntil: 'load' });
|
|
528
|
+
await page.screenshot({ type: 'png', path: file, fullPage: true });
|
|
529
|
+
} finally {
|
|
530
|
+
await page.close();
|
|
531
|
+
await fs.rm(htmlFile, { force: true });
|
|
532
|
+
}
|
|
533
|
+
written.push(file);
|
|
534
|
+
}
|
|
535
|
+
return written;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function sheetHtml(tiles: SheetTile[], heading: string, cols: number, fromDir: string): string {
|
|
539
|
+
const esc = (s: string): string => s.replace(/[&<>"]/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
540
|
+
const src = (file: string): string => path.relative(fromDir, file).split(path.sep).map(encodeURIComponent).join('/');
|
|
541
|
+
const figures = tiles
|
|
542
|
+
.map((t) => `<figure><figcaption><b>${esc(t.title)}</b><span>${esc(t.detail)}</span></figcaption><img src="${src(t.file)}"></figure>`)
|
|
543
|
+
.join('\n');
|
|
544
|
+
return `<!doctype html><html><head><meta charset="utf-8"><style>
|
|
545
|
+
html, body { margin: 0; background: #18181b; color: #f4f4f5; font: 18px/1.25 -apple-system, "Helvetica Neue", Arial, sans-serif; }
|
|
546
|
+
body { padding: ${SHEET_PAD}px; }
|
|
547
|
+
h1 { margin: 0 0 ${SHEET_GAP}px; font-size: 22px; font-weight: 600; color: #a1a1aa; }
|
|
548
|
+
main { display: grid; grid-template-columns: repeat(${cols}, ${TILE_WIDTH}px); gap: ${SHEET_GAP}px; }
|
|
549
|
+
figure { margin: 0; }
|
|
550
|
+
figcaption { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
|
|
551
|
+
figcaption b { font-size: 20px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
552
|
+
figcaption span { font: 15px/1.2 ui-monospace, Menlo, monospace; color: #a1a1aa; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
553
|
+
img { display: block; width: ${TILE_WIDTH}px; height: auto; outline: 1px solid #52525b; }
|
|
554
|
+
</style></head><body><h1>${esc(heading)}</h1><main>
|
|
555
|
+
${figures}
|
|
556
|
+
</main></body></html>`;
|
|
557
|
+
}
|