]> git.immae.eu Git - github/fretlink/node-dhall-json-bin.git/blobdiff - preinstall.js
Add Windows support
[github/fretlink/node-dhall-json-bin.git] / preinstall.js
index 1b859fc42ec98503bbb03a94e0983105c3355cb8..071f1590eb2c9e922dc61abf5fd0083878e88855 100644 (file)
@@ -1,10 +1,15 @@
 "use strict";
 
+const fs = require("fs");
+const path = require("path");
+
 const { https } = require("follow-redirects");
 const tar = require("tar");
 const unbz2 = require("unbzip2-stream");
+const unzipper = require("unzipper");
 
 const pkg = require("./package.json");
+const bindir = path.join(__dirname, "bin");
 
 const trim = str => str && String(str).trim();
 
@@ -16,8 +21,29 @@ if (!dhallJsonVersion) throw new Error("Missing DHALL_JSON_VERSION environment v
 
 const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`;
 
-const url = `${release}-x86_64-linux.tar.bz2`;
-https.get(url, res => {
-  if (res.statusCode >= 400) throw new Error(`Error fetching ${url}: ${res.statusMessage}`);
-  res.pipe(unbz2()).pipe(tar.x({ C: __dirname }));
-});
+const get = (archive, callback) => {
+  const url = `${release}-${archive}`;
+  return https.get(url, res => {
+    if (res.statusCode >= 400) throw new Error(`Error fetching ${url}: ${res.statusMessage}`);
+    return callback(res);
+  });
+};
+
+if (process.platform === "win32") {
+  get("x86_64-windows.zip", res =>
+    res.pipe(unzipper.Extract({ path: bindir }))
+  );
+} else {
+  get("x86_64-linux.tar.bz2", res =>
+    res.pipe(unbz2()).pipe(tar.x({ C: __dirname }).on("finish", () => {
+      fs.readdir(bindir, (err, names) => {
+        if (err) throw err;
+        for (const name of names) {
+          fs.rename(path.join(bindir, name), path.join(bindir, name + ".exe"), err => {
+            if (err) throw err;
+          });
+        }
+      });
+    }))
+  );
+}