Comparing version 0.6.16 to 0.7.0
140
index.js
const assert = require('assert'); | ||
const lodash = require('lodash'); | ||
const clc = require('cli-color'); | ||
module.exports = (spec, params, options = {}) => { | ||
const metas = Object.keys(spec.required).map(key => { | ||
const meta = spec.required[key]; | ||
meta.key = key; | ||
if (meta.default !== undefined) { | ||
if (meta.options && !lodash.includes(meta.options, meta.default)) { | ||
meta.options = [meta.default, ...meta.options]; | ||
const mapMetas = metas => Object.keys(metas).map(key => { | ||
const meta = metas[key]; | ||
meta.key = key; | ||
if (meta.default !== undefined) { | ||
if (meta.options && !lodash.includes(meta.options, meta.default)) { | ||
meta.options = [meta.default, ...meta.options]; | ||
} | ||
if (meta.type === undefined) { | ||
if (Number.isInteger(meta.default)) { | ||
meta.type = 'integer'; | ||
} | ||
if (meta.type === undefined) { | ||
if (Number.isInteger(meta.default)) { | ||
meta.type = 'integer'; | ||
} | ||
if (meta.example === undefined) { | ||
meta.example = meta.default; | ||
} | ||
if (meta.example === undefined) { | ||
meta.example = meta.default; | ||
} | ||
} | ||
return meta; | ||
}); | ||
if (process.env.mode !== 'quiet') { | ||
console.error(formatSpec(spec)); | ||
} | ||
return metas.reduce((props, meta) => { | ||
const key = meta.key; | ||
try { | ||
if (options.debug) { | ||
console.log('meta', meta); | ||
} | ||
if (params[key]) { | ||
const value = params[key]; | ||
if (!value.length) { | ||
throw new Error(`Property '${key}' is empty'`); | ||
} | ||
const parsedValue = (meta.type === 'integer') | ||
? parseInt(value) | ||
: (meta.elementType === 'string') | ||
? value.split(',') | ||
: value | ||
; | ||
if (meta.options && !lodash.includes(meta.options, parsedValue)) { | ||
throw new Error(`Invalid '${key}'`); | ||
} | ||
props[key] = parsedValue; | ||
} else if (props[key]) { | ||
} else if (meta.default !== undefined) { | ||
props[key] = meta.default; | ||
} else if (options.required === false) { | ||
} else { | ||
const meta = spec.required[key]; | ||
if (meta.required !== false) { | ||
throw new Error(`Missing required '${key}'`); | ||
} | ||
} | ||
return props; | ||
} catch (err) { | ||
throw err; | ||
return meta; | ||
}); | ||
const reduceMetas = (metas, params, defaults) => metas.reduce((props, meta) => { | ||
const key = meta.key; | ||
if (params[key]) { | ||
const value = params[key]; | ||
if (!value.length) { | ||
throw new Error(`Property '${key}' is empty'`); | ||
} | ||
}, options.defaults || {}); | ||
}; | ||
const parsedValue = (meta.type === 'integer') | ||
? parseInt(value) | ||
: (meta.elementType === 'string') | ||
? value.split(',') | ||
: value | ||
; | ||
if (meta.options && !lodash.includes(meta.options, parsedValue)) { | ||
throw new Error(`Invalid '${key}'`); | ||
} | ||
props[key] = parsedValue; | ||
} else if (props[key]) { | ||
} else if (meta.default !== undefined) { | ||
props[key] = meta.default; | ||
} else { | ||
const meta = metas[key]; | ||
if (meta.required !== false) { | ||
throw new Error(`Missing required '${key}'`); | ||
} | ||
} | ||
return props; | ||
}, defaults || {}); | ||
const formatSpec = spec => [ | ||
clc.green.bold(spec.description), | ||
clc.white.bold('Options:'), | ||
...Object.keys(spec.required).map( | ||
key => spec.required[key] | ||
).map( | ||
formatMeta | ||
).map( | ||
lines => lines.map(line => ` ${clc.cyan(line)}`).join('\n') | ||
) | ||
].join('\n'); | ||
const formatMeta = meta => { | ||
@@ -101,1 +78,32 @@ let lines = [lodash.capitalize(meta.description.slice(0, 1)) + meta.description.slice(1)]; | ||
}; | ||
const formatMetas = metas => Object.keys(metas).map( | ||
key => metas[key] | ||
).map( | ||
formatMeta | ||
).map( | ||
lines => lines.map(line => ` ${clc.cyan(line)}`).join('\n') | ||
); | ||
module.exports = (pkg, specf, params, options = {}) => { | ||
const spec = specf(pkg); | ||
assert(process.env.NODE_ENV, 'NODE_ENV'); | ||
assert(spec.env, 'spec.env'); | ||
const formatSpec = (description, heading, metas) => [ | ||
clc.green.bold(description), | ||
clc.white.bold(heading), | ||
...formatMetas(metas) | ||
].join('\n'); | ||
spec.env = mapMetas(spec.env); | ||
spec.defaults = spec.defaults || {}; | ||
if (process.env.mode !== 'quiet') { | ||
console.error(formatSpec(spec.description, 'Environment:', spec.env)); | ||
} | ||
const env = reduceMetas(spec.env, process.env, spec.defaults[process.env.NODE_ENV]); | ||
if (!spec.config) { | ||
return env; | ||
} | ||
assert(typeof spec.config === 'function', 'spec.config function of env'); | ||
const configMetas = mapMetas(spec.config(env)); | ||
return reduceMetas(configMetas, process.env, env); | ||
}; |
{ | ||
"name": "app-spec", | ||
"version": "0.6.16", | ||
"version": "0.7.0", | ||
"description": "Get application configuration from spec of required environment variables.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "./bin/test.js" | ||
}, | ||
@@ -13,16 +13,8 @@ "author": "@evanxsummers", | ||
"type": "git", | ||
"url": "git+https://github.com/evanx/app-spec.git" | ||
"url": "https://github.com/evanx/app-spec.git" | ||
}, | ||
"bin": { | ||
"git-commit": "./bin/git-commit.sh", | ||
"npm-publish": "./bin/npm-publish.sh", | ||
"docker-rebuild": "./bin/docker-rebuild.sh", | ||
"docker-redeploy": "./bin/docker-redeploy.sh", | ||
"docker-rm": "./bin/docker-rm.sh", | ||
"docker-network-rm": "./bin/docker-network-rm.sh", | ||
"docker-inspect-ip": "./bin/docker-inspect-ip.sh", | ||
"docker-run-redis": "./bin/docker-run-redis.sh", | ||
"docker-rm-redis": "./bin/docker-rm-redis.sh" | ||
}, | ||
"homepage": "https://github.com/evanx/app-spec" | ||
"homepage": "https://github.com/evanx/app-spec", | ||
"dependencies": { | ||
"app-bin": "^0.1.1" | ||
} | ||
} |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
249
1
8913
1
8
7
+ Addedapp-bin@^0.1.1
+ Addedapp-bin@0.1.19(transitive)