aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorCyril Sobierajewicz <cyril.sobierajewicz@fretlink.com>2019-05-18 20:26:46 +0200
committerCyril Sobierajewicz <cyril.sobierajewicz@fretlink.com>2019-05-20 13:51:27 +0200
commit38da8e9a800416eea71e642525756929f10adc56 (patch)
tree23fef9b0dbb29dc1cbd3e0b0f1c263037f50d309
parent6337a3be1c44fa988f28d8f7747f1edd75dbbaf5 (diff)
downloadnode-dhall-json-bin-38da8e9a800416eea71e642525756929f10adc56.tar.gz
node-dhall-json-bin-38da8e9a800416eea71e642525756929f10adc56.tar.zst
node-dhall-json-bin-38da8e9a800416eea71e642525756929f10adc56.zip
Accept versions provided by npm configuration parameters
-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}