51 lines
4.3 KiB
JavaScript
51 lines
4.3 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { createHash, randomUUID } from 'node:crypto';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
// Export the committed program. No running host, user data directory, installed
|
|
// application, credentials, build output or historical evidence is consulted.
|
|
const directories = ['src', 'public', 'scripts', 'tests', 'examples', 'benchmarks', 'research', 'licenses'];
|
|
const files = ['package.json', 'package-lock.json', 'vite.config.ts', 'tsconfig.json', 'index.html', 'mobile.html',
|
|
'.gitignore', '.gitattributes', 'README.md', 'CHANGELOG.md', 'DESIGN.md', 'AGENTS.md', 'THIRD_PARTY_NOTICES.txt',
|
|
'Start-Workbench.cmd', 'Stop-Workbench.cmd', 'Start-Desktop.cmd'];
|
|
const guide = name => /^docs\/[^/]+\.md$/.test(name) || /^docs\/research\/[^/]+\.(?:md|json)$/.test(name);
|
|
|
|
export function packageSource({ root = process.cwd(), outputRoot = path.join(root, 'releases') } = {}) {
|
|
root = path.resolve(root); outputRoot = path.resolve(outputRoot);
|
|
const git = args => execFileSync('git', args, { cwd: root, windowsHide: true, maxBuffer: 32 * 1024 * 1024 });
|
|
const sourceCommit = git(['rev-parse', 'HEAD']).toString().trim();
|
|
const version = JSON.parse(git(['show', sourceCommit + ':package.json'])).version;
|
|
if (!/^\d+\.\d+\.\d+(?:-[a-z0-9.-]+)?$/i.test(version)) throw new Error('Invalid committed program version.');
|
|
const tree = git(['ls-tree', '-r', '--long', '-z', sourceCommit]).toString('utf8').split('\0').filter(Boolean).map(line => {
|
|
const match = /^(\d+) (\w+) ([a-f0-9]+)\s+(\d+|-)\t([\s\S]+)$/.exec(line);
|
|
if (!match) throw new Error('Invalid committed source entry.');
|
|
return { mode: match[1], type: match[2], gitBlob: match[3], bytes: Number(match[4]), path: match[5] };
|
|
});
|
|
const entries = tree.filter(row => files.includes(row.path) || directories.some(name => row.path.startsWith(name + '/')) || guide(row.path));
|
|
if (!entries.some(row => row.path === 'package-lock.json') || !entries.some(row => row.path.startsWith('src/'))) throw new Error('Committed source and dependency lock are required.');
|
|
if (entries.some(row => row.type !== 'blob' || !['100644', '100755'].includes(row.mode))) throw new Error('Source release requires regular committed files, without links or submodules.');
|
|
const roots = [...directories.filter(name => entries.some(row => row.path.startsWith(name + '/'))), ...files.filter(name => entries.some(row => row.path === name)), ...entries.filter(row => guide(row.path)).map(row => row.path)];
|
|
const name = `Agent-Workbench-${version}-source-code-${sourceCommit.slice(0, 8)}-${randomUUID()}`;
|
|
fs.mkdirSync(outputRoot, { recursive: true });
|
|
const archive = path.join(outputRoot, name + '.zip'), fd = fs.openSync(archive, 'wx');
|
|
try { execFileSync('git', ['archive', '--format=zip', '--prefix=Agent-Workbench-' + version + '-source/', sourceCommit, '--', ...roots], { cwd: root, windowsHide: true, stdio: ['ignore', fd, 'pipe'] }); }
|
|
finally { fs.closeSync(fd); }
|
|
const result = { kind: 'source-code', version, sourceCommit, sourceTree: git(['rev-parse', sourceCommit + '^{tree}']).toString().trim(),
|
|
path: archive, bytes: fs.statSync(archive).size, sha256: createHash('sha256').update(fs.readFileSync(archive)).digest('hex'),
|
|
createdAt: new Date().toISOString(), files: entries.map(({ path, bytes, gitBlob }) => ({ path, bytes, gitBlob })),
|
|
scope: 'Committed program, build scripts, tests, examples, dependency lock, licenses and text guides. No user projects, sessions, API credentials, installed runtimes or historical evidence archives. Build with Node 24, npm ci and npm run build. Historical acceptance drivers may require separately retained evidence or external engines.' };
|
|
fs.writeFileSync(path.join(outputRoot, name + '.json'), JSON.stringify(result, null, 2) + '\n', { flag: 'wx' });
|
|
return result;
|
|
}
|
|
|
|
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
const args = process.argv.slice(2);
|
|
if (args.length === 1 && args[0] === '--help') console.log('node scripts/package-source.mjs — ZIP of committed program source only; no running Workbench, build, installation or user data required.');
|
|
else {
|
|
if (args.length) throw new Error('Use --help.');
|
|
const result = packageSource(); console.log(JSON.stringify({ ...result, files: result.files.length }, null, 2));
|
|
}
|
|
}
|