( value: unknown, variables: Record<string, string | string[]>, )
| 16 | * @returns The processed value with all variables replaced |
| 17 | */ |
| 18 | export function replaceVariables( |
| 19 | value: unknown, |
| 20 | variables: Record<string, string | string[]>, |
| 21 | ): unknown { |
| 22 | if (typeof value === "string") { |
| 23 | let result = value; |
| 24 | |
| 25 | // Replace all variables in the string |
| 26 | for (const [key, replacement] of Object.entries(variables)) { |
| 27 | const pattern = new RegExp(`\\$\\{${key}\\}`, "g"); |
| 28 | |
| 29 | // Check if this pattern actually exists in the string |
| 30 | if (result.match(pattern)) { |
| 31 | if (Array.isArray(replacement)) { |
| 32 | console.warn( |
| 33 | `Cannot replace ${key} with array value in string context: "${value}"`, |
| 34 | { key, replacement }, |
| 35 | ); |
| 36 | } else { |
| 37 | result = result.replace(pattern, replacement); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return result; |
| 43 | } else if (Array.isArray(value)) { |
| 44 | // For arrays, we need to handle special case of array expansion |
| 45 | const result: unknown[] = []; |
| 46 | |
| 47 | for (const item of value) { |
| 48 | if ( |
| 49 | typeof item === "string" && |
| 50 | item.match(/^\$\{user_config\.[^}]+\}$/) |
| 51 | ) { |
| 52 | // This is a user config variable that might expand to multiple values |
| 53 | const varName = item.match(/^\$\{([^}]+)\}$/)?.[1]; |
| 54 | if (varName && variables[varName]) { |
| 55 | const replacement = variables[varName]; |
| 56 | if (Array.isArray(replacement)) { |
| 57 | // Expand array inline |
| 58 | result.push(...replacement); |
| 59 | } else { |
| 60 | result.push(replacement); |
| 61 | } |
| 62 | } else { |
| 63 | // Variable not found, keep original |
| 64 | result.push(item); |
| 65 | } |
| 66 | } else { |
| 67 | // Recursively process non-variable items |
| 68 | result.push(replaceVariables(item, variables)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return result; |
| 73 | } else if (value && typeof value === "object") { |
| 74 | const result: Record<string, unknown> = {}; |
| 75 | for (const [key, val] of Object.entries(value)) { |
no outgoing calls
no test coverage detected
searching dependent graphs…