Files
compras/scripts/requisition-ledger-runtime-artifact.mjs
T
nala eb32c74c56
tests / ci (push) Waiting to run
commit compras1 2811
2026-11-28 07:11:35 +02:00

51 lines
3.1 KiB
JavaScript

// Package the already-used common QA runtime into a private Compras artefact.
// No database, operational package, settings or SGF source is modified.
import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import { spawnSync } from 'node:child_process';
const workspace = 'C:/laragon/www/compras';
if (path.resolve(process.cwd()) !== path.resolve(workspace)) throw Error('Compras workspace required');
const canonical = 'C:/laragon/www/sgf-tempo/packages';
const hash = bytes => crypto.createHash('sha256').update(bytes).digest('hex');
const sources = ['finance-bundle/Bundle.php'];
for (const name of ['payables', 'general-ledger']) {
for (const file of ['bootstrap.php', 'README.md']) {
if (fs.existsSync(path.join(canonical, name, file))) sources.push(`${name}/${file}`);
}
for (const dir of ['src', 'ui']) {
for (const file of fs.readdirSync(path.join(canonical, name, dir)).sort()) {
if (!/^[A-Za-z0-9_-]+\.php$/.test(file)) throw Error('Unexpected runtime source');
sources.push(`${name}/${dir}/${file}`);
}
}
}
const content = sources.map(source => {
const actual = path.join(canonical, source);
if (!fs.lstatSync(actual).isFile() || fs.lstatSync(actual).isSymbolicLink()) throw Error('Regular runtime files required');
return { file: source === 'finance-bundle/Bundle.php' ? 'Bundle.php' : source, bytes: fs.readFileSync(actual) };
}).sort((a, b) => a.file.localeCompare(b.file));
const files = content.map(({ file, bytes }) => ({ file, sha256: hash(bytes) }));
const manifest = JSON.stringify({ contract: 'COMMON-FINANCE-BUNDLE-v1', synthetic_only: true, files }, null, 2) + '\n';
const pin = hash(manifest);
const root = path.join(workspace, 'storage/framework/testing', `requisition-ledger-runtime-qa-${pin.slice(0, 16)}`);
if (!fs.existsSync(root)) {
fs.mkdirSync(root);
for (const { file, bytes } of content) {
fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true });
fs.writeFileSync(path.join(root, file), bytes, { flag: 'wx' });
}
fs.writeFileSync(path.join(root, 'manifest.json'), manifest, { flag: 'wx' });
}
if (hash(fs.readFileSync(path.join(root, 'manifest.json'))) !== pin) throw Error('Existing artefact differs; never overwrite');
// The caller fixes this manifest and loader hash before loading any executable.
for (const { file, sha256 } of files) {
const target = path.join(root, file);
if (!fs.lstatSync(target).isFile() || fs.lstatSync(target).isSymbolicLink() || hash(fs.readFileSync(target)) !== sha256) throw Error('Artefact byte mismatch');
}
const code = 'require $argv[1]."/Bundle.php"; echo json_encode(Tempo\\Finance\\Composition\\Bundle::verify($argv[1],$argv[2]),JSON_THROW_ON_ERROR);';
const checked = spawnSync('php', ['-r', code, root, pin], { windowsHide: true, encoding: 'utf8' });
if (checked.status !== 0) throw Error('Existing common bundle verifier refused the private artefact');
console.log(JSON.stringify({ root, manifest_sha256: pin, files: files.length, verified: true, synthetic_only: true, installed_or_activated: false }));