mirror of https://github.com/coder/code-server.git
Implement open flag
This commit is contained in:
parent
9446cc8245
commit
9b0b337dc0
|
@ -10,7 +10,7 @@ import pkg from "vs/platform/product/node/package";
|
||||||
|
|
||||||
import { MainServer, WebviewServer } from "vs/server/src/server";
|
import { MainServer, WebviewServer } from "vs/server/src/server";
|
||||||
import "vs/server/src/tar";
|
import "vs/server/src/tar";
|
||||||
import { generateCertificate, generatePassword } from "vs/server/src/util";
|
import { generateCertificate, generatePassword, open } from "vs/server/src/util";
|
||||||
|
|
||||||
interface Args extends ParsedArgs {
|
interface Args extends ParsedArgs {
|
||||||
"allow-http"?: boolean;
|
"allow-http"?: boolean;
|
||||||
|
@ -192,6 +192,11 @@ const main = async (): Promise<void> => {
|
||||||
} else {
|
} else {
|
||||||
console.log(" - Not serving HTTPS");
|
console.log(" - Not serving HTTPS");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (args["open"]) {
|
||||||
|
await open(serverAddress).catch(console.error);
|
||||||
|
console.log(" - Opened URL");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
main().catch((error) => {
|
main().catch((error) => {
|
||||||
|
|
38
src/util.ts
38
src/util.ts
|
@ -1,3 +1,4 @@
|
||||||
|
import * as cp from "child_process";
|
||||||
import * as crypto from "crypto";
|
import * as crypto from "crypto";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import * as os from "os";
|
import * as os from "os";
|
||||||
|
@ -76,3 +77,40 @@ export const getMediaMime = (filePath?: string): string => {
|
||||||
".json": "application/json",
|
".json": "application/json",
|
||||||
}[extname(filePath)]) || "text/plain";
|
}[extname(filePath)]) || "text/plain";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const isWsl = async (): Promise<boolean> => {
|
||||||
|
return process.platform === "linux"
|
||||||
|
&& os.release().toLowerCase().indexOf("microsoft") !== -1
|
||||||
|
|| (await util.promisify(fs.readFile)("/proc/version", "utf8"))
|
||||||
|
.toLowerCase().indexOf("microsoft") !== -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const open = async (url: string): Promise<void> => {
|
||||||
|
let command: string;
|
||||||
|
const args = <string[]>[];
|
||||||
|
const options = <cp.SpawnOptions>{};
|
||||||
|
const platform = await isWsl() ? "wsl" : process.platform;
|
||||||
|
switch (platform) {
|
||||||
|
case "darwin":
|
||||||
|
command = "open";
|
||||||
|
break;
|
||||||
|
case "win32":
|
||||||
|
case "wsl":
|
||||||
|
command = platform === "wsl" ? "cmd.exe" : "cmd";
|
||||||
|
args.push("/c", "start", '""', "/b");
|
||||||
|
url = url.replace(/&/g, "^&");
|
||||||
|
default:
|
||||||
|
command = "xdg-open";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
args.push(url);
|
||||||
|
const proc = cp.spawn(command, args, options);
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
proc.on("error", reject);
|
||||||
|
proc.on("close", (code) => {
|
||||||
|
return code !== 0
|
||||||
|
? reject(new Error(`Failed to open with code ${code}`))
|
||||||
|
: resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue