-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
56 lines (42 loc) · 1.12 KB
/
index.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
const { cleanup, importCost, Lang } = require("import-cost");
process.stdin.setEncoding("utf8");
const receive = async () => {
let result = "";
for await (const chunk of process.stdin) result += chunk;
return result;
};
// Pad data with '|' for combined chunks to be parsable
const give = (data) =>
process.nextTick(() => process.stdout.write(`${JSON.stringify(data)}|`));
const filetypes = new Map([
["j", Lang.JAVASCRIPT],
["s", Lang.SVELTE],
["t", Lang.TYPESCRIPT],
["v", Lang.VUE],
]);
const init = async () => {
const [path, filetype] = process.argv.slice(2);
const lang = filetypes.get(filetype[0]);
const contents = await receive();
const emitter = importCost(path, contents, lang);
emitter.on("error", (error) => {
give({ type: "error", error });
cleanup();
});
emitter.on("calculated", ({ line, string, size, gzip }) => {
give({
type: "calculated",
data: { line, string, size, gzip },
});
});
// Send done to ensure job stdin stays open
emitter.on("done", (_) => {
give({
type: "done",
});
cleanup();
});
};
try {
init();
} catch {}