Files
openmotion/scripts/package.ts
open-design-crew[bot] 77008f2cce [OPEND-4726] feat(motion): share Prism templates and separate DEV/prod packages (#171)
* feat(motion): share Prism templates across Studio and MCP

* fix(packaging): select template environment in CI and full ZIPs

* fix(packaging): support the macOS archive tool

---------

Co-authored-by: lefarcen <935902669@qq.com>
Co-authored-by: open-design-crew[bot] <299007234+open-design-crew[bot]@users.noreply.github.com>
2026-11-09 14:12:38 +08:00

56 lines
3.8 KiB
TypeScript

import { isMain } from '../shared/entry.ts';
import { readFile, mkdir } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { distributionFiles } from './distribution.ts';
// ZIP "store" format, so packaging has no runtime dependencies.
function crc32(data: Buffer) {
let crc = 0xffffffff;
for (const byte of data) {
crc ^= byte;
for (let n = 0; n < 8; n++) crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1));
}
return (crc ^ 0xffffffff) >>> 0;
}
export function zip(entries: { name: string; data: Buffer }[]): Buffer {
const local: Buffer[] = [], central: Buffer[] = []; let offset = 0, directorySize = 0;
for (const entry of entries) {
const name = Buffer.from(entry.name); const crc = crc32(entry.data);
const header = Buffer.alloc(30); header.writeUInt32LE(0x04034b50, 0); header.writeUInt16LE(20, 4);
header.writeUInt16LE(0x800, 6); header.writeUInt16LE(33, 12); header.writeUInt32LE(crc, 14);
header.writeUInt32LE(entry.data.length, 18); header.writeUInt32LE(entry.data.length, 22); header.writeUInt16LE(name.length, 26);
const dir = Buffer.alloc(46); dir.writeUInt32LE(0x02014b50, 0); dir.writeUInt16LE(20, 4); dir.writeUInt16LE(20, 6);
dir.writeUInt16LE(0x800, 8); dir.writeUInt16LE(33, 14); dir.writeUInt32LE(crc, 16);
dir.writeUInt32LE(entry.data.length, 20); dir.writeUInt32LE(entry.data.length, 24); dir.writeUInt16LE(name.length, 28); dir.writeUInt32LE(offset, 42);
local.push(header, name, entry.data); central.push(dir, name);
offset += header.length + name.length + entry.data.length; directorySize += dir.length + name.length;
}
const end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50); end.writeUInt16LE(entries.length, 8); end.writeUInt16LE(entries.length, 10);
end.writeUInt32LE(directorySize, 12); end.writeUInt32LE(offset, 16);
return Buffer.concat([...local, ...central, end]);
}
const root = fileURLToPath(new URL('../', import.meta.url));
async function main() {
const { packageTemplateConfig } = await import('../shared/template-config.ts');
const { releaseProfile } = await import('../shared/mcp-registry.ts');
const flags = process.argv.slice(2);
if (flags.length > 2 || flags.some(flag => !/^--(?:mcp-profile=(?:legacy|phase1)|template-env=(?:dev|prod))$/.test(flag))) throw new Error('Usage: package.ts [--mcp-profile=legacy|phase1] [--template-env=dev|prod]');
const profile = flags.find(flag => flag.startsWith('--mcp-profile='))?.split('=')[1] ?? releaseProfile;
const templateConfig = packageTemplateConfig(flags.find(flag => flag.startsWith('--template-env='))?.split('=')[1]);
const { mcpRegistry } = await import('../mcp.ts');
const registry = mcpRegistry(profile);
const { check } = await import('./check.ts');
await check();
const manifest = JSON.parse(await readFile(path.join(root, '.codex-plugin/plugin.json'), 'utf8'));
const files = await distributionFiles(profile);
const overrides: Record<string, unknown> = { 'shared/mcp-release.json': { profile }, 'shared/template-config.json': templateConfig };
const entries = await Promise.all(files.sort().map(async file => ({ name: manifest.name + '/' + file,
data: overrides[file] ? Buffer.from(JSON.stringify(overrides[file], null, 2) + '\n') : await readFile(path.join(root, file)) })));
await mkdir(path.join(root, 'dist'), { recursive: true });
const output = `dist/${manifest.name}-${manifest.version}${profile === 'legacy' ? '' : '-' + profile}${templateConfig.environment === 'dev' ? '-dev' : ''}.zip`;
await (await import('node:fs/promises')).writeFile(path.join(root, output), zip(entries));
console.log(`${output}: ${entries.length} files, templates ${templateConfig.environment}, MCP ${profile}: ${registry.tools.length} tools (no personal media or native executables).`);
}
if (isMain(import.meta.url)) await main();