Roo-Code/src/utils/config.ts
Trung Dang 01a7a66ca0
feat: add injectEnv util, support env ref in mcp config (#2679)
* feat: support environment variables reference in mcp `env` config

* tests(src/utils/config): add test for `injectEnv`

* fix(injectEnv): use `env == null` and `??` check instead of `!env`, `||`

* refactor: remove unnecessary type declare

* chore!: simplify regexp, remove replacement for env vars with dots
2025-04-23 03:13:04 -04:00

25 lines
1,002 B
TypeScript

/**
* Deeply injects environment variables into a configuration object/string/json
*
* Uses VSCode env:name pattern: https://code.visualstudio.com/docs/reference/variables-reference#_environment-variables
*
* Does not mutate original object
*/
export async function injectEnv(config: string | Record<PropertyKey, any>, notFoundValue: any = "") {
// Use simple regex replace for now, will see if object traversal and recursion is needed here (e.g: for non-serializable objects)
const isObject = typeof config === "object"
let _config = isObject ? JSON.stringify(config) : config
_config = _config.replace(/\$\{env:([\w]+)\}/g, (_, name) => {
// Check if null or undefined
// intentionally using == to match null | undefined
// eslint-disable-next-line eqeqeq
if (process.env[name] == null)
console.warn(`[injectEnv] env variable ${name} referenced but not found in process.env`)
return process.env[name] ?? notFoundValue
})
return isObject ? JSON.parse(_config) : _config
}