forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.ts
More file actions
79 lines (74 loc) · 2.68 KB
/
registry.ts
File metadata and controls
79 lines (74 loc) · 2.68 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
import { injectable } from 'inversify';
import { Options } from 'winreg';
import { Architecture } from '../utils/platform';
import { IRegistry, RegistryHive } from './types';
enum RegistryArchitectures {
x86 = 'x86',
x64 = 'x64'
}
@injectable()
export class RegistryImplementation implements IRegistry {
public async getKeys(key: string, hive: RegistryHive, arch?: Architecture) {
return getRegistryKeys({ hive: translateHive(hive)!, arch: translateArchitecture(arch), key });
}
public async getValue(key: string, hive: RegistryHive, arch?: Architecture, name: string = '') {
return getRegistryValue({ hive: translateHive(hive)!, arch: translateArchitecture(arch), key }, name);
}
}
export function getArchitectureDisplayName(arch?: Architecture) {
switch (arch) {
case Architecture.x64:
return '64-bit';
case Architecture.x86:
return '32-bit';
default:
return '';
}
}
async function getRegistryValue(options: Options, name: string = '') {
// tslint:disable-next-line:no-require-imports
const Registry = require('winreg') as typeof import('winreg');
return new Promise<string | undefined | null>(resolve => {
new Registry(options).get(name, (error, result) => {
if (error || !result || typeof result.value !== 'string') {
return resolve(undefined);
}
resolve(result.value);
});
});
}
async function getRegistryKeys(options: Options): Promise<string[]> {
// tslint:disable-next-line:no-require-imports
const Registry = require('winreg') as typeof import('winreg');
// https://github.com/python/peps/blob/master/pep-0514.txt#L85
return new Promise<string[]>(resolve => {
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: RegistryHive): string | undefined {
// tslint:disable-next-line:no-require-imports
const Registry = require('winreg') as typeof import('winreg');
switch (hive) {
case RegistryHive.HKCU:
return Registry.HKCU;
case RegistryHive.HKLM:
return Registry.HKLM;
default:
return;
}
}