Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/npm/preinstall.js
3520 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
const nodeVersion = /^(\d+)\.(\d+)\.(\d+)/.exec(process.versions.node);
7
const majorNodeVersion = parseInt(nodeVersion[1]);
8
const minorNodeVersion = parseInt(nodeVersion[2]);
9
const patchNodeVersion = parseInt(nodeVersion[3]);
10
11
if (!process.env['VSCODE_SKIP_NODE_VERSION_CHECK']) {
12
if (majorNodeVersion < 22 || (majorNodeVersion === 22 && minorNodeVersion < 15) || (majorNodeVersion === 22 && minorNodeVersion === 15 && patchNodeVersion < 1)) {
13
console.error('\x1b[1;31m*** Please use Node.js v22.15.1 or later for development.\x1b[0;0m');
14
throw new Error();
15
}
16
}
17
18
if (process.env['npm_execpath'].includes('yarn')) {
19
console.error('\x1b[1;31m*** Seems like you are using `yarn` which is not supported in this repo any more, please use `npm i` instead. ***\x1b[0;0m');
20
throw new Error();
21
}
22
23
const path = require('path');
24
const fs = require('fs');
25
const cp = require('child_process');
26
const os = require('os');
27
28
if (process.platform === 'win32') {
29
if (!hasSupportedVisualStudioVersion()) {
30
console.error('\x1b[1;31m*** Invalid C/C++ Compiler Toolchain. Please check https://github.com/microsoft/vscode/wiki/How-to-Contribute#prerequisites.\x1b[0;0m');
31
console.error('\x1b[1;31m*** If you have Visual Studio installed in a custom location, you can specify it via the environment variable:\x1b[0;0m');
32
console.error('\x1b[1;31m*** set vs2022_install=<path> (or vs2019_install for older versions)\x1b[0;0m');
33
throw new Error();
34
}
35
installHeaders();
36
}
37
38
if (process.arch !== os.arch()) {
39
console.error(`\x1b[1;31m*** ARCHITECTURE MISMATCH: The node.js process is ${process.arch}, but your OS architecture is ${os.arch()}. ***\x1b[0;0m`);
40
console.error(`\x1b[1;31m*** This can greatly increase the build time of vs code. ***\x1b[0;0m`);
41
}
42
43
function hasSupportedVisualStudioVersion() {
44
const fs = require('fs');
45
const path = require('path');
46
// Translated over from
47
// https://source.chromium.org/chromium/chromium/src/+/master:build/vs_toolchain.py;l=140-175
48
const supportedVersions = ['2022', '2019'];
49
50
const availableVersions = [];
51
for (const version of supportedVersions) {
52
// Check environment variable first (explicit override)
53
let vsPath = process.env[`vs${version}_install`];
54
if (vsPath && fs.existsSync(vsPath)) {
55
availableVersions.push(version);
56
break;
57
}
58
59
// Check default installation paths
60
const programFiles86Path = process.env['ProgramFiles(x86)'];
61
const programFiles64Path = process.env['ProgramFiles'];
62
63
const vsTypes = ['Enterprise', 'Professional', 'Community', 'Preview', 'BuildTools', 'IntPreview'];
64
if (programFiles64Path) {
65
vsPath = `${programFiles64Path}/Microsoft Visual Studio/${version}`;
66
if (vsTypes.some(vsType => fs.existsSync(path.join(vsPath, vsType)))) {
67
availableVersions.push(version);
68
break;
69
}
70
}
71
72
if (programFiles86Path) {
73
vsPath = `${programFiles86Path}/Microsoft Visual Studio/${version}`;
74
if (vsTypes.some(vsType => fs.existsSync(path.join(vsPath, vsType)))) {
75
availableVersions.push(version);
76
break;
77
}
78
}
79
}
80
81
return availableVersions.length;
82
}
83
84
function installHeaders() {
85
cp.execSync(`npm.cmd ${process.env['npm_command'] || 'ci'}`, {
86
env: process.env,
87
cwd: path.join(__dirname, 'gyp'),
88
stdio: 'inherit'
89
});
90
91
// The node gyp package got installed using the above npm command using the gyp/package.json
92
// file checked into our repository. So from that point it is save to construct the path
93
// to that executable
94
const node_gyp = path.join(__dirname, 'gyp', 'node_modules', '.bin', 'node-gyp.cmd');
95
const result = cp.execFileSync(node_gyp, ['list'], { encoding: 'utf8', shell: true });
96
const versions = new Set(result.split(/\n/g).filter(line => !line.startsWith('gyp info')).map(value => value));
97
98
const local = getHeaderInfo(path.join(__dirname, '..', '..', '.npmrc'));
99
const remote = getHeaderInfo(path.join(__dirname, '..', '..', 'remote', '.npmrc'));
100
101
if (local !== undefined && !versions.has(local.target)) {
102
// Both disturl and target come from a file checked into our repository
103
cp.execFileSync(node_gyp, ['install', '--dist-url', local.disturl, local.target], { shell: true });
104
}
105
106
if (remote !== undefined && !versions.has(remote.target)) {
107
// Both disturl and target come from a file checked into our repository
108
cp.execFileSync(node_gyp, ['install', '--dist-url', remote.disturl, remote.target], { shell: true });
109
}
110
}
111
112
/**
113
* @param {string} rcFile
114
* @returns {{ disturl: string; target: string } | undefined}
115
*/
116
function getHeaderInfo(rcFile) {
117
const lines = fs.readFileSync(rcFile, 'utf8').split(/\r\n?/g);
118
let disturl, target;
119
for (const line of lines) {
120
let match = line.match(/\s*disturl=*\"(.*)\"\s*$/);
121
if (match !== null && match.length >= 1) {
122
disturl = match[1];
123
}
124
match = line.match(/\s*target=*\"(.*)\"\s*$/);
125
if (match !== null && match.length >= 1) {
126
target = match[1];
127
}
128
}
129
return disturl !== undefined && target !== undefined
130
? { disturl, target }
131
: undefined;
132
}
133
134