The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { envKeys } from "./Log.ts"; |
| 5 | import { resolveSecretsOption, type SecretsLayer, type SecretsOption } from "./Provider.ts"; |
| 6 | |
| 7 | export interface ProcessEnvOptions { |
| 8 | /** |
| 9 | * Leave the process environment out of this stack entirely. The entry |
| 10 | * stays in the list but contributes nothing. |
| 11 | * @default false |
| 12 | */ |
| 13 | disabled?: boolean; |
| 14 | } |
| 15 | |
| 16 | /** A `secrets` entry that loads the process environment. */ |
| 17 | export class ProcessEnvProvider extends Data.TaggedClass("alchemy/SecretProvider::ProcessEnv")<{ |
| 18 | readonly layer: SecretsLayer; |
| 19 | }> {} |
| 20 | |
| 21 | /** |
| 22 | * The process environment as a secrets provider. |
| 23 | * |
| 24 | * It is implicit: every stack ends its `secrets` list with the shell, so a |
| 25 | * variable exported in the terminal or by CI overrides every provider. List |
| 26 | * it yourself to put it elsewhere, typically first so it becomes a fallback |
| 27 | * that a secrets manager overrides, or pass `disabled: true` to leave it out: |
| 28 | * |
| 29 | * ```ts |
| 30 | * secrets: [Secrets.ProcessEnv(), Doppler.Secrets({ project: "app", config: "prd" })] |
| 31 | * secrets: [Doppler.Secrets({ project: "app", config: "prd" }), Secrets.ProcessEnv({ disabled: true })] |
| 32 | * ``` |
| 33 | */ |
| 34 | export const ProcessEnv = (options: SecretsOption<ProcessEnvOptions> = {}) => |
| 35 | new ProcessEnvProvider({ |
| 36 | layer: ConfigProvider.layerAdd( |
| 37 | Effect.gen(function* () { |
| 38 | const resolved = yield* resolveSecretsOption(options); |
| 39 | if (resolved.disabled) return ConfigProvider.fromEnv({ env: {} }); |
| 40 | const environment = ConfigProvider.fromEnv({ |
| 41 | preserveEmptyStrings: true, |
| 42 | }); |
| 43 | // A shell has far too many variables to list; the count is enough. |
| 44 | const keys = yield* envKeys(environment); |
| 45 | yield* Effect.logDebug(`Loaded ${keys.length} secrets from the process environment`); |
| 46 | return environment; |
| 47 | }), |
| 48 | { asPrimary: true }, |
| 49 | ), |
| 50 | }); |