// Rebuilds index.toml and the index hash in pack.toml from the files in this repo. // Run after adding/removing/editing anything that the server should receive: node tools/reindex.mjs // Indexed: mods/ (*.pw.toml as metafiles, raw jars as files), config/, defaultconfigs/, kubejs/. import fs from 'fs'; import path from 'path'; import crypto from 'crypto'; import { fileURLToPath } from 'url'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const INDEXED_DIRS = ['mods', 'config', 'defaultconfigs', 'kubejs']; const sha256 = buf => crypto.createHash('sha256').update(buf).digest('hex'); // Hash what git will check out (LF), not what a Windows working copy may hold (CRLF). const lf = buf => (buf.includes(0) ? buf : Buffer.from(buf.toString('utf8').replace(/\r\n/g, '\n'), 'utf8')); const walk = rel => fs.readdirSync(path.join(root, rel), { withFileTypes: true }).flatMap(d => d.isDirectory() ? walk(path.posix.join(rel, d.name)) : [path.posix.join(rel, d.name)]); const files = INDEXED_DIRS.filter(d => fs.existsSync(path.join(root, d))) .flatMap(walk) .sort((a, b) => a.localeCompare(b)) .map(rel => { const entry = ['[[files]]', `file = ${JSON.stringify(rel)}`, `hash = ${JSON.stringify(sha256(lf(fs.readFileSync(path.join(root, rel)))))}`]; if (rel.startsWith('mods/') && rel.endsWith('.pw.toml')) entry.push('metafile = true'); return entry.join('\n') + '\n'; }); const index = 'hash-format = "sha256"\n\n' + files.join('\n'); fs.writeFileSync(path.join(root, 'index.toml'), index); const packPath = path.join(root, 'pack.toml'); const pack = fs.readFileSync(packPath, 'utf8').replace(/\r\n/g, '\n') .replace(/(\[index\][\s\S]*?\nhash = ")[0-9a-f]+(")/, `$1${sha256(Buffer.from(index))}$2`); fs.writeFileSync(packPath, pack); console.log(`indexed ${files.length} files`);