Files
Agent-Workbench/scripts/package.mjs
T
2026-10-29 11:23:43 +08:00

105 lines
7.7 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
const root = process.cwd();
const meta = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const lock = JSON.parse(fs.readFileSync('package-lock.json', 'utf8'));
const releases = path.resolve(root, 'releases');
fs.mkdirSync(releases, { recursive: true });
const baseName = 'Agent-Workbench-' + meta.version;
const refresh = process.argv.includes('--refresh');
const buildSuffix = !refresh && fs.existsSync(path.join(releases, baseName + '-win-x64')) ? '-' + new Date().toISOString().replace(/[-:TZ.]/g, '') : '';
const portableRoot = path.join(releases, baseName + '-win-x64' + buildSuffix);
const sourceRoot = path.join(releases, baseName + '-source' + buildSuffix);
for (const target of [portableRoot, sourceRoot]) {
if (!path.resolve(target).startsWith(releases + path.sep) || (fs.existsSync(target) && !refresh)) throw new Error('Release output must be a new folder under releases.');
fs.mkdirSync(target, { recursive: true });
}
const portableFiles = ['dist-client', 'dist-server', 'docs', 'examples', 'README.md', 'CHANGELOG.md', 'DESIGN.md', 'Start-Workbench.cmd', 'Stop-Workbench.cmd'];
const sourceFiles = ['src', 'public', 'scripts', 'tests', 'docs', 'examples', 'package.json', 'package-lock.json', 'vite.config.ts', 'tsconfig.json', 'index.html', '.gitignore', 'README.md', 'CHANGELOG.md', 'DESIGN.md', 'task_plan.md', 'findings.md', 'progress.md', 'Start-Workbench.cmd', 'Stop-Workbench.cmd'];
const copyTree = (source, destination) => fs.cpSync(source, destination, { recursive: true, filter: (from, to) => {
if (!fs.statSync(from).isFile() || !fs.existsSync(to)) return true;
return !fs.readFileSync(from).equals(fs.readFileSync(to));
} });
for (const name of portableFiles) copyTree(path.join(root, name), path.join(portableRoot, name));
fs.mkdirSync(path.join(portableRoot, 'scripts'), { recursive: true });
for (const name of ['Start-Workbench.ps1', 'Stop-Workbench.ps1', 'upgrade-running.mjs']) fs.copyFileSync(path.join(root, 'scripts', name), path.join(portableRoot, 'scripts', name));
for (const name of sourceFiles) copyTree(path.join(root, name), path.join(sourceRoot, name));
const preferredNode = path.resolve(root, '..', 'tools', 'node-v24.22.1-win-x64', 'node.exe');
const nodeBinary = process.env.AGENT_WORKBENCH_NODE_PATH || (fs.existsSync(preferredNode) ? preferredNode : process.execPath);
const nodeLicense = path.join(path.dirname(nodeBinary), 'LICENSE');
if (!fs.existsSync(nodeLicense)) throw new Error('The bundled Node runtime requires its complete LICENSE next to node.exe.');
const nodeVersion = execFileSync(nodeBinary, ['--version'], { encoding: 'utf8', windowsHide: true }).trim();
if (!/^v24\./.test(nodeVersion)) throw new Error('This package was validated with Node 24.');
fs.mkdirSync(path.join(portableRoot, 'runtime'), { recursive: true });
const runtimeDestination = path.join(portableRoot, 'runtime', 'node.exe');
const digest = file => createHash('sha256').update(fs.readFileSync(file)).digest('hex');
if (!fs.existsSync(runtimeDestination) || digest(runtimeDestination) !== digest(nodeBinary)) fs.copyFileSync(nodeBinary, runtimeDestination);
const notices = ['Agent Workbench third-party notices', '',
'Original product code is supplied for the project owner. Its redistribution terms are determined by the project owner.',
'Third-party components remain governed by their own licenses. Full available license texts are reproduced below and in licenses/.', '',
'The release does not contain Claude Code or Cursor client binaries or recovered application source.', ''];
const licenseRoot = path.join(root, 'licenses'); fs.mkdirSync(licenseRoot, { recursive: true });
let licenseCount = 0;
for (const [location, entry] of Object.entries(lock.packages).sort(([a], [b]) => a.localeCompare(b))) {
if (!location.startsWith('node_modules/')) continue;
const packageRoot = path.resolve(root, location);
if (!packageRoot.startsWith(path.join(root, 'node_modules') + path.sep) || !fs.existsSync(packageRoot)) continue;
const pkg = JSON.parse(fs.readFileSync(path.join(packageRoot, 'package.json'), 'utf8'));
const files = fs.readdirSync(packageRoot).filter(name => /^(licen[cs]e|copying)([.-].*)?$/i.test(name) && fs.statSync(path.join(packageRoot, name)).isFile());
notices.push('='.repeat(72), (pkg.name || location) + ' ' + (pkg.version || entry.version),
'License: ' + (typeof pkg.license === 'object' ? JSON.stringify(pkg.license) : pkg.license || entry.license || 'see upstream package'),
'Source package: ' + location, '');
if (!files.length) notices.push('License metadata is recorded above; associated parent package license text is included where supplied.', '');
for (const file of files) {
const text = fs.readFileSync(path.join(packageRoot, file), 'utf8');
notices.push(file, '', text, '');
fs.writeFileSync(path.join(licenseRoot, (pkg.name + '-' + pkg.version + '-' + file).replace(/[^a-z0-9_.-]/gi, '_') + '.txt'), text);
licenseCount++;
}
}
const nodeLicenseText = fs.readFileSync(nodeLicense, 'utf8');
fs.writeFileSync(path.join(licenseRoot, 'Node-' + nodeVersion + '-LICENSE.txt'), nodeLicenseText);
notices.push('='.repeat(72), 'Node.js ' + nodeVersion, '', nodeLicenseText);
fs.writeFileSync(path.join(root, 'THIRD_PARTY_NOTICES.txt'), notices.join('\n'));
for (const output of [portableRoot, sourceRoot]) {
fs.cpSync(licenseRoot, path.join(output, 'licenses'), { recursive: true });
fs.copyFileSync(path.join(root, 'THIRD_PARTY_NOTICES.txt'), path.join(output, 'THIRD_PARTY_NOTICES.txt'));
}
function filesIn(directory, relative = '') {
return fs.readdirSync(path.join(directory, relative), { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)).flatMap(entry => {
const file = path.join(relative, entry.name);
return entry.isDirectory() ? filesIn(directory, file) : [file];
});
}
function manifest(directory, kind) {
const files = filesIn(directory).filter(file => file !== 'MANIFEST.json').map(file => {
const bytes = fs.readFileSync(path.join(directory, file));
return { path: file.replaceAll('\\', '/'), bytes: bytes.length, sha256: createHash('sha256').update(bytes).digest('hex') };
});
const result = { app: 'agent-workbench', version: meta.version, kind, platform: 'win32-x64', nodeVersion,
createdAt: new Date().toISOString(), files };
fs.writeFileSync(path.join(directory, 'MANIFEST.json'), JSON.stringify(result, null, 2));
return result;
}
const portableManifest = manifest(portableRoot, 'portable');
manifest(sourceRoot, 'source');
const quote = value => "'" + value.replaceAll("'", "''") + "'";
function archive(directory) {
const destination = directory + '.zip';
if (!destination.startsWith(releases + path.sep)) throw new Error('Archive path is outside releases.');
if (fs.existsSync(destination)) fs.unlinkSync(destination);
const command = "$ErrorActionPreference='Stop'; Add-Type -AssemblyName System.IO.Compression.FileSystem; [IO.Compression.ZipFile]::CreateFromDirectory(" + quote(directory) + ', ' + quote(destination) + ', [IO.Compression.CompressionLevel]::Optimal, $true)';
execFileSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', command], { windowsHide: true, stdio: 'pipe' });
return { path: destination, bytes: fs.statSync(destination).size, sha256: createHash('sha256').update(fs.readFileSync(destination)).digest('hex') };
}
const result = { portableRoot, sourceRoot, portable: archive(portableRoot), source: archive(sourceRoot), nodeVersion, portableFiles: portableManifest.files.length, licenseTexts: licenseCount + 1 };
fs.writeFileSync(path.join(releases, 'latest.json'), JSON.stringify(result, null, 2));
console.log(JSON.stringify(result, null, 2));