diff --git a/src/variableParser.ts b/src/variableParser.ts index 69f88f9..33ea8ec 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -5,12 +5,29 @@ import { isDict } from "./utils/typeUtils"; import { checkVariables } from "./checkTypes"; import { Variables } from "./variables"; +function replaceProcessEnvVars(vars: Variables): Variables { + // replace all instances of $env.NAME in the values of the dict with process.env.NAME if it exists + const replacedVars: Variables = {}; + for (const key in vars) { + const value = vars[key]; + if (typeof value !== "string") { + replacedVars[key] = value; + continue; + } + + replacedVars[key] = value.replace(/\$env\.([A-Za-z0-9_]+)/g, (match, procVarName) => { + return process.env[procVarName] ?? match; + }); + } + + return replacedVars; +} + // we may pass an empty string if the document is not actually a bundle export function getBundleVariables(doc: string | undefined): Variables { let parsedData = doc ? YAML.parse(doc) : {}; - if (!isDict(parsedData)) { + if (!isDict(parsedData)) throw new Error("Bundle could not be parsed. Is your bundle a valid YAML document?"); - } const variables = parsedData.variables; if (variables !== undefined) { @@ -41,7 +58,7 @@ export function getEnvironments(bundleContent: string | undefined, varFileConten export function loadVariables( envName: string | undefined, bundleContent: string | undefined, - varFileContents: string[], + varFileContents: string[] ): Variables { if (!envName) return {};