UNPKG

610 BJavaScriptView Raw
1const pro = require("util").promisify;
2const fs = require("fs");
3
4function codeGen(level = 0) {
5
6 lines = [];
7
8 return {
9
10 wl(...words) {
11 lines.push({
12 level,
13 string: words.join(" ")
14 });
15 return this;
16 },
17
18 begin(...words) {
19 this.wl(...words);
20 level++;
21 return this;
22 },
23
24 end(...words) {
25 level--;
26 this.wl(...words);
27 return this;
28 },
29
30 toString() {
31 return lines.map(line => " ".repeat(line.level * 2) + line.string).join("\n");
32 },
33
34 async toFile(fileName) {
35 pro(fs.writeFile)(fileName, this.toString(), "utf8");
36 }
37 };
38};
39
40module.exports = codeGen;
\No newline at end of file