1/**
2 * Pack the command-line bundle as an npm tarball and put it where the page is
3 * deployed, so that
4 *
5 * npx https://concept-collection.github.io/turing-surface-cache/fill.tgz
6 *
7 * installs and runs it anywhere node is. Nothing is published to the registry:
8 * npm installs a tarball from a URL as happily as from a package name, and
9 * this way the command line is always the same commit as the page it fills the
10 * cache for.
11 *
12 * The package.json written here is the whole published manifest. It has no
13 * numbl dependency — the compiler is bundled into fill.js exactly as it is
14 * into the page — and its only real dependency is h5wasm, whose node build
15 * reads its wasm off disk and so cannot be bundled. Dawn is optional, so the
16 * install still succeeds on a machine that has no prebuilt binary for it; the
17 * CLI says what to do about that if it comes to it.
18 *
19 * Usage: node scripts/pack-cli.mjs (after `vite build --config vite.cli.config.ts`)
20 */
21import { execFileSync } from 'node:child_process';
22import { mkdtemp, copyFile, writeFile, readFile, rm, mkdir } from 'node:fs/promises';
23import { tmpdir } from 'node:os';
24import { join } from 'node:path';
26const root = new URL('..', import.meta.url).pathname;
27const pkg = JSON.parse(await readFile(join(root, 'package.json'), 'utf8'));
29const manifest = {
30 name: 'turing-surface-fill',
31 version: pkg.version,
32 description: 'Fill the turing-surface-cache shared cache from the command line',
33 license: pkg.license,
34 type: 'module',
35 engines: pkg.engines,
36 bin: { 'turing-surface-fill': 'fill.js' },
37 files: ['fill.js'],
38 dependencies: { h5wasm: pkg.dependencies.h5wasm },
39 optionalDependencies: { webgpu: pkg.optionalDependencies.webgpu },
40};
42const stage = await mkdtemp(join(tmpdir(), 'turing-fill-'));
43try {
44 await copyFile(join(root, 'dist-cli/fill.js'), join(stage, 'fill.js'));
45 await writeFile(join(stage, 'package.json'), `${JSON.stringify(manifest, null, 2)}\n`);
46 const out = execFileSync('npm', ['pack', '--silent', '--pack-destination', stage], {
47 cwd: stage,
48 encoding: 'utf8',
49 }).trim();
50 await mkdir(join(root, 'dist'), { recursive: true });
51 await copyFile(join(stage, out), join(root, 'dist/fill.tgz'));
52 console.log(`dist/fill.tgz (${manifest.name} ${manifest.version})`);
53} finally {
54 await rm(stage, { recursive: true, force: true });
55}