forked from rudderlabs/rudder-transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvUtils.ts
More file actions
70 lines (63 loc) · 1.63 KB
/
Copy pathenvUtils.ts
File metadata and controls
70 lines (63 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
export interface EnvOverride {
[key: string]: string | undefined;
}
export interface EnvSnapshot {
[key: string]: string | undefined;
}
export class EnvManager {
private snapshots: Map<string, EnvSnapshot> = new Map();
/**
* Take a snapshot of current environment variables
* @param id Unique identifier for the snapshot
* @param keys Array of environment variable keys to snapshot
*/
takeSnapshot(id: string, keys: string[]): void {
const snapshot: EnvSnapshot = {};
keys.forEach((key) => {
snapshot[key] = process.env[key];
});
this.snapshots.set(id, snapshot);
}
/**
* Apply environment variable overrides
* @param overrides Object with environment variable overrides
*/
applyOverrides(overrides: EnvOverride): void {
Object.entries(overrides).forEach(([key, value]) => {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
});
}
/**
* Restore environment variables from snapshot
* @param id Snapshot identifier to restore from
*/
restoreSnapshot(id: string): void {
const snapshot = this.snapshots.get(id);
if (snapshot) {
Object.entries(snapshot).forEach(([key, value]) => {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
});
this.snapshots.delete(id);
}
}
/**
* Clean up all snapshots
*/
cleanup(): void {
this.snapshots.clear();
}
/**
* Get the number of active snapshots (for debugging)
*/
getSnapshotCount(): number {
return this.snapshots.size;
}
}