@incanta/config
Advanced tools
Comparing version 0.5.5 to 0.5.6
@@ -5,4 +5,12 @@ export interface IConfigOptions { | ||
} | ||
export interface IConfigSettings { | ||
defaults?: { | ||
dir?: string; | ||
env?: string; | ||
}; | ||
extraDirs?: string[]; | ||
} | ||
export default class Config { | ||
private configDir; | ||
private extraConfigDirs; | ||
private values; | ||
@@ -18,2 +26,3 @@ private normalizedValues; | ||
dir(): string; | ||
configEnvDir(configEnv: string): string | null; | ||
getConfiguredEnv(): any; | ||
@@ -20,0 +29,0 @@ get<T>(key: string): T; |
@@ -13,2 +13,3 @@ "use strict"; | ||
configDir = ""; | ||
extraConfigDirs = []; | ||
values; | ||
@@ -43,2 +44,5 @@ normalizedValues; | ||
} | ||
if (configSettings.extraDirs && Array.isArray(configSettings.extraDirs)) { | ||
this.extraConfigDirs = configSettings.extraDirs; | ||
} | ||
} | ||
@@ -50,16 +54,12 @@ this.configDir = | ||
path_1.default.join(process.cwd(), defaultConfigDir); | ||
const configEnvDir = options?.configEnv || process.env["NODE_CONFIG_ENV"] || defaultConfigEnv; | ||
const configFolderOptions = loader_1.Loader.readConfigSettings(path_1.default.join(this.configDir, configEnvDir)); | ||
const configEnv = options?.configEnv || process.env["NODE_CONFIG_ENV"] || defaultConfigEnv; | ||
const configEnvDir = this.configEnvDir(configEnv); | ||
const configFolderOptions = loader_1.Loader.readConfigSettings(configEnvDir || path_1.default.join(this.configDir, "default")); | ||
const defaultValues = loader_1.Loader.loadRoot(path_1.default.join(this.configDir, "default"), { | ||
...configFolderOptions, | ||
parentNames: [], | ||
}); | ||
}, this); | ||
let envValues = {}; | ||
if (configEnvDir) { | ||
if (fs_1.default.existsSync(path_1.default.join(this.configDir, configEnvDir))) { | ||
envValues = loader_1.Loader.loadRoot(path_1.default.join(this.configDir, configEnvDir), configFolderOptions); | ||
} | ||
else if (process.env["NODE_CONFIG_SKIP_ENV_WARNING"] !== "true") { | ||
console.log(`Cannot use config environment '${configEnvDir}' because ${path_1.default.join(this.configDir, configEnvDir)} doesn't exist`); | ||
} | ||
envValues = loader_1.Loader.loadRoot(configEnvDir, configFolderOptions, this); | ||
} | ||
@@ -84,2 +84,16 @@ const overrideValues = loader_1.Loader.loadFile(path_1.default.join(this.configDir, "override.json"), {}); | ||
} | ||
configEnvDir(configEnv) { | ||
if (fs_1.default.existsSync(path_1.default.join(this.configDir, configEnv))) { | ||
return path_1.default.join(this.configDir, configEnv); | ||
} | ||
for (const extraDir of this.extraConfigDirs) { | ||
if (fs_1.default.existsSync(path_1.default.join(extraDir, configEnv))) { | ||
return path_1.default.join(extraDir, configEnv); | ||
} | ||
} | ||
if (process.env["NODE_CONFIG_SKIP_ENV_WARNING"] !== "true") { | ||
console.warn(`Cannot find config environment ${configEnv} in ${this.configDir} or extra dirs: ${this.extraConfigDirs.join(", ")}`); | ||
} | ||
return null; | ||
} | ||
getConfiguredEnv() { | ||
@@ -86,0 +100,0 @@ const extraEnv = {}; |
@@ -0,1 +1,2 @@ | ||
import Config from "./config"; | ||
export interface IConfigFolderOptions { | ||
@@ -9,5 +10,5 @@ variableCasing?: "original" | "camel" | "both"; | ||
static loadFile(filePath: string, options: IConfigFolderOptions): any; | ||
static loadRoot(folder: string, options: IConfigFolderOptions): any; | ||
static loadRoot(folder: string, options: IConfigFolderOptions, config: Config): any; | ||
static load(folder: string, options: IConfigFolderOptions): any; | ||
} | ||
//# sourceMappingURL=loader.d.ts.map |
@@ -117,3 +117,3 @@ "use strict"; | ||
} | ||
static loadRoot(folder, options) { | ||
static loadRoot(folder, options, config) { | ||
const baseObj = {}; | ||
@@ -126,8 +126,10 @@ if (options.parentNames) { | ||
} | ||
const parentFolder = path_1.default.join(folder, "..", parentName); | ||
const parentOptions = Loader.readConfigSettings(parentFolder); | ||
(0, lodash_merge_1.default)(baseObj, Loader.loadRoot(parentFolder, { | ||
...options, | ||
parentNames: parentOptions.parentNames, | ||
})); | ||
const parentFolder = config.configEnvDir(parentName); | ||
if (parentFolder) { | ||
const parentOptions = Loader.readConfigSettings(parentFolder); | ||
(0, lodash_merge_1.default)(baseObj, Loader.loadRoot(parentFolder, { | ||
...options, | ||
parentNames: parentOptions.parentNames, | ||
}, config)); | ||
} | ||
} | ||
@@ -134,0 +136,0 @@ } |
{ | ||
"name": "@incanta/config", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "exports": { |
@@ -12,4 +12,13 @@ import merge from "lodash.merge"; | ||
export interface IConfigSettings { | ||
defaults?: { | ||
dir?: string; | ||
env?: string; | ||
}; | ||
extraDirs?: string[]; | ||
} | ||
export default class Config { | ||
private configDir: string = ""; | ||
private extraConfigDirs: string[] = []; | ||
@@ -44,3 +53,3 @@ private values: any; | ||
if (fs.existsSync(path.join(process.cwd(), "config-settings.json"))) { | ||
const configSettings = JSON.parse( | ||
const configSettings: IConfigSettings = JSON.parse( | ||
fs.readFileSync( | ||
@@ -61,2 +70,6 @@ path.join(process.cwd(), "config-settings.json"), | ||
} | ||
if (configSettings.extraDirs && Array.isArray(configSettings.extraDirs)) { | ||
this.extraConfigDirs = configSettings.extraDirs; | ||
} | ||
} | ||
@@ -70,7 +83,9 @@ | ||
const configEnvDir = | ||
const configEnv = | ||
options?.configEnv || process.env["NODE_CONFIG_ENV"] || defaultConfigEnv; | ||
const configEnvDir = this.configEnvDir(configEnv); | ||
const configFolderOptions = Loader.readConfigSettings( | ||
path.join(this.configDir, configEnvDir) | ||
configEnvDir || path.join(this.configDir, "default") | ||
); | ||
@@ -83,3 +98,4 @@ | ||
parentNames: [], | ||
} | ||
}, | ||
this | ||
); | ||
@@ -89,15 +105,3 @@ | ||
if (configEnvDir) { | ||
if (fs.existsSync(path.join(this.configDir, configEnvDir))) { | ||
envValues = Loader.loadRoot( | ||
path.join(this.configDir, configEnvDir), | ||
configFolderOptions | ||
); | ||
} else if (process.env["NODE_CONFIG_SKIP_ENV_WARNING"] !== "true") { | ||
console.log( | ||
`Cannot use config environment '${configEnvDir}' because ${path.join( | ||
this.configDir, | ||
configEnvDir | ||
)} doesn't exist` | ||
); | ||
} | ||
envValues = Loader.loadRoot(configEnvDir, configFolderOptions, this); | ||
} | ||
@@ -138,2 +142,24 @@ | ||
public configEnvDir(configEnv: string): string | null { | ||
if (fs.existsSync(path.join(this.configDir, configEnv))) { | ||
return path.join(this.configDir, configEnv); | ||
} | ||
for (const extraDir of this.extraConfigDirs) { | ||
if (fs.existsSync(path.join(extraDir, configEnv))) { | ||
return path.join(extraDir, configEnv); | ||
} | ||
} | ||
if (process.env["NODE_CONFIG_SKIP_ENV_WARNING"] !== "true") { | ||
console.warn( | ||
`Cannot find config environment ${configEnv} in ${ | ||
this.configDir | ||
} or extra dirs: ${this.extraConfigDirs.join(", ")}` | ||
); | ||
} | ||
return null; | ||
} | ||
public getConfiguredEnv(): any { | ||
@@ -140,0 +166,0 @@ const extraEnv: any = {}; |
@@ -7,2 +7,3 @@ import fs from "fs"; | ||
import merge from "lodash.merge"; | ||
import Config from "./config"; | ||
@@ -149,3 +150,7 @@ export interface IConfigFolderOptions { | ||
public static loadRoot(folder: string, options: IConfigFolderOptions): any { | ||
public static loadRoot( | ||
folder: string, | ||
options: IConfigFolderOptions, | ||
config: Config | ||
): any { | ||
const baseObj: any = {}; | ||
@@ -159,11 +164,18 @@ | ||
} | ||
const parentFolder = path.join(folder, "..", parentName); | ||
const parentOptions = Loader.readConfigSettings(parentFolder); | ||
merge( | ||
baseObj, | ||
Loader.loadRoot(parentFolder, { | ||
...options, | ||
parentNames: parentOptions.parentNames, | ||
}) | ||
); | ||
const parentFolder = config.configEnvDir(parentName); | ||
if (parentFolder) { | ||
const parentOptions = Loader.readConfigSettings(parentFolder); | ||
merge( | ||
baseObj, | ||
Loader.loadRoot( | ||
parentFolder, | ||
{ | ||
...options, | ||
parentNames: parentOptions.parentNames, | ||
}, | ||
config | ||
) | ||
); | ||
} | ||
} | ||
@@ -170,0 +182,0 @@ } |
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
77626
1415