const fs = require('fs'), p = require('path'), c = require('crypto') const root = p.resolve(__dirname, '../..') const dest = fs.readFileSync(p.join(root, 'server/docs/cleanup-backup-location.txt'), 'utf8').replace(/^\uFEFF/, '').trim() const skip = new Set(['node_modules', 'vendor', 'runtime', 'dist', '.git']) const manifest = [] const hash = x => c.createHash('sha256').update(fs.readFileSync(x)).digest('hex') function walk(d) { for (const e of fs.readdirSync(d, { withFileTypes: true })) { if (skip.has(e.name) || e.name.startsWith('.env') || e.name.endsWith('.zip') || e.isSymbolicLink()) continue const src = p.join(d, e.name) if (e.isDirectory()) walk(src) else { const rel = p.relative(root, src), out = p.join(dest, 'source', rel) fs.mkdirSync(p.dirname(out), { recursive: true }) if (fs.existsSync(out)) throw Error('Backup already exists: ' + rel) fs.copyFileSync(src, out) if (hash(src) !== hash(out)) throw Error('Verification failed: ' + rel) manifest.push({ path: rel, sha256: hash(src) }) } } } walk(root) fs.writeFileSync(p.join(dest, 'source-manifest.json'), JSON.stringify(manifest, null, 2)) console.log('Verified source files:', manifest.length)