@theia/cli 1.53.0-next.5 → 1.53.0-next.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,363 +1,364 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2020 Ericsson and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- /* eslint-disable @typescript-eslint/no-explicit-any */
18
-
19
- import { OVSXApiFilterImpl, OVSXClient, VSXTargetPlatform } from '@theia/ovsx-client';
20
- import * as chalk from 'chalk';
21
- import * as decompress from 'decompress';
22
- import { promises as fs } from 'fs';
23
- import * as path from 'path';
24
- import * as temp from 'temp';
25
- import { DEFAULT_SUPPORTED_API_VERSION } from '@theia/application-package/lib/api';
26
- import { RequestContext, RequestService } from '@theia/request';
27
- import { RateLimiter } from 'limiter';
28
- import escapeStringRegexp = require('escape-string-regexp');
29
-
30
- temp.track();
31
-
32
- /**
33
- * Available options when downloading.
34
- */
35
- export interface DownloadPluginsOptions {
36
- /**
37
- * Determines if a plugin should be unpacked.
38
- * Defaults to `false`.
39
- */
40
- packed?: boolean;
41
-
42
- /**
43
- * Determines if failures while downloading plugins should be ignored.
44
- * Defaults to `false`.
45
- */
46
- ignoreErrors?: boolean;
47
-
48
- /**
49
- * The supported vscode API version.
50
- * Used to determine extension compatibility.
51
- */
52
- apiVersion?: string;
53
-
54
- /**
55
- * Fetch plugins in parallel
56
- */
57
- parallel?: boolean;
58
-
59
- rateLimit?: number;
60
- }
61
-
62
- interface PluginDownload {
63
- id: string,
64
- downloadUrl: string,
65
- version?: string | undefined
66
- }
67
-
68
- export default async function downloadPlugins(ovsxClient: OVSXClient, requestService: RequestService, options: DownloadPluginsOptions = {}): Promise<void> {
69
- const {
70
- packed = false,
71
- ignoreErrors = false,
72
- apiVersion = DEFAULT_SUPPORTED_API_VERSION,
73
- rateLimit = 15,
74
- parallel = true
75
- } = options;
76
-
77
- const rateLimiter = new RateLimiter({ tokensPerInterval: rateLimit, interval: 'second' });
78
- const apiFilter = new OVSXApiFilterImpl(ovsxClient, apiVersion);
79
-
80
- // Collect the list of failures to be appended at the end of the script.
81
- const failures: string[] = [];
82
-
83
- // Resolve the `package.json` at the current working directory.
84
- const pck = JSON.parse(await fs.readFile(path.resolve('package.json'), 'utf8'));
85
-
86
- // Resolve the directory for which to download the plugins.
87
- const pluginsDir = pck.theiaPluginsDir || 'plugins';
88
-
89
- // Excluded extension ids.
90
- const excludedIds = new Set<string>(pck.theiaPluginsExcludeIds || []);
91
-
92
- const parallelOrSequence = async (tasks: (() => unknown)[]) => {
93
- if (parallel) {
94
- await Promise.all(tasks.map(task => task()));
95
- } else {
96
- for (const task of tasks) {
97
- await task();
98
- }
99
- }
100
- };
101
-
102
- // Downloader wrapper
103
- const downloadPlugin = async (plugin: PluginDownload): Promise<void> => {
104
- await downloadPluginAsync(requestService, rateLimiter, failures, plugin.id, plugin.downloadUrl, pluginsDir, packed, plugin.version);
105
- };
106
-
107
- const downloader = async (plugins: PluginDownload[]) => {
108
- await parallelOrSequence(plugins.map(plugin => () => downloadPlugin(plugin)));
109
- };
110
-
111
- await fs.mkdir(pluginsDir, { recursive: true });
112
-
113
- if (!pck.theiaPlugins) {
114
- console.log(chalk.red('error: missing mandatory \'theiaPlugins\' property.'));
115
- return;
116
- }
117
- try {
118
- console.warn('--- downloading plugins ---');
119
- // Download the raw plugins defined by the `theiaPlugins` property.
120
- // This will include both "normal" plugins as well as "extension packs".
121
- const pluginsToDownload = Object.entries(pck.theiaPlugins)
122
- .filter((entry: [string, unknown]): entry is [string, string] => typeof entry[1] === 'string')
123
- .map(([id, url]) => ({ id, downloadUrl: resolveDownloadUrlPlaceholders(url) }));
124
- await downloader(pluginsToDownload);
125
-
126
- const handleDependencyList = async (dependencies: (string | string[])[]) => {
127
- // De-duplicate extension ids to only download each once:
128
- const ids = new Set<string>(dependencies.flat());
129
- await parallelOrSequence(Array.from(ids, id => async () => {
130
- try {
131
- await rateLimiter.removeTokens(1);
132
- const extension = await apiFilter.findLatestCompatibleExtension({
133
- extensionId: id,
134
- includeAllVersions: true,
135
- targetPlatform
136
- });
137
- const version = extension?.version;
138
- const downloadUrl = extension?.files.download;
139
- if (downloadUrl) {
140
- await rateLimiter.removeTokens(1);
141
- await downloadPlugin({ id, downloadUrl, version });
142
- } else {
143
- failures.push(`No download url for extension pack ${id} (${version})`);
144
- }
145
- } catch (err) {
146
- console.error(err);
147
- failures.push(err.message);
148
- }
149
- }));
150
- };
151
-
152
- console.warn('--- collecting extension-packs ---');
153
- const extensionPacks = await collectExtensionPacks(pluginsDir, excludedIds);
154
- if (extensionPacks.size > 0) {
155
- console.warn(`--- resolving ${extensionPacks.size} extension-packs ---`);
156
- await handleDependencyList(Array.from(extensionPacks.values()));
157
- }
158
-
159
- console.warn('--- collecting extension dependencies ---');
160
- const pluginDependencies = await collectPluginDependencies(pluginsDir, excludedIds);
161
- if (pluginDependencies.length > 0) {
162
- console.warn(`--- resolving ${pluginDependencies.length} extension dependencies ---`);
163
- await handleDependencyList(pluginDependencies);
164
- }
165
-
166
- } finally {
167
- temp.cleanupSync();
168
- }
169
- for (const failure of failures) {
170
- console.error(failure);
171
- }
172
- if (!ignoreErrors && failures.length > 0) {
173
- throw new Error('Errors downloading some plugins. To make these errors non fatal, re-run with --ignore-errors');
174
- }
175
- }
176
-
177
- const targetPlatform = `${process.platform}-${process.arch}` as VSXTargetPlatform;
178
-
179
- const placeholders: Record<string, string> = {
180
- targetPlatform
181
- };
182
- function resolveDownloadUrlPlaceholders(url: string): string {
183
- for (const [name, value] of Object.entries(placeholders)) {
184
- url = url.replace(new RegExp(escapeStringRegexp(`\${${name}}`), 'g'), value);
185
- }
186
- return url;
187
- }
188
-
189
- /**
190
- * Downloads a plugin, will make multiple attempts before actually failing.
191
- * @param requestService
192
- * @param failures reference to an array storing all failures.
193
- * @param plugin plugin short name.
194
- * @param pluginUrl url to download the plugin at.
195
- * @param target where to download the plugin in.
196
- * @param packed whether to decompress or not.
197
- */
198
- async function downloadPluginAsync(
199
- requestService: RequestService,
200
- rateLimiter: RateLimiter,
201
- failures: string[],
202
- plugin: string,
203
- pluginUrl: string,
204
- pluginsDir: string,
205
- packed: boolean,
206
- version?: string
207
- ): Promise<void> {
208
- if (!plugin) {
209
- return;
210
- }
211
- let fileExt: string;
212
- if (pluginUrl.endsWith('tar.gz')) {
213
- fileExt = '.tar.gz';
214
- } else if (pluginUrl.endsWith('vsix')) {
215
- fileExt = '.vsix';
216
- } else if (pluginUrl.endsWith('theia')) {
217
- fileExt = '.theia'; // theia plugins.
218
- } else {
219
- failures.push(chalk.red(`error: '${plugin}' has an unsupported file type: '${pluginUrl}'`));
220
- return;
221
- }
222
- const targetPath = path.resolve(pluginsDir, `${plugin}${packed === true ? fileExt : ''}`);
223
-
224
- // Skip plugins which have previously been downloaded.
225
- if (await isDownloaded(targetPath)) {
226
- console.warn('- ' + plugin + ': already downloaded - skipping');
227
- return;
228
- }
229
-
230
- const maxAttempts = 5;
231
- const retryDelay = 2000;
232
-
233
- let attempts: number;
234
- let lastError: Error | undefined;
235
- let response: RequestContext | undefined;
236
-
237
- for (attempts = 0; attempts < maxAttempts; attempts++) {
238
- if (attempts > 0) {
239
- await new Promise(resolve => setTimeout(resolve, retryDelay));
240
- }
241
- lastError = undefined;
242
- try {
243
- await rateLimiter.removeTokens(1);
244
- response = await requestService.request({
245
- url: pluginUrl
246
- });
247
- } catch (error) {
248
- lastError = error;
249
- continue;
250
- }
251
- const status = response.res.statusCode;
252
- const retry = status && (status === 429 || status === 439 || status >= 500);
253
- if (!retry) {
254
- break;
255
- }
256
- }
257
- if (lastError) {
258
- failures.push(chalk.red(`x ${plugin}: failed to download, last error:\n ${lastError}`));
259
- return;
260
- }
261
- if (typeof response === 'undefined') {
262
- failures.push(chalk.red(`x ${plugin}: failed to download (unknown reason)`));
263
- return;
264
- }
265
- if (response.res.statusCode !== 200) {
266
- failures.push(chalk.red(`x ${plugin}: failed to download with: ${response.res.statusCode}`));
267
- return;
268
- }
269
-
270
- if ((fileExt === '.vsix' || fileExt === '.theia') && packed === true) {
271
- // Download .vsix without decompressing.
272
- await fs.writeFile(targetPath, response.buffer);
273
- } else {
274
- await fs.mkdir(targetPath, { recursive: true });
275
- const tempFile = temp.path('theia-plugin-download');
276
- await fs.writeFile(tempFile, response.buffer);
277
- await decompress(tempFile, targetPath);
278
- }
279
-
280
- console.warn(chalk.green(`+ ${plugin}${version ? `@${version}` : ''}: downloaded successfully ${attempts > 1 ? `(after ${attempts} attempts)` : ''}`));
281
- }
282
-
283
- /**
284
- * Determine if the resource for the given path is already downloaded.
285
- * @param filePath the resource path.
286
- *
287
- * @returns `true` if the resource is already downloaded, else `false`.
288
- */
289
- async function isDownloaded(filePath: string): Promise<boolean> {
290
- return fs.stat(filePath).then(() => true, () => false);
291
- }
292
-
293
- /**
294
- * Walk the plugin directory and collect available extension paths.
295
- * @param pluginDir the plugin directory.
296
- * @returns the list of all available extension paths.
297
- */
298
- async function collectPackageJsonPaths(pluginDir: string): Promise<string[]> {
299
- const packageJsonPathList: string[] = [];
300
- const files = await fs.readdir(pluginDir);
301
- // Recursively fetch the list of extension `package.json` files.
302
- for (const file of files) {
303
- const filePath = path.join(pluginDir, file);
304
- if ((await fs.stat(filePath)).isDirectory()) {
305
- packageJsonPathList.push(...await collectPackageJsonPaths(filePath));
306
- } else if (path.basename(filePath) === 'package.json' && !path.dirname(filePath).includes('node_modules')) {
307
- packageJsonPathList.push(filePath);
308
- }
309
- }
310
- return packageJsonPathList;
311
- }
312
-
313
- /**
314
- * Get the mapping of extension-pack paths and their included plugin ids.
315
- * - If an extension-pack references an explicitly excluded `id` the `id` will be omitted.
316
- * @param pluginDir the plugin directory.
317
- * @param excludedIds the list of plugin ids to exclude.
318
- * @returns the mapping of extension-pack paths and their included plugin ids.
319
- */
320
- async function collectExtensionPacks(pluginDir: string, excludedIds: Set<string>): Promise<Map<string, string[]>> {
321
- const extensionPackPaths = new Map<string, string[]>();
322
- const packageJsonPaths = await collectPackageJsonPaths(pluginDir);
323
- await Promise.all(packageJsonPaths.map(async packageJsonPath => {
324
- const json = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
325
- const extensionPack: unknown = json.extensionPack;
326
- if (Array.isArray(extensionPack)) {
327
- extensionPackPaths.set(packageJsonPath, extensionPack.filter(id => {
328
- if (excludedIds.has(id)) {
329
- console.log(chalk.yellow(`'${id}' referred to by '${json.name}' (ext pack) is excluded because of 'theiaPluginsExcludeIds'`));
330
- return false; // remove
331
- }
332
- return true; // keep
333
- }));
334
- }
335
- }));
336
- return extensionPackPaths;
337
- }
338
-
339
- /**
340
- * Get the mapping of paths and their included plugin ids.
341
- * - If an extension-pack references an explicitly excluded `id` the `id` will be omitted.
342
- * @param pluginDir the plugin directory.
343
- * @param excludedIds the list of plugin ids to exclude.
344
- * @returns the mapping of extension-pack paths and their included plugin ids.
345
- */
346
- async function collectPluginDependencies(pluginDir: string, excludedIds: Set<string>): Promise<string[]> {
347
- const dependencyIds: string[] = [];
348
- const packageJsonPaths = await collectPackageJsonPaths(pluginDir);
349
- await Promise.all(packageJsonPaths.map(async packageJsonPath => {
350
- const json = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
351
- const extensionDependencies: unknown = json.extensionDependencies;
352
- if (Array.isArray(extensionDependencies)) {
353
- for (const dependency of extensionDependencies) {
354
- if (excludedIds.has(dependency)) {
355
- console.log(chalk.yellow(`'${dependency}' referred to by '${json.name}' is excluded because of 'theiaPluginsExcludeIds'`));
356
- } else {
357
- dependencyIds.push(dependency);
358
- }
359
- }
360
- }
361
- }));
362
- return dependencyIds;
363
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2020 Ericsson and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ /* eslint-disable @typescript-eslint/no-explicit-any */
18
+
19
+ import { OVSXApiFilterImpl, OVSXClient, VSXTargetPlatform } from '@theia/ovsx-client';
20
+ import * as chalk from 'chalk';
21
+ import * as decompress from 'decompress';
22
+ import { promises as fs } from 'fs';
23
+ import * as path from 'path';
24
+ import * as temp from 'temp';
25
+ import { DEFAULT_SUPPORTED_API_VERSION } from '@theia/application-package/lib/api';
26
+ import { RequestContext, RequestService } from '@theia/request';
27
+ import { RateLimiter } from 'limiter';
28
+ import escapeStringRegexp = require('escape-string-regexp');
29
+
30
+ temp.track();
31
+
32
+ /**
33
+ * Available options when downloading.
34
+ */
35
+ export interface DownloadPluginsOptions {
36
+ /**
37
+ * Determines if a plugin should be unpacked.
38
+ * Defaults to `false`.
39
+ */
40
+ packed?: boolean;
41
+
42
+ /**
43
+ * Determines if failures while downloading plugins should be ignored.
44
+ * Defaults to `false`.
45
+ */
46
+ ignoreErrors?: boolean;
47
+
48
+ /**
49
+ * The supported vscode API version.
50
+ * Used to determine extension compatibility.
51
+ */
52
+ apiVersion?: string;
53
+
54
+ /**
55
+ * Fetch plugins in parallel
56
+ */
57
+ parallel?: boolean;
58
+ }
59
+
60
+ interface PluginDownload {
61
+ id: string,
62
+ downloadUrl: string,
63
+ version?: string | undefined
64
+ }
65
+
66
+ export default async function downloadPlugins(
67
+ ovsxClient: OVSXClient,
68
+ rateLimiter: RateLimiter,
69
+ requestService: RequestService,
70
+ options: DownloadPluginsOptions = {}
71
+ ): Promise<void> {
72
+ const {
73
+ packed = false,
74
+ ignoreErrors = false,
75
+ apiVersion = DEFAULT_SUPPORTED_API_VERSION,
76
+ parallel = true
77
+ } = options;
78
+
79
+ const apiFilter = new OVSXApiFilterImpl(ovsxClient, apiVersion);
80
+
81
+ // Collect the list of failures to be appended at the end of the script.
82
+ const failures: string[] = [];
83
+
84
+ // Resolve the `package.json` at the current working directory.
85
+ const pck = JSON.parse(await fs.readFile(path.resolve('package.json'), 'utf8'));
86
+
87
+ // Resolve the directory for which to download the plugins.
88
+ const pluginsDir = pck.theiaPluginsDir || 'plugins';
89
+
90
+ // Excluded extension ids.
91
+ const excludedIds = new Set<string>(pck.theiaPluginsExcludeIds || []);
92
+
93
+ const parallelOrSequence = async (tasks: (() => unknown)[]) => {
94
+ if (parallel) {
95
+ await Promise.all(tasks.map(task => task()));
96
+ } else {
97
+ for (const task of tasks) {
98
+ await task();
99
+ }
100
+ }
101
+ };
102
+
103
+ // Downloader wrapper
104
+ const downloadPlugin = async (plugin: PluginDownload): Promise<void> => {
105
+ await downloadPluginAsync(requestService, rateLimiter, failures, plugin.id, plugin.downloadUrl, pluginsDir, packed, plugin.version);
106
+ };
107
+
108
+ const downloader = async (plugins: PluginDownload[]) => {
109
+ await parallelOrSequence(plugins.map(plugin => () => downloadPlugin(plugin)));
110
+ };
111
+
112
+ await fs.mkdir(pluginsDir, { recursive: true });
113
+
114
+ if (!pck.theiaPlugins) {
115
+ console.log(chalk.red('error: missing mandatory \'theiaPlugins\' property.'));
116
+ return;
117
+ }
118
+ try {
119
+ console.warn('--- downloading plugins ---');
120
+ // Download the raw plugins defined by the `theiaPlugins` property.
121
+ // This will include both "normal" plugins as well as "extension packs".
122
+ const pluginsToDownload = Object.entries(pck.theiaPlugins)
123
+ .filter((entry: [string, unknown]): entry is [string, string] => typeof entry[1] === 'string')
124
+ .map(([id, url]) => ({ id, downloadUrl: resolveDownloadUrlPlaceholders(url) }));
125
+ await downloader(pluginsToDownload);
126
+
127
+ const handleDependencyList = async (dependencies: (string | string[])[]) => {
128
+ // De-duplicate extension ids to only download each once:
129
+ const ids = new Set<string>(dependencies.flat());
130
+ await parallelOrSequence(Array.from(ids, id => async () => {
131
+ try {
132
+ await rateLimiter.removeTokens(1);
133
+ const extension = await apiFilter.findLatestCompatibleExtension({
134
+ extensionId: id,
135
+ includeAllVersions: true,
136
+ targetPlatform
137
+ });
138
+ const version = extension?.version;
139
+ const downloadUrl = extension?.files.download;
140
+ if (downloadUrl) {
141
+ await rateLimiter.removeTokens(1);
142
+ await downloadPlugin({ id, downloadUrl, version });
143
+ } else {
144
+ failures.push(`No download url for extension pack ${id} (${version})`);
145
+ }
146
+ } catch (err) {
147
+ console.error(err);
148
+ failures.push(err.message);
149
+ }
150
+ }));
151
+ };
152
+
153
+ console.warn('--- collecting extension-packs ---');
154
+ const extensionPacks = await collectExtensionPacks(pluginsDir, excludedIds);
155
+ if (extensionPacks.size > 0) {
156
+ console.warn(`--- resolving ${extensionPacks.size} extension-packs ---`);
157
+ await handleDependencyList(Array.from(extensionPacks.values()));
158
+ }
159
+
160
+ console.warn('--- collecting extension dependencies ---');
161
+ const pluginDependencies = await collectPluginDependencies(pluginsDir, excludedIds);
162
+ if (pluginDependencies.length > 0) {
163
+ console.warn(`--- resolving ${pluginDependencies.length} extension dependencies ---`);
164
+ await handleDependencyList(pluginDependencies);
165
+ }
166
+
167
+ } finally {
168
+ temp.cleanupSync();
169
+ }
170
+ for (const failure of failures) {
171
+ console.error(failure);
172
+ }
173
+ if (!ignoreErrors && failures.length > 0) {
174
+ throw new Error('Errors downloading some plugins. To make these errors non fatal, re-run with --ignore-errors');
175
+ }
176
+ }
177
+
178
+ const targetPlatform = `${process.platform}-${process.arch}` as VSXTargetPlatform;
179
+
180
+ const placeholders: Record<string, string> = {
181
+ targetPlatform
182
+ };
183
+ function resolveDownloadUrlPlaceholders(url: string): string {
184
+ for (const [name, value] of Object.entries(placeholders)) {
185
+ url = url.replace(new RegExp(escapeStringRegexp(`\${${name}}`), 'g'), value);
186
+ }
187
+ return url;
188
+ }
189
+
190
+ /**
191
+ * Downloads a plugin, will make multiple attempts before actually failing.
192
+ * @param requestService
193
+ * @param failures reference to an array storing all failures.
194
+ * @param plugin plugin short name.
195
+ * @param pluginUrl url to download the plugin at.
196
+ * @param target where to download the plugin in.
197
+ * @param packed whether to decompress or not.
198
+ */
199
+ async function downloadPluginAsync(
200
+ requestService: RequestService,
201
+ rateLimiter: RateLimiter,
202
+ failures: string[],
203
+ plugin: string,
204
+ pluginUrl: string,
205
+ pluginsDir: string,
206
+ packed: boolean,
207
+ version?: string
208
+ ): Promise<void> {
209
+ if (!plugin) {
210
+ return;
211
+ }
212
+ let fileExt: string;
213
+ if (pluginUrl.endsWith('tar.gz')) {
214
+ fileExt = '.tar.gz';
215
+ } else if (pluginUrl.endsWith('vsix')) {
216
+ fileExt = '.vsix';
217
+ } else if (pluginUrl.endsWith('theia')) {
218
+ fileExt = '.theia'; // theia plugins.
219
+ } else {
220
+ failures.push(chalk.red(`error: '${plugin}' has an unsupported file type: '${pluginUrl}'`));
221
+ return;
222
+ }
223
+ const targetPath = path.resolve(pluginsDir, `${plugin}${packed === true ? fileExt : ''}`);
224
+
225
+ // Skip plugins which have previously been downloaded.
226
+ if (await isDownloaded(targetPath)) {
227
+ console.warn('- ' + plugin + ': already downloaded - skipping');
228
+ return;
229
+ }
230
+
231
+ const maxAttempts = 5;
232
+ const retryDelay = 2000;
233
+
234
+ let attempts: number;
235
+ let lastError: Error | undefined;
236
+ let response: RequestContext | undefined;
237
+
238
+ for (attempts = 0; attempts < maxAttempts; attempts++) {
239
+ if (attempts > 0) {
240
+ await new Promise(resolve => setTimeout(resolve, retryDelay));
241
+ }
242
+ lastError = undefined;
243
+ try {
244
+ await rateLimiter.removeTokens(1);
245
+ response = await requestService.request({
246
+ url: pluginUrl
247
+ });
248
+ } catch (error) {
249
+ lastError = error;
250
+ continue;
251
+ }
252
+ const status = response.res.statusCode;
253
+ const retry = status && (status === 429 || status === 439 || status >= 500);
254
+ if (!retry) {
255
+ break;
256
+ }
257
+ }
258
+ if (lastError) {
259
+ failures.push(chalk.red(`x ${plugin}: failed to download, last error:\n ${lastError}`));
260
+ return;
261
+ }
262
+ if (typeof response === 'undefined') {
263
+ failures.push(chalk.red(`x ${plugin}: failed to download (unknown reason)`));
264
+ return;
265
+ }
266
+ if (response.res.statusCode !== 200) {
267
+ failures.push(chalk.red(`x ${plugin}: failed to download with: ${response.res.statusCode}`));
268
+ return;
269
+ }
270
+
271
+ if ((fileExt === '.vsix' || fileExt === '.theia') && packed === true) {
272
+ // Download .vsix without decompressing.
273
+ await fs.writeFile(targetPath, response.buffer);
274
+ } else {
275
+ await fs.mkdir(targetPath, { recursive: true });
276
+ const tempFile = temp.path('theia-plugin-download');
277
+ await fs.writeFile(tempFile, response.buffer);
278
+ await decompress(tempFile, targetPath);
279
+ }
280
+
281
+ console.warn(chalk.green(`+ ${plugin}${version ? `@${version}` : ''}: downloaded successfully ${attempts > 1 ? `(after ${attempts} attempts)` : ''}`));
282
+ }
283
+
284
+ /**
285
+ * Determine if the resource for the given path is already downloaded.
286
+ * @param filePath the resource path.
287
+ *
288
+ * @returns `true` if the resource is already downloaded, else `false`.
289
+ */
290
+ async function isDownloaded(filePath: string): Promise<boolean> {
291
+ return fs.stat(filePath).then(() => true, () => false);
292
+ }
293
+
294
+ /**
295
+ * Walk the plugin directory and collect available extension paths.
296
+ * @param pluginDir the plugin directory.
297
+ * @returns the list of all available extension paths.
298
+ */
299
+ async function collectPackageJsonPaths(pluginDir: string): Promise<string[]> {
300
+ const packageJsonPathList: string[] = [];
301
+ const files = await fs.readdir(pluginDir);
302
+ // Recursively fetch the list of extension `package.json` files.
303
+ for (const file of files) {
304
+ const filePath = path.join(pluginDir, file);
305
+ if ((await fs.stat(filePath)).isDirectory()) {
306
+ packageJsonPathList.push(...await collectPackageJsonPaths(filePath));
307
+ } else if (path.basename(filePath) === 'package.json' && !path.dirname(filePath).includes('node_modules')) {
308
+ packageJsonPathList.push(filePath);
309
+ }
310
+ }
311
+ return packageJsonPathList;
312
+ }
313
+
314
+ /**
315
+ * Get the mapping of extension-pack paths and their included plugin ids.
316
+ * - If an extension-pack references an explicitly excluded `id` the `id` will be omitted.
317
+ * @param pluginDir the plugin directory.
318
+ * @param excludedIds the list of plugin ids to exclude.
319
+ * @returns the mapping of extension-pack paths and their included plugin ids.
320
+ */
321
+ async function collectExtensionPacks(pluginDir: string, excludedIds: Set<string>): Promise<Map<string, string[]>> {
322
+ const extensionPackPaths = new Map<string, string[]>();
323
+ const packageJsonPaths = await collectPackageJsonPaths(pluginDir);
324
+ await Promise.all(packageJsonPaths.map(async packageJsonPath => {
325
+ const json = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
326
+ const extensionPack: unknown = json.extensionPack;
327
+ if (Array.isArray(extensionPack)) {
328
+ extensionPackPaths.set(packageJsonPath, extensionPack.filter(id => {
329
+ if (excludedIds.has(id)) {
330
+ console.log(chalk.yellow(`'${id}' referred to by '${json.name}' (ext pack) is excluded because of 'theiaPluginsExcludeIds'`));
331
+ return false; // remove
332
+ }
333
+ return true; // keep
334
+ }));
335
+ }
336
+ }));
337
+ return extensionPackPaths;
338
+ }
339
+
340
+ /**
341
+ * Get the mapping of paths and their included plugin ids.
342
+ * - If an extension-pack references an explicitly excluded `id` the `id` will be omitted.
343
+ * @param pluginDir the plugin directory.
344
+ * @param excludedIds the list of plugin ids to exclude.
345
+ * @returns the mapping of extension-pack paths and their included plugin ids.
346
+ */
347
+ async function collectPluginDependencies(pluginDir: string, excludedIds: Set<string>): Promise<string[]> {
348
+ const dependencyIds: string[] = [];
349
+ const packageJsonPaths = await collectPackageJsonPaths(pluginDir);
350
+ await Promise.all(packageJsonPaths.map(async packageJsonPath => {
351
+ const json = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
352
+ const extensionDependencies: unknown = json.extensionDependencies;
353
+ if (Array.isArray(extensionDependencies)) {
354
+ for (const dependency of extensionDependencies) {
355
+ if (excludedIds.has(dependency)) {
356
+ console.log(chalk.yellow(`'${dependency}' referred to by '${json.name}' is excluded because of 'theiaPluginsExcludeIds'`));
357
+ } else {
358
+ dependencyIds.push(dependency);
359
+ }
360
+ }
361
+ }
362
+ }));
363
+ return dependencyIds;
364
+ }