// scripts/ci/generate-checksums.mjs // Computes SHA-256 hashes for all binaries in a release directory import { createReadStream } from 'node:fs'; import { readdir, writeFile, stat } from 'node:fs/promises'; import { createHash } from 'node:crypto'; import { join, resolve } from 'node:path'; async function sha256File(filePath) { return new Promise((resolveHash, reject) => { const hash = createHash('sha256'); const stream = createReadStream(filePath); stream.on('data', (chunk) => hash.update(chunk)); stream.on('end', () => resolveHash(hash.digest('hex'))); stream.on('error', reject); }); } async function main() { const targetDir = resolve(process.argv[2] || './apps/desktop/release'); console.log(`Scanning release assets in: ${targetDir}`); const lines = []; async function scan(dir) { const entries = await readdir(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = join(dir, entry.name); if (entry.isDirectory()) { await scan(fullPath); } else if (/\.(exe|dmg|zip|blockmap|yml)$/i.test(entry.name) && !entry.name.includes('SHA256SUMS')) { const hash = await sha256File(fullPath); const relName = entry.name; lines.push(`${hash} ${relName}`); console.log(`✓ ${relName}: ${hash}`); } } } try { await scan(targetDir); if (lines.length > 0) { const outPath = join(targetDir, 'SHA256SUMS.txt'); await writeFile(outPath, lines.join('\n') + '\n', 'utf8'); console.log(`\nChecksums written to: ${outPath}`); } else { console.log('No matching release binaries found.'); } } catch (err) { console.error('Error calculating checksums:', err.message); process.exit(1); } } main();