@jepz20/conman
Advanced tools
Comparing version 0.0.7 to 0.0.8
{ | ||
"name": "@jepz20/conman", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Configuration manager that supports ttl and plugabble sources", | ||
@@ -17,3 +17,3 @@ "main": "./src/conman", | ||
}, | ||
"gitHead": "349fbbbbe10fe177bab4de63d6c81d8ec7b06b78", | ||
"gitHead": "072fbce84301f7f39f0c7385d9b9c4b9e165f895", | ||
"lint-staged": { | ||
@@ -20,0 +20,0 @@ "src/**/*.*": [ |
@@ -12,3 +12,2 @@ # CONMAN | ||
## Getting Started | ||
```js | ||
@@ -197,3 +196,3 @@ // Require conman | ||
- **build(mandatory)**: a function that returns an object or a promise that returns an object | ||
- **build(mandatory)**: a function that returns an object or a promise that returns an object. The build method receives the config built from the sources until that point. | ||
- **type(mandatory)**: a string which identifies the type of source | ||
@@ -205,11 +204,15 @@ - **name(optional)**: in case you want to identify each instance of your source you can include a name. If a name exists it would be used instead of the `type` | ||
```js | ||
const source = (obj, params) => { | ||
const source = (obj, { key, name } = {}) => { | ||
return { | ||
build() { | ||
build(config) { // recieves the config up until that point | ||
if (config.extra) { | ||
return { ...obj, extra: config.extra }; | ||
} | ||
return obj; | ||
}, | ||
type: 'syncSource', | ||
name: params.name | ||
key, | ||
name | ||
}; | ||
}; | ||
``` |
@@ -7,2 +7,3 @@ const { mergeDeepRight } = require('ramda'); | ||
const obfuscate = require('./helpers/obfuscate'); | ||
const serialize = require('./helpers/serialize'); | ||
@@ -47,2 +48,5 @@ const SECOND = 1000; | ||
// _serialPromises(() => { | ||
// }) | ||
function _writeToFile(cacheObject, opts) { | ||
@@ -161,21 +165,12 @@ if (!opts.useFile) { | ||
function _buildSources(_sources) { | ||
const sourcesTypes = _sources.map(({ name, type }) => name || type); | ||
const sourcesKeys = _sources.map(source => source.key); | ||
logger( | ||
'log', | ||
`Build triggered for sources: "${sourcesTypes.join()}" at ${new Date().toISOString()}` | ||
); | ||
return Promise.all(_sources.map(source => source.build())).then(configs => { | ||
logger( | ||
'log', | ||
`Build completed for sources: "${sourcesTypes.join()}" at ${new Date().toISOString()}` | ||
); | ||
return configs.reduce((acc, config, index) => { | ||
const sourceKey = sourcesKeys[index]; | ||
if (sourceKey) { | ||
return mergeDeepRight(acc, { [sourceKey]: config }); | ||
} | ||
return mergeDeepRight(acc, config); | ||
}, {}); | ||
}); | ||
async function buildSource(config, source) { | ||
const sourceConfig = await source.build(config); | ||
const parseConfig = source.key | ||
? { [source.key]: sourceConfig } | ||
: sourceConfig; | ||
return mergeDeepRight(config, parseConfig); | ||
} | ||
return serialize(_sources, buildSource, {}); | ||
} | ||
@@ -188,11 +183,18 @@ | ||
function _buildAndSaveCache(_sources) { | ||
return _buildSources(_sources).then(configs => { | ||
return _writeToFile(_prepareCacheObject(configs), options).then(() => | ||
_setPrivateCache(configs) | ||
); | ||
}); | ||
async function _buildAndSaveCache(_sources) { | ||
const sourcesTypes = _sources.map(({ name, type }) => name || type); | ||
logger( | ||
'log', | ||
`Build triggered for sources: "${sourcesTypes.join()}" at ${new Date().toISOString()}` | ||
); | ||
const configs = await _buildSources(_sources); | ||
logger( | ||
'log', | ||
`Build completed for sources: "${sourcesTypes.join()}" at ${new Date().toISOString()}` | ||
); | ||
await _writeToFile(_prepareCacheObject(configs), options); | ||
return _setPrivateCache(configs); | ||
} | ||
function build() { | ||
async function build() { | ||
_triggerInterval(); | ||
@@ -202,8 +204,7 @@ | ||
if (!privateCache && options.useFile) { | ||
return _readFromFile(options).then(cache => { | ||
if (!cache) { | ||
return _buildAndSaveCache(sources); | ||
} | ||
return _setPrivateCache(cache); | ||
}); | ||
const cache = await _readFromFile(options); | ||
if (!cache) { | ||
return _buildAndSaveCache(sources); | ||
} | ||
return _setPrivateCache(cache); | ||
} | ||
@@ -210,0 +211,0 @@ |
@@ -6,5 +6,8 @@ jest.mock('jsonfile'); | ||
const source = (obj, { key, name } = {}) => { | ||
const source = (obj = {}, { key, name } = {}) => { | ||
return { | ||
build() { | ||
build(config) { | ||
if (config.extra) { | ||
return { ...obj, extra: config.extra }; | ||
} | ||
return obj; | ||
@@ -18,8 +21,13 @@ }, | ||
const asyncSource = obj => { | ||
const asyncSource = (obj = {}, { key, name } = {}) => { | ||
return { | ||
build() { | ||
build(config) { | ||
if (config.extra) { | ||
return Promise.resolve({ ...obj, extra: config.extra }); | ||
} | ||
return Promise.resolve(obj); | ||
}, | ||
type: 'asyncSource' | ||
type: 'asyncSource', | ||
key, | ||
name | ||
}; | ||
@@ -63,2 +71,24 @@ }; | ||
it('should build with one synchronous source and pass the configf', async () => { | ||
const source1 = source({ test: 'test', extra: 'extra config' }); | ||
const source2 = source({ test: 'usesConfig' }); | ||
const config = await conman() | ||
.addSource(source1) | ||
.addSource(source2) | ||
.build(); | ||
conman.stop(); | ||
expect(config).toEqual({ test: 'usesConfig', extra: 'extra config' }); | ||
}); | ||
it('should build with one asynchronous source and pass the configf', async () => { | ||
const source1 = asyncSource({ test: 'test', extra: 'extra config async' }); | ||
const source2 = asyncSource({ test: 'usesConfig' }); | ||
const config = await conman() | ||
.addSource(source1) | ||
.addSource(source2) | ||
.build(); | ||
conman.stop(); | ||
expect(config).toEqual({ test: 'usesConfig', extra: 'extra config async' }); | ||
}); | ||
it('should build with one synchronous source', async () => { | ||
@@ -65,0 +95,0 @@ const source1 = source({ test: 'test async' }); |
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
264579
11
716
216