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