This repository was archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgenerateDoc.ts
85 lines (76 loc) · 2.41 KB
/
generateDoc.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
75
76
77
78
79
80
81
82
83
84
85
import fs from "fs";
import path from "path";
import { CommandBuildElements } from "../src/lib/commandBuilder";
interface Command {
command: string;
subcommands: CommandBuildElements[];
}
interface CommandElement extends CommandBuildElements {
markdown?: string;
}
// get all the *.json under commands folder recursively.
// and get the content of its corresponding md file (if any).
// add this content into the json file
const getAllDecorators = (curDir: string): CommandBuildElements[] => {
const allFiles = fs.readdirSync(curDir);
const jsonFiles = allFiles.filter((f) => f.endsWith(".json"));
const arrJson: CommandElement[] = [];
jsonFiles.forEach((fileName) => {
const json = require(path.join(curDir, fileName)) as CommandElement;
if (!json.disabled) {
const mdPath = path.join(
curDir,
fileName.replace(/decorator\.json$/, "md")
);
if (fs.existsSync(mdPath)) {
json.markdown = fs.readFileSync(mdPath, "utf8");
}
arrJson.push(json);
}
});
return arrJson;
};
// get sub folders under commands folder.
const getSubDirectories = (curDir: string): string[] => {
return fs
.readdirSync(curDir)
.map((f) => path.join(curDir, f))
.filter((p) => fs.lstatSync(p).isDirectory());
};
// get the list of command from the array of
// command object. e.g `spk infra generate` and
// `spk deployment dashboard`
const listCommands = (
allCommands: Command[]
): { [key: string]: CommandBuildElements } => {
const mainCommands: { [key: string]: CommandBuildElements } = {};
allCommands.forEach((cmd) => {
let level1 = cmd.command;
if (level1 === "commands") {
level1 = "";
} else {
level1 = level1 + " ";
}
cmd.subcommands.forEach((c) => {
const key = `${level1}${c.command.replace(/ .+/, "")}`;
mainCommands[key] = c;
});
});
return mainCommands;
};
const dir = path.join(process.cwd(), "src", "commands");
const commandDirs = getSubDirectories(dir);
commandDirs.unshift(dir); // this is needed because `spk init` is outside `commands` folder
const commands: Command[] = commandDirs
.map((d) => {
return {
command: path.basename(d),
subcommands: getAllDecorators(d),
};
})
.filter((item) => item.subcommands.length > 0);
const mapCommands = listCommands(commands);
fs.writeFileSync(
path.join(process.cwd(), "docs", "commands", "data.json"),
JSON.stringify(mapCommands, null, 2)
);