Skip to content
Source
Sign inSign up

instanceEnvironment.ts

33 lines1.3 KB

1import type { ProviderInstanceEnvironment } from "@t3tools/contracts";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as Effect from "effect/Effect";
4​
5import { expandHomeVariable } from "./pathExpansion.ts";
6​
7export const mergeProviderInstanceEnvironment = Effect.fn(function* (
8 environment: ProviderInstanceEnvironment | undefined,
9 baseEnv: NodeJS.ProcessEnv = process.env,
10) {
11 if (!environment || environment.length === 0) {
12 return baseEnv;
13 }
14​
15 const home = yield* HostProcess.HomeDirectory;
16 const windows = (yield* HostProcess.Platform) === "win32";
17 const next: NodeJS.ProcessEnv = { ...baseEnv };
18 for (const variable of environment) {
19 // An unresolved secret reference carries no value; the registry resolves it for the provider.
20 if (typeof variable.value !== "string") continue;
21 // Windows reads names without regard to case, and a child gets the first spelling in sort
22 // order. Upper case sorts first, so the configured value wins even where a spawn merges the
23 // parent environment back in.
24 const name = windows ? variable.name.toUpperCase() : variable.name;
25 if (windows) {
26 for (const inherited of Object.keys(next)) {
27 if (inherited.toUpperCase() === name) delete next[inherited];
28 }
29 }
30 next[name] = expandHomeVariable(name, variable.value, home);
31 }
32 return next;
33});