diff options
Diffstat (limited to 'preinstall.js')
-rw-r--r-- | preinstall.js | 36 |
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 | ||
3 | const fs = require("fs"); | ||
4 | const path = require("path"); | ||
5 | |||
3 | const { https } = require("follow-redirects"); | 6 | const { https } = require("follow-redirects"); |
4 | const tar = require("tar"); | 7 | const tar = require("tar"); |
5 | const unbz2 = require("unbzip2-stream"); | 8 | const unbz2 = require("unbzip2-stream"); |
9 | const unzipper = require("unzipper"); | ||
6 | 10 | ||
7 | const pkg = require("./package.json"); | 11 | const pkg = require("./package.json"); |
12 | const bindir = path.join(__dirname, "bin"); | ||
8 | 13 | ||
9 | const trim = str => str && String(str).trim(); | 14 | const 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 | ||
17 | const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`; | 22 | const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`; |
18 | 23 | ||
19 | const url = `${release}-x86_64-linux.tar.bz2`; | 24 | const get = (archive, callback) => { |
20 | https.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 | |||
32 | if (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 | } | ||