Skip to content

Commit

Permalink
feat: adding functionality to replace '$env.NAME' instances with proc…
Browse files Browse the repository at this point in the history
…ess.env.NAME
  • Loading branch information
Varun0157 committed May 27, 2024
1 parent 76c1e09 commit f26c6e1
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/variableParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {};

Expand Down

0 comments on commit f26c6e1

Please sign in to comment.