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