#!/usr/bin/env node /** * Build the deterministic v0.4.0 ZIP package. * * CSV and Parquet are canonical. The XLSX is copied as a frozen, checksum-only * convenience artifact because its external runtime is not in package.json. */ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { createHash } from "node:crypto"; import { spawnSync } from "node:child_process"; import { fileURLToPath } from "node:url"; const VERSION = "0.4.0"; const FIXED_TIME = new Date("2026-08-15T00:00:00.000Z"); const here = path.dirname(fileURLToPath(import.meta.url)); const root = path.resolve(here, ".."); const packageName = `agency-transfer-election-cases-v${VERSION}`; const workbookName = `agency-transfer-election-evidence-index-v${VERSION}.xlsx`; const releaseDir = path.join(root, "release"); const workbookPath = path.join(releaseDir, workbookName); const outputPath = path.join(releaseDir, `${packageName}.zip`); const atomicOutputPath = path.join(releaseDir, `.${packageName}.zip.${process.pid}.tmp`); const rootFiles = [ ".gitignore", "CHANGELOG.md", "CITATION.cff", "CONTRIBUTING.md", "LICENSE", "README.md", "package-lock.json", "package.json", "requirements-validation.txt" ]; const sourceDirs = ["data", "docs", "parquet", "research", "scripts"]; function sha256(buffer) { return createHash("sha256").update(buffer).digest("hex"); } async function filesBelow(base, relative = "") { const directory = path.join(base, relative); const entries = await fs.readdir(directory, { withFileTypes: true }); const files = []; for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) { const child = path.join(relative, entry.name); if (entry.isSymbolicLink()) throw new Error(`Symlink is not allowed in release package: ${child}`); if (entry.isDirectory()) files.push(...await filesBelow(base, child)); else if (entry.isFile()) files.push(child.split(path.sep).join("/")); else throw new Error(`Unsupported release entry: ${child}`); } return files; } await fs.mkdir(releaseDir, { recursive: true }); await fs.access(workbookPath); const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), `${packageName}-`)); const stage = path.join(tempRoot, packageName); const tempZip = path.join(tempRoot, `${packageName}.zip`); try { await fs.mkdir(stage, { recursive: true }); for (const filename of rootFiles) { await fs.copyFile(path.join(root, filename), path.join(stage, filename)); } for (const dirname of sourceDirs) { await fs.cp(path.join(root, dirname), path.join(stage, dirname), { recursive: true, dereference: false }); } await fs.mkdir(path.join(stage, "workbook")); await fs.copyFile(workbookPath, path.join(stage, "workbook", workbookName)); const payloadFiles = (await filesBelow(stage)).sort(); const entries = []; for (const filename of payloadFiles) { const buffer = await fs.readFile(path.join(stage, filename)); entries.push({ path: filename, bytes: buffer.byteLength, sha256: sha256(buffer) }); } const manifest = { manifest_version: 1, dataset_version: VERSION, package_root: packageName, frozen_at: FIXED_TIME.toISOString(), file_count: entries.length, files: entries }; await fs.writeFile(path.join(stage, "PACKAGE-MANIFEST.json"), `${JSON.stringify(manifest, null, 2)}\n`); await fs.writeFile( path.join(stage, "PACKAGE-SHA256SUMS"), `${entries.map((entry) => `${entry.sha256} ${entry.path}`).join("\n")}\n` ); const allFiles = (await filesBelow(stage)).sort(); const allDirs = new Set([stage]); for (const filename of allFiles) { let current = path.dirname(path.join(stage, filename)); while (current.startsWith(stage)) { allDirs.add(current); if (current === stage) break; current = path.dirname(current); } await fs.utimes(path.join(stage, filename), FIXED_TIME, FIXED_TIME); } for (const directory of [...allDirs].sort((a, b) => b.length - a.length)) { await fs.utimes(directory, FIXED_TIME, FIXED_TIME); } const zipEntries = allFiles.map((filename) => `${packageName}/${filename}`); const result = spawnSync("zip", ["-X", "-q", tempZip, "-@"], { cwd: tempRoot, input: `${zipEntries.join("\n")}\n`, encoding: "utf8", env: { ...process.env, TZ: "UTC" } }); if (result.error) throw result.error; if (result.status !== 0) throw new Error(`zip failed (${result.status}): ${String(result.stderr || result.stdout).trim()}`); // The staging directory may be mounted on a different filesystem than the // repository. Copy to a same-directory temporary file, then rename atomically. await fs.copyFile(tempZip, atomicOutputPath); await fs.rename(atomicOutputPath, outputPath); const output = await fs.readFile(outputPath); process.stdout.write(`${JSON.stringify({ version: VERSION, file: path.relative(root, outputPath), bytes: output.byteLength, sha256: sha256(output), packaged_files: entries.length })}\n`); } finally { await fs.rm(atomicOutputPath, { force: true }); await fs.rm(tempRoot, { recursive: true, force: true }); }