This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
install
135 lines (107 loc) · 4.4 KB
/
install
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
#!/usr/bin/env node
var semver = require('semver');
var fs = require('fs');
var path = require('path');
var shared = require('../lib/shared');
process.on('uncaughtException', function (err) {
exitWithError(err);
});
var engine = process.env.npm_package_engines_vscode;
if (!engine) {
exitWithError('Missing VSCode engine declaration in package.json.');
}
var vscodeDtsTypescriptPath = path.join(path.dirname(__dirname), 'vscode.d.ts');
console.log('Detected VS Code engine version: ' + engine);
getURLMatchingEngine(engine, function (_, data) {
console.log('Fetching vscode.d.ts from: ' + data.url);
shared.getUrlContents(data.url, process.env.GITHUB_TOKEN, null, function (error, contents) {
if (error) {
exitWithError(error);
}
if (contents === 'Not Found') {
exitWithError(new Error('Could not find vscode.d.ts at the provided URL. Please report this to https://github.com/Microsoft/vscode/issues'));
}
if (data.version !== '*' && semver.lt(data.version, '1.7.0')) {
// Older versions of vscode.d.ts need a massage to play nice.
contents = vscodeDtsToTypescript(contents);
}
fs.writeFileSync(vscodeDtsTypescriptPath, contents);
console.log('vscode.d.ts successfully installed!\n');
});
});
function vscodeDtsToTypescript(contents) {
var markerHit = false;
var lines = contents.split('\n').filter(function (line) {
if (!markerHit && (line === '// when used for JS*' || line === 'declare module \'vscode\' {')) {
markerHit = true;
}
return !markerHit;
});
lines.unshift('/// <reference path="./thenable.d.ts" />');
lines.push('export = vscode;'); // this is to enable TS module resolution support
return lines.join('\n');
}
function getURLMatchingEngine(engine, callback) {
if (engine === '*') {
// master
return callback(null, {
url: 'https://raw.githubusercontent.com/Microsoft/vscode/master/src/vs/vscode.d.ts',
version: '*'
});
}
shared.getUrlContents('https://update.code.visualstudio.com/api/releases/stable', null, { "X-API-Version": "2" }, function (error, tagsRaw) {
if (error) {
exitWithError(error);
}
var tagsAndCommits;
try {
tagsAndCommits = JSON.parse(tagsRaw);
} catch (error) {
exitWithError(error);
}
var mapTagsToCommits = Object.create(null);
for (var i = 0; i < tagsAndCommits.length; i++) {
var tagAndCommit = tagsAndCommits[i];
mapTagsToCommits[tagAndCommit.version] = tagAndCommit.id;
}
var tags = Object.keys(mapTagsToCommits);
var tag = minSatisfying(tags, engine);
// check if master is on the version specified
if (!tag) {
return shared.getUrlContents('https://raw.githubusercontent.com/Microsoft/vscode/master/package.json', process.env.GITHUB_TOKEN, null, function (error, packageJson) {
if (error) {
exitWithError(error);
}
var version = JSON.parse(packageJson).version;
if (semver.satisfies(version, engine)) {
// master
return callback(null, {
url: 'https://raw.githubusercontent.com/Microsoft/vscode/master/src/vs/vscode.d.ts',
version: version
});
}
exitWithError('Could not find satifying VSCode for version ' + engine + ' in the tags: [' + tags.join(', ') + '] or on master: ' + version);
});
}
console.log('Found minimal version that qualifies engine range: ' + tag);
return callback(null, {
url: 'https://raw.githubusercontent.com/Microsoft/vscode/' + mapTagsToCommits[tag] + '/src/vs/vscode.d.ts',
version: tag
});
});
}
function minSatisfying(versions, range) {
return versions.filter(function (version) {
try {
return semver.satisfies(version, range);
} catch (error) {
return false; // version might be invalid so we return as not matching
}
}).sort(function (a, b) {
return semver.compare(a, b);
})[0] || null;
}
function exitWithError(error) {
console.error('Error installing vscode.d.ts: ' + error.toString());
process.exit(1);
}