forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
197 lines (172 loc) · 6.67 KB
/
cli.ts
File metadata and controls
197 lines (172 loc) · 6.67 KB
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
193
194
195
196
197
import * as os from "os";
import * as path from "path";
import { validatePaths } from "vs/code/node/paths";
import { parseMainProcessArgv } from "vs/platform/environment/node/argvHelper";
import { ParsedArgs } from "vs/platform/environment/common/environment";
import { buildHelpMessage, buildVersionMessage, options } from "vs/platform/environment/node/argv";
import pkg from "vs/platform/product/node/package";
import product from "vs/platform/product/node/product";
import { MainServer, WebviewServer } from "vs/server/src/server";
import "vs/server/src/tar";
import { generateCertificate, generatePassword, open, unpackExecutables } from "vs/server/src/util";
interface Args extends ParsedArgs {
"allow-http"?: boolean;
auth?: boolean;
cert?: string;
"cert-key"?: string;
"extra-builtin-extensions-dir"?: string;
"extra-extensions-dir"?: string;
host?: string;
open?: string;
port?: string;
socket?: string;
"webview-port"?: string;
"webview-socket"?: string;
}
// The last item is _ which is like -- so our options need to come before it.
const last = options.pop()!;
// Remove options that won't work or don't make sense.
let i = options.length;
while (i--) {
switch (options[i].id) {
case "add":
case "diff":
case "file-uri":
case "folder-uri":
case "goto":
case "new-window":
case "reuse-window":
case "wait":
case "disable-gpu":
// TODO: pretty sure these don't work but not 100%.
case "max-memory":
case "prof-startup":
case "inspect-extensions":
case "inspect-brk-extensions":
options.splice(i, 1);
break;
}
}
options.push({ id: "allow-http", type: "boolean", cat: "o", description: "Allow http connections." });
options.push({ id: "cert", type: "string", cat: "o", description: "Path to certificate." });
options.push({ id: "cert-key", type: "string", cat: "o", description: "Path to certificate key." });
options.push({ id: "extra-builtin-extensions-dir", type: "string", cat: "o", description: "Path to extra builtin extension directory." });
options.push({ id: "extra-extensions-dir", type: "string", cat: "o", description: "Path to extra user extension directory." });
options.push({ id: "host", type: "string", cat: "o", description: "Host for the main and webview servers." });
options.push({ id: "no-auth", type: "boolean", cat: "o", description: "Disable password authentication." });
options.push({ id: "open", type: "boolean", cat: "o", description: "Open in the browser on startup." });
options.push({ id: "port", type: "string", cat: "o", description: "Port for the main server." });
options.push({ id: "socket", type: "string", cat: "o", description: "Listen on a socket instead of host:port." });
options.push({ id: "webview-port", type: "string", cat: "o", description: "Port for the webview server." });
options.push({ id: "webview-socket", type: "string", cat: "o", description: "Listen on a socket instead of host:port." });
options.push(last);
interface IMainCli {
main: (argv: ParsedArgs) => Promise<void>;
}
const main = async (): Promise<void> => {
const args = validatePaths(parseMainProcessArgv(process.argv)) as Args;
["extra-extensions-dir", "extra-builtin-extensions-dir"].forEach((key) => {
if (typeof args[key] === "string") {
args[key] = [args[key]];
}
});
if (!product.extensionsGallery) {
product.extensionsGallery = {
serviceUrl: process.env.SERVICE_URL || "https://v1.extapi.coder.com",
itemUrl: process.env.ITEM_URL || "",
controlUrl: "",
recommendationsUrl: "",
};
}
const version = `${(pkg as any).codeServerVersion || "development"}-vsc${pkg.version}`;
if (args.help) {
const executable = `${product.applicationName}${os.platform() === "win32" ? ".exe" : ""}`;
return console.log(buildHelpMessage(product.nameLong, executable, version, undefined, false));
}
if (args.version) {
return console.log(buildVersionMessage(version, product.commit));
}
const shouldSpawnCliProcess = (): boolean => {
return !!args["install-source"]
|| !!args["list-extensions"]
|| !!args["install-extension"]
|| !!args["uninstall-extension"]
|| !!args["locate-extension"]
|| !!args["telemetry"];
};
if (shouldSpawnCliProcess()) {
const cli = await new Promise<IMainCli>((c, e) => require(["vs/code/node/cliProcessMain"], c, e));
await cli.main(args);
return process.exit(0); // There is a WriteStream instance keeping it open.
}
const extra = args["_"] || [];
const options = {
allowHttp: args["allow-http"],
auth: typeof args.auth !== "undefined" ? args.auth : true,
cert: args.cert,
certKey: args["cert-key"],
folderUri: extra.length > 1 ? extra[extra.length - 1] : undefined,
host: args.host,
password: process.env.PASSWORD,
};
if (!options.host) {
options.host = !options.auth || options.allowHttp ? "localhost" : "0.0.0.0";
}
let usingGeneratedCert = false;
if (!options.allowHttp && (!options.cert || !options.certKey)) {
const { cert, certKey } = await generateCertificate();
options.cert = cert;
options.certKey = certKey;
usingGeneratedCert = true;
}
let usingGeneratedPassword = false;
if (options.auth && !options.password) {
options.password = await generatePassword();
usingGeneratedPassword = true;
}
const webviewPort = args["webview-port"];
const webviewServer = new WebviewServer({
...options,
port: typeof webviewPort !== "undefined" && parseInt(webviewPort, 10) || 8444,
socket: args["webview-socket"],
});
const server = new MainServer({
...options,
port: typeof args.port !== "undefined" && parseInt(args.port, 10) || 8443,
socket: args.socket,
}, webviewServer, args);
const [webviewAddress, serverAddress, /* ignore */] = await Promise.all([
webviewServer.listen(),
server.listen(),
unpackExecutables(),
]);
console.log(`Main server listening on ${serverAddress}`);
console.log(`Webview server listening on ${webviewAddress}`);
if (usingGeneratedPassword) {
console.log(" - Password is", options.password);
console.log(" - To use your own password, set the PASSWORD environment variable");
} else if (options.auth) {
console.log(" - Using custom password for authentication");
} else {
console.log(" - No authentication");
}
if (!options.allowHttp && options.cert && options.certKey) {
console.log(
usingGeneratedCert
? ` - Using generated certificate and key in ${path.dirname(options.cert)} for HTTPS`
: " - Using provided certificate and key for HTTPS",
);
} else {
console.log(" - Not serving HTTPS");
}
if (!args.socket && args.open) {
// The web socket doesn't seem to work if using 0.0.0.0.
const openAddress = `http://localhost:${server.options.port}`;
await open(openAddress).catch(console.error);
console.log(` - Opened ${openAddress}`);
}
};
main().catch((error) => {
console.error(error);
process.exit(1);
});