-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUtilities.ts
138 lines (123 loc) · 3.1 KB
/
Utilities.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { exec } from "node:child_process";
import { promisify } from "node:util";
/**
* Provides utilities for the extension.
*/
export class Utilities
{
/**
* The environment-variables which may contain the username.
*/
private static envVars: string[] = [
"GIT_AUTHOR_NAME",
"GIT_COMMITTER_NAME",
"HGUSER",
"C9_USER"
];
/**
* Gets the full name of the current user.
*
* @returns
* The full name of the current user.
*/
public static async GetFullName(): Promise<string>
{
let methods: Array<() => Promise<string>> = [];
methods = [
async () => this.CheckEnv(),
async () => this.CheckGit()
];
if (process.platform === "win32")
{
methods.push(this.CheckWmic);
}
else if (process.platform === "darwin")
{
methods.push(this.CheckOsaScript);
}
for (let method of methods)
{
try
{
let result = await method();
if (result)
{
return result.trim();
}
}
catch
{ }
}
return "";
}
/**
* Executes the command.
*
* @param command
* The command to execute.
*
* @returns
* The output of the command.
*/
private static async ExecuteCommand(command: string): Promise<string>
{
let result = await promisify(exec)(command);
if (result.stderr)
{
throw new Error(result.stderr);
}
else
{
return result.stdout;
}
}
/**
* Tries to figure out the username using environment-variables.
*
* @returns
* The result of the environment-lookup.
*/
private static CheckEnv(): string
{
let environment = process.env;
let variableNames = this.envVars.filter(x => x in environment);
for (let variableName of variableNames)
{
if (environment[variableName] !== null)
{
return environment[variableName];
}
}
return null;
}
/**
* Tries to figure out the username using git's global settings.
*
* @returns
* The result of the git-lookup.
*/
private static async CheckGit(): Promise<string>
{
return this.ExecuteCommand("git config --global user.name");
}
/**
* Tries to figure out the username using wmic.
*
* @returns
* The result of the wmic-lookup.
*/
private static async CheckWmic(): Promise<string>
{
return (await this.ExecuteCommand('wmic useraccount where name="%username%" get fullname')).replace("FullName", "").trim();
}
/**
* Tries to figure out the username using osascript.
*
* @returns
* The result of the osascript-lookup.
*/
private static async CheckOsaScript(): Promise<string>
{
return this.ExecuteCommand("osascript -e long user name of (system info)");
}
}