Skip to content

Commit

Permalink
feat: accounting for undefined variables in environment variable repl…
Browse files Browse the repository at this point in the history
…acement
  • Loading branch information
Varun0157 committed Jun 10, 2024
1 parent 98ae00d commit c541655
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/variableParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,34 @@ export function getEnvironments(bundleContent: string | undefined, varFileConten
return [...uniqueNames];
}

function replaceEnvironmentVariables(vars: Variables): Variables {
function replaceEnvironmentVariables(vars: Variables): {
replacedVars: Variables;
undefinedVars: string[];
} {
const PREFIX = "$env.";

const undefinedVars: string[] = [];
const getVal = (val: any): any => {
if (typeof val !== "string" || !val.startsWith(PREFIX)) return val;

const envVarName = val.slice(PREFIX.length);
return envVarName in process.env ? process.env[envVarName] : val;
if (envVarName in process.env) return process.env[envVarName];

undefinedVars.push(envVarName);
};

const replacedVars: Variables = {};
for (const key in vars) replacedVars[key] = getVal(vars[key]);

return replacedVars;
return { replacedVars, undefinedVars };
}

export function loadVariables(
envName: string | undefined,
bundleContent: string | undefined,
varFileContents: string[],
): Variables {
if (!envName) return {};
): { vars: Variables; undefinedVars: string[] } {
if (!envName) return { vars: {}, undefinedVars: [] };

const allBundleVariables = getBundleVariables(bundleContent);
const bundleVars: Variables = allBundleVariables[envName] ?? {};
Expand All @@ -71,6 +77,7 @@ export function loadVariables(
});

const basicVars = Object.assign({}, envVars, bundleVars);
const envReplaced = replaceEnvironmentVariables(basicVars);
return envReplaced;
const { replacedVars: vars, undefinedVars } = replaceEnvironmentVariables(basicVars);

return { vars, undefinedVars };
}

0 comments on commit c541655

Please sign in to comment.