-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-man.js
211 lines (163 loc) · 4.86 KB
/
generate-man.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Script to generate MANUAL.md
"use strict";
const Path = require("path");
const FS = require("fs");
const ChildProcess = require("child_process");
const PMV_CLI_BIN = Path.resolve(__dirname, "target", "release", "pmv-cli");
const MAN_MD_FILE = Path.resolve(__dirname, "MANUAL.md");
async function callPMV_CLI(args) {
return new Promise((resolve, reject) => {
try {
const p = ChildProcess.spawn(PMV_CLI_BIN, args);
let result = "";
p.stdout.on("data", data => {
result += data.toString();
});
p.stdout.on("end", () => {
resolve(result);
});
} catch (e) {
reject(e);
}
});
}
function writeResult(res) {
FS.writeFileSync(MAN_MD_FILE, res);
}
function parseHelp(text) {
const lines = text.split("\n");
const commandDesc = lines[0] || "";
let usage = "";
let commands = [];
let args = [];
let options = [];
let current = "";
for (let i = 1; i < lines.length; i++) {
let line = lines[i].trim();
if (!line) {
current = "";
}
if (line.startsWith("Usage:") && !current) {
usage = line.split(":").slice(1).join(":").trim();
continue;
} else if (line === "Commands:") {
current = "c";
continue;
} else if (line === "Options:") {
current = "o";
continue;
} else if (line === "Arguments:") {
current = "a";
continue;
}
const spl = line.split(" ");
if (current === "c") {
// Command
const commandName = spl[0].trim();
const commandDesc = spl.slice(1).join(" ").trim();
if (commandName !== "help") {
commands.push({
name: commandName,
desc: commandDesc,
});
}
} else if (current === "o") {
// Option
const optionSyntax = spl[0].trim();
const optionDesc = spl.slice(1).join(" ").trim();
options.push({
syntax: optionSyntax,
desc: optionDesc,
});
} else if (current === "a") {
// Option
const argName = spl[0].trim();
const argDesc = spl.slice(1).join(" ").trim();
args.push({
name: argName,
desc: argDesc,
});
}
}
return {
desc: commandDesc,
usage: usage,
args: args,
commands: commands,
options: options,
};
}
function toMd(ph, cmdStack) {
const baseLink = "#command" + (cmdStack.length > 0 ? ("-" + cmdStack.join("-")) : "");
const lines = [];
lines.push(ph.desc);
lines.push("");
lines.push("<ins>**Usage:**</ins>");
lines.push("");
lines.push("```");
lines.push(ph.usage)
lines.push("```");
lines.push("");
if (ph.commands.length > 0) {
lines.push("<ins>**Commands:**</ins>");
lines.push("");
lines.push("| Command | Description |");
lines.push("| --- | --- |");
for (let cmd of ph.commands) {
lines.push(`| [${cmd.name}](${baseLink}-${cmd.name}) | ${cmd.desc} |`);
}
lines.push("");
}
if (ph.args.length > 0) {
lines.push("<ins>**Arguments:**</ins>");
lines.push("");
lines.push("| Argument | Description |");
lines.push("| --- | --- |");
for (let arg of ph.args) {
lines.push(`| \`${arg.name}\` | ${arg.desc} |`);
}
lines.push("");
}
if (ph.options.length > 0) {
lines.push("<ins>**Options:**</ins>");
lines.push("");
lines.push("| Option | Description |");
lines.push("| --- | --- |");
for (let opt of ph.options) {
lines.push(`| \`${opt.syntax}\` | ${opt.desc} |`);
}
lines.push("");
}
return lines;
}
async function resolveRecursive(level, lines, cmdStack) {
let prefixTitle = "#";
for (let i = 0; i < level; i++) {
prefixTitle += "#";
}
if (cmdStack.length > 0) {
lines.push(prefixTitle + " Command: " + cmdStack.join(" "));
} else {
lines.push(prefixTitle + " Manual");
}
lines.push("");
const baseHelp = parseHelp(await callPMV_CLI(cmdStack.concat(["--help"])));
toMd(baseHelp, cmdStack).forEach(line => {
lines.push(line);
});
for (let cmd of baseHelp.commands) {
if (cmd.name === "help") {
continue;
}
await resolveRecursive(level + 1, lines, cmdStack.concat(cmd.name));
}
}
async function main() {
const lines = [];
await resolveRecursive(0, lines, []);
writeResult(lines.join("\n"));
}
main().catch(e => {
console.error(e);
process.exit(1);
});