]>
Commit | Line | Data |
---|---|---|
a4e6dec5 CS |
1 | "use strict"; |
2 | ||
03aaa7d3 CS |
3 | const fs = require("fs"); |
4 | const path = require("path"); | |
5 | ||
a4e6dec5 | 6 | const { https } = require("follow-redirects"); |
6337a3be | 7 | const semver = require("semver"); |
a4e6dec5 CS |
8 | const tar = require("tar"); |
9 | const unbz2 = require("unbzip2-stream"); | |
03aaa7d3 | 10 | const unzipper = require("unzipper"); |
a4e6dec5 CS |
11 | |
12 | const pkg = require("./package.json"); | |
03aaa7d3 | 13 | const bindir = path.join(__dirname, "bin"); |
a4e6dec5 CS |
14 | |
15 | const trim = str => str && String(str).trim(); | |
16 | ||
17 | const dhallVersion = trim(pkg["dhall-version"] || process.env.DHALL_VERSION); | |
18 | if (!dhallVersion) throw new Error("Missing DHALL_VERSION environment variable."); | |
19 | ||
20 | const dhallJsonVersion = trim(pkg["dhall-json-version"] || process.env.DHALL_JSON_VERSION); | |
21 | if (!dhallJsonVersion) throw new Error("Missing DHALL_JSON_VERSION environment variable."); | |
6337a3be CS |
22 | if (semver.valid(dhallJsonVersion) && semver.lt(dhallJsonVersion, "1.2.8")) { |
23 | throw new Error(`This release of the \`${pkg.name}\` npm package installs \`json-to-dhall\`, which isn’t provided by \`dhall-json@<1.2.8\`.`); | |
24 | } | |
a4e6dec5 CS |
25 | |
26 | const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`; | |
27 | ||
03aaa7d3 CS |
28 | const get = (archive, callback) => { |
29 | const url = `${release}-${archive}`; | |
30 | return https.get(url, res => { | |
31 | if (res.statusCode >= 400) throw new Error(`Error fetching ${url}: ${res.statusMessage}`); | |
32 | return callback(res); | |
33 | }); | |
34 | }; | |
35 | ||
36 | if (process.platform === "win32") { | |
37 | get("x86_64-windows.zip", res => | |
38 | res.pipe(unzipper.Extract({ path: bindir })) | |
39 | ); | |
40 | } else { | |
41 | get("x86_64-linux.tar.bz2", res => | |
42 | res.pipe(unbz2()).pipe(tar.x({ C: __dirname }).on("finish", () => { | |
43 | fs.readdir(bindir, (err, names) => { | |
44 | if (err) throw err; | |
45 | for (const name of names) { | |
46 | fs.rename(path.join(bindir, name), path.join(bindir, name + ".exe"), err => { | |
47 | if (err) throw err; | |
48 | }); | |
49 | } | |
50 | }); | |
51 | })) | |
52 | ); | |
53 | } |