mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-11-05 07:39:46 +00:00
* 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
25 lines
1,002 B
TypeScript
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
|
|
}
|