-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate-github-readme.ts
More file actions
77 lines (65 loc) · 2.31 KB
/
Copy pathgenerate-github-readme.ts
File metadata and controls
77 lines (65 loc) · 2.31 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bun
import { readFileSync, writeFileSync } from "node:fs"
import { resolve } from "node:path"
type ManifestVariable = {
name: string
required?: boolean
sensitive?: boolean
defaultValue?: string
githubSource: string
description: string
}
type Manifest = {
variables: ManifestVariable[]
}
const START_MARKER = "<!-- GENERATED_ENV_TABLE_START -->"
const END_MARKER = "<!-- GENERATED_ENV_TABLE_END -->"
const MANIFEST_PATH = resolve(import.meta.dir, "..", "env", "deploy.manifest.json")
const README_PATH = resolve(import.meta.dir, "..", "README.md")
function readManifest(path: string) {
const parsed = JSON.parse(readFileSync(path, "utf8")) as Manifest
if (!Array.isArray(parsed.variables)) {
throw new Error(`Manifest must contain variables array: ${path}`)
}
return parsed.variables
}
function markdownEscape(text: string) {
return text.replaceAll("|", "\\|")
}
function buildGeneratedBlock(vars: ManifestVariable[]) {
const rows = vars.map((v) => {
const required = v.required ? "yes" : "no"
const notes = [
v.description,
v.defaultValue !== undefined ? `Default: \`${v.defaultValue}\`.` : "",
v.sensitive ? "Sensitive." : "",
]
.filter(Boolean)
.join(" ")
return `| \`${v.name}\` | \`${v.githubSource}\` | ${required} | ${markdownEscape(notes)} |`
})
return [
START_MARKER,
"<!-- This block is GENERATED. Edit .github/env/deploy.manifest.json and run `bun .github/scripts/generate-github-readme.ts`. -->",
"| Name | Source | Required | Description |",
"| --- | --- | --- | --- |",
...rows,
END_MARKER,
].join("\n")
}
function main() {
const variables = readManifest(MANIFEST_PATH)
const generatedBlock = buildGeneratedBlock(variables)
const readme = readFileSync(README_PATH, "utf8")
if (!readme.includes(START_MARKER) || !readme.includes(END_MARKER)) {
throw new Error(`README is missing generation markers: ${START_MARKER} / ${END_MARKER}`)
}
const start = readme.indexOf(START_MARKER)
const end = readme.indexOf(END_MARKER)
const before = readme.slice(0, start).trimEnd()
const after = readme.slice(end + END_MARKER.length).trimStart()
const updated = `${before}\n\n${generatedBlock}\n\n${after}\n`
writeFileSync(README_PATH, updated, "utf8")
process.stdout.write(`Updated ${README_PATH}\n`)
}
main()