blob: efebe0b0f42bace3a49de7ab7f9b25cbaf7f02da (
plain)
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
|
import * as os from "os";
import * as path from "path";
/**
* Expands the character `~` to the user's home directory
*/
export function expandUser(file_path: string): string {
if (os.platform() == "win32") {
return file_path;
}
if (!file_path) {
return "";
}
if (!file_path.startsWith("~")) {
return file_path;
}
const path_len = file_path.length;
if (path_len == 1) {
return os.homedir();
}
if (file_path.charAt(1) == path.sep) {
return path.join(os.homedir(), file_path.substring(1));
}
const sep_index = file_path.indexOf(path.sep);
const user_name_end = sep_index == -1 ? file_path.length : sep_index;
const user_name = file_path.substring(1, user_name_end);
try {
if (user_name == os.userInfo().username) {
return path.join(os.homedir(), file_path.substring(user_name_end));
}
} catch (err) {
return file_path;
}
return file_path;
}
|