Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
feat: Allows for configuration as an object (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcjfunk authored Feb 9, 2022
1 parent bcf6780 commit a864038
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"check-coverage": true,
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const Determination = require('@vrbo/determination');
**Determination.create(options)**

- `options` (_Object_) - an options object containing:
- `config` (_String_) - required path to a JSON configuration.
- `config` (_String_ | _Object_) - required, either a path to a JSON configuration file or an object.
- `basedir` (_String_) - optional path used for resolving relative imports within configs. If config is a file, it defaults to the config file's directory. If config is an object, it defaults to `process.cwd()`.
- `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). 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.
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const Resolver = require('./resolver');
const Store = require('./store');

const schema = Joi.object({
config: Joi.string().required(),
config: Joi.alternatives(Joi.string(), Joi.object()).required(),
basedir: Joi.string(),
criteria: Joi.object().default({}),
protocols: Joi.object().default({}),
defaults: Joi.alternatives(Joi.string(), Joi.object()).default({}),
Expand Down
15 changes: 12 additions & 3 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ const loadAndParseJson = function (file) {
}
};

const resolver = async function ({ config, criteria, protocols, defaults, overrides }) {
const resolver = async function ({ config, basedir, criteria, protocols, defaults, overrides }) {

const basedir = Path.dirname(config);
const configobject = loadAndParseJson(config);
let configobject = config;

if (typeof config === 'string') {
configobject = loadAndParseJson(config);
basedir = Path.dirname(config);
}

if (typeof defaults === 'string') {
defaults = loadAndParseJson(defaults);
Expand All @@ -91,6 +95,10 @@ const resolver = async function ({ config, criteria, protocols, defaults, overri

const importsResolved = await resolveProtocols(resolvedCriteria, {
import(key) {
if (!basedir) {
console.log('@vrbo/determination: No basedir set, defaulting to "process.cwd()" for resolving relative json imports.');
basedir = process.cwd();
}
return resolveCriteria(loadAndParseJson(Path.resolve(Path.join(basedir, key))), criteria);
}
});
Expand All @@ -112,6 +120,7 @@ const resolver = async function ({ config, criteria, protocols, defaults, overri
result = result[prop];
}

/* istanbul ignore next */
return keys.length ? null : result;
}
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
},
"scripts": {
"commit": "cz",
"cover": "nyc npm test",
"lint": "eslint lib",
"test": "npm run lint && ENV_TEST=5678 tape test/*.js"
"unit": "npm run lint && ENV_TEST=5678 tape test/*.js",
"test": "nyc npm run unit"
},
"config": {
"commitizen": {
Expand Down
44 changes: 44 additions & 0 deletions test/test-determination.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,50 @@ Test('test determination', (t) => {

});

t.test('resolve import, no basedir', async (t) => {
t.plan(1);

const criteria = {
pass: 'false'
};

const configObj = {
a: 'import:./test/fixtures/a.json'
};

try {
const config = await Determination.create({ config: configObj, criteria }).resolve();

t.equal(config.get('a.test.value'), false, 'criteria resolved.');
}
catch (error) {
console.log(error);
}

});

t.test('resolve with config as object', async (t) => {
t.plan(3);

const configObj = {
test: 'foo',
copy: 'config:test',
array: ['config:test']
};

try {
const config = await Determination.create({ config: configObj, basedir: '/' }).resolve();

t.equal(config.get('test'), 'foo', 'criteria resolved.');
t.equal(config.get('copy'), 'foo', 'config resolved.');
t.equal(config.get('array')[0], 'foo', 'array resolved protocol.');
}
catch (error) {
console.log(error);
}

});

t.test('resolve with defaults', async (t) => {
t.plan(3);

Expand Down

0 comments on commit a864038

Please sign in to comment.