From 9b0b337dc0f229424decca59fc8f57f558fee63d Mon Sep 17 00:00:00 2001 From: Asher Date: Mon, 15 Jul 2019 13:31:05 -0500 Subject: [PATCH] Implement open flag --- src/cli.ts | 7 ++++++- src/util.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index cc866de28..d06b7d2c5 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -10,7 +10,7 @@ import pkg from "vs/platform/product/node/package"; import { MainServer, WebviewServer } from "vs/server/src/server"; 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 { "allow-http"?: boolean; @@ -192,6 +192,11 @@ const main = async (): Promise => { } else { console.log(" - Not serving HTTPS"); } + + if (args["open"]) { + await open(serverAddress).catch(console.error); + console.log(" - Opened URL"); + } }; main().catch((error) => { diff --git a/src/util.ts b/src/util.ts index a4f3ac791..a4995030a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,3 +1,4 @@ +import * as cp from "child_process"; import * as crypto from "crypto"; import * as fs from "fs"; import * as os from "os"; @@ -76,3 +77,40 @@ export const getMediaMime = (filePath?: string): string => { ".json": "application/json", }[extname(filePath)]) || "text/plain"; }; + +export const isWsl = async (): Promise => { + 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 => { + let command: string; + const args = []; + const options = {}; + 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(); + }); + }); +};