@untool/core
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -6,2 +6,18 @@ # Change Log | ||
<a name="0.11.0"></a> | ||
# [0.11.0](https://github.com/untool/untool/compare/v0.10.0...v0.11.0) (2018-07-03) | ||
### Bug Fixes | ||
* **core:** add missing dependency to package.json ([a9f6a1a](https://github.com/untool/untool/commit/a9f6a1a)) | ||
### Features | ||
* **core:** add env specific config placeholders ([3cb1739](https://github.com/untool/untool/commit/3cb1739)) | ||
<a name="0.10.0"></a> | ||
@@ -8,0 +24,0 @@ # [0.10.0](https://github.com/untool/untool/compare/v0.9.0...v0.10.0) (2018-06-26) |
@@ -82,16 +82,9 @@ const debug = require('debug')('untool:config'); | ||
const loadConfig = (context, config) => { | ||
const nsp = process.env.UNTOOL_NSP || 'untool'; | ||
const env = process.env[nsp.toUpperCase() + '_ENV'] || process.env.NODE_ENV; | ||
const { loadSync, searchSync } = cosmiconfig(nsp, { stopDir: context }); | ||
const configPath = config && resolvePreset(context, config); | ||
const result = configPath ? loadSync(configPath) : searchSync(context); | ||
return ( | ||
result && { | ||
...result, | ||
config: | ||
result && result.config && result.config.env | ||
? merge(result.config, result.config.env[env]) | ||
: result.config, | ||
} | ||
const { loadSync, searchSync } = cosmiconfig( | ||
process.env.UNTOOL_NSP || 'untool', | ||
{ stopDir: context } | ||
); | ||
return config | ||
? loadSync(resolvePreset(context, config)) | ||
: searchSync(context); | ||
}; | ||
@@ -135,20 +128,20 @@ | ||
const substitutePlaceholders = (config) => { | ||
const placeholdify = (config) => { | ||
const flatConfig = flatten(config); | ||
const keys = Object.keys(flatConfig); | ||
const regExp = new RegExp(`<(${keys.map(escapeRegExp).join('|')})>`, 'g'); | ||
const substituteRecursive = (item) => { | ||
const flatKeys = Object.keys(flatConfig); | ||
const regExp = new RegExp(`<(${flatKeys.map(escapeRegExp).join('|')})>`, 'g'); | ||
const replaceRecursive = (item) => { | ||
if (Array.isArray(item)) { | ||
return item.map(substituteRecursive); | ||
return item.map(replaceRecursive); | ||
} | ||
if (isPlainObject(item)) { | ||
return Object.keys(item).reduce((result, key) => { | ||
result[key] = substituteRecursive(item[key]); | ||
result[key] = replaceRecursive(item[key]); | ||
return result; | ||
}, {}); | ||
} | ||
if (typeof item === 'string') { | ||
return item.replace(regExp, (_, match) => { | ||
var result = flatConfig[match].toString(); | ||
return regExp.test(result) ? substituteRecursive(result) : result; | ||
if (regExp.test(item)) { | ||
return item.replace(regExp, (_, key) => { | ||
const result = flatConfig[key] || ''; | ||
return regExp.test(result) ? replaceRecursive(result) : result; | ||
}); | ||
@@ -158,3 +151,3 @@ } | ||
}; | ||
return substituteRecursive(config); | ||
return replaceRecursive(config); | ||
}; | ||
@@ -176,3 +169,3 @@ | ||
const result = { | ||
...substitutePlaceholders(config), | ||
...placeholdify(config), | ||
mixins: resolveMixins(rootDir, config.mixins), | ||
@@ -179,0 +172,0 @@ }; |
@@ -5,2 +5,3 @@ const debug = require('debug')('untool:core'); | ||
const { getConfig } = require('./config'); | ||
const { environmentalize } = require('./env'); | ||
@@ -14,3 +15,3 @@ exports.Mixin = class Mixin { | ||
exports.bootstrap = function bootstrap(...args) { | ||
const config = getConfig(); | ||
const config = environmentalize(getConfig()); | ||
const mixins = config.mixins.core.map((mixin) => require(mixin)); | ||
@@ -23,3 +24,2 @@ const strategies = { | ||
}; | ||
debug(mixins.map(({ name, strategies }) => ({ [name]: strategies }))); | ||
@@ -26,0 +26,0 @@ |
const debug = require('debug')('untool:runtime'); | ||
const define = require('mixinable'); | ||
const { environmentalize } = require('./env'); | ||
exports.Mixin = class Mixin { | ||
@@ -11,4 +13,4 @@ constructor(config) { | ||
exports.render = function render(...renderArgs) { | ||
return (config) => { | ||
const { mixins } = config; | ||
return (baseConfig, mixins) => { | ||
const config = environmentalize(baseConfig); | ||
const strategies = mixins.reduce( | ||
@@ -18,8 +20,7 @@ (result, mixin) => Object.assign({}, result, mixin.strategies), | ||
); | ||
const createMixinable = define(strategies)(...mixins); | ||
debug(mixins.map(({ name, strategies }) => ({ [name]: strategies }))); | ||
const createMixinable = define(strategies)(...mixins); | ||
return function universalRenderMiddleware(...callArgs) { | ||
return function universalRender(...callArgs) { | ||
createMixinable(config, ...renderArgs).render(...callArgs); | ||
@@ -26,0 +27,0 @@ }; |
{ | ||
"name": "@untool/core", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "untool core", | ||
@@ -27,2 +27,3 @@ "main": "lib/core.js", | ||
"enhanced-resolve": "^4.0.0", | ||
"escape-string-regexp": "^1.0.5", | ||
"find-up": "^3.0.0", | ||
@@ -29,0 +30,0 @@ "flat": "^4.0.0", |
@@ -26,17 +26,18 @@ # `@untool/core` | ||
`@untool/core` comes with support for environment specific configuration. For example, you can pin [`@untool/express`](https://github.com/untool/untool/blob/master/packages/express/README.md)'s port to a specific value in production and have it search for a free port in develoment and test environments. | ||
`@untool/core` comes with support for environment specific configuration. For example, [`@untool/express`](https://github.com/untool/untool/blob/master/packages/express/README.md) uses this placeholder based mechanism to bind the server port to the value of an environment variable. | ||
```json | ||
{ | ||
"port": null, | ||
"env": { | ||
"production": { | ||
"port": 12345 | ||
} | ||
} | ||
"port": "[PORT]" | ||
} | ||
``` | ||
You can even use placeholders everywhere throughout your configuration. Nested configuration structures will be flattened before being used for placeholder substitution. | ||
Now if you start your app in an environment in which the corresponding variable is defined, it will be picked up _at runtime_. | ||
```bash | ||
$ PORT=12345 un start | ||
``` | ||
There is another kind of placeholders. It can be used to reference other configuration values. Nested structures will be flattened before being used for placeholder substitution. | ||
```json | ||
@@ -52,3 +53,3 @@ { | ||
`@untool/core` looks for configuration data in rather many places. It only uses the first config it finds, so make sure you do not have multiple configs lying around: | ||
`@untool/core` looks for configuration data in a couple of places. It only uses the first config it finds, so make sure you do not have multiple configs lying around: | ||
@@ -55,0 +56,0 @@ - an `untool` property in your project's `package.json` file |
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
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
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
19416
7
222
193
4
10
+ Addedescape-string-regexp@^1.0.5
+ Addedescape-string-regexp@1.0.5(transitive)