summaryrefslogtreecommitdiff
path: root/gen-docs
blob: 405cba9b617cb0ae93ef359311a9d9a06b9b8591 (plain)
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
#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"

README_HEADER_PATH="$SCRIPT_DIR/README_HEADER.md"
TARGET_README_PATH="$SCRIPT_DIR/README.md"

SCRIPT_DIR="$SCRIPT_DIR" \
README_HEADER_PATH="$README_HEADER_PATH" \
TARGET_README_PATH="$TARGET_README_PATH" \
node --input-type=module <<'EOF'
import { execFileSync } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
import path from "node:path";

const placeholder = "___DOCS___";
const scriptDir = process.env.SCRIPT_DIR;
const headerPath = process.env.README_HEADER_PATH;
const targetPath = process.env.TARGET_README_PATH;

if (!scriptDir || !headerPath || !targetPath) {
  throw new Error("missing required README path environment variables");
}

const generatorPath = path.join(scriptDir, "docs", "src", "generate.ts");
const docs = execFileSync(
  "node",
  ["--experimental-strip-types", generatorPath, "--stdout"],
  { encoding: "utf8" },
).trimEnd();

const header = readFileSync(headerPath, "utf8");

if (!header.includes(placeholder)) {
  throw new Error(`placeholder '${placeholder}' not found in ${headerPath}`);
}

const combined = header.split(placeholder).join(docs);
const output = combined.endsWith("\n") ? combined : `${combined}\n`;

writeFileSync(targetPath, output, "utf8");
console.log(`[docs] wrote ${targetPath}`);
EOF