Skip to content

Commit 12af311

Browse files
committed
Use our logger instead of raw console.log
1 parent 62719ab commit 12af311

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@types/node": "^10.12.12"
2424
},
2525
"dependencies": {
26+
"@coder/logger": "^1.1.5",
2627
"httpolyglot": "^0.1.2",
2728
"pem": "^1.14.2",
2829
"safe-compare": "^1.1.4"

src/cli.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import product from "vs/platform/product/node/product";
99

1010
import { MainServer } from "vs/server/src/server";
1111
import "vs/server/src/tar";
12-
import { AuthType, buildAllowedMessage, generateCertificate, generatePassword, open, unpackExecutables } from "vs/server/src/util";
12+
import { AuthType, buildAllowedMessage, generateCertificate, generatePassword, localRequire, open, unpackExecutables } from "vs/server/src/util";
13+
14+
const { logger } = localRequire<typeof import("@coder/logger/out/index")>("@coder/logger/out/index");
1315

1416
interface Args extends ParsedArgs {
1517
auth?: AuthType;
@@ -67,7 +69,7 @@ interface IMainCli {
6769
main: (argv: ParsedArgs) => Promise<void>;
6870
}
6971

70-
const main = async (): Promise<void> => {
72+
const main = async (): Promise<void | void[]> => {
7173
const args = validatePaths(parseMainProcessArgv(process.argv)) as Args;
7274
["extra-extensions-dir", "extra-builtin-extensions-dir"].forEach((key) => {
7375
if (typeof args[key] === "string") {
@@ -91,7 +93,7 @@ const main = async (): Promise<void> => {
9193
}
9294

9395
if (args.version) {
94-
return console.log(buildVersionMessage(version, product.commit));
96+
return buildVersionMessage(version, product.commit).split("\n").map((line) => logger.info(line));
9597
}
9698

9799
const shouldSpawnCliProcess = (): boolean => {
@@ -146,32 +148,32 @@ const main = async (): Promise<void> => {
146148
server.listen(),
147149
unpackExecutables(),
148150
]);
149-
console.log(`Server listening on ${serverAddress}`);
151+
logger.info(`Server listening on ${serverAddress}`);
150152

151153
if (options.auth && !process.env.PASSWORD) {
152-
console.log(" - Password is", options.password);
153-
console.log(" - To use your own password, set the PASSWORD environment variable");
154+
logger.info(` - Password is ${options.password}`);
155+
logger.info(" - To use your own password, set the PASSWORD environment variable");
154156
} else if (options.auth) {
155-
console.log(" - Using custom password for authentication");
157+
logger.info(" - Using custom password for authentication");
156158
} else {
157-
console.log(" - No authentication");
159+
logger.info(" - No authentication");
158160
}
159161

160162
if (server.protocol === "https") {
161-
console.log(
163+
logger.info(
162164
args.cert
163165
? ` - Using provided certificate${args["cert-key"] ? " and key" : ""} for HTTPS`
164166
: ` - Using generated certificate and key for HTTPS`,
165167
);
166168
} else {
167-
console.log(" - Not serving HTTPS");
169+
logger.info(" - Not serving HTTPS");
168170
}
169171

170172
if (!server.options.socket && args.open) {
171173
// The web socket doesn't seem to work if using 0.0.0.0.
172174
const openAddress = `http://localhost:${server.options.port}`;
173175
await open(openAddress).catch(console.error);
174-
console.log(` - Opened ${openAddress}`);
176+
logger.info(` - Opened ${openAddress}`);
175177
}
176178
};
177179

src/server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import { Connection, ManagementConnection, ExtensionHostConnection } from "vs/se
5757
import { ExtensionEnvironmentChannel, FileProviderChannel , } from "vs/server/src/channel";
5858
import { TelemetryClient } from "vs/server/src/insights";
5959
import { Protocol } from "vs/server/src/protocol";
60-
import { AuthType, getMediaMime, getUriTransformer, tmpdir } from "vs/server/src/util";
60+
import { AuthType, getMediaMime, getUriTransformer, localRequire, tmpdir } from "vs/server/src/util";
6161

6262
export enum HttpCode {
6363
Ok = 200,
@@ -124,7 +124,7 @@ export abstract class Server {
124124
};
125125
this.protocol = this.options.cert ? "https" : "http";
126126
if (this.protocol === "https") {
127-
const httpolyglot = require.__$__nodeRequire(path.resolve(__dirname, "../node_modules/httpolyglot/lib/index")) as typeof import("httpolyglot");
127+
const httpolyglot = localRequire<typeof import("httpolyglot")>("httpolyglot/lib/index");
128128
this.server = httpolyglot.createServer({
129129
cert: this.options.cert && fs.readFileSync(this.options.cert),
130130
key: this.options.certKey && fs.readFileSync(this.options.certKey),
@@ -213,7 +213,7 @@ export abstract class Server {
213213
const parsedUrl = request.url ? url.parse(request.url, true) : { query: {}};
214214
const fullPath = decodeURIComponent(parsedUrl.pathname || "/");
215215
const match = fullPath.match(/^(\/?[^/]*)(.*)$/);
216-
let [, base, requestPath] = match
216+
let [/* ignore */, base, requestPath] = match
217217
? match.map((p) => p.replace(/\/+$/, ""))
218218
: ["", "", ""];
219219
if (base.indexOf(".") !== -1) { // Assume it's a file at the root.
@@ -363,7 +363,7 @@ export abstract class Server {
363363
if (!this.options.auth) {
364364
return true;
365365
}
366-
const safeCompare = require.__$__nodeRequire(path.resolve(__dirname, "../node_modules/safe-compare/index")) as typeof import("safe-compare");
366+
const safeCompare = localRequire<typeof import("safe-compare")>("safe-compare/index");
367367
if (typeof payload === "undefined") {
368368
payload = this.parseCookies<LoginPayload>(request);
369369
}

src/util.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const generateCertificate = async (): Promise<{ cert: string, certKey: st
3030
]);
3131

3232
if (!exists[0] || !exists[1]) {
33-
const pem = require.__$__nodeRequire(path.resolve(__dirname, "../node_modules/pem/lib/pem")) as typeof import("pem");
33+
const pem = localRequire<typeof import("pem")>("pem/lib/pem");
3434
const certs = await new Promise<import("pem").CertificateCreationResult>((resolve, reject): void => {
3535
pem.createCertificate({ selfSigned: true }, (error, result) => {
3636
if (error) {
@@ -117,3 +117,11 @@ export const buildAllowedMessage = (t: typeof AuthType): string => {
117117
const values = <string[]>Object.keys(t).map((k) => t[k]);
118118
return `Allowed value${values.length === 1 ? " is" : "s are"} ${values.map((t) => `'${t}'`).join(",")}`;
119119
};
120+
121+
/**
122+
* Require a local module. This is necessary since VS Code's loader only looks
123+
* at the root for Node modules.
124+
*/
125+
export const localRequire = <T>(modulePath: string): T => {
126+
return require.__$__nodeRequire(path.resolve(__dirname, "../node_modules", modulePath));
127+
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# yarn lockfile v1
33

44

5+
"@coder/logger@^1.1.5":
6+
version "1.1.5"
7+
resolved "https://registry.yarnpkg.com/@coder/logger/-/logger-1.1.5.tgz#e5b0e6207a00b6b54e9c63ad8afab60643b10f25"
8+
integrity sha512-ehOcZ2HXCDTKIjORPDvEzJyNk3X2vOE4Tcb78UTHR71fG6CIL1KP5Rx4Nj5M4Jg2X5laouWwbG9oWtkmQeKkJg==
9+
510
"@types/node@*", "@types/node@^10.12.12":
611
version "10.14.12"
712
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.12.tgz#0eec3155a46e6c4db1f27c3e588a205f767d622f"

0 commit comments

Comments
 (0)