import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { officialSnapshot, contractDifference, verifyOfficialContracts } from './official-contract-check.mjs'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const args = process.argv.slice(2); if (args.includes('--help')) { console.log('node scripts/verify-official-contracts.mjs [--output NEW_FILE] | --freeze NEW_FILE | --diff OLD_FILE NEW_FILE [--output NEW_FILE]\nVerification is offline. Freeze never overwrites; preserve prior snapshots and review diffs before updating INDEX.json and regression tests. Diff-only is not a compatibility pass.'); } else { let output, report; try { if (args[0] === '--freeze' && args.length === 2) { fs.writeFileSync(path.resolve(args[1]), JSON.stringify(officialSnapshot(root), null, 2) + '\n', { flag: 'wx' }); console.log(JSON.stringify({ frozen: args[1], scope: 'snapshot only; review and regressions still required' })); } else { const remaining = [...args], outputIndex = remaining.indexOf('--output'); if (outputIndex !== -1) { output = remaining[outputIndex + 1]; if (!output) throw new Error('--output needs a new filename'); remaining.splice(outputIndex, 2); } if (!remaining.length) report = verifyOfficialContracts(root); else if (remaining[0] === '--diff' && remaining.length === 3) report = { mode: 'review-diff-only', ...contractDifference(JSON.parse(fs.readFileSync(path.resolve(remaining[1]))), JSON.parse(fs.readFileSync(path.resolve(remaining[2])))) }; else throw new Error('Unknown arguments; use --help'); if (output) fs.writeFileSync(path.resolve(output), JSON.stringify(report, null, 2) + '\n', { flag: 'wx' }); console.log(JSON.stringify({ passed: report.passed ?? null, mode: report.mode ?? 'verification', registryVersion: report.registryVersion, changes: report.changes?.length, records: report.recordsCount, testFiles: report.testFiles?.length })); } } catch(error) { if (output && !fs.existsSync(path.resolve(output))) fs.writeFileSync(path.resolve(output), JSON.stringify({ passed: false, error: error.message, scope: 'offline official contract verification' }, null, 2) + '\n', { flag: 'wx' }); console.error(error.stack); process.exitCode = 1; } }