aboutsummaryrefslogtreecommitdiffhomepage
path: root/preinstall.js
diff options
context:
space:
mode:
Diffstat (limited to 'preinstall.js')
-rw-r--r--preinstall.js36
1 files changed, 31 insertions, 5 deletions
diff --git a/preinstall.js b/preinstall.js
index 1b859fc..071f159 100644
--- a/preinstall.js
+++ b/preinstall.js
@@ -1,10 +1,15 @@
1"use strict"; 1"use strict";
2 2
3const fs = require("fs");
4const path = require("path");
5
3const { https } = require("follow-redirects"); 6const { https } = require("follow-redirects");
4const tar = require("tar"); 7const tar = require("tar");
5const unbz2 = require("unbzip2-stream"); 8const unbz2 = require("unbzip2-stream");
9const unzipper = require("unzipper");
6 10
7const pkg = require("./package.json"); 11const pkg = require("./package.json");
12const bindir = path.join(__dirname, "bin");
8 13
9const trim = str => str && String(str).trim(); 14const trim = str => str && String(str).trim();
10 15
@@ -16,8 +21,29 @@ if (!dhallJsonVersion) throw new Error("Missing DHALL_JSON_VERSION environment v
16 21
17const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`; 22const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`;
18 23
19const url = `${release}-x86_64-linux.tar.bz2`; 24const get = (archive, callback) => {
20https.get(url, res => { 25 const url = `${release}-${archive}`;
21 if (res.statusCode >= 400) throw new Error(`Error fetching ${url}: ${res.statusMessage}`); 26 return https.get(url, res => {
22 res.pipe(unbz2()).pipe(tar.x({ C: __dirname })); 27 if (res.statusCode >= 400) throw new Error(`Error fetching ${url}: ${res.statusMessage}`);
23}); 28 return callback(res);
29 });
30};
31
32if (process.platform === "win32") {
33 get("x86_64-windows.zip", res =>
34 res.pipe(unzipper.Extract({ path: bindir }))
35 );
36} else {
37 get("x86_64-linux.tar.bz2", res =>
38 res.pipe(unbz2()).pipe(tar.x({ C: __dirname }).on("finish", () => {
39 fs.readdir(bindir, (err, names) => {
40 if (err) throw err;
41 for (const name of names) {
42 fs.rename(path.join(bindir, name), path.join(bindir, name + ".exe"), err => {
43 if (err) throw err;
44 });
45 }
46 });
47 }))
48 );
49}