|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +/** |
| 7 | + * Environment variable configuration source. |
| 8 | + * |
| 9 | + * Maps SFCC_* environment variables to NormalizedConfig fields. |
| 10 | + * Not included in default sources — opt-in only via `sourcesBefore`. |
| 11 | + * |
| 12 | + * @internal This module is internal to the SDK. Use ConfigResolver instead. |
| 13 | + */ |
| 14 | +import type {AuthMethod} from '../../auth/types.js'; |
| 15 | +import {getPopulatedFields} from '../mapping.js'; |
| 16 | +import type {ConfigSource, ConfigLoadResult, NormalizedConfig, ResolveConfigOptions} from '../types.js'; |
| 17 | +import {getLogger} from '../../logging/logger.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Mapping of SFCC_* environment variable names to NormalizedConfig field names. |
| 21 | + */ |
| 22 | +const ENV_VAR_MAP: Record<string, keyof NormalizedConfig> = { |
| 23 | + SFCC_SERVER: 'hostname', |
| 24 | + SFCC_WEBDAV_SERVER: 'webdavHostname', |
| 25 | + SFCC_CODE_VERSION: 'codeVersion', |
| 26 | + SFCC_USERNAME: 'username', |
| 27 | + SFCC_PASSWORD: 'password', |
| 28 | + SFCC_CERTIFICATE: 'certificate', |
| 29 | + SFCC_CERTIFICATE_PASSPHRASE: 'certificatePassphrase', |
| 30 | + SFCC_SELFSIGNED: 'selfSigned', |
| 31 | + SFCC_CLIENT_ID: 'clientId', |
| 32 | + SFCC_CLIENT_SECRET: 'clientSecret', |
| 33 | + SFCC_OAUTH_SCOPES: 'scopes', |
| 34 | + SFCC_SHORTCODE: 'shortCode', |
| 35 | + SFCC_TENANT_ID: 'tenantId', |
| 36 | + SFCC_AUTH_METHODS: 'authMethods', |
| 37 | + SFCC_ACCOUNT_MANAGER_HOST: 'accountManagerHost', |
| 38 | + SFCC_SANDBOX_API_HOST: 'sandboxApiHost', |
| 39 | +}; |
| 40 | + |
| 41 | +/** Fields that should be parsed as comma-separated arrays. */ |
| 42 | +const ARRAY_FIELDS = new Set<keyof NormalizedConfig>(['scopes', 'authMethods']); |
| 43 | + |
| 44 | +/** Fields that should be parsed as booleans. */ |
| 45 | +const BOOLEAN_FIELDS = new Set<keyof NormalizedConfig>(['selfSigned']); |
| 46 | + |
| 47 | +/** |
| 48 | + * Configuration source that reads SFCC_* environment variables. |
| 49 | + * |
| 50 | + * Priority -10 (higher than dw.json at 0), matching CLI behavior where |
| 51 | + * env vars override file-based config. |
| 52 | + * |
| 53 | + * Not added to default sources — opt-in only. The CLI handles env vars |
| 54 | + * via oclif flag `env:` mappings; this source is for consumers like |
| 55 | + * the VS Code extension that call `resolveConfig()` directly. |
| 56 | + * |
| 57 | + * @example |
| 58 | + * ```typescript |
| 59 | + * import { resolveConfig, EnvSource } from '@salesforce/b2c-tooling-sdk/config'; |
| 60 | + * |
| 61 | + * const config = resolveConfig({}, { |
| 62 | + * sourcesBefore: [new EnvSource()], |
| 63 | + * }); |
| 64 | + * ``` |
| 65 | + * |
| 66 | + * @internal |
| 67 | + */ |
| 68 | +export class EnvSource implements ConfigSource { |
| 69 | + readonly name = 'EnvSource'; |
| 70 | + readonly priority = -10; |
| 71 | + |
| 72 | + private readonly env: Record<string, string | undefined>; |
| 73 | + |
| 74 | + /** |
| 75 | + * @param env - Environment object to read from. Defaults to `process.env`. |
| 76 | + */ |
| 77 | + constructor(env?: Record<string, string | undefined>) { |
| 78 | + this.env = env ?? process.env; |
| 79 | + } |
| 80 | + |
| 81 | + load(_options: ResolveConfigOptions): ConfigLoadResult | undefined { |
| 82 | + const logger = getLogger(); |
| 83 | + const config: NormalizedConfig = {}; |
| 84 | + |
| 85 | + for (const [envVar, configField] of Object.entries(ENV_VAR_MAP)) { |
| 86 | + const value = this.env[envVar]; |
| 87 | + if (value === undefined || value === '') continue; |
| 88 | + |
| 89 | + if (BOOLEAN_FIELDS.has(configField)) { |
| 90 | + (config as Record<string, unknown>)[configField] = value === 'true' || value === '1'; |
| 91 | + } else if (ARRAY_FIELDS.has(configField)) { |
| 92 | + (config as Record<string, unknown>)[configField] = value |
| 93 | + .split(',') |
| 94 | + .map((s) => s.trim()) |
| 95 | + .filter(Boolean) as string[] | AuthMethod[]; |
| 96 | + } else { |
| 97 | + (config as Record<string, unknown>)[configField] = value; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + const fields = getPopulatedFields(config); |
| 102 | + if (fields.length === 0) { |
| 103 | + logger.trace('[EnvSource] No SFCC_* environment variables found'); |
| 104 | + return undefined; |
| 105 | + } |
| 106 | + |
| 107 | + logger.trace({fields}, '[EnvSource] Loaded config from environment variables'); |
| 108 | + |
| 109 | + return {config, location: 'environment variables'}; |
| 110 | + } |
| 111 | +} |
0 commit comments