-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclosure_cfg.js
85 lines (68 loc) · 2.19 KB
/
closure_cfg.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
const fs = require("fs");
const https = require("https");
const { URL, URLSearchParams } = require("url");
const js = fs.readFileSync(`${__dirname}/${process.argv[2]}`, {
encoding: "utf-8",
});
const params = new URLSearchParams([
["externs", ""],
["input0", ""],
["input1", js],
["conformanceConfig", ""],
["CHECK_TYPES", "on"],
["REWRITE_MODULES_BEFORE_TYPECHECKING", "on"],
["CLOSURE_PASS", "on"],
["PRESERVE_TYPE_ANNOTATIONS", "on"],
["PRETTY_PRINT", "on"],
]);
function fetch(url) {
const chunks = [];
return new Promise((resolve, reject) => {
const req = https.request(
url,
{
method: "POST",
},
(res) => {
res.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
res.on("error", (err) => reject(err));
res.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
}
);
req.on("error", (err) => reject(err));
req.end();
});
}
function recolour(dot) {
return dot
.replaceAll(
'[label="UNCOND", fontcolor="red", weight=0.01, color="red"]',
'[label="UNCOND", fontcolor="purple", weight=0.01, color="purple"]'
)
.replaceAll(
'[label="ON_FALSE", fontcolor="red", weight=0.01, color="red"]',
'[label="ON_FALSE", fontcolor="orange", weight=0.01, color="orange"]'
) .replaceAll(
'[label="ON_TRUE", fontcolor="red", weight=0.01, color="red"]',
'[label="ON_TRUE", fontcolor="green", weight=0.01, color="green"]'
);
}
// function removeDeadNodes(dot) {
// return dot.replaceAll(/node\d+ -> node\d+ \[weight=1\];/g, "");
// }
(async function () {
const url = new URL("https://closure-compiler-debugger.appspot.com/compile");
url.search = params.toString();
const res = await fetch(url);
const startTag = '<script type="text/dot" class="dot">';
const start = res.indexOf(startTag) + startTag.length;
const endTag = "</script>";
const end = res.indexOf(endTag, start);
let dot = res.substring(start, end);
// let dot = fs.readFileSync(`${__dirname}/closure_cfg.dot`, {
// encoding: "utf8",
// });
dot = recolour(dot);
// dot = removeDeadNodes(dot);
await fs.promises.writeFile(`${__dirname}/closure_cfg.dot`, dot);
})();