concept-collection / turing-surface-cache
turing-surface-cache / scripts / pack-cli.mjs
60 lines · 2.7 KBBlameHistoryRaw
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 // The bundle's own floor, not the repo's: node 18 is the oldest with fetch,
36 // and the launcher checks for it. Kept in step with cli-launcher.cjs.
37 engines: { node: '>=18' },
38 // The bin is the launcher, not the bundle: an old node must reach a sentence
39 // rather than a syntax error (scripts/cli-launcher.cjs).
40 bin: { 'turing-surface-fill': 'launch.cjs' },
41 files: ['launch.cjs', 'fill.js'],
42 dependencies: { h5wasm: pkg.dependencies.h5wasm },
43 optionalDependencies: { webgpu: pkg.optionalDependencies.webgpu },
44};
46const stage = await mkdtemp(join(tmpdir(), 'turing-fill-'));
47try {
48 await copyFile(join(root, 'dist-cli/fill.js'), join(stage, 'fill.js'));
49 await copyFile(join(root, 'scripts/cli-launcher.cjs'), join(stage, 'launch.cjs'));
50 await writeFile(join(stage, 'package.json'), `${JSON.stringify(manifest, null, 2)}\n`);
51 const out = execFileSync('npm', ['pack', '--silent', '--pack-destination', stage], {
52 cwd: stage,
53 encoding: 'utf8',
54 }).trim();
55 await mkdir(join(root, 'dist'), { recursive: true });
56 await copyFile(join(stage, out), join(root, 'dist/fill.tgz'));
57 console.log(`dist/fill.tgz (${manifest.name} ${manifest.version})`);
58} finally {
59 await rm(stage, { recursive: true, force: true });