This repository has been archived by the owner on May 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 617
/
Aliases.ts
63 lines (48 loc) · 1.56 KB
/
Aliases.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
import {executeCommandWithShellConfig} from "../PTY";
import * as _ from "lodash";
export const aliasesFromConfig: Dictionary<string> = {};
export async function loadAliasesFromConfig(): Promise<void> {
const lines = await executeCommandWithShellConfig("alias");
lines.map(parseAlias).forEach(parsed => aliasesFromConfig[parsed.name] = parsed.value);
}
export function parseAlias(line: string) {
let [short, long] = line.split("=");
if (short && long) {
const nameCapture = /(alias )?(.*)/.exec(short);
const valueCapture = /'?([^']*)'?/.exec(long);
if (nameCapture && valueCapture) {
return {
name: nameCapture[2],
value: valueCapture[1],
};
} else {
throw `Alias line is incorrect: ${line}`;
}
} else {
throw `Can't parse alias line: ${line}`;
}
}
export class Aliases {
private storage: Dictionary<string>;
constructor(aliases: Dictionary<string>) {
this.storage = _.clone(aliases);
}
add(name: string, value: string) {
this.storage[name] = value;
}
has(name: string): name is ExistingAlias {
return name in this.storage;
}
get(name: ExistingAlias): string {
return this.storage[name];
}
getNameByValue(value: string): string | undefined {
return _.findKey(this.storage, storageValue => storageValue === value);
}
remove(name: string) {
delete this.storage[name];
}
toObject(): Dictionary<string> {
return this.storage;
}
}