-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.js
144 lines (123 loc) · 4.03 KB
/
build.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
// NOTE: This script doesn't work on Windows.
// This script doesn't work for unknown reason. Use Go tool.
const fs = require("fs");
const path = require("path");
// pipe :: [Function] -> Function
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
// directoryFiles :: String -> [String]
function directoryFiles(dir, result = []) {
fs.readdirSync(dir).forEach(item =>
fs.statSync(path.join(dir, item)).isDirectory()
? directoryFiles(path.join(dir, item), result)
: result.push(path.join(dir, item))
);
return result;
}
// objectToString :: Object -> String
const objectToString = o => JSON.stringify(o, null, 2);
// jsonFiles :: [String] -> [String]
const jsonFiles = filesArray =>
filesArray.filter(f => f.trim().toLowerCase().slice(-5) === ".json");
// filesContent :: [String] -> [[String, Object]]
const filesContent = files =>
files.map(f => {
try {
return {
key: path.parse(f).dir.split("/")[1] + "." + path.parse(f).name,
value: JSON.parse(fs.readFileSync(f, "utf-8")),
};
} catch (err) {
throw Error(`Cannot process json file: ${f}\nError: ${err}`);
}
});
// arrayToObject :: [[String, Object]] -> Object
const arrayToObject = arrayOfObjects =>
arrayOfObjects.reduce((acc, c) => {
acc[c.key] = c.value;
return acc;
}, {});
// write :: String -> IO
const write = str =>
fs.writeFileSync(path.join("snippets", "snippets.json"), str);
const app = pipe(
directoryFiles,
jsonFiles,
filesContent,
arrayToObject,
objectToString,
write
);
// Entry Point
app("./nsroot"); // Generate snippet file
// Generate Documentation file
// TODO: Numerous minor parsing bugs to fix!
const snippets = JSON.parse(
fs.readFileSync(path.join(__dirname, "./snippets/snippets.json"), "utf-8")
);
const nameSpaces = [
...new Set(Object.keys(snippets).map(snippet => snippet.split(".")[0])),
].sort();
structure = new Map();
for (const ns of nameSpaces) {
members = Object.keys(snippets).filter(
snippet => snippet.split(".")[0] === ns
);
structure.set(ns, members.sort());
}
const toc = ["# Commands"];
const content = [];
for (const [ns, members] of structure.entries()) {
toc.push(`### ${ns}`);
for (const member of members) {
let entry = Object.entries(snippets).filter(entry => entry[0] === member);
const snippet = entry[0];
const snippetName = snippet[0];
const prefix = snippet[1].prefix;
const body = snippet[1].body;
const description = snippet[1].description;
// fs.writeFileSync(
// path.join(__dirname, "ZZZ.json"),
// JSON.stringify(entry[0], null, 2)
// );
const title = Array.isArray(prefix) ? prefix.join(" , ") : prefix;
const link = title.split(" ").join("-");
toc.push(` - [${title}](#${link})`);
content.push(`## ${title}`);
content.push(`${description} [↑](#Commands)`);
// const rgx = /\$\{\d+:(.*?)\}/g;
// let out = "```bash\n";
// let code = "";
// let y;
// if (Array.isArray(body)) {
// let tmp = body.map(line => line.split("\n").join(""));
// tmp = tmp.map(line => line.split("\t").join(" "));
// tmp = tmp.map(line => unescape(line).split("\\$").join("$"));
// tmp = tmp.map(line => line.split(/\\\$/g).join("$"));
// tmp = tmp.map(line => line.split(/\\\}/g).join("}"));
// tmp = tmp.map(line => {
// let y;
// while ((y = rgx.exec(line)) !== null) {
// line = line.split(y[0]).join(y[1]);
// }
// return line;
// });
// code = tmp.join("\n");
// } else {
// // single line body
// code = unescape(body).split("\\$").join("$");
// code = code.split(/\\\$/g).join("$");
// code = code.split("\n").join("");
// while ((y = rgx.exec(code)) !== null) {
// code = code.split(y[0]).join(y[1]);
// }
// code = code.split(/\\\}/g).join("}");
// }
// out += code.trim();
// out += "\n```\n\n";
// content.push(out);
}
}
fs.writeFileSync(
path.join(__dirname, "COMMANDS.md"),
toc.concat(content).join("\n\n")
);