Repository navigation
Expand file tree
/
Copy pathxterm-update.js
More file actions
161 lines (146 loc) · 5.17 KB
/
Copy pathxterm-update.js
File metadata and controls
161 lines (146 loc) · 5.17 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const cp = require('child_process');
const path = require('path');
const moduleNames = [
'@xterm/xterm',
'@xterm/addon-clipboard',
'@xterm/addon-image',
'@xterm/addon-ligatures',
'@xterm/addon-progress',
'@xterm/addon-search',
'@xterm/addon-serialize',
'@xterm/addon-unicode11',
'@xterm/addon-webgl',
];
const backendOnlyModuleNames = [
'@xterm/headless'
];
const vscodeDir = process.argv.length >= 3 ? process.argv[2] : process.cwd();
if (!path.basename(vscodeDir).match(/.*vscode.*/)) {
console.error('The cwd is not "vscode" root');
return;
}
function getLatestModuleVersion(moduleName) {
return new Promise((resolve, reject) => {
cp.exec(`pnpm view ${moduleName} versions --json`, { cwd: vscodeDir }, (err, stdout, stderr) => {
if (err) {
reject(err);
}
let versions = JSON.parse(stdout);
// Fix format if there is only a single version published
if (typeof versions === 'string') {
versions = [versions];
}
// Unlike npm, pnpm lists the versions in string order (`6.1.0-beta.99` after `6.1.0-beta.327`)
resolve([...versions].sort(compareVersions)[versions.length - 1]);
});
});
}
/**
* Compares two versions by semver precedence (build metadata is ignored).
*/
function compareVersions(a, b) {
const parse = version => {
const { major, minor, patch, prerelease } = /^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-(?<prerelease>[^+]+))?/.exec(version).groups;
return { release: [major, minor, patch].map(Number), prerelease: prerelease ? prerelease.split('.') : [] };
};
const x = parse(a);
const y = parse(b);
for (let i = 0; i < 3; i++) {
if (x.release[i] !== y.release[i]) {
return x.release[i] - y.release[i];
}
}
// A release has a higher precedence than its prereleases
if (x.prerelease.length === 0 || y.prerelease.length === 0) {
return y.prerelease.length - x.prerelease.length;
}
for (let i = 0; i < Math.max(x.prerelease.length, y.prerelease.length); i++) {
const p = x.prerelease[i];
const q = y.prerelease[i];
if (p === q) {
continue;
}
if (p === undefined || q === undefined) {
return p === undefined ? -1 : 1;
}
const pNumeric = /^\d+$/.test(p);
const qNumeric = /^\d+$/.test(q);
if (pNumeric && qNumeric) {
return Number(p) - Number(q);
}
if (pNumeric !== qNumeric) {
return pNumeric ? -1 : 1;
}
return p < q ? -1 : 1;
}
return 0;
}
/**
* `pnpm add name@version` saves the exact version, so ask for the caret range that the
* package.json files use.
*/
function toCaretRange(moduleWithVersion) {
const index = moduleWithVersion.lastIndexOf('@');
return `${moduleWithVersion.slice(0, index)}@^${moduleWithVersion.slice(index + 1)}`;
}
/**
* Runs `pnpm add` in `cwd`. The repository root is a pnpm workspace with several projects, where
* pnpm adds dependencies only with `--workspace-root`.
*/
function pnpmAdd(cwd, modulesWithVersion) {
const workspaceRoot = cwd === vscodeDir ? ' --workspace-root' : '';
cp.execSync(`pnpm add${workspaceRoot} ${modulesWithVersion.map(toCaretRange).join(' ')}`, { cwd });
}
async function update() {
console.log('Fetching latest versions');
const allModules = moduleNames.concat(backendOnlyModuleNames);
const versionPromises = [];
for (const m of allModules) {
versionPromises.push(getLatestModuleVersion(m));
}
const latestVersionsArray = await Promise.all(versionPromises);
const latestVersions = {};
for (const [i, v] of latestVersionsArray.entries()) {
latestVersions[allModules[i]] = v;
}
console.log('Detected versions:');
for (const m of moduleNames.concat(backendOnlyModuleNames)) {
console.log(` ${m}@${latestVersions[m]}`);
}
const pkg = require(path.join(vscodeDir, 'package.json'));
const modulesWithVersion = [];
for (const m of moduleNames) {
const moduleWithVersion = `${m}@${latestVersions[m]}`;
if (pkg.dependencies[m] === latestVersions[m]) {
console.log(`Skipping ${moduleWithVersion}, already up to date`);
continue;
}
modulesWithVersion.push(moduleWithVersion);
}
if (modulesWithVersion.length > 0) {
for (const cwd of [vscodeDir, path.join(vscodeDir, 'remote'), path.join(vscodeDir, 'remote/web')]) {
console.log(`${path.join(cwd, 'package.json')}: Updating\n ${modulesWithVersion.join('\n ')}`);
pnpmAdd(cwd, modulesWithVersion);
}
}
const backendOnlyModulesWithVersion = [];
for (const m of backendOnlyModuleNames) {
const moduleWithVersion = `${m}@${latestVersions[m]}`;
if (pkg.dependencies[m] === latestVersions[m]) {
console.log(`Skipping ${moduleWithVersion}, already up to date`);
continue;
}
backendOnlyModulesWithVersion.push(moduleWithVersion);
}
if (backendOnlyModulesWithVersion.length > 0) {
for (const cwd of [vscodeDir, path.join(vscodeDir, 'remote')]) {
console.log(`${path.join(cwd, 'package.json')}: Updating\n ${backendOnlyModulesWithVersion.join('\n ')}`);
pnpmAdd(cwd, backendOnlyModulesWithVersion);
}
}
}
update();