File size: 1,546 Bytes
bdd8da5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env node

/** Materialize the deterministic, passing audit report in both compatibility locations. */

import fs from "node:fs/promises";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";

const here = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(here, "..");
const dataDir = path.join(root, "data");
const result = spawnSync(process.execPath, [path.join(here, "audit-v03.mjs"), dataDir], {
  cwd: root,
  encoding: "utf8",
  maxBuffer: 32 * 1024 * 1024
});
if (result.error) throw result.error;
if (result.status !== 0) {
  process.stderr.write(result.stdout || result.stderr || "Audit failed without output\n");
  process.exitCode = result.status ?? 1;
} else {
  const report = JSON.parse(result.stdout);
  if (report.auditor !== "agency-transfer-election-cases-v0.4.0/audit-v04" || report.status !== "pass") {
    throw new Error("Refusing to store an audit with an unexpected auditor ID or non-pass status");
  }
  const stable = `${JSON.stringify(report, null, 2)}\n`;
  const temporary = [];
  for (const name of ["audit.json", "audit-v03.json"]) {
    const temp = path.join(dataDir, `.${name}.${process.pid}.tmp`);
    await fs.writeFile(temp, stable);
    temporary.push([temp, path.join(dataDir, name)]);
  }
  for (const [temp, target] of temporary) await fs.rename(temp, target);
  process.stdout.write(`${JSON.stringify({ auditor: report.auditor, files: temporary.map(([, target]) => path.relative(root, target)) })}\n`);
}