-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·53 lines (47 loc) · 1.37 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const Elm = require("./elm.min.js").Elm;
function getPluginVersion() {
const file = path.resolve(__dirname, "package.json");
const nodeJson = JSON.parse(fs.readFileSync(file, "utf8"));
return nodeJson.version;
}
function getLibraryVersion() {
let version = "latest";
try {
const file = path.resolve(__dirname, "elm.json");
const elmJson = JSON.parse(fs.readFileSync(file, "utf8"));
version =
elmJson.dependencies.direct["eriktim/elm-protocol-buffers"] || version;
} catch (e) {
// not fatal
}
return version;
}
const pluginVersion = getPluginVersion();
const libraryVersion = getLibraryVersion();
const app = Elm.Main.init({
flags: {
plugin: pluginVersion,
library: libraryVersion,
},
});
function sendToGenerator(request) {
return new Promise((resolve) => {
const responseHandler = (response) => {
resolve(Buffer.from(response, "base64"));
app.ports.response.unsubscribe(responseHandler);
};
app.ports.response.subscribe(responseHandler);
app.ports.request.send(request.toString("base64"));
});
}
const chunks = [];
process.stdin.on("data", (chunk) => {
chunks.push(chunk);
});
process.stdin.on("end", () => {
const request = Buffer.concat(chunks);
sendToGenerator(request).then((response) => process.stdout.write(response));
});