Implement open flag

This commit is contained in:
Asher 2019-07-15 13:31:05 -05:00
parent 9446cc8245
commit 9b0b337dc0
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 44 additions and 1 deletions

View File

@ -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) => {

View File

@ -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();
});
});
};