aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--preinstall.js50
1 files changed, 45 insertions, 5 deletions
diff --git a/preinstall.js b/preinstall.js
index 071f159..cb843a9 100644
--- a/preinstall.js
+++ b/preinstall.js
@@ -11,13 +11,53 @@ const unzipper = require("unzipper");
11const pkg = require("./package.json"); 11const pkg = require("./package.json");
12const bindir = path.join(__dirname, "bin"); 12const bindir = path.join(__dirname, "bin");
13 13
14const trim = str => str && String(str).trim(); 14const formatVersionKey = name =>
15 `${name}-version`;
16const toSnakeCase = name =>
17 name.replace(/-/g, '_');
18const formatNpmCfgVar = name =>
19 toSnakeCase(name.toLowerCase());
20const formatEnvVar = name =>
21 toSnakeCase(name.toUpperCase());
22const prefixNpmCfgKey = key =>
23 `${pkg.name}:${key}`;
15 24
16const dhallVersion = trim(pkg["dhall-version"] || process.env.DHALL_VERSION); 25const readVersionWith = (lookup, discard = () => {}) => name => {
17if (!dhallVersion) throw new Error("Missing DHALL_VERSION environment variable."); 26 const key = formatVersionKey(name);
27 const version = lookup(key);
28 return version ? {
29 get version() { return String(version).trim() },
30 discard() { discard(key, version) },
31 orElse(alt) { alt.discard(); return this }
32 } : {
33 get version() {
34 throw new Error(`Missing \`${name}\` version! You can provide it as a \`${prefixNpmCfgKey(key)}\` npm configuration parameter or as a \`${formatEnvVar(key)}\` environment variable.`);
35 },
36 discard() {},
37 orElse(alt) { return alt }
38 };
39};
40
41const readPkgVersion = readVersionWith(key => pkg[key]);
42const readCfgVersion = readVersionWith(key => {
43 return process.env[`npm_config_${formatNpmCfgVar(pkg.name)}_${formatNpmCfgVar(key)}`];
44}, (key, version) => {
45 console.warn(`Ignoring \`${prefixNpmCfgKey(key)}\` npm configuration parameter (${version}).`);
46});
47const readEnvVersion = readVersionWith(key => {
48 return process.env[formatEnvVar(key)];
49}, (key, version) => {
50 console.warn(`Ignoring \`${formatEnvVar(key)}\` environment variable (${version}).`);
51});
52
53const readVersion = name =>
54 readPkgVersion(name)
55 .orElse(readEnvVersion(name))
56 .orElse(readCfgVersion(name))
57 .version;
18 58
19const dhallJsonVersion = trim(pkg["dhall-json-version"] || process.env.DHALL_JSON_VERSION); 59const dhallVersion = readVersion("dhall");
20if (!dhallJsonVersion) throw new Error("Missing DHALL_JSON_VERSION environment variable."); 60const dhallJsonVersion = readVersion("dhall-json");
21 61
22const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`; 62const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`;
23 63