Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/builtInExtensionsCG.ts
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
import fs from 'fs';
7
import path from 'path';
8
import url from 'url';
9
import ansiColors from 'ansi-colors';
10
import { IExtensionDefinition } from './builtInExtensions';
11
12
const root = path.dirname(path.dirname(__dirname));
13
const rootCG = path.join(root, 'extensionsCG');
14
const productjson = JSON.parse(fs.readFileSync(path.join(__dirname, '../../product.json'), 'utf8'));
15
const builtInExtensions = <IExtensionDefinition[]>productjson.builtInExtensions || [];
16
const webBuiltInExtensions = <IExtensionDefinition[]>productjson.webBuiltInExtensions || [];
17
const token = process.env['GITHUB_TOKEN'];
18
19
const contentBasePath = 'raw.githubusercontent.com';
20
const contentFileNames = ['package.json', 'package-lock.json'];
21
22
async function downloadExtensionDetails(extension: IExtensionDefinition): Promise<void> {
23
const extensionLabel = `${extension.name}@${extension.version}`;
24
const repository = url.parse(extension.repo).path!.substr(1);
25
const repositoryContentBaseUrl = `https://${token ? `${token}@` : ''}${contentBasePath}/${repository}/v${extension.version}`;
26
27
28
async function getContent(fileName: string): Promise<{ fileName: string; body: Buffer | undefined | null }> {
29
try {
30
const response = await fetch(`${repositoryContentBaseUrl}/${fileName}`);
31
if (response.ok) {
32
return { fileName, body: Buffer.from(await response.arrayBuffer()) };
33
} else if (response.status === 404) {
34
return { fileName, body: undefined };
35
} else {
36
return { fileName, body: null };
37
}
38
} catch (e) {
39
return { fileName, body: null };
40
}
41
}
42
43
const promises = contentFileNames.map(getContent);
44
45
console.log(extensionLabel);
46
const results = await Promise.all(promises);
47
for (const result of results) {
48
if (result.body) {
49
const extensionFolder = path.join(rootCG, extension.name);
50
fs.mkdirSync(extensionFolder, { recursive: true });
51
fs.writeFileSync(path.join(extensionFolder, result.fileName), result.body);
52
console.log(` - ${result.fileName} ${ansiColors.green('✔︎')}`);
53
} else if (result.body === undefined) {
54
console.log(` - ${result.fileName} ${ansiColors.yellow('⚠️')}`);
55
} else {
56
console.log(` - ${result.fileName} ${ansiColors.red('🛑')}`);
57
}
58
}
59
60
// Validation
61
if (!results.find(r => r.fileName === 'package.json')?.body) {
62
// throw new Error(`The "package.json" file could not be found for the built-in extension - ${extensionLabel}`);
63
}
64
if (!results.find(r => r.fileName === 'package-lock.json')?.body) {
65
// throw new Error(`The "package-lock.json" could not be found for the built-in extension - ${extensionLabel}`);
66
}
67
}
68
69
async function main(): Promise<void> {
70
for (const extension of [...builtInExtensions, ...webBuiltInExtensions]) {
71
await downloadExtensionDetails(extension);
72
}
73
}
74
75
main().then(() => {
76
console.log(`Built-in extensions component data downloaded ${ansiColors.green('✔︎')}`);
77
process.exit(0);
78
}, err => {
79
console.log(`Built-in extensions component data could not be downloaded ${ansiColors.red('🛑')}`);
80
console.error(err);
81
process.exit(1);
82
});
83
84