-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstorage.ts
67 lines (56 loc) · 1.82 KB
/
storage.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from "path";
import * as fs from "fs-extra";
const windowRegistry =
process.platform === "win32"
? require("../build/Release/windows.node")
: null;
const deviceIdFileName = "deviceid";
function getDirectory(): string {
let folder: string;
if (!process.env.HOME) {
throw new Error("Home directory not found");
}
if (process.platform === "darwin") {
folder = path.join(process.env.HOME, "Library", "Application Support");
} else if (process.platform === "linux") {
folder =
process.env.XDG_CACHE_HOME ?? path.join(process.env.HOME, ".cache");
} else {
throw new Error("Unsupported platform");
}
return path.join(folder, "Microsoft", "DeveloperTools");
}
function getDeviceIdFilePath(): string {
return path.join(getDirectory(), deviceIdFileName);
}
export async function getDeviceId(): Promise<string | undefined> {
if (process.platform === "win32") {
return windowRegistry?.GetDeviceId() as string;
} else {
if (!(await exists(getDeviceIdFilePath()))) {
return undefined;
} else {
return fs.readFile(getDeviceIdFilePath(), "utf8");
}
}
}
async function exists(path: string): Promise<boolean> {
try {
await fs.promises.access(path);
return true;
} catch {
return false;
}
}
export async function setDeviceId(deviceId: string): Promise<void> {
if (process.platform === "win32") {
windowRegistry?.SetDeviceId(deviceId);
} else {
await fs.ensureDir(getDirectory());
await fs.writeFile(getDeviceIdFilePath(), deviceId, "utf8");
}
}