move index script

This commit is contained in:
Barrett Ruth 2023-01-01 20:16:37 -06:00
parent 297e13bc78
commit 6627489a6d
2 changed files with 1 additions and 1 deletions

52
index.js Normal file
View file

@ -0,0 +1,52 @@
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 init = async () => {
const [path, filetype] = process.argv.slice(2);
const lang =
filetype.substring(0, "typescript".length) === "typescript"
? Lang.TYPESCRIPT
: Lang.JAVASCRIPT;
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 {}