( serviceEnv: string | null, projectEnv?: string | null, environmentEnv?: string | null, )
| 397 | }; |
| 398 | |
| 399 | export const prepareEnvironmentVariables = ( |
| 400 | serviceEnv: string | null, |
| 401 | projectEnv?: string | null, |
| 402 | environmentEnv?: string | null, |
| 403 | ) => { |
| 404 | const projectVars = parse(projectEnv ?? ""); |
| 405 | const environmentVars = parse(environmentEnv ?? ""); |
| 406 | const serviceVars = parse(serviceEnv ?? ""); |
| 407 | |
| 408 | const resolvedVars = Object.entries(serviceVars).map(([key, value]) => { |
| 409 | let resolvedValue = value; |
| 410 | |
| 411 | // Replace project variables |
| 412 | if (projectVars) { |
| 413 | resolvedValue = resolvedValue.replace( |
| 414 | /\$\{\{project\.(.*?)\}\}/g, |
| 415 | (_, ref) => { |
| 416 | if (projectVars[ref] !== undefined) { |
| 417 | return projectVars[ref]; |
| 418 | } |
| 419 | throw new Error( |
| 420 | `Invalid project environment variable: project.${ref}`, |
| 421 | ); |
| 422 | }, |
| 423 | ); |
| 424 | } |
| 425 | |
| 426 | // Replace environment variables |
| 427 | if (environmentVars) { |
| 428 | resolvedValue = resolvedValue.replace( |
| 429 | /\$\{\{environment\.(.*?)\}\}/g, |
| 430 | (_, ref) => { |
| 431 | if (environmentVars[ref] !== undefined) { |
| 432 | return environmentVars[ref]; |
| 433 | } |
| 434 | throw new Error(`Invalid environment variable: environment.${ref}`); |
| 435 | }, |
| 436 | ); |
| 437 | } |
| 438 | |
| 439 | // Replace self-references (service variables) |
| 440 | resolvedValue = resolvedValue.replace(/\$\{\{(.*?)\}\}/g, (_, ref) => { |
| 441 | if (serviceVars[ref] !== undefined) { |
| 442 | return serviceVars[ref]; |
| 443 | } |
| 444 | throw new Error(`Invalid service environment variable: ${ref}`); |
| 445 | }); |
| 446 | |
| 447 | return `${key}=${resolvedValue}`; |
| 448 | }); |
| 449 | |
| 450 | return resolvedVars; |
| 451 | }; |
| 452 | |
| 453 | export const prepareEnvironmentVariablesForShell = ( |
| 454 | serviceEnv: string | null, |
no outgoing calls
no test coverage detected