File size: 1,570 Bytes
518343a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env node
import { createHash } from "node:crypto";
import { readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
import { join, relative, sep } from "node:path";

const buildDir = process.argv[2];
if (!buildDir) {
  console.error("usage: write-manifest.mjs <buildDir>");
  process.exit(2);
}

const MANIFEST_NAME = "MANIFEST.json";

function walk(dir, acc = []) {
  for (const entry of readdirSync(dir)) {
    const full = join(dir, entry);
    const st = statSync(full);
    if (st.isDirectory()) {
      walk(full, acc);
    } else if (st.isFile()) {
      acc.push(full);
    }
  }
  return acc;
}

const files = walk(buildDir)
  .map((abs) => relative(buildDir, abs).split(sep).join("/"))
  .filter((rel) => rel !== MANIFEST_NAME)
  .sort();

const entries = files.map((rel) => {
  const abs = join(buildDir, rel);
  const buf = readFileSync(abs);
  return {
    path: rel,
    size: buf.byteLength,
    sha256: createHash("sha256").update(buf).digest("hex"),
  };
});

const manifest = {
  name: "a11oy-uds",
  version: process.env.VERSION ?? "0.0.0",
  gitSha: process.env.GIT_SHA ?? "unknown",
  builtAt: process.env.BUILD_TS ?? new Date().toISOString(),
  hashAlgorithm: "sha256",
  sourcePackaged: process.env.SOURCE_PACKAGED === "1",
  fileCount: entries.length,
  totalBytes: entries.reduce((n, e) => n + e.size, 0),
  files: entries,
};

writeFileSync(
  join(buildDir, MANIFEST_NAME),
  JSON.stringify(manifest, null, 2) + "\n",
);

console.log(
  `[a11oy-uds] manifest: ${entries.length} files, ${manifest.totalBytes} bytes`,
);