@incanta/config
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -9,3 +9,7 @@ export interface IConfigOptions { | ||
private customValues; | ||
private envVarConfig; | ||
constructor(options?: IConfigOptions); | ||
dir(): string; | ||
private getFormattedConfig; | ||
getConfiguredEnv(): any; | ||
get<T>(key: string): T; | ||
@@ -12,0 +16,0 @@ getWithParts<T>(keyParts: string[]): T; |
@@ -15,2 +15,3 @@ "use strict"; | ||
customValues; | ||
envVarConfig; | ||
constructor(options) { | ||
@@ -38,3 +39,49 @@ this.values = {}; | ||
(0, lodash_merge_1.default)(this.values, defaultValues, envValues, localValues, overrideValues); | ||
// load the environment variables that are configured to be injected | ||
// using config-env | ||
const dirContents = fs_1.default.readdirSync(this.configDir, { encoding: "utf-8" }); | ||
this.envVarConfig = {}; | ||
for (const file of dirContents) { | ||
if (file.startsWith("environment.")) { | ||
this.envVarConfig = loader_1.Loader.loadFile(path_1.default.join(this.configDir, file)); | ||
break; | ||
} | ||
} | ||
} | ||
dir() { | ||
return this.configDir; | ||
} | ||
getFormattedConfig(format) { | ||
let value = ""; | ||
while (format.length > 0) { | ||
const match = /\${([a-z.]+)}/g.exec(format); | ||
if (match === null) { | ||
value += format; | ||
format = ""; | ||
} | ||
else { | ||
value += format.slice(0, match.index); | ||
format = format.substring(match.index + match[0].length); | ||
const configKey = match[1]; | ||
value += `${this.get(configKey)}`; | ||
} | ||
} | ||
return value; | ||
} | ||
getConfiguredEnv() { | ||
const extraEnv = {}; | ||
const envKeys = Object.keys(this.envVarConfig); | ||
for (const key of envKeys) { | ||
const configKey = this.envVarConfig[key]; | ||
const envValue = this.get(configKey); | ||
if (typeof envValue === "object" && | ||
typeof envValue.format !== "undefined") { | ||
extraEnv[key] = this.getFormattedConfig(envValue.format); | ||
} | ||
else { | ||
extraEnv[key] = `${envValue}`; | ||
} | ||
} | ||
return extraEnv; | ||
} | ||
get(key) { | ||
@@ -41,0 +88,0 @@ const parts = key.split("."); |
{ | ||
"name": "@incanta/config", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"main": "lib/index.js", | ||
@@ -9,2 +9,6 @@ "exports": { | ||
}, | ||
"bin": { | ||
"config-env": "./lib/config-env.js" | ||
}, | ||
"packageManager": "yarn@1.22.19", | ||
"repository": "git@github.com:IncantaGames/config.git", | ||
@@ -30,2 +34,3 @@ "author": "Incanta Games", | ||
"@typescript-eslint/parser": "^5.48.0", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.31.0", | ||
@@ -32,0 +37,0 @@ "eslint-config-prettier": "^8.6.0", |
@@ -17,2 +17,4 @@ import merge from "lodash.merge"; | ||
private envVarConfig: any; | ||
public constructor(options?: IConfigOptions) { | ||
@@ -52,4 +54,59 @@ this.values = {}; | ||
merge(this.values, defaultValues, envValues, localValues, overrideValues); | ||
// load the environment variables that are configured to be injected | ||
// using config-env | ||
const dirContents = fs.readdirSync(this.configDir, { encoding: "utf-8" }); | ||
this.envVarConfig = {}; | ||
for (const file of dirContents) { | ||
if (file.startsWith("environment.")) { | ||
this.envVarConfig = Loader.loadFile(path.join(this.configDir, file)); | ||
break; | ||
} | ||
} | ||
} | ||
public dir(): string { | ||
return this.configDir; | ||
} | ||
private getFormattedConfig(format: string): string { | ||
let value = ""; | ||
while (format.length > 0) { | ||
const match = /\${([a-z.]+)}/g.exec(format); | ||
if (match === null) { | ||
value += format; | ||
format = ""; | ||
} else { | ||
value += format.slice(0, match.index); | ||
format = format.substring(match.index + match[0].length); | ||
const configKey = match[1]; | ||
value += `${this.get<any>(configKey)}`; | ||
} | ||
} | ||
return value; | ||
} | ||
public getConfiguredEnv(): any { | ||
const extraEnv: any = {}; | ||
const envKeys = Object.keys(this.envVarConfig); | ||
for (const key of envKeys) { | ||
const configKey = this.envVarConfig[key]; | ||
const envValue = this.get<any>(configKey); | ||
if ( | ||
typeof envValue === "object" && | ||
typeof envValue.format !== "undefined" | ||
) { | ||
extraEnv[key] = this.getFormattedConfig(envValue.format); | ||
} else { | ||
extraEnv[key] = `${envValue}`; | ||
} | ||
} | ||
return extraEnv; | ||
} | ||
public get<T>(key: string): T { | ||
@@ -56,0 +113,0 @@ const parts = key.split("."); |
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
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
29625
21
524
16
7
1