]> git.immae.eu Git - github/fretlink/node-dhall-json-bin.git/blob - preinstall.js
071f1590eb2c9e922dc61abf5fd0083878e88855
[github/fretlink/node-dhall-json-bin.git] / preinstall.js
1 "use strict";
2
3 const fs = require("fs");
4 const path = require("path");
5
6 const { https } = require("follow-redirects");
7 const tar = require("tar");
8 const unbz2 = require("unbzip2-stream");
9 const unzipper = require("unzipper");
10
11 const pkg = require("./package.json");
12 const bindir = path.join(__dirname, "bin");
13
14 const trim = str => str && String(str).trim();
15
16 const dhallVersion = trim(pkg["dhall-version"] || process.env.DHALL_VERSION);
17 if (!dhallVersion) throw new Error("Missing DHALL_VERSION environment variable.");
18
19 const dhallJsonVersion = trim(pkg["dhall-json-version"] || process.env.DHALL_JSON_VERSION);
20 if (!dhallJsonVersion) throw new Error("Missing DHALL_JSON_VERSION environment variable.");
21
22 const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`;
23
24 const get = (archive, callback) => {
25 const url = `${release}-${archive}`;
26 return https.get(url, res => {
27 if (res.statusCode >= 400) throw new Error(`Error fetching ${url}: ${res.statusMessage}`);
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 }