aboutsummaryrefslogblamecommitdiffhomepage
path: root/preinstall.js
blob: 3fa23826d370bbdd3021e1814e2595bf4e27f319 (plain) (tree)
1
2
3
4
5
6
7
8
9

             


                             
                                              
                                 

                                        
                                     

                                      
                                           







                                                                                           


                                                                                                                                                  


                                                                                                                               

























                                                                                              
"use strict";

const fs = require("fs");
const path = require("path");

const { https } = require("follow-redirects");
const semver = require("semver");
const tar = require("tar");
const unbz2 = require("unbzip2-stream");
const unzipper = require("unzipper");

const pkg = require("./package.json");
const bindir = path.join(__dirname, "bin");

const trim = str => str && String(str).trim();

const dhallVersion = trim(pkg["dhall-version"] || process.env.DHALL_VERSION);
if (!dhallVersion) throw new Error("Missing DHALL_VERSION environment variable.");

const dhallJsonVersion = trim(pkg["dhall-json-version"] || process.env.DHALL_JSON_VERSION);
if (!dhallJsonVersion) throw new Error("Missing DHALL_JSON_VERSION environment variable.");
if (semver.valid(dhallJsonVersion) && semver.lt(dhallJsonVersion, "1.2.8")) {
  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\`.`);
}

const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`;

const get = (archive, callback) => {
  const url = `${release}-${archive}`;
  return https.get(url, res => {
    if (res.statusCode >= 400) throw new Error(`Error fetching ${url}: ${res.statusMessage}`);
    return callback(res);
  });
};

if (process.platform === "win32") {
  get("x86_64-windows.zip", res =>
    res.pipe(unzipper.Extract({ path: bindir }))
  );
} else {
  get("x86_64-linux.tar.bz2", res =>
    res.pipe(unbz2()).pipe(tar.x({ C: __dirname }).on("finish", () => {
      fs.readdir(bindir, (err, names) => {
        if (err) throw err;
        for (const name of names) {
          fs.rename(path.join(bindir, name), path.join(bindir, name + ".exe"), err => {
            if (err) throw err;
          });
        }
      });
    }))
  );
}