Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/scripts/code-web.js
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
// @ts-check
7
8
const testWebLocation = require.resolve('@vscode/test-web');
9
10
const fs = require('fs');
11
const path = require('path');
12
const cp = require('child_process');
13
14
const minimist = require('minimist');
15
const fancyLog = require('fancy-log');
16
const ansiColors = require('ansi-colors');
17
const open = require('open');
18
const https = require('https');
19
20
const APP_ROOT = path.join(__dirname, '..');
21
const WEB_DEV_EXTENSIONS_ROOT = path.join(APP_ROOT, '.build', 'builtInWebDevExtensions');
22
23
const WEB_PLAYGROUND_VERSION = '0.0.13';
24
25
async function main() {
26
27
const args = minimist(process.argv.slice(2), {
28
boolean: [
29
'help',
30
'playground'
31
],
32
string: [
33
'host',
34
'port',
35
'extensionPath',
36
'browser',
37
'browserType'
38
],
39
});
40
41
if (args.help) {
42
console.log(
43
'./scripts/code-web.sh|bat[, folderMountPath[, options]]\n' +
44
' Start with an empty workspace and no folder opened in explorer\n' +
45
' folderMountPath Open local folder (eg: use `.` to open current directory)\n' +
46
' --playground Include the vscode-web-playground extension\n'
47
);
48
startServer(['--help']);
49
return;
50
}
51
52
const serverArgs = [];
53
54
const HOST = args['host'] ?? 'localhost';
55
const PORT = args['port'] ?? '8080';
56
57
if (args['host'] === undefined) {
58
serverArgs.push('--host', HOST);
59
}
60
if (args['port'] === undefined) {
61
serverArgs.push('--port', PORT);
62
}
63
64
// only use `./scripts/code-web.sh --playground` to add vscode-web-playground extension by default.
65
if (args['playground'] === true) {
66
serverArgs.push('--extensionPath', WEB_DEV_EXTENSIONS_ROOT);
67
serverArgs.push('--folder-uri', 'memfs:///sample-folder');
68
await ensureWebDevExtensions(args['verbose']);
69
}
70
71
let openSystemBrowser = false;
72
if (!args['browser'] && !args['browserType']) {
73
serverArgs.push('--browserType', 'none');
74
openSystemBrowser = true;
75
}
76
77
serverArgs.push('--sourcesPath', APP_ROOT);
78
79
serverArgs.push(...process.argv.slice(2).filter(v => !v.startsWith('--playground') && v !== '--no-playground'));
80
81
startServer(serverArgs);
82
if (openSystemBrowser) {
83
open.default(`http://${HOST}:${PORT}/`);
84
}
85
}
86
87
function startServer(runnerArguments) {
88
const env = { ...process.env };
89
90
console.log(`Starting @vscode/test-web: ${testWebLocation} ${runnerArguments.join(' ')}`);
91
const proc = cp.spawn(process.execPath, [testWebLocation, ...runnerArguments], { env, stdio: 'inherit' });
92
93
proc.on('exit', (code) => process.exit(code));
94
95
process.on('exit', () => proc.kill());
96
process.on('SIGINT', () => {
97
proc.kill();
98
process.exit(128 + 2); // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events
99
});
100
process.on('SIGTERM', () => {
101
proc.kill();
102
process.exit(128 + 15); // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events
103
});
104
}
105
106
async function directoryExists(path) {
107
try {
108
return (await fs.promises.stat(path)).isDirectory();
109
} catch {
110
return false;
111
}
112
}
113
114
/** @return {Promise<void>} */
115
async function downloadPlaygroundFile(fileName, httpsLocation, destinationRoot) {
116
const destination = path.join(destinationRoot, fileName);
117
await fs.promises.mkdir(path.dirname(destination), { recursive: true });
118
const fileStream = fs.createWriteStream(destination);
119
return (new Promise((resolve, reject) => {
120
const request = https.get(path.posix.join(httpsLocation, fileName), response => {
121
response.pipe(fileStream);
122
fileStream.on('finish', () => {
123
fileStream.close();
124
resolve();
125
});
126
});
127
request.on('error', reject);
128
}));
129
}
130
131
async function ensureWebDevExtensions(verbose) {
132
133
// Playground (https://github.com/microsoft/vscode-web-playground)
134
const webDevPlaygroundRoot = path.join(WEB_DEV_EXTENSIONS_ROOT, 'vscode-web-playground');
135
const webDevPlaygroundExists = await directoryExists(webDevPlaygroundRoot);
136
137
let downloadPlayground = false;
138
if (webDevPlaygroundExists) {
139
try {
140
const webDevPlaygroundPackageJson = JSON.parse(((await fs.promises.readFile(path.join(webDevPlaygroundRoot, 'package.json'))).toString()));
141
if (webDevPlaygroundPackageJson.version !== WEB_PLAYGROUND_VERSION) {
142
downloadPlayground = true;
143
}
144
} catch (error) {
145
downloadPlayground = true;
146
}
147
} else {
148
downloadPlayground = true;
149
}
150
151
if (downloadPlayground) {
152
if (verbose) {
153
fancyLog(`${ansiColors.magenta('Web Development extensions')}: Downloading vscode-web-playground to ${webDevPlaygroundRoot}`);
154
}
155
const playgroundRepo = `https://raw.githubusercontent.com/microsoft/vscode-web-playground/main/`;
156
await Promise.all(['package.json', 'dist/extension.js', 'dist/extension.js.map'].map(
157
fileName => downloadPlaygroundFile(fileName, playgroundRepo, webDevPlaygroundRoot)
158
));
159
160
} else {
161
if (verbose) {
162
fancyLog(`${ansiColors.magenta('Web Development extensions')}: Using existing vscode-web-playground in ${webDevPlaygroundRoot}`);
163
}
164
}
165
}
166
167
main();
168
169