determination
Advanced tools
Comparing version 1.3.0 to 2.0.0
### 2.0.0 | ||
- Protocols are bound with context `config`. | ||
- [BREAKING] `defaults` and `overrides` are merged prior to resolving criteria or handlers. | ||
### 1.3.0 | ||
@@ -3,0 +8,0 @@ |
@@ -11,52 +11,24 @@ 'use strict'; | ||
const shortstopHandler = function (protocols, basedir) { | ||
const resolveCriteria = function (config, criteria) { | ||
const store = new Confidence.Store(config); | ||
return store.get('/', criteria); | ||
}; | ||
const resolveProtocols = function (config, protocols) { | ||
const shortstop = Shortstop.create(); | ||
for (const [key, value] of Entries(protocols)) { | ||
shortstop.use(key, value); | ||
shortstop.use(key, value.bind(config)); | ||
} | ||
shortstop.use('import', (key) => { | ||
return LoadJson(Path.resolve(Path.join(basedir, key))); | ||
}); | ||
return new Promise((resolve, reject) => { | ||
shortstop.resolve(config, (error, result) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
return function (config, criteria) { | ||
const store = new Confidence.Store(config); | ||
const resolved = store.get('/', criteria); | ||
return new Promise((resolve, reject) => { | ||
shortstop.resolve(resolved, (error, result) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
resolve(result); | ||
}); | ||
resolve(result); | ||
}); | ||
}; | ||
}; | ||
const resolveConfigProtocol = function (config, criteria) { | ||
const resolve = shortstopHandler({ | ||
config(key) { | ||
const keys = key.split('.'); | ||
let result = config; | ||
while (result && keys.length) { | ||
const prop = keys.shift(); | ||
if (!result.hasOwnProperty(prop)) { | ||
return undefined; | ||
} | ||
result = result[prop]; | ||
} | ||
return keys.length ? null : result; | ||
} | ||
}); | ||
return resolve(config, criteria); | ||
}; | ||
@@ -66,2 +38,3 @@ | ||
const basedir = Path.dirname(config); | ||
const configobject = LoadJson(config); | ||
@@ -76,20 +49,40 @@ | ||
const resolve = shortstopHandler(protocols, Path.dirname(config)); | ||
criteria = Hoek.applyToDefaults(criteria, { env: process.env }); | ||
return Co.wrap(function *() { | ||
const resolvedDefaults = yield resolve(defaults, criteria); | ||
const mergedDefaults = Hoek.applyToDefaults(configobject, resolvedDefaults); | ||
const overriden = Hoek.applyToDefaults(configobject, overrides); | ||
const resolvedBase = yield resolve(mergedDefaults, criteria); | ||
const merged = Hoek.applyToDefaults(defaults, overriden); | ||
const resolvedOverrides = yield resolve(overrides, criteria); | ||
const resolvedCriteria = resolveCriteria(merged, criteria); | ||
const merged = Hoek.applyToDefaults(resolvedBase, resolvedOverrides); | ||
const importsResolved = yield resolveProtocols(resolvedCriteria, { | ||
import(key) { | ||
return resolveCriteria(LoadJson(Path.resolve(Path.join(basedir, key))), criteria); | ||
} | ||
}); | ||
const resolved = yield resolveConfigProtocol(merged, criteria); | ||
const baseResolved = yield resolveProtocols(importsResolved, protocols); | ||
return resolved; | ||
const configResolved = yield resolveProtocols(baseResolved, { | ||
config(key) { | ||
const keys = key.split('.'); | ||
let result = this; | ||
while (result && keys.length) { | ||
const prop = keys.shift(); | ||
if (!result.hasOwnProperty(prop)) { | ||
return undefined; | ||
} | ||
result = result[prop]; | ||
} | ||
return keys.length ? null : result; | ||
} | ||
}); | ||
return configResolved; | ||
}); | ||
@@ -96,0 +89,0 @@ }; |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"author": "Trevor Livingston <tlivings@gmail.com>", | ||
@@ -13,0 +13,0 @@ "repository": { |
@@ -20,5 +20,5 @@ # determination | ||
- `criteria` (_Object_) - optional resolution criteria. See [confidence](https://github.com/hapijs/confidence). Minimally will always contain `process.env` under the key `env`. | ||
- `protocols` (_Object_) - optional mapping of protocols for [shortstop](https://github.com/krakenjs/shortstop). | ||
- `defaults` (_Object_ | _String_) - optional default configuration values. | ||
- `overrides` (_Object_ | _String_) - optional override configuration values. | ||
- `protocols` (_Object_) - optional mapping of protocols for [shortstop](https://github.com/krakenjs/shortstop). Protocols are bound with context `config`, where `config` is the configuration being resolved. Obviously this doesn't work with arrow functions. | ||
- `defaults` (_Object_ | _String_) - optional default pre-resolved configuration values. | ||
- `overrides` (_Object_ | _String_) - optional override pre-resolved configuration values. | ||
- returns - a resolver. | ||
@@ -72,2 +72,32 @@ | ||
### Custom Protocol Handlers | ||
An example of utilizing a custom protocol handler is below. This takes advantage of the context bound to the handler. | ||
`config.json` | ||
```json | ||
{ | ||
"thing1": "one", | ||
"thing2": "two", | ||
"things": "eval:${thing1} and ${thing2}" | ||
} | ||
``` | ||
and | ||
```javascript | ||
const Determination = require('determination'); | ||
const VM = require('vm'); | ||
const protocols = { | ||
eval(expression) { | ||
return VM.runInNewContext('`' + expression + '`', this); | ||
} | ||
}; | ||
Determination.create({ config: Path.join(__dirname, './config.json'), protocols }).resolve((error, config) => { | ||
config.get('things'); //"one and two" | ||
}); | ||
``` | ||
### Resolution Order | ||
@@ -74,0 +104,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11749
111
158