-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdocify.js
93 lines (81 loc) · 3.75 KB
/
docify.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
const NODE_DIR = "./node_modules";
const README_FILE = "./docs/README.md";
const SUMMARY_FILE = "./docs/SUMMARY.md";
const EXCLUDE_FILE = "./docs/exclude.txt";
const TEMPLATE_DIR = "./docs/templates";
const PAGES_DIR = "./docs/pages";
const CONTRACT_INPUT_DIR = "./contracts";
const CONTRACT_OUTPUT_DIR = "./docs/pages/governance-contracts";
const fs = require("fs");
const path = require("path");
const { config } = require("hardhat");
const spawnSync = require("child_process").spawnSync;
const excludeInputList = lines(EXCLUDE_FILE).map(line => CONTRACT_INPUT_DIR + "/" + line);
const excludeOutputList = lines(EXCLUDE_FILE).map(line => CONTRACT_OUTPUT_DIR + "/" + line);
const relativePath = path.relative(path.dirname(SUMMARY_FILE), PAGES_DIR);
function lines(pathName) {
return fs.readFileSync(pathName, {encoding: "utf8"}).split("\r").join("").split("\n");
}
function formatTitle(title) {
return title.replace(/\b\w/g, l => l.toUpperCase()).replace(/-|_/g, " ")
}
function scan(pathName, indentation) {
if (!excludeInputList.includes(pathName)) {
if (fs.lstatSync(pathName).isDirectory()) {
if(fs.existsSync(pathName + "/README.md")) {
const link = pathName.slice(PAGES_DIR.length) + "/README.md";
fs.appendFileSync(SUMMARY_FILE, indentation + "* [" + formatTitle(path.basename(pathName)) + "](" + relativePath + link + ")\n");
} else {
fs.appendFileSync(SUMMARY_FILE, indentation + "* " + formatTitle(path.basename(pathName)) + "\n");
}
for (const fileName of fs.readdirSync(pathName))
scan(pathName + "/" + fileName, indentation + " ");
}
else if (pathName.endsWith(".md") && !pathName.endsWith("README.md")) {
const text = formatTitle(path.basename(pathName).slice(0, -3));
const link = pathName.slice(PAGES_DIR.length);
fs.appendFileSync(SUMMARY_FILE, indentation + "* [" + text + "](" + relativePath + link + ")\n");
}
}
}
function fix(pathName) {
if (!excludeOutputList.includes(pathName)) {
if (fs.lstatSync(pathName).isDirectory()) {
for (const fileName of fs.readdirSync(pathName))
fix(pathName + "/" + fileName);
}
else if (pathName.endsWith(".md")) {
fs.writeFileSync(pathName, lines(pathName).filter(line => line.trim().length > 0).join("\n\n") + "\n");
}
} else {
if (fs.lstatSync(pathName).isDirectory()) {
fs.rmdirSync(pathName, { recursive: true });
} else {
fs.unlinkSync(pathName);
}
}
}
console.log("Creating .gitbook.yaml file...")
fs.writeFileSync (".gitbook.yaml", "root: ./\n");
fs.appendFileSync(".gitbook.yaml", "structure:\n");
fs.appendFileSync(".gitbook.yaml", " readme: " + README_FILE + "\n");
fs.appendFileSync(".gitbook.yaml", " summary: " + SUMMARY_FILE + "\n");
console.log("Generating contract documentation...")
const args = [
NODE_DIR + "/solidity-docgen/dist/cli.js",
"--input=" + CONTRACT_INPUT_DIR,
"--output=" + CONTRACT_OUTPUT_DIR,
"--templates=" + TEMPLATE_DIR,
"--solc-module=" + NODE_DIR + "/hardhat/node_modules/solc",
"--solc-settings=" + JSON.stringify(config.settings)
];
const result = spawnSync("node", args, {stdio: ["inherit", "inherit", "pipe"]});
if (result.stderr.length > 0)
throw new Error(result.stderr);
console.log("Cleaning up documentation...")
fix(PAGES_DIR);
console.log("Generating SUMMARY.md file (Table of Contents)...")
fs.writeFileSync (SUMMARY_FILE, "# Summary\n");
for (const fileName of fs.readdirSync(PAGES_DIR))
scan(PAGES_DIR + "/" + fileName, "");
console.log("Documentation finalized.")