@incanta/config 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/lib/config.d.ts +17 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +83 -0
- package/lib/config.js.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/lib/loader.d.ts.map +1 -0
- package/lib/loader.js +93 -0
- package/lib/loader.js.map +1 -0
- package/package.json +39 -0
- package/src/config.ts +108 -0
- package/src/index.ts +13 -0
- package/src/loader.ts +107 -0
- package/test/config-env/config-env.test.ts +29 -0
- package/test/config-env/default/thing.yaml +1 -0
- package/test/config-env/dev/thing.yaml +1 -0
- package/test/config-env/production/thing.yaml +1 -0
- package/test/config-json/config-json.test.ts +23 -0
- package/test/config-json/default/foo.json +3 -0
- package/test/config-json/default/goodbye/index.json +8 -0
- package/test/config-json/default/index.json +3 -0
- package/test/config-json5/config-json5.test.ts +23 -0
- package/test/config-json5/default/foo.json5 +4 -0
- package/test/config-json5/default/goodbye/index.json5 +9 -0
- package/test/config-json5/default/index.json5 +4 -0
- package/test/config-jsonc/config-jsonc.test.ts +23 -0
- package/test/config-jsonc/default/foo.jsonc +4 -0
- package/test/config-jsonc/default/goodbye/index.jsonc +9 -0
- package/test/config-jsonc/default/index.jsonc +4 -0
- package/test/config-local/config-local.test.ts +13 -0
- package/test/config-local/default/thing.yaml +1 -0
- package/test/config-local/local/thing.yaml +1 -0
- package/test/config-local/production/thing.yaml +1 -0
- package/test/config-multi/config-multi.test.ts +23 -0
- package/test/config-multi/default/foo.json +3 -0
- package/test/config-multi/default/goodbye/index.json5 +9 -0
- package/test/config-multi/default/index.yaml +1 -0
- package/test/config-override/config-override.test.ts +13 -0
- package/test/config-override/default/thing.yaml +1 -0
- package/test/config-override/local/thing.yaml +1 -0
- package/test/config-override/override.json +5 -0
- package/test/config-override/production/thing.yaml +1 -0
- package/test/config-yaml/config-yaml.test.ts +23 -0
- package/test/config-yaml/default/foo.yaml +1 -0
- package/test/config-yaml/default/goodbye/index.yaml +5 -0
- package/test/config-yaml/default/index.yaml +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Incanta Games
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface IConfigOptions {
|
|
2
|
+
configDir?: string;
|
|
3
|
+
configEnv?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class Config {
|
|
6
|
+
private configDir;
|
|
7
|
+
private values;
|
|
8
|
+
private customValues;
|
|
9
|
+
constructor(options?: IConfigOptions);
|
|
10
|
+
get<T>(key: string): T;
|
|
11
|
+
getWithParts<T>(keyParts: string[]): T;
|
|
12
|
+
has(key: string): boolean;
|
|
13
|
+
set<T>(key: string, value: T): void;
|
|
14
|
+
setWithParts<T>(keyParts: string[], value: T): void;
|
|
15
|
+
save(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,cAAc;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,qBAAa,MAAM;IACjB,OAAO,CAAC,SAAS,CAAS;IAE1B,OAAO,CAAC,MAAM,CAAM;IACpB,OAAO,CAAC,YAAY,CAAM;gBAEP,OAAO,CAAC,EAAE,cAAc;IAgCpC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC;IAKtB,YAAY,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,CAAC;IAkBtC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAUzB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAKnC,YAAY,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAe7C,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAMnC"}
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Config = void 0;
|
|
7
|
+
const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
|
+
const loader_1 = require("./loader");
|
|
11
|
+
class Config {
|
|
12
|
+
configDir;
|
|
13
|
+
values;
|
|
14
|
+
customValues;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.values = {};
|
|
17
|
+
this.customValues = {};
|
|
18
|
+
this.configDir = options?.configDir || path_1.default.join(process.cwd(), "config");
|
|
19
|
+
const defaultValues = loader_1.Loader.load(path_1.default.join(this.configDir, "default"));
|
|
20
|
+
let envValues = {};
|
|
21
|
+
const configEnvDir = options?.configEnv || process.env["NODE_CONFIG_ENV"];
|
|
22
|
+
if (configEnvDir) {
|
|
23
|
+
if (fs_1.default.existsSync(path_1.default.join(this.configDir, configEnvDir))) {
|
|
24
|
+
envValues = loader_1.Loader.load(path_1.default.join(this.configDir, configEnvDir));
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
console.log(`Cannot use environment deployment value of ${configEnvDir} because ${path_1.default.join(this.configDir, configEnvDir)} doesn't exist`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const localValues = loader_1.Loader.load(path_1.default.join(this.configDir, "local"));
|
|
31
|
+
const overrideValues = loader_1.Loader.loadFile(path_1.default.join(this.configDir, "override.json"));
|
|
32
|
+
(0, lodash_merge_1.default)(this.values, defaultValues, envValues, localValues, overrideValues);
|
|
33
|
+
}
|
|
34
|
+
get(key) {
|
|
35
|
+
const parts = key.split(".");
|
|
36
|
+
return this.getWithParts(parts);
|
|
37
|
+
}
|
|
38
|
+
getWithParts(keyParts) {
|
|
39
|
+
if (keyParts.length === 0) {
|
|
40
|
+
throw new Error("Cannot use an empty key");
|
|
41
|
+
}
|
|
42
|
+
let obj = this.values;
|
|
43
|
+
for (const part of keyParts) {
|
|
44
|
+
if (typeof obj[part] === "undefined") {
|
|
45
|
+
throw new Error(`Could not find value for key ${keyParts.join(".")}`);
|
|
46
|
+
}
|
|
47
|
+
obj = obj[part];
|
|
48
|
+
}
|
|
49
|
+
return obj;
|
|
50
|
+
}
|
|
51
|
+
has(key) {
|
|
52
|
+
try {
|
|
53
|
+
this.get(key);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
set(key, value) {
|
|
61
|
+
const parts = key.split(".");
|
|
62
|
+
this.setWithParts(parts, value);
|
|
63
|
+
}
|
|
64
|
+
setWithParts(keyParts, value) {
|
|
65
|
+
if (keyParts.length === 0) {
|
|
66
|
+
throw new Error("Cannot use an empty key");
|
|
67
|
+
}
|
|
68
|
+
const obj = {};
|
|
69
|
+
for (let i = 0; i < keyParts.length - 1; i++) {
|
|
70
|
+
obj[keyParts[i]] = {};
|
|
71
|
+
}
|
|
72
|
+
obj[keyParts[keyParts.length - 1]] = value;
|
|
73
|
+
(0, lodash_merge_1.default)(this.customValues, obj);
|
|
74
|
+
}
|
|
75
|
+
async save() {
|
|
76
|
+
const fileContents = JSON.stringify(this.customValues, null, 2);
|
|
77
|
+
await fs_1.default.promises.writeFile(this.configDir, fileContents, {
|
|
78
|
+
encoding: "utf-8",
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.Config = Config;
|
|
83
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;;;;AAAA,gEAAiC;AACjC,gDAAwB;AACxB,4CAAoB;AACpB,qCAAkC;AAOlC,MAAa,MAAM;IACT,SAAS,CAAS;IAElB,MAAM,CAAM;IACZ,YAAY,CAAM;IAE1B,YAAmB,OAAwB;QACzC,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC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C,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;SACvB;QAED,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;QAE3C,IAAA,sBAAK,EAAC,IAAI,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;IAChC,CAAC;IAEM,KAAK,CAAC,IAAI;QACf,MAAM,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QAChE,MAAM,YAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,EAAE,YAAY,EAAE;YACxD,QAAQ,EAAE,OAAO;SAClB,CAAC,CAAC;IACL,CAAC;CACF;AAjGD,wBAiGC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;;AAYlC,kBAAqB"}
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,qCAAkC;AAElC,IAAI,MAAM,GAAuB,SAAS,CAAC;AAE3C,SAAS,SAAS;IAChB,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,GAAG,IAAI,eAAM,EAAE,CAAC;KACvB;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,iBAAS,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAOA,qBAAa,MAAM;WACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG;WAgC/B,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG;CAkExC"}
|
package/lib/loader.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Loader = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const jsonc_parser_1 = require("jsonc-parser");
|
|
10
|
+
const json5_1 = __importDefault(require("json5"));
|
|
11
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
12
|
+
const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
13
|
+
class Loader {
|
|
14
|
+
static loadFile(filePath) {
|
|
15
|
+
if (!fs_1.default.existsSync(filePath)) {
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
const fileContents = fs_1.default.readFileSync(filePath, {
|
|
19
|
+
encoding: "utf-8",
|
|
20
|
+
});
|
|
21
|
+
const fileParts = filePath.split(".");
|
|
22
|
+
const extension = fileParts[fileParts.length - 1];
|
|
23
|
+
if (/^ya?ml$/i.exec(extension)) {
|
|
24
|
+
// yaml file
|
|
25
|
+
return js_yaml_1.default.load(fileContents);
|
|
26
|
+
}
|
|
27
|
+
else if (/^json$/i.exec(extension)) {
|
|
28
|
+
// json file
|
|
29
|
+
return JSON.parse(fileContents);
|
|
30
|
+
}
|
|
31
|
+
else if (/^jsonc$/i.exec(extension)) {
|
|
32
|
+
// jsonc file
|
|
33
|
+
return (0, jsonc_parser_1.parse)(fileContents); // todo: handle error callback
|
|
34
|
+
}
|
|
35
|
+
else if (/^json5$/i.exec(extension)) {
|
|
36
|
+
// json5 file
|
|
37
|
+
return json5_1.default.parse(fileContents);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
console.log(`Invalid file name ${filePath}. Only yml, yaml, json, jsonc, json5 extensions are supported (case insensitive).`);
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
static load(folder) {
|
|
45
|
+
if (!fs_1.default.existsSync(folder)) {
|
|
46
|
+
return {};
|
|
47
|
+
}
|
|
48
|
+
const contents = fs_1.default.readdirSync(folder, {
|
|
49
|
+
encoding: "utf-8",
|
|
50
|
+
withFileTypes: true,
|
|
51
|
+
});
|
|
52
|
+
const baseObj = {};
|
|
53
|
+
for (const content of contents) {
|
|
54
|
+
if (!content.isDirectory() && content.name.startsWith("index.")) {
|
|
55
|
+
(0, lodash_merge_1.default)(baseObj, Loader.loadFile(path_1.default.join(folder, content.name)));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const content of contents) {
|
|
59
|
+
if (content.isDirectory()) {
|
|
60
|
+
const key = content.name;
|
|
61
|
+
if (typeof baseObj[key] !== "undefined") {
|
|
62
|
+
console.log(`Could not load the directory ${key} because we already loaded a config for a file with the same name`);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const obj = Loader.load(path_1.default.join(folder, content.name));
|
|
66
|
+
baseObj[key] = obj;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
if (content.name.startsWith("index.")) {
|
|
70
|
+
// we already loaded this to be be in the base config
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const fileParts = content.name.split(".");
|
|
74
|
+
if (fileParts.length === 2) {
|
|
75
|
+
const key = fileParts[0];
|
|
76
|
+
if (typeof baseObj[key] !== "undefined") {
|
|
77
|
+
console.log(`Could not load the file ${path_1.default.join(folder, content.name)} because we already loaded a config for a folder with the same name`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const obj = Loader.loadFile(path_1.default.join(folder, content.name));
|
|
81
|
+
baseObj[key] = obj;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
console.log(`Invalid file name ${content.name}. Config files must have a supported extension and contain no extra periods in the file name`);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return baseObj;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.Loader = Loader;
|
|
93
|
+
//# sourceMappingURL=loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,+CAAmD;AACnD,kDAA0B;AAC1B,sDAA2B;AAC3B,gEAAiC;AAEjC,MAAa,MAAM;IACV,MAAM,CAAC,QAAQ,CAAC,QAAgB;QACrC,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;YAC5B,OAAO,EAAE,CAAC;SACX;QAED,MAAM,YAAY,GAAG,YAAE,CAAC,YAAY,CAAC,QAAQ,EAAE;YAC7C,QAAQ,EAAE,OAAO;SAClB,CAAC,CAAC;QAEH,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACtC,MAAM,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAElD,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;YAC9B,YAAY;YACZ,OAAO,iBAAI,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;SAChC;aAAM,IAAI,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;YACpC,YAAY;YACZ,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;SACjC;aAAM,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;YACrC,aAAa;YACb,OAAO,IAAA,oBAAU,EAAC,YAAY,CAAC,CAAC,CAAC,8BAA8B;SAChE;aAAM,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,EAA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;iBAAM;gBACL,IAAI,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;oBACrC,qDAAqD;oBACrD,SAAS;iBACV;gBAED,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBAC1C,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;oBAC1B,MAAM,GAAG,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;oBAEzB,IAAI,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,WAAW,EAAE;wBACvC,OAAO,CAAC,GAAG,CACT,2BAA2B,cAAI,CAAC,IAAI,CAClC,MAAM,EACN,OAAO,CAAC,IAAI,CACb,qEAAqE,CACvE,CAAC;wBACF,SAAS;qBACV;oBAED,MAAM,GAAG,GAAG,MAAM,CAAC,QAAQ,CAAC,cAAI,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;oBAE7D,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;iBACpB;qBAAM;oBACL,OAAO,CAAC,GAAG,CACT,qBAAqB,OAAO,CAAC,IAAI,8FAA8F,CAChI,CAAC;oBACF,SAAS;iBACV;aACF;SACF;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AAnGD,wBAmGC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@incanta/config",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "lib/index.js",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./lib/index.js",
|
|
7
|
+
"./config": "./lib/config.js"
|
|
8
|
+
},
|
|
9
|
+
"repository": "git@github.com:IncantaGames/config.git",
|
|
10
|
+
"author": "Incanta Games",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc -b .",
|
|
14
|
+
"test": "jest"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"js-yaml": "^4.1.0",
|
|
18
|
+
"json5": "^2.2.3",
|
|
19
|
+
"jsonc-parser": "^3.2.0",
|
|
20
|
+
"lodash.merge": "^4.6.2"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@jest/globals": "^29.5.0",
|
|
24
|
+
"@types/js-yaml": "^4.0.5",
|
|
25
|
+
"@types/lodash.merge": "^4.6.7",
|
|
26
|
+
"@types/node": "^18.11.18",
|
|
27
|
+
"@typescript-eslint/eslint-plugin": "^5.48.0",
|
|
28
|
+
"@typescript-eslint/parser": "^5.48.0",
|
|
29
|
+
"eslint": "^8.31.0",
|
|
30
|
+
"eslint-config-prettier": "^8.6.0",
|
|
31
|
+
"eslint-plugin-import": "^2.26.0",
|
|
32
|
+
"eslint-plugin-prefer-arrow": "^1.2.3",
|
|
33
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
34
|
+
"jest": "^29.5.0",
|
|
35
|
+
"prettier": "^2.8.1",
|
|
36
|
+
"ts-jest": "^29.1.0",
|
|
37
|
+
"typescript": "^5.0.4"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import merge from "lodash.merge";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import { Loader } from "./loader";
|
|
5
|
+
|
|
6
|
+
export interface IConfigOptions {
|
|
7
|
+
configDir?: string;
|
|
8
|
+
configEnv?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class Config {
|
|
12
|
+
private configDir: string;
|
|
13
|
+
|
|
14
|
+
private values: any;
|
|
15
|
+
private customValues: any;
|
|
16
|
+
|
|
17
|
+
public constructor(options?: IConfigOptions) {
|
|
18
|
+
this.values = {};
|
|
19
|
+
this.customValues = {};
|
|
20
|
+
|
|
21
|
+
this.configDir = options?.configDir || path.join(process.cwd(), "config");
|
|
22
|
+
|
|
23
|
+
const defaultValues = Loader.load(path.join(this.configDir, "default"));
|
|
24
|
+
|
|
25
|
+
let envValues: any = {};
|
|
26
|
+
const configEnvDir = options?.configEnv || process.env["NODE_CONFIG_ENV"];
|
|
27
|
+
if (configEnvDir) {
|
|
28
|
+
if (fs.existsSync(path.join(this.configDir, configEnvDir))) {
|
|
29
|
+
envValues = Loader.load(path.join(this.configDir, configEnvDir));
|
|
30
|
+
} else {
|
|
31
|
+
console.log(
|
|
32
|
+
`Cannot use environment deployment value of ${configEnvDir} because ${path.join(
|
|
33
|
+
this.configDir,
|
|
34
|
+
configEnvDir
|
|
35
|
+
)} doesn't exist`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const localValues = Loader.load(path.join(this.configDir, "local"));
|
|
41
|
+
|
|
42
|
+
const overrideValues = Loader.loadFile(
|
|
43
|
+
path.join(this.configDir, "override.json")
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
merge(this.values, defaultValues, envValues, localValues, overrideValues);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public get<T>(key: string): T {
|
|
50
|
+
const parts = key.split(".");
|
|
51
|
+
return this.getWithParts<T>(parts);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public getWithParts<T>(keyParts: string[]): T {
|
|
55
|
+
if (keyParts.length === 0) {
|
|
56
|
+
throw new Error("Cannot use an empty key");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let obj = this.values;
|
|
60
|
+
|
|
61
|
+
for (const part of keyParts) {
|
|
62
|
+
if (typeof obj[part] === "undefined") {
|
|
63
|
+
throw new Error(`Could not find value for key ${keyParts.join(".")}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
obj = obj[part];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return obj as T;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public has(key: string): boolean {
|
|
73
|
+
try {
|
|
74
|
+
this.get(key);
|
|
75
|
+
} catch {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public set<T>(key: string, value: T): void {
|
|
83
|
+
const parts = key.split(".");
|
|
84
|
+
this.setWithParts<T>(parts, value);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public setWithParts<T>(keyParts: string[], value: T): void {
|
|
88
|
+
if (keyParts.length === 0) {
|
|
89
|
+
throw new Error("Cannot use an empty key");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const obj: any = {};
|
|
93
|
+
for (let i = 0; i < keyParts.length - 1; i++) {
|
|
94
|
+
obj[keyParts[i]] = {};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
obj[keyParts[keyParts.length - 1]] = value;
|
|
98
|
+
|
|
99
|
+
merge(this.customValues, obj);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public async save(): Promise<void> {
|
|
103
|
+
const fileContents = JSON.stringify(this.customValues, null, 2);
|
|
104
|
+
await fs.promises.writeFile(this.configDir, fileContents, {
|
|
105
|
+
encoding: "utf-8",
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/index.ts
ADDED
package/src/loader.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { parse as parseJsonc } from "jsonc-parser";
|
|
4
|
+
import JSON5 from "json5";
|
|
5
|
+
import YAML from "js-yaml";
|
|
6
|
+
import merge from "lodash.merge";
|
|
7
|
+
|
|
8
|
+
export class Loader {
|
|
9
|
+
public static loadFile(filePath: string): any {
|
|
10
|
+
if (!fs.existsSync(filePath)) {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const fileContents = fs.readFileSync(filePath, {
|
|
15
|
+
encoding: "utf-8",
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const fileParts = filePath.split(".");
|
|
19
|
+
const extension = fileParts[fileParts.length - 1];
|
|
20
|
+
|
|
21
|
+
if (/^ya?ml$/i.exec(extension)) {
|
|
22
|
+
// yaml file
|
|
23
|
+
return YAML.load(fileContents);
|
|
24
|
+
} else if (/^json$/i.exec(extension)) {
|
|
25
|
+
// json file
|
|
26
|
+
return JSON.parse(fileContents);
|
|
27
|
+
} else if (/^jsonc$/i.exec(extension)) {
|
|
28
|
+
// jsonc file
|
|
29
|
+
return parseJsonc(fileContents); // todo: handle error callback
|
|
30
|
+
} else if (/^json5$/i.exec(extension)) {
|
|
31
|
+
// json5 file
|
|
32
|
+
return JSON5.parse(fileContents);
|
|
33
|
+
} else {
|
|
34
|
+
console.log(
|
|
35
|
+
`Invalid file name ${filePath}. Only yml, yaml, json, jsonc, json5 extensions are supported (case insensitive).`
|
|
36
|
+
);
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public static load(folder: string): any {
|
|
42
|
+
if (!fs.existsSync(folder)) {
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const contents = fs.readdirSync(folder, {
|
|
47
|
+
encoding: "utf-8",
|
|
48
|
+
withFileTypes: true,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const baseObj: any = {};
|
|
52
|
+
|
|
53
|
+
for (const content of contents) {
|
|
54
|
+
if (!content.isDirectory() && content.name.startsWith("index.")) {
|
|
55
|
+
merge(baseObj, Loader.loadFile(path.join(folder, content.name)));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const content of contents) {
|
|
60
|
+
if (content.isDirectory()) {
|
|
61
|
+
const key = content.name;
|
|
62
|
+
|
|
63
|
+
if (typeof baseObj[key] !== "undefined") {
|
|
64
|
+
console.log(
|
|
65
|
+
`Could not load the directory ${key} because we already loaded a config for a file with the same name`
|
|
66
|
+
);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const obj = Loader.load(path.join(folder, content.name));
|
|
71
|
+
|
|
72
|
+
baseObj[key] = obj;
|
|
73
|
+
} else {
|
|
74
|
+
if (content.name.startsWith("index.")) {
|
|
75
|
+
// we already loaded this to be be in the base config
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const fileParts = content.name.split(".");
|
|
80
|
+
if (fileParts.length === 2) {
|
|
81
|
+
const key = fileParts[0];
|
|
82
|
+
|
|
83
|
+
if (typeof baseObj[key] !== "undefined") {
|
|
84
|
+
console.log(
|
|
85
|
+
`Could not load the file ${path.join(
|
|
86
|
+
folder,
|
|
87
|
+
content.name
|
|
88
|
+
)} because we already loaded a config for a folder with the same name`
|
|
89
|
+
);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const obj = Loader.loadFile(path.join(folder, content.name));
|
|
94
|
+
|
|
95
|
+
baseObj[key] = obj;
|
|
96
|
+
} else {
|
|
97
|
+
console.log(
|
|
98
|
+
`Invalid file name ${content.name}. Config files must have a supported extension and contain no extra periods in the file name`
|
|
99
|
+
);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return baseObj;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { Config } from "../../src/config";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
describe("Config with env folder", () => {
|
|
6
|
+
test("correctly loaded production directory", () => {
|
|
7
|
+
const config = new Config({
|
|
8
|
+
configDir: path.join(__dirname),
|
|
9
|
+
configEnv: "production",
|
|
10
|
+
});
|
|
11
|
+
expect(config.get<number>("thing.test")).toBe(2);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("correctly loaded dev directory", () => {
|
|
15
|
+
const config = new Config({
|
|
16
|
+
configDir: path.join(__dirname),
|
|
17
|
+
configEnv: "dev",
|
|
18
|
+
});
|
|
19
|
+
expect(config.get<number>("thing.test")).toBe(3);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("correctly shows default value for invalid env directory", () => {
|
|
23
|
+
const config = new Config({
|
|
24
|
+
configDir: path.join(__dirname),
|
|
25
|
+
configEnv: "invalid",
|
|
26
|
+
});
|
|
27
|
+
expect(config.get<number>("thing.test")).toBe(1);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 3
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 2
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { Config } from "../../src/config";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
describe("Config with JSON files", () => {
|
|
6
|
+
const config = new Config({
|
|
7
|
+
configDir: path.join(__dirname),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("correctly loaded default directory", () => {
|
|
11
|
+
expect(config.get<string>("hello")).toBe("world");
|
|
12
|
+
expect(config.get<number>("foo.bar")).toBe(42);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("correctly loaded sub default directory", () => {
|
|
16
|
+
const arr = config.get<number[]>("goodbye.universe");
|
|
17
|
+
expect(arr.length).toBe(4);
|
|
18
|
+
expect(arr[0]).toBe(1);
|
|
19
|
+
expect(arr[1]).toBe(3);
|
|
20
|
+
expect(arr[2]).toBe(3);
|
|
21
|
+
expect(arr[3]).toBe(7);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { Config } from "../../src/config";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
describe("Config with JSON5 files", () => {
|
|
6
|
+
const config = new Config({
|
|
7
|
+
configDir: path.join(__dirname),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("correctly loaded default directory", () => {
|
|
11
|
+
expect(config.get<string>("hello")).toBe("world");
|
|
12
|
+
expect(config.get<number>("foo.bar")).toBe(42);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("correctly loaded sub default directory", () => {
|
|
16
|
+
const arr = config.get<number[]>("goodbye.universe");
|
|
17
|
+
expect(arr.length).toBe(4);
|
|
18
|
+
expect(arr[0]).toBe(1);
|
|
19
|
+
expect(arr[1]).toBe(3);
|
|
20
|
+
expect(arr[2]).toBe(3);
|
|
21
|
+
expect(arr[3]).toBe(7);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { Config } from "../../src/config";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
describe("Config with JSONC files", () => {
|
|
6
|
+
const config = new Config({
|
|
7
|
+
configDir: path.join(__dirname),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("correctly loaded default directory", () => {
|
|
11
|
+
expect(config.get<string>("hello")).toBe("world");
|
|
12
|
+
expect(config.get<number>("foo.bar")).toBe(42);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("correctly loaded sub default directory", () => {
|
|
16
|
+
const arr = config.get<number[]>("goodbye.universe");
|
|
17
|
+
expect(arr.length).toBe(4);
|
|
18
|
+
expect(arr[0]).toBe(1);
|
|
19
|
+
expect(arr[1]).toBe(3);
|
|
20
|
+
expect(arr[2]).toBe(3);
|
|
21
|
+
expect(arr[3]).toBe(7);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { Config } from "../../src/config";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
describe("Config with local folder", () => {
|
|
6
|
+
const config = new Config({
|
|
7
|
+
configDir: path.join(__dirname),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("correctly loaded local directory", () => {
|
|
11
|
+
expect(config.get<number>("thing.test")).toBe(3);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 3
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 2
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { Config } from "../../src/config";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
describe("Config with mixed file types", () => {
|
|
6
|
+
const config = new Config({
|
|
7
|
+
configDir: path.join(__dirname),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("correctly loaded default directory", () => {
|
|
11
|
+
expect(config.get<string>("hello")).toBe("world");
|
|
12
|
+
expect(config.get<number>("foo.bar")).toBe(42);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("correctly loaded sub default directory", () => {
|
|
16
|
+
const arr = config.get<number[]>("goodbye.universe");
|
|
17
|
+
expect(arr.length).toBe(4);
|
|
18
|
+
expect(arr[0]).toBe(1);
|
|
19
|
+
expect(arr[1]).toBe(3);
|
|
20
|
+
expect(arr[2]).toBe(3);
|
|
21
|
+
expect(arr[3]).toBe(7);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hello: world
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { Config } from "../../src/config";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
describe("Config with override folder", () => {
|
|
6
|
+
test("correctly loaded production directory", () => {
|
|
7
|
+
const config = new Config({
|
|
8
|
+
configDir: path.join(__dirname),
|
|
9
|
+
configEnv: "production",
|
|
10
|
+
});
|
|
11
|
+
expect(config.get<number>("thing.test")).toBe(4);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 3
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
test: 2
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, test } from "@jest/globals";
|
|
2
|
+
import { Config } from "../../src/config";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
describe("Config with YAML files", () => {
|
|
6
|
+
const config = new Config({
|
|
7
|
+
configDir: path.join(__dirname),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("correctly loaded default directory", () => {
|
|
11
|
+
expect(config.get<string>("hello")).toBe("world");
|
|
12
|
+
expect(config.get<number>("foo.bar")).toBe(42);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("correctly loaded sub default directory", () => {
|
|
16
|
+
const arr = config.get<number[]>("goodbye.universe");
|
|
17
|
+
expect(arr.length).toBe(4);
|
|
18
|
+
expect(arr[0]).toBe(1);
|
|
19
|
+
expect(arr[1]).toBe(3);
|
|
20
|
+
expect(arr[2]).toBe(3);
|
|
21
|
+
expect(arr[3]).toBe(7);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
bar: 42
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hello: world
|