]> git.immae.eu Git - github/fretlink/node-dhall-json-bin.git/blame - preinstall.js
Add `json-to-dhall`
[github/fretlink/node-dhall-json-bin.git] / preinstall.js
CommitLineData
a4e6dec5
CS
1"use strict";
2
03aaa7d3
CS
3const fs = require("fs");
4const path = require("path");
5
a4e6dec5 6const { https } = require("follow-redirects");
6337a3be 7const semver = require("semver");
a4e6dec5
CS
8const tar = require("tar");
9const unbz2 = require("unbzip2-stream");
03aaa7d3 10const unzipper = require("unzipper");
a4e6dec5
CS
11
12const pkg = require("./package.json");
03aaa7d3 13const bindir = path.join(__dirname, "bin");
a4e6dec5
CS
14
15const trim = str => str && String(str).trim();
16
17const dhallVersion = trim(pkg["dhall-version"] || process.env.DHALL_VERSION);
18if (!dhallVersion) throw new Error("Missing DHALL_VERSION environment variable.");
19
20const dhallJsonVersion = trim(pkg["dhall-json-version"] || process.env.DHALL_JSON_VERSION);
21if (!dhallJsonVersion) throw new Error("Missing DHALL_JSON_VERSION environment variable.");
6337a3be
CS
22if (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
26const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`;
27
03aaa7d3
CS
28const 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
36if (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}