diff options
-rw-r--r-- | preinstall.js | 50 |
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"); | |||
11 | const pkg = require("./package.json"); | 11 | const pkg = require("./package.json"); |
12 | const bindir = path.join(__dirname, "bin"); | 12 | const bindir = path.join(__dirname, "bin"); |
13 | 13 | ||
14 | const trim = str => str && String(str).trim(); | 14 | const formatVersionKey = name => |
15 | `${name}-version`; | ||
16 | const toSnakeCase = name => | ||
17 | name.replace(/-/g, '_'); | ||
18 | const formatNpmCfgVar = name => | ||
19 | toSnakeCase(name.toLowerCase()); | ||
20 | const formatEnvVar = name => | ||
21 | toSnakeCase(name.toUpperCase()); | ||
22 | const prefixNpmCfgKey = key => | ||
23 | `${pkg.name}:${key}`; | ||
15 | 24 | ||
16 | const dhallVersion = trim(pkg["dhall-version"] || process.env.DHALL_VERSION); | 25 | const readVersionWith = (lookup, discard = () => {}) => name => { |
17 | if (!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 | |||
41 | const readPkgVersion = readVersionWith(key => pkg[key]); | ||
42 | const 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 | }); | ||
47 | const readEnvVersion = readVersionWith(key => { | ||
48 | return process.env[formatEnvVar(key)]; | ||
49 | }, (key, version) => { | ||
50 | console.warn(`Ignoring \`${formatEnvVar(key)}\` environment variable (${version}).`); | ||
51 | }); | ||
52 | |||
53 | const readVersion = name => | ||
54 | readPkgVersion(name) | ||
55 | .orElse(readEnvVersion(name)) | ||
56 | .orElse(readCfgVersion(name)) | ||
57 | .version; | ||
18 | 58 | ||
19 | const dhallJsonVersion = trim(pkg["dhall-json-version"] || process.env.DHALL_JSON_VERSION); | 59 | const dhallVersion = readVersion("dhall"); |
20 | if (!dhallJsonVersion) throw new Error("Missing DHALL_JSON_VERSION environment variable."); | 60 | const dhallJsonVersion = readVersion("dhall-json"); |
21 | 61 | ||
22 | const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`; | 62 | const release = `https://github.com/dhall-lang/dhall-haskell/releases/download/${dhallVersion}/dhall-json-${dhallJsonVersion}`; |
23 | 63 | ||