66 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { execFileSync } from 'node:child_process';
import { maintainAfterPackaging } from './local-build-retention.mjs';
import { assertPiNodeVersion } from '../src/runtime-policy.mjs';
import {
AGENT_DIRECTORIES, BUILD_README, LAUNCHER, NATIVE_FILES, checkClientPackage, inventoryFiles, regularFile,
} from './client-package-integrity.mjs';
// A local portable build. Never copy a profile, credential, project or request journal.
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
export async function packageClient({
binary, destination, node = process.execPath, nodeLicense,
sourceRoot = root, nativeRoot = path.resolve(sourceRoot, '../detail-photocraft'),
}) {
if (!binary || !destination) throw Error('指定已验证的原生程序与新的输出目录');
sourceRoot = path.resolve(sourceRoot);
destination = path.resolve(destination);
binary = path.resolve(binary);
node = path.resolve(node);
nodeLicense = path.resolve(nodeLicense || path.join(path.dirname(node), 'LICENSE'));
if (destination === sourceRoot || destination.startsWith(sourceRoot + path.sep)) throw Error('输出目录必须独立于源码');
for (const file of [binary, node, nodeLicense, ...NATIVE_FILES.map(file => path.join(nativeRoot, file))]) await regularFile(file);
const version = execFileSync(node, ['--version'], { encoding: 'utf8', windowsHide: true }).trim();
assertPiNodeVersion(version);
const piPackage = path.join(sourceRoot, 'node_modules/@earendil-works/pi-coding-agent/package.json');
const { version: piVersion } = JSON.parse(await fs.readFile(piPackage, 'utf8'));
await fs.mkdir(path.dirname(destination), { recursive: true });
await fs.mkdir(destination); // Refuse to overwrite an existing client or user data.
await fs.copyFile(binary, path.join(destination, 'photocraft.exe'));
const agent = path.join(destination, 'agent');
await fs.mkdir(agent);
await fs.copyFile(node, path.join(agent, 'node.exe'));
await fs.copyFile(nodeLicense, path.join(agent, 'LICENSE-Node.txt'));
for (const file of ['package.json', 'package-lock.json']) await fs.copyFile(path.join(sourceRoot, file), path.join(agent, file));
for (const dir of AGENT_DIRECTORIES) await fs.cp(path.join(sourceRoot, dir), path.join(agent, dir), { recursive: true, dereference: true });
for (const file of NATIVE_FILES) {
const target = path.join(destination, file);
await fs.mkdir(path.dirname(target), { recursive: true });
await fs.copyFile(path.join(nativeRoot, file), target);
}
await fs.writeFile(path.join(destination, LAUNCHER), '@echo off\r\nstart "" "%~dp0photocraft.exe"\r\n', 'utf8');
const readme = [
'由壹点详情页本地开发构建',
'客户端界面使用 HarmonyOS Sans SC(鸿蒙黑体)。授权条款见 assets/ui-fonts/LICENSE-HarmonyOS-Sans.txt。',
'双击“启动客户端.cmd”。Agent运行组件已随包提供,无需另装Node。',
'账户、个人Key、项目与会话保存在当前Windows用户的独立配置和凭据库,不在本目录内。',
'本包未签名,尚不是正式安装程序。Node ' + version + ';Pi SDK ' + piVersion + '。',
'',
].join('\r\n');
await fs.writeFile(path.join(destination, BUILD_README), readme, 'utf8');
const files = await inventoryFiles(destination);
const manifest = { at: new Date().toISOString(), kind: 'unsigned-local-development-portable', node: version, pi: piVersion, bytes: files.reduce((sum, file) => sum + file.bytes, 0), files };
await fs.writeFile(path.join(destination, 'build-manifest.json'), JSON.stringify(manifest, null, 2));
return { destination, ...await checkClientPackage({ destination }), node: version, pi: piVersion };
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
const [binary, destination, node, nodeLicense] = process.argv.slice(2);
const result = await packageClient({ binary, destination, node, nodeLicense });
const cleanup = await maintainAfterPackaging(result.destination, path.resolve(binary));
console.log(JSON.stringify({ ...result, cleanup }));
}