Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions tools/build-scenes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Bundles every catalog entry (the reference scenes in scenes/*.ts and the games) for the renderer:
// out/bundles/<id>.js, run with `threert-render play | live | render <bundle> --assets <repo root>`.
//
// node tools/build-scenes.ts [cornell-box bistro ...]
import { join } from "node:path";
import { bundleGame, repoRoot } from "./bundle-game.ts";
import { bundlesDir, sources } from "./catalog.ts";

const only = process.argv.slice(2);
for (const [id, src] of Object.entries(sources())) {
if (only.length && !only.includes(id)) continue;
const only = new Set(process.argv.slice(2));
const catalog = sources();
const unknown = [...only].filter((id) => !Object.hasOwn(catalog, id));
if (unknown.length) {
console.error(`build-scenes: unknown scene IDs: ${unknown.map((id) => JSON.stringify(id)).join(", ")}`);
console.error(`Available scene IDs: ${Object.keys(catalog).join(", ")}`);
process.exit(1);
}

for (const [id, src] of Object.entries(catalog)) {
if (only.size && !only.has(id)) continue;
const r = await bundleGame(src, join(bundlesDir, `${id}.js`));
console.log(`${r.out.replace(repoRoot + "/", "").padEnd(34)} ${(r.bytes / 1024).toFixed(0).padStart(5)} KiB ${r.ms.toFixed(0)} ms`);
}
51 changes: 51 additions & 0 deletions tools/catalog/test/build-scenes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { spawnSync } from "node:child_process";
import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import assert from "node:assert/strict";
import { test } from "node:test";

// Run the real catalog and CLI against an isolated checkout. A stand-in
// bundler records calls without requiring a GPU or modifying out/ in the repo.
const tools = new URL("../../", import.meta.url);

test("build-scenes rejects unknown IDs before rebuilding any selected scene", (t) => {
const root = mkdtempSync(join(tmpdir(), "threert-build-scenes-"));
t.after(() => rmSync(root, { recursive: true, force: true }));
mkdirSync(join(root, "tools"));
mkdirSync(join(root, "scenes"));
mkdirSync(join(root, "out/bundles"), { recursive: true });
for (const file of ["build-scenes.ts", "catalog.ts"]) {
copyFileSync(new URL(file, tools), join(root, "tools", file));
}
for (const id of ["cornell-box", "sun-room"]) writeFileSync(join(root, "scenes", id + ".ts"), "");
writeFileSync(join(root, "tools/bundle-game.ts"), `
import { writeFileSync } from "node:fs";
export const repoRoot = new URL("../", import.meta.url).pathname;
export async function bundleGame(src, out) {
writeFileSync(out, src);
return { out, bytes: 1, ms: 0 };
}
`);

const out = join(root, "out/bundles");
const existing = join(out, "cornell-box.js");
writeFileSync(existing, "previous bundle");
const run = (...ids: string[]) => spawnSync(process.execPath, [join(root, "tools/build-scenes.ts"), ...ids], {
cwd: root, encoding: "utf8",
});

const invalid = run("cornell-box", "cornell-bxo", "constructor");
assert.equal(invalid.status, 1, invalid.stderr);
assert.match(invalid.stderr, /unknown scene IDs: "cornell-bxo", "constructor"/);
assert.match(invalid.stderr, /Available scene IDs:.*cornell-box.*sun-room/);
assert.equal(invalid.stdout, "");
assert.deepEqual(readdirSync(out), ["cornell-box.js"]);
assert.equal(readFileSync(existing, "utf8"), "previous bundle");

const valid = run("sun-room");
assert.equal(valid.status, 0, valid.stderr);
assert.deepEqual(readdirSync(out).sort(), ["cornell-box.js", "sun-room.js"]);
assert.equal(readFileSync(existing, "utf8"), "previous bundle");
assert.equal(readFileSync(join(out, "sun-room.js"), "utf8"), join(root, "scenes", "sun-room.ts"));
});