@incanta/config
Advanced tools
Comparing version 0.1.16 to 0.1.17
@@ -12,3 +12,2 @@ export interface IConfigOptions { | ||
dir(): string; | ||
private getFormattedConfig; | ||
getConfiguredEnv(): any; | ||
@@ -15,0 +14,0 @@ get<T>(key: string): T; |
@@ -51,19 +51,2 @@ "use strict"; | ||
} | ||
getFormattedConfig(prefix, 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(`${prefix}.${configKey}`)}`; | ||
} | ||
} | ||
return value; | ||
} | ||
getConfiguredEnv() { | ||
@@ -75,9 +58,3 @@ const extraEnv = {}; | ||
const envValue = this.get(configKey); | ||
if (typeof envValue === "object" && | ||
typeof envValue.format !== "undefined") { | ||
extraEnv[key] = this.getFormattedConfig(configKey, envValue.format); | ||
} | ||
else { | ||
extraEnv[key] = `${envValue}`; | ||
} | ||
extraEnv[key] = `${envValue}`; | ||
} | ||
@@ -101,3 +78,3 @@ return extraEnv; | ||
} | ||
const variableRegex = /\$\{[a-zA-Z\-_0-9.]+\}/g; | ||
const variableRegex = /\$\{[a-zA-Z\-_0-9./]+\}/g; | ||
const replaceValue = (value) => { | ||
@@ -107,3 +84,9 @@ const regexResult = value.matchAll(variableRegex); | ||
for (const match of regexResult) { | ||
const keyToReplace = match[0].slice(2, match[0].length - 1); | ||
let keyToReplace = match[0].slice(2, match[0].length - 1); | ||
if (keyToReplace.startsWith("./")) { | ||
// convert relative path to absolute path | ||
keyToReplace = `${keyParts | ||
.slice(0, keyParts.length - 1) | ||
.join(".")}.${keyToReplace.slice(2)}`; | ||
} | ||
const newValue = this.tryGet(keyToReplace); | ||
@@ -125,3 +108,3 @@ if (newValue !== null) { | ||
const value = curObj[property]; | ||
if (typeof value === "string" && property !== "format") { | ||
if (typeof value === "string") { | ||
curObj[property] = replaceValue(value); | ||
@@ -143,8 +126,3 @@ } | ||
else { | ||
if (obj.format) { | ||
obj = this.getFormattedConfig(keyParts.join("."), obj.format); | ||
} | ||
else { | ||
walkObject(obj); | ||
} | ||
walkObject(obj); | ||
} | ||
@@ -151,0 +129,0 @@ } |
{ | ||
"name": "@incanta/config", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "exports": { |
@@ -68,20 +68,2 @@ import merge from "lodash.merge"; | ||
private getFormattedConfig(prefix: string, 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>(`${prefix}.${configKey}`)}`; | ||
} | ||
} | ||
return value; | ||
} | ||
public getConfiguredEnv(): any { | ||
@@ -96,10 +78,3 @@ const extraEnv: any = {}; | ||
if ( | ||
typeof envValue === "object" && | ||
typeof envValue.format !== "undefined" | ||
) { | ||
extraEnv[key] = this.getFormattedConfig(configKey, envValue.format); | ||
} else { | ||
extraEnv[key] = `${envValue}`; | ||
} | ||
extraEnv[key] = `${envValue}`; | ||
} | ||
@@ -130,3 +105,3 @@ | ||
const variableRegex = /\$\{[a-zA-Z\-_0-9.]+\}/g; | ||
const variableRegex = /\$\{[a-zA-Z\-_0-9./]+\}/g; | ||
@@ -138,3 +113,9 @@ const replaceValue = (value: string): string => { | ||
for (const match of regexResult) { | ||
const keyToReplace = match[0].slice(2, match[0].length - 1); | ||
let keyToReplace = match[0].slice(2, match[0].length - 1); | ||
if (keyToReplace.startsWith("./")) { | ||
// convert relative path to absolute path | ||
keyToReplace = `${keyParts | ||
.slice(0, keyParts.length - 1) | ||
.join(".")}.${keyToReplace.slice(2)}`; | ||
} | ||
const newValue = this.tryGet<string | number | boolean>(keyToReplace); | ||
@@ -157,3 +138,3 @@ if (newValue !== null) { | ||
const value = curObj[property]; | ||
if (typeof value === "string" && property !== "format") { | ||
if (typeof value === "string") { | ||
curObj[property] = replaceValue(value); | ||
@@ -173,7 +154,3 @@ } else if (typeof value === "object") { | ||
} else { | ||
if (obj.format) { | ||
obj = this.getFormattedConfig(keyParts.join("."), obj.format); | ||
} else { | ||
walkObject(obj); | ||
} | ||
walkObject(obj); | ||
} | ||
@@ -180,0 +157,0 @@ } |
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
34770
621