-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathgen_cli_docs.ts
74 lines (57 loc) · 1.88 KB
/
gen_cli_docs.ts
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
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run
/**
* Generates markdown documentation for the game executable.
*/
import { chunk } from "$std/collections/chunk.ts"
export type Flag = { option: string; desc: string }
export type Section = { title: string; flags: Flag[] }
/**
* Use simple heuristics to parse the command line help text.
*
* ### Heuristics
* - first line is the section title
* - each flags and description is on a separate line
*/
export const parseSection = (section: string): Section => {
// first line is the title
const [title, ...lines] = section.split("\n")
const flags = chunk(lines, 2).map(([option, desc]) => ({ option, desc: desc.trim() }))
return { title: title.replace(":", ""), flags }
}
export const flagToMarkdown = ({ option, desc }: Flag): string => /*md*/ `
### \`${option}\`
${desc}.`
export const sectionToMarkdown = ({ title, flags }: Section): string => /*md*/ `
## ${title}
${flags.map(flagToMarkdown).join("\n")}`
const toMarkdown = (text: string): string => {
const sections = text.trim().split("\n\n").map(parseSection).map(sectionToMarkdown).join("\n")
return /*md*/ `\
---
title: CLI Options
editUrl: false
sidebar:
badge:
text: Generated
status: note
---
:::note
This page is auto-generated from \`tools/gen_cli_docs.ts\` and should not be edited directly.
:::
The game executable can not only run your favorite roguelike,
but also provides a number of command line options to help modders and developers.
---
${sections}
`
}
if (import.meta.main) {
const command = new Deno.Command("./cataclysm-bn-tiles", { args: ["--help"] })
const { stdout } = await command.output()
const text = new TextDecoder().decode(stdout)
const result = toMarkdown(text)
const docsUrl = new URL(
"../doc/src/content/docs/en/dev/reference/cli_options.md",
import.meta.url,
)
await Deno.writeTextFile(docsUrl, result)
}