forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.ts
More file actions
69 lines (60 loc) · 1.74 KB
/
platform.ts
File metadata and controls
69 lines (60 loc) · 1.74 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { EnvironmentVariables } from '../variables/types';
export enum Architecture {
Unknown = 1,
x86 = 2,
x64 = 3,
}
export enum OSType {
Unknown = 'Unknown',
Windows = 'Windows',
OSX = 'OSX',
Linux = 'Linux',
}
// Return the OS type for the given platform string.
export function getOSType(platform: string = process.platform): OSType {
if (/^win/.test(platform)) {
return OSType.Windows;
} else if (/^darwin/.test(platform)) {
return OSType.OSX;
} else if (/^linux/.test(platform)) {
return OSType.Linux;
} else {
return OSType.Unknown;
}
}
const architectures: Record<string, Architecture> = {
x86: Architecture.x86, // 32-bit
x64: Architecture.x64, // 64-bit
'': Architecture.Unknown,
};
/**
* Identify the host's native architecture/bitness.
*/
export function getArchitecture(): Architecture {
const fromProc = architectures[process.arch];
if (fromProc !== undefined) {
return fromProc;
}
const arch = require('arch');
return architectures[arch()] || Architecture.Unknown;
}
/**
* Look up the requested env var value (or undefined` if not set).
*/
export function getEnvironmentVariable(key: string): string | undefined {
return ((process.env as any) as EnvironmentVariables)[key];
}
/**
* Get the current user's home directory.
*
* The lookup is limited to environment variables.
*/
export function getUserHomeDir(): string | undefined {
if (getOSType() === OSType.Windows) {
return getEnvironmentVariable('USERPROFILE');
}
return getEnvironmentVariable('HOME') || getEnvironmentVariable('HOMEPATH');
}