ai-chatbot/scripts/zcode-agent/stage-runtime.mjs

25 lines
1.9 KiB
JavaScript

import { cp, copyFile, mkdir, readFile, writeFile, chmod } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import { resolve, join } from 'node:path';
import { execFileSync } from 'node:child_process';
const source = resolve(process.argv[2] || '');
const destination = resolve(process.argv[3] || '.local/zcode-runtime');
const expected = '0a1fc56d279fd1001f78387bd81aec1c26767411';
const head = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: source, encoding: 'utf8' }).trim();
if (head !== expected) throw new Error('ZCode 源码提交与固定基线不同');
if (execFileSync('git', ['diff', '--name-only', 'HEAD'], { cwd: source, encoding: 'utf8' }).trim())
throw new Error('不能将已修改的上游源码冒充固定版本');
const built = join(source, 'apps/zcode-cli/packages/cli/dist');
await mkdir(join(destination, 'bin'), { recursive: true });
for (const file of ['zcode.cjs', 'THIRD-PARTY-NOTICES.md']) await copyFile(join(built, file), join(destination, file));
await cp(join(built, 'provider'), join(destination, 'provider'), { recursive: true });
await copyFile(join(source, 'LICENSE'), join(destination, 'LICENSE'));
await copyFile(resolve('services/zcode-agent/bridge.mjs'), join(destination, 'bridge.mjs'));
// Deployment replaces this development binary with the verified Linux Node 24
// binary. No global Node installation or existing application runtime changes.
await copyFile(process.execPath, join(destination, 'bin/node')); await chmod(join(destination, 'bin/node'), 0o755);
const sha256 = createHash('sha256').update(await readFile(join(destination, 'zcode.cjs'))).digest('hex');
await writeFile(join(destination, 'manifest.json'), JSON.stringify({ project: 'zai-org/ZCode', commit: expected,
cliVersion: '0.17.0', sha256, builtWithNode: process.version }, null, 2) + '\n');
console.log(JSON.stringify({ destination, commit: expected, sha256 }));