]> git.immae.eu Git - github/fretlink/node-dhall-json-bin.git/blob - preinstall.js
3fa23826d370bbdd3021e1814e2595bf4e27f319
[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 semver = require("semver");
8 const tar = require("tar");
9 const unbz2 = require("unbzip2-stream");
10 const unzipper = require("unzipper");
11
12 const pkg = require("./package.json");
13 const bindir = path.join(__dirname, "bin");
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.");
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 }
25
26 const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`;
27
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 }