@incanta/config
Advanced tools
Comparing version 0.1.10 to 0.1.11
@@ -35,5 +35,4 @@ "use strict"; | ||
} | ||
const localValues = loader_1.Loader.load(path_1.default.join(this.configDir, "local")); | ||
const overrideValues = loader_1.Loader.loadFile(path_1.default.join(this.configDir, "override.json")); | ||
(0, lodash_merge_1.default)(this.values, defaultValues, envValues, localValues, overrideValues); | ||
(0, lodash_merge_1.default)(this.values, defaultValues, envValues, overrideValues); | ||
// load the environment variables that are configured to be injected | ||
@@ -40,0 +39,0 @@ // using config-env |
@@ -48,2 +48,22 @@ "use strict"; | ||
} | ||
const baseObj = {}; | ||
const configFiles = ["_config.json", "config.json"]; | ||
for (const configFile of configFiles) { | ||
if (fs_1.default.existsSync(path_1.default.join(folder, configFile))) { | ||
try { | ||
const folderConfig = JSON.parse(fs_1.default.readFileSync(path_1.default.join(folder, configFile), { | ||
encoding: "utf-8", | ||
})); | ||
if (folderConfig.parentName) { | ||
console.log(`Loading parent config ${folderConfig.parentName}`); | ||
(0, lodash_merge_1.default)(baseObj, Loader.load(path_1.default.join(folder, "..", folderConfig.parentName))); | ||
} | ||
break; | ||
} | ||
catch (e) { | ||
console.error(`Invalid JSON in ${path_1.default.join(folder, configFile)} file; skipping configuration`); | ||
break; | ||
} | ||
} | ||
} | ||
const contents = fs_1.default.readdirSync(folder, { | ||
@@ -53,3 +73,2 @@ encoding: "utf-8", | ||
}); | ||
const baseObj = {}; | ||
for (const content of contents) { | ||
@@ -63,8 +82,7 @@ if (!content.isDirectory() && /^_?index\./.exec(content.name) !== null) { | ||
const key = content.name; | ||
if (typeof baseObj[key] !== "undefined") { | ||
console.log(`Could not load the directory ${key} because we already loaded a config for a file with the same name`); | ||
continue; | ||
if (typeof baseObj[key] === "undefined") { | ||
baseObj[key] = {}; | ||
} | ||
const obj = Loader.load(path_1.default.join(folder, content.name)); | ||
baseObj[key] = obj; | ||
(0, lodash_merge_1.default)(baseObj[key], obj); | ||
} | ||
@@ -79,8 +97,7 @@ else { | ||
const key = fileParts[0]; | ||
if (typeof baseObj[key] !== "undefined") { | ||
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`); | ||
continue; | ||
if (typeof baseObj[key] === "undefined") { | ||
baseObj[key] = {}; | ||
} | ||
const obj = Loader.loadFile(path_1.default.join(folder, content.name)); | ||
baseObj[key] = obj; | ||
(0, lodash_merge_1.default)(baseObj[key], obj); | ||
} | ||
@@ -87,0 +104,0 @@ else { |
{ | ||
"name": "@incanta/config", | ||
"version": "0.1.10", | ||
"version": "0.1.11", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "exports": { |
@@ -46,4 +46,2 @@ import merge from "lodash.merge"; | ||
const localValues = Loader.load(path.join(this.configDir, "local")); | ||
const overrideValues = Loader.loadFile( | ||
@@ -53,3 +51,3 @@ path.join(this.configDir, "override.json") | ||
merge(this.values, defaultValues, envValues, localValues, overrideValues); | ||
merge(this.values, defaultValues, envValues, overrideValues); | ||
@@ -56,0 +54,0 @@ // load the environment variables that are configured to be injected |
@@ -8,2 +8,6 @@ import fs from "fs"; | ||
export interface IConfigOptions { | ||
parentName?: string; | ||
} | ||
export class Loader { | ||
@@ -47,2 +51,36 @@ public static loadFile(filePath: string): any { | ||
const baseObj: any = {}; | ||
const configFiles = ["_config.json", "config.json"]; | ||
for (const configFile of configFiles) { | ||
if (fs.existsSync(path.join(folder, configFile))) { | ||
try { | ||
const folderConfig: IConfigOptions = JSON.parse( | ||
fs.readFileSync(path.join(folder, configFile), { | ||
encoding: "utf-8", | ||
}) | ||
); | ||
if (folderConfig.parentName) { | ||
console.log(`Loading parent config ${folderConfig.parentName}`); | ||
merge( | ||
baseObj, | ||
Loader.load(path.join(folder, "..", folderConfig.parentName)) | ||
); | ||
} | ||
break; | ||
} catch (e: any) { | ||
console.error( | ||
`Invalid JSON in ${path.join( | ||
folder, | ||
configFile | ||
)} file; skipping configuration` | ||
); | ||
break; | ||
} | ||
} | ||
} | ||
const contents = fs.readdirSync(folder, { | ||
@@ -53,4 +91,2 @@ encoding: "utf-8", | ||
const baseObj: any = {}; | ||
for (const content of contents) { | ||
@@ -66,7 +102,4 @@ if (!content.isDirectory() && /^_?index\./.exec(content.name) !== null) { | ||
if (typeof baseObj[key] !== "undefined") { | ||
console.log( | ||
`Could not load the directory ${key} because we already loaded a config for a file with the same name` | ||
); | ||
continue; | ||
if (typeof baseObj[key] === "undefined") { | ||
baseObj[key] = {}; | ||
} | ||
@@ -76,3 +109,3 @@ | ||
baseObj[key] = obj; | ||
merge(baseObj[key], obj); | ||
} else { | ||
@@ -88,10 +121,4 @@ if (/^_?index\./.exec(content.name) !== null) { | ||
if (typeof baseObj[key] !== "undefined") { | ||
console.log( | ||
`Could not load the file ${path.join( | ||
folder, | ||
content.name | ||
)} because we already loaded a config for a folder with the same name` | ||
); | ||
continue; | ||
if (typeof baseObj[key] === "undefined") { | ||
baseObj[key] = {}; | ||
} | ||
@@ -101,3 +128,3 @@ | ||
baseObj[key] = obj; | ||
merge(baseObj[key], obj); | ||
} else { | ||
@@ -104,0 +131,0 @@ console.log( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
35318
22
627