Skip to content

Commit 9b0b337

Browse files
committed
Implement open flag
1 parent 9446cc8 commit 9b0b337

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import pkg from "vs/platform/product/node/package";
1010

1111
import { MainServer, WebviewServer } from "vs/server/src/server";
1212
import "vs/server/src/tar";
13-
import { generateCertificate, generatePassword } from "vs/server/src/util";
13+
import { generateCertificate, generatePassword, open } from "vs/server/src/util";
1414

1515
interface Args extends ParsedArgs {
1616
"allow-http"?: boolean;
@@ -192,6 +192,11 @@ const main = async (): Promise<void> => {
192192
} else {
193193
console.log(" - Not serving HTTPS");
194194
}
195+
196+
if (args["open"]) {
197+
await open(serverAddress).catch(console.error);
198+
console.log(" - Opened URL");
199+
}
195200
};
196201

197202
main().catch((error) => {

src/util.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as cp from "child_process";
12
import * as crypto from "crypto";
23
import * as fs from "fs";
34
import * as os from "os";
@@ -76,3 +77,40 @@ export const getMediaMime = (filePath?: string): string => {
7677
".json": "application/json",
7778
}[extname(filePath)]) || "text/plain";
7879
};
80+
81+
export const isWsl = async (): Promise<boolean> => {
82+
return process.platform === "linux"
83+
&& os.release().toLowerCase().indexOf("microsoft") !== -1
84+
|| (await util.promisify(fs.readFile)("/proc/version", "utf8"))
85+
.toLowerCase().indexOf("microsoft") !== -1;
86+
};
87+
88+
export const open = async (url: string): Promise<void> => {
89+
let command: string;
90+
const args = <string[]>[];
91+
const options = <cp.SpawnOptions>{};
92+
const platform = await isWsl() ? "wsl" : process.platform;
93+
switch (platform) {
94+
case "darwin":
95+
command = "open";
96+
break;
97+
case "win32":
98+
case "wsl":
99+
command = platform === "wsl" ? "cmd.exe" : "cmd";
100+
args.push("/c", "start", '""', "/b");
101+
url = url.replace(/&/g, "^&");
102+
default:
103+
command = "xdg-open";
104+
break;
105+
}
106+
args.push(url);
107+
const proc = cp.spawn(command, args, options);
108+
await new Promise((resolve, reject) => {
109+
proc.on("error", reject);
110+
proc.on("close", (code) => {
111+
return code !== 0
112+
? reject(new Error(`Failed to open with code ${code}`))
113+
: resolve();
114+
});
115+
});
116+
};

0 commit comments

Comments
 (0)