返回 CodeWhale
build-pet.mjs
根目录 / pet / scripts / build-pet.mjs
1 #!/usr/bin/env node
2 /** Local TypeScript build plus deterministic embedded-runtime packaging. */
3 import { readFile, writeFile, mkdir } from 'node:fs/promises';
4 import { fileURLToPath } from 'node:url';
5 import { dirname } from 'node:path';
6 import { spawnSync } from 'node:child_process';
7 import ts from 'typescript';
8 const root = fileURLToPath(new URL('../', import.meta.url));
9 const result = spawnSync(process.execPath, [root + 'node_modules/typescript/bin/tsc', '-p', root + 'tsconfig.json'], { stdio: 'inherit' });
10 if (result.status !== 0) process.exit(result.status || 1);
11 const modules = new Map();
12 async function moduleFor(name) {
13 if (modules.has(name)) return;
14 if (!/^[a-z-]+$/.test(name)) throw new Error('Unexpected native module dependency.');
15 const source = await readFile(root + `src/core/${name}.ts`, 'utf8');
16 const js = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText;
17 modules.set(name, js);
18 for (const m of js.matchAll(/require\("\.\/([a-z-]+)\.js"\)/g)) await moduleFor(m[1]);
19 }
20 for (const entry of ['pet-engine', 'dot-field']) await moduleFor(entry);
21 // The embedded QuickJS host has no structuredClone API. Pet inputs are validated JSON.
22 const bundle = `/* Generated from pet/src/core. Run npm --prefix pet run sync. */\n(function(global){\n'use strict';\nif(!global.structuredClone)global.structuredClone=value=>JSON.parse(JSON.stringify(value));\nconst factories={},cache={};\n`
23 + [...modules].map(([id, js]) => `factories[${JSON.stringify(id)}]=function(exports,require){\n${js}\n};\n`).join('')
24 + `function load(id){id=id.replace(/^\\.\\//,'').replace(/\\.js$/,'');if(cache[id])return cache[id];if(!factories[id])throw Error('Missing core module');const e=cache[id]={};factories[id](e,load);return e;}\nglobal.PetDotField=load('dot-field');global.PetEngineTelemetry=load('pet-engine').PetEngineTelemetry;\n})(globalThis);\n`;
25 await writeFile(root + 'dist/pet-native.js', bundle);
26 // The committed TUI copy is generated from src/core. `--check` regenerates it
27 // in memory and fails on any byte difference, so a green `npm run check`
28 // cannot sit beside a stale embedded TUI core.
29 const check = process.argv.includes('--check');
30 const generated = [];
31 if (check || process.argv.includes('--tui')) {
32 const destination = root + '../crates/tui/src/tui/pet_watch/';
33 generated.push([destination + 'pet-native.js', Buffer.from(bundle)]);
34 }
35 if (check) {
36 const stale = [];
37 for (const [path, bytes] of generated) {
38 const current = await readFile(path).catch(error => { if (error.code === 'ENOENT') return undefined; throw error; });
39 if (!current || !bytes.equals(current)) stale.push(path.slice(root.length));
40 }
41 if (stale.length) {
42 console.error(`Generated native artifacts are stale: ${stale.join(', ')}. Run npm --prefix pet run sync and commit them.`);
43 process.exit(1);
44 }
45 } else {
46 for (const [path, bytes] of generated) { await mkdir(dirname(path), { recursive: true }); await writeFile(path, bytes); }
47 }
48 console.log(`Built pet native core (${modules.size} shared modules), entirely local${check ? '; generated native artifacts match' : ''}.`);
49
49 lines Plain Text