| 1 | import * as ScriptProgress from "@t3tools/provider-core/server/ScriptProgress"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | |
| 4 | const ShellEnvironmentPolicy = Schema.StructWithRest( |
| 5 | Schema.Struct({ |
| 6 | set: Schema.optionalKey(Schema.NullOr(Schema.Record(Schema.String, Schema.String))), |
| 7 | include_only: Schema.optionalKey(Schema.NullOr(Schema.Array(Schema.String))), |
| 8 | }), |
| 9 | [Schema.Record(Schema.String, Schema.Json)], |
| 10 | ); |
| 11 | |
| 12 | const decodeShellEnvironmentPolicy = Schema.decodeUnknownSync(ShellEnvironmentPolicy); |
| 13 | |
| 14 | export function codexScriptProgressConfig( |
| 15 | policy: Schema.Json | undefined, |
| 16 | environment: Readonly<Record<string, string>>, |
| 17 | inheritedNodeOptions?: string, |
| 18 | ): Readonly<Record<string, Schema.Json>> { |
| 19 | const current = decodeShellEnvironmentPolicy(policy ?? {}); |
| 20 | const nodeOptions = [inheritedNodeOptions, current.set?.NODE_OPTIONS].filter(Boolean).join(" "); |
| 21 | const shellEnvironment = { |
| 22 | ...current.set, |
| 23 | ...(nodeOptions ? { NODE_OPTIONS: nodeOptions } : {}), |
| 24 | }; |
| 25 | const merged = ScriptProgress.withScriptProgressEnvironment(shellEnvironment, environment); |
| 26 | return { |
| 27 | "shell_environment_policy.set": merged, |
| 28 | ...(current.include_only == null || current.include_only.length === 0 |
| 29 | ? {} |
| 30 | : { |
| 31 | "shell_environment_policy.include_only": [ |
| 32 | ...new Set([...current.include_only, ...Object.keys(environment)]), |
| 33 | ], |
| 34 | }), |
| 35 | }; |
| 36 | } |