Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/sanity/src/detectors.ts
5232 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import fs from 'fs';
7
import os from 'os';
8
import { webkit } from 'playwright';
9
10
/**
11
* The capabilities of the current environment.
12
*/
13
export type Capability =
14
| 'linux' | 'darwin' | 'windows' | 'alpine'
15
| 'x64' | 'arm64' | 'arm32'
16
| 'deb' | 'rpm' | 'snap'
17
| 'desktop'
18
| 'browser'
19
| 'wsl'
20
| 'github-account';
21
22
/**
23
* Detect the capabilities of the current environment.
24
*/
25
export function detectCapabilities(): ReadonlySet<Capability> {
26
const capabilities = new Set<Capability>();
27
detectOS(capabilities);
28
detectArch(capabilities);
29
detectPackageManagers(capabilities);
30
detectDesktop(capabilities);
31
detectBrowser(capabilities);
32
detectWSL(capabilities);
33
detectGitHubAccount(capabilities);
34
return capabilities;
35
}
36
37
/**
38
* Detect the operating system.
39
*/
40
function detectOS(capabilities: Set<Capability>) {
41
switch (os.platform()) {
42
case 'linux':
43
if (fs.existsSync('/etc/alpine-release')) {
44
capabilities.add('alpine');
45
} else {
46
capabilities.add('linux');
47
}
48
break;
49
case 'darwin':
50
capabilities.add('darwin');
51
break;
52
case 'win32':
53
capabilities.add('windows');
54
break;
55
default:
56
throw new Error(`Unsupported platform: ${os.platform()}`);
57
}
58
}
59
60
/**
61
* Detect the architecture.
62
*/
63
function detectArch(capabilities: Set<Capability>) {
64
let arch = os.arch();
65
66
if (os.platform() === 'win32') {
67
const winArch = process.env.PROCESSOR_ARCHITEW6432 || process.env.PROCESSOR_ARCHITECTURE;
68
if (winArch === 'ARM64') {
69
arch = 'arm64';
70
} else if (winArch === 'AMD64') {
71
arch = 'x64';
72
}
73
}
74
75
switch (arch) {
76
case 'x64':
77
capabilities.add('x64');
78
break;
79
case 'arm64':
80
capabilities.add('arm64');
81
break;
82
case 'arm':
83
capabilities.add('arm32');
84
break;
85
default:
86
throw new Error(`Unsupported architecture: ${arch}`);
87
}
88
}
89
90
/**
91
* Detect the package managers.
92
*/
93
function detectPackageManagers(capabilities: Set<Capability>) {
94
if (os.platform() !== 'linux') {
95
return;
96
}
97
if (fs.existsSync('/usr/bin/dpkg')) {
98
capabilities.add('deb');
99
}
100
if (fs.existsSync('/usr/bin/dnf') || fs.existsSync('/usr/bin/yum')) {
101
capabilities.add('rpm');
102
}
103
if (fs.existsSync('/run/snapd.socket')) {
104
capabilities.add('snap');
105
}
106
}
107
108
/**
109
* Detect if a desktop environment is available.
110
*/
111
function detectDesktop(capabilities: Set<Capability>) {
112
if (os.platform() !== 'linux' || !!process.env.DISPLAY) {
113
capabilities.add('desktop');
114
}
115
}
116
117
/**
118
* Detect if a browser environment is available.
119
*/
120
function detectBrowser(capabilities: Set<Capability>) {
121
switch (os.platform()) {
122
case 'linux': {
123
const path = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH;
124
if (path && fs.existsSync(path)) {
125
capabilities.add('browser');
126
}
127
break;
128
}
129
case 'darwin': {
130
if (fs.existsSync(webkit.executablePath())) {
131
capabilities.add('browser');
132
}
133
break;
134
}
135
case 'win32': {
136
const path =
137
process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH ??
138
`${process.env['ProgramFiles(x86)']}\\Microsoft\\Edge\\Application\\msedge.exe`;
139
140
if (fs.existsSync(path)) {
141
capabilities.add('browser');
142
}
143
break;
144
}
145
}
146
}
147
148
/**
149
* Detect if WSL is available on Windows.
150
*/
151
function detectWSL(capabilities: Set<Capability>) {
152
if (os.platform() === 'win32') {
153
const wslPath = `${process.env.SystemRoot}\\System32\\wsl.exe`;
154
if (fs.existsSync(wslPath)) {
155
capabilities.add('wsl');
156
}
157
}
158
}
159
160
/**
161
* Detect if GitHub account and password are available in the environment.
162
*/
163
function detectGitHubAccount(capabilities: Set<Capability>) {
164
if (process.env.GITHUB_ACCOUNT && process.env.GITHUB_PASSWORD) {
165
capabilities.add('github-account');
166
}
167
}
168
169
170