import fs from 'node:fs/promises'; import path from 'node:path'; import crypto from 'node:crypto'; import { execFileSync } from 'node:child_process'; import assert from 'node:assert/strict'; const frontend = process.argv[2]; assert.ok(frontend, 'Provide the verified frontend build directory.'); const roots = [ 'app', 'bootstrap', 'config', 'database', 'resources', 'routes', 'integrations', 'packages', 'public', 'composer.json', 'composer.lock', 'package.json', 'package-lock.json', 'artisan', '.gitattributes', '.env.example', 'scripts/verify-compras-release.php', 'scripts/build-compras-release.mjs', 'scripts/archive-compras-release.py', 'scripts/deployment/compras-tempo-deploy.sh', ]; const listed = ['git', 'ls-files']; const tracked = execFileSync( listed[0], [...listed.slice(1), '-z', '--', ...roots], { encoding: 'utf8' }, ).split('\0'); const added = execFileSync( 'git', ['ls-files', '--others', '--exclude-standard', '-z', '--', ...roots], { encoding: 'utf8' }, ).split('\0'); const hash = (bytes) => crypto.createHash('sha256').update(bytes).digest('hex'); const source = {}; const exact = []; for (const file of [...new Set([...tracked, ...added])] .filter(Boolean) .sort()) { if (file.startsWith('bootstrap/cache/') || file.startsWith('public/build/')) continue; const info = await fs.lstat(file); if (!info.isFile() || info.isSymbolicLink()) continue; const bytes = await fs.readFile(file); const pinned = file.startsWith('packages/budget-control/') || file.startsWith('packages/payables/') || file === 'resources/data/geography/cartografia-moz.json' || bytes.includes(0); if (pinned) exact.push(file); source[file] = hash( pinned ? bytes : Buffer.from(bytes.toString('utf8').replaceAll('\r\n', '\n')), ); } const buildBytes = await fs.readFile(path.join(frontend, 'manifest.json')); const build = JSON.parse(buildBytes); const assets = {}; for (const entry of Object.values(build)) { for (const file of [ entry.file, ...(entry.css ?? []), ...(entry.assets ?? []), ]) { assert.ok(file.startsWith('assets/') && !file.includes('..')); assets[file] = hash(await fs.readFile(path.join(frontend, file))); } } const manifest = { contract: 'compras.release.v1', product: 'compras', source_eol: 'lf', exact_source_files: exact, source_files: source, frontend: { manifest_sha256: hash(buildBytes), entry: build['resources/js/app.tsx'].file, fonts_manifest_sha256: hash( await fs.readFile(path.join(frontend, 'fonts-manifest.json')), ), files: Object.fromEntries( Object.entries(assets).sort(([a], [b]) => a.localeCompare(b, 'en')), ), }, components: [ { setting: 'COMPRAS_RUNTIME_ROOT', manifest_sha256: '80483320a54af6f123498e43c1435a20922455f65860be88bd259e90efcbc0b7', }, { setting: 'COMPRAS_INVENTORY_ROOT', manifest_sha256: '8608e6551dea6448983ead401ba900b440ffdeb1446ce52de6509d5eba0e87bd', }, { setting: 'COMPRAS_ORDER_CANCELLATION_ROOT', manifest_sha256: '52df7927aa2689598ce1be92de1483694347a696245704abb9e3925db7abfc0f', }, ], }; manifest.release_id = 'compras-' + hash(JSON.stringify(manifest)).slice(0, 16); await fs.writeFile( 'compras-release.json', JSON.stringify(manifest, null, 2) + '\n', ); console.log( JSON.stringify({ release_id: manifest.release_id, source_files: Object.keys(source).length, asset_files: Object.keys(assets).length, }), );