forked from railsware/upterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.ts
70 lines (54 loc) · 1.63 KB
/
Environment.ts
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
import {delimiter} from "path";
import {executeCommandWithShellConfig} from "./PTY";
import {clone} from "lodash";
import {homeDirectory} from "./utils/Common";
const env: Dictionary<string> = {};
export async function loadEnvironment(): Promise<void> {
const lines = await executeCommandWithShellConfig("env");
lines.forEach(line => {
let [key, value] = line.trim().split("=");
env[key] = value;
});
}
export default class Environment {
private storage: Dictionary<string> = clone(env);
set(key: string, value: string): void {
this.storage[key] = value;
}
setMany(pairs: Dictionary<string>): void {
for (const key of Object.keys(pairs)) {
this.set(key, pairs[key]);
}
}
toObject(): ProcessEnvironment {
return <ProcessEnvironment>this.storage;
}
map<R>(mapper: (key: string, value: string) => R): Array<R> {
const result: Array<R> = [];
for (const key of Object.keys(this.storage)) {
result.push(mapper(key, this.storage[key]));
}
return result;
}
get(key: string): string {
return this.storage[key];
}
has(key: string): boolean {
return key in this.storage;
}
get path(): string {
return this.get("PATH");
}
cdpath(pwd: string): string[] {
return (this.get("CDPATH") || "").split(delimiter).map(path => path || pwd);
}
get pwd(): string {
if (!this.get("PWD")) {
this.pwd = homeDirectory();
}
return this.get("PWD");
}
set pwd(value: string) {
this.set("PWD", value);
}
}