commit 86b1e02df693abb6bb2386c8a64168556ac894ca
parent d29527705754536e4b0d704e840656b24ece86b4
Author: nolash <dev@holbrook.no>
Date: Wed, 11 Nov 2020 19:35:18 +0100
Add test plus prefixed override
Diffstat:
6 files changed, 109 insertions(+), 10 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -0,0 +1,4 @@
+* 0.0.1
+ - Parse and process multiple ini files in directory
+ - Override with env vars
+ - Method to pass dict for override
diff --git a/package-lock.json b/package-lock.json
@@ -4,6 +4,12 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
+ "@types/mocha": {
+ "version": "8.0.4",
+ "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-8.0.4.tgz",
+ "integrity": "sha512-M4BwiTJjHmLq6kjON7ZoI2JMlBvpY3BYSdiP6s/qCT3jb1s9/DeJF0JELpAxiVSIxXDzfNKe+r7yedMIoLbknQ==",
+ "dev": true
+ },
"@types/node": {
"version": "14.14.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.7.tgz",
@@ -46,6 +52,12 @@
"picomatch": "^2.0.4"
}
},
+ "arg": {
+ "version": "4.1.3",
+ "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
+ "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
+ "dev": true
+ },
"argparse": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
@@ -92,6 +104,12 @@
"integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
"dev": true
},
+ "buffer-from": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
+ "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
+ "dev": true
+ },
"camelcase": {
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz",
@@ -405,6 +423,12 @@
"chalk": "^4.0.0"
}
},
+ "make-error": {
+ "version": "1.3.6",
+ "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
+ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
+ "dev": true
+ },
"minimatch": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
@@ -567,6 +591,22 @@
"integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=",
"dev": true
},
+ "source-map": {
+ "version": "0.6.1",
+ "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
+ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
+ "dev": true
+ },
+ "source-map-support": {
+ "version": "0.5.19",
+ "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz",
+ "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==",
+ "dev": true,
+ "requires": {
+ "buffer-from": "^1.0.0",
+ "source-map": "^0.6.0"
+ }
+ },
"sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
@@ -616,6 +656,19 @@
"is-number": "^7.0.0"
}
},
+ "ts-node": {
+ "version": "9.0.0",
+ "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.0.0.tgz",
+ "integrity": "sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==",
+ "dev": true,
+ "requires": {
+ "arg": "^4.1.0",
+ "diff": "^4.0.1",
+ "make-error": "^1.1.1",
+ "source-map-support": "^0.5.17",
+ "yn": "3.1.1"
+ }
+ },
"typescript": {
"version": "4.0.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.5.tgz",
@@ -851,6 +904,12 @@
"dev": true
}
}
+ },
+ "yn": {
+ "version": "3.1.1",
+ "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
+ "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
+ "dev": true
}
}
}
diff --git a/package.json b/package.json
@@ -4,7 +4,7 @@
"description": "Merge all ini configuration files from a folder",
"main": "dist/index.js",
"scripts": {
- "test": "echo \"Error: no test specified\" && exit 1",
+ "test": "mocha -r ts-node/register tests/*.ts",
"build": "tsc -d --outDir dist/",
"prepare": "npm run build"
},
@@ -27,7 +27,9 @@
"ini": "^1.3.5"
},
"devDependencies": {
+ "@types/mocha": "^8.0.4",
"mocha": "^8.2.1",
+ "ts-node": "^9.0.0",
"typescript": "^4.0.5"
}
}
diff --git a/src/config.ts b/src/config.ts
@@ -10,15 +10,11 @@ class Config {
require: Array<string>
env_prefix: string
- constructor(filepath:string, env_prefix?:string) {
+ constructor(filepath:string) {
this.filepath = filepath;
this.store = {};
this.censor = [];
this.require = [];
- this.env_prefix = '';
- if (env_prefix !== undefined) {
- this.env_prefix = env_prefix + "_";
- }
}
public process() {
@@ -55,13 +51,17 @@ class Config {
}
}
- public override(o:object) {
+ public override(o:object, prefix:string='') {
for (const k of Object.keys(o)) {
- if (this.store[k] === undefined) {
+ if (k.substring(0, prefix.length) != prefix) {
+ continue;
+ }
+ const kk = k.substring(prefix.length);
+ if (this.store[kk] === undefined) {
throw 'unknown configuration key ' + k;
}
- console.debug('Replace ' + k + ': ' + o[k]);
- this.store[k] = o[k];
+ console.debug('Replace ' + kk + ': ' + o[k]);
+ this.store[kk] = o[k];
}
}
diff --git a/tests/1_basic.ts b/tests/1_basic.ts
@@ -0,0 +1,32 @@
+import * as assert from 'assert';
+
+import { Config } from '../src/config';
+
+describe('basic', () => {
+ it('process', () => {
+ const c = new Config(__dirname + '/testdata');
+ c.process();
+ assert.equal(c.get('FOO_BAR'), 'baz');
+ });
+
+ it('override_simple', () => {
+ const c = new Config(__dirname + '/testdata');
+ c.process();
+ const o = {
+ 'FOO_BAR': 42,
+ }
+ c.override(o);
+ assert.equal(c.get('FOO_BAR'), 42);
+ });
+
+ it('override_prefix', () => {
+ const c = new Config(__dirname + '/testdata');
+ c.process();
+ const o = {
+ 'XXX_FOO_BAR': 42,
+ }
+ c.override(o, 'XXX_');
+ assert.equal(c.get('FOO_BAR'), 42);
+
+ });
+});
diff --git a/tests/testdata/foo.ini b/tests/testdata/foo.ini
@@ -0,0 +1,2 @@
+[foo]
+bar = baz