for (let i = 0; i < template.length; i++) { if (template[i] === "$") { // Escaped: $${VAR} -> literal ${VAR} if (template[i + 1] === "$" && template[i + 2] === "{") { const start = i + 3; const end = template.indexOf("}", start); if (end !== -1) { const name = template.slice(start, end); if (ENV_VAR_NAME.test(name)) {
chunks.push(`\${${name}}`);
i = end; continue;
}
}
}
// Substitution: ${VAR} -> env value if (template[i + 1] === "{") { const start = i + 2; const end = template.indexOf("}", start); if (end !== -1) { const name = template.slice(start, end); if (ENV_VAR_NAME.test(name)) { const val = env[name]; if (val === undefined || val === "") { returnnull;
}
chunks.push(val);
i = end; continue;
}
}
}
}
chunks.push(template[i]);
}
return chunks.join("");
}
/** *Deep-walktheincomingconfigandrestore`${VAR}`referencesfromthe *pre-substitutionparsedconfigwherevertheresolvedvaluematches. * *@paramincoming-Theresolvedconfigabouttobewritten *@paramparsed-Thepre-substitutionparsedconfig(fromthecurrentfileondisk) *@paramenv-Environmentvariablesforverification *@returnsAnewconfigobjectwithenvvarreferencesrestoredwhereappropriate
*/
export function restoreEnvVarRefs(
incoming: unknown,
parsed: unknown,
env: NodeJS.ProcessEnv = process.env,
): unknown { // If parsed has no env var refs at this level, return incoming as-is if (parsed === null || parsed === undefined) { return incoming;
}
// String leaf: check if parsed was a ${VAR} template that resolves to incoming if (typeof incoming === "string" && typeof parsed === "string") { if (hasEnvVarRef(parsed)) { const resolved = tryResolveString(parsed, env); if (resolved === incoming) { // The incoming value matches what the env var resolves to — restore the reference return parsed;
}
} return incoming;
}
// Arrays: walk element by element if (Array.isArray(incoming) && Array.isArray(parsed)) { return incoming.map((item, i) =>
i < parsed.length ? restoreEnvVarRefs(item, parsed[i], env) : item,
);
}
// Objects: walk key by key if (isPlainObject(incoming) && isPlainObject(parsed)) { const result: Record<string, unknown> = {}; for (const [key, value] of Object.entries(incoming)) { if (key in parsed) {
result[key] = restoreEnvVarRefs(value, parsed[key], env);
} else { // New key added by caller — keep as-is
result[key] = value;
}
} return result;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.