forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.ts
More file actions
81 lines (76 loc) · 2.5 KB
/
registry.ts
File metadata and controls
81 lines (76 loc) · 2.5 KB
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
import * as Registry from 'winreg';
enum RegistryArchitectures {
x86 = 'x86',
x64 = 'x64'
}
export enum Architecture {
Unknown = 1,
x86 = 2,
x64 = 3
}
export enum Hive {
HKCU, HKLM
}
export interface IRegistry {
getKeys(key: string, hive: Hive, arch?: Architecture): Promise<string[]>;
getValue(key: string, hive: Hive, arch?: Architecture, name?: string): Promise<string | undefined | null>;
}
export class RegistryImplementation implements IRegistry {
public async getKeys(key: string, hive: Hive, arch?: Architecture) {
return getRegistryKeys({ hive: translateHive(hive)!, arch: translateArchitecture(arch), key });
}
public async getValue(key: string, hive: Hive, arch?: Architecture, name: string = '') {
return getRegistryValue({ hive: translateHive(hive)!, arch: translateArchitecture(arch), key }, name);
}
}
export function getArchitectureDislayName(arch?: Architecture) {
switch (arch) {
case Architecture.x64:
return '64-bit';
case Architecture.x86:
return '32-bit';
default:
return '';
}
}
async function getRegistryValue(options: Registry.Options, name: string = '') {
return new Promise<string | undefined | null>((resolve, reject) => {
new Registry(options).get(name, (error, result) => {
if (error || !result || typeof result.value !== 'string') {
return resolve(undefined);
}
resolve(result.value);
});
});
}
async function getRegistryKeys(options: Registry.Options): Promise<string[]> {
// https://github.com/python/peps/blob/master/pep-0514.txt#L85
return new Promise<string[]>((resolve, reject) => {
new Registry(options).keys((error, result) => {
if (error || !Array.isArray(result)) {
return resolve([]);
}
resolve(result.filter(item => typeof item.key === 'string').map(item => item.key));
});
});
}
function translateArchitecture(arch?: Architecture): RegistryArchitectures | undefined {
switch (arch) {
case Architecture.x86:
return RegistryArchitectures.x86;
case Architecture.x64:
return RegistryArchitectures.x64;
default:
return;
}
}
function translateHive(hive: Hive): string | undefined {
switch (hive) {
case Hive.HKCU:
return Registry.HKCU;
case Hive.HKLM:
return Registry.HKLM;
default:
return;
}
}