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