mirror of https://github.com/coder/code-server.git
refactor: open function (#5257)
* refactor: fix type annotations in open There was no clear reason as to why we needed to use type assertions when initializing both `args` and `options` in `open` so I refactored them both. * refactor: create constructOpenOptions * refactor: add urlSearch and remove options * feat: add tests for constructOpenOptions
This commit is contained in:
parent
113ad85b37
commit
61138b4a61
|
@ -404,6 +404,37 @@ export const isWsl = async (
|
|||
}
|
||||
}
|
||||
|
||||
interface OpenOptions {
|
||||
args: string[]
|
||||
command: string
|
||||
urlSearch: string
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper function to construct options for `open` function.
|
||||
*
|
||||
* Extract to make it easier to test.
|
||||
*
|
||||
* @param platform - platform on machine
|
||||
* @param urlSearch - url.search
|
||||
* @returns an object with args, command, options and urlSearch
|
||||
*/
|
||||
export function constructOpenOptions(platform: NodeJS.Platform | "wsl", urlSearch: string): OpenOptions {
|
||||
const args: string[] = []
|
||||
let command = platform === "darwin" ? "open" : "xdg-open"
|
||||
if (platform === "win32" || platform === "wsl") {
|
||||
command = platform === "wsl" ? "cmd.exe" : "cmd"
|
||||
args.push("/c", "start", '""', "/b")
|
||||
urlSearch = urlSearch.replace(/&/g, "^&")
|
||||
}
|
||||
|
||||
return {
|
||||
args,
|
||||
command,
|
||||
urlSearch,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Try opening an address using whatever the system has set for opening URLs.
|
||||
*/
|
||||
|
@ -416,16 +447,10 @@ export const open = async (address: URL | string): Promise<void> => {
|
|||
if (url.hostname === "0.0.0.0") {
|
||||
url.hostname = "localhost"
|
||||
}
|
||||
const args = [] as string[]
|
||||
const options = {} as cp.SpawnOptions
|
||||
const platform = (await isWsl(process.platform, os.release(), "/proc/version")) ? "wsl" : process.platform
|
||||
let command = platform === "darwin" ? "open" : "xdg-open"
|
||||
if (platform === "win32" || platform === "wsl") {
|
||||
command = platform === "wsl" ? "cmd.exe" : "cmd"
|
||||
args.push("/c", "start", '""', "/b")
|
||||
url.search = url.search.replace(/&/g, "^&")
|
||||
}
|
||||
const proc = cp.spawn(command, [...args, url.toString()], options)
|
||||
const { command, args, urlSearch } = constructOpenOptions(platform, url.search)
|
||||
url.search = urlSearch
|
||||
const proc = cp.spawn(command, [...args, url.toString()], {})
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
proc.on("error", reject)
|
||||
proc.on("close", (code) => {
|
||||
|
|
|
@ -544,3 +544,37 @@ describe("open", () => {
|
|||
await expect(util.open(address)).rejects.toThrow("Cannot open socket paths")
|
||||
})
|
||||
})
|
||||
describe("constructOpenOptions", () => {
|
||||
it("should return options for darwin", () => {
|
||||
const platform: NodeJS.Platform | "wsl" = "darwin"
|
||||
const url = new URL("localhost:8080")
|
||||
const { args, command, urlSearch } = util.constructOpenOptions(platform, url.search)
|
||||
expect(args).toStrictEqual([])
|
||||
expect(command).toBe("open")
|
||||
expect(urlSearch).toBe("")
|
||||
})
|
||||
it("should return options for linux", () => {
|
||||
const platform: NodeJS.Platform | "wsl" = "linux"
|
||||
const url = new URL("localhost:8080")
|
||||
const { args, command, urlSearch } = util.constructOpenOptions(platform, url.search)
|
||||
expect(args).toStrictEqual([])
|
||||
expect(command).toBe("xdg-open")
|
||||
expect(urlSearch).toBe("")
|
||||
})
|
||||
it("should return options for win32", () => {
|
||||
const platform: NodeJS.Platform | "wsl" = "win32"
|
||||
const url = new URL("localhost:8080?q=&test")
|
||||
const { args, command, urlSearch } = util.constructOpenOptions(platform, url.search)
|
||||
expect(args).toStrictEqual(["/c", "start", '""', "/b"])
|
||||
expect(command).toBe("cmd")
|
||||
expect(urlSearch).toBe("?q=^&test")
|
||||
})
|
||||
it("should return options for wsl", () => {
|
||||
const platform: NodeJS.Platform | "wsl" = "wsl"
|
||||
const url = new URL("localhost:8080?q=&test")
|
||||
const { args, command, urlSearch } = util.constructOpenOptions(platform, url.search)
|
||||
expect(args).toStrictEqual(["/c", "start", '""', "/b"])
|
||||
expect(command).toBe("cmd.exe")
|
||||
expect(urlSearch).toBe("?q=^&test")
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue