mirror of
https://git.tuxpa.in/a/code-server.git
synced 2024-12-27 04:35:25 +00:00
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 "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<void> => {
|
||||
} else {
|
||||
console.log(" - Not serving HTTPS");
|
||||
}
|
||||
|
||||
if (args["open"]) {
|
||||
await open(serverAddress).catch(console.error);
|
||||
console.log(" - Opened URL");
|
||||
}
|
||||
};
|
||||
|
||||
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 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<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
Block a user