Skip to content

refactor: open function #5257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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) => {
Expand Down
35 changes: 35 additions & 0 deletions test/unit/node/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,38 @@ describe("isWsl", () => {
})
})
})

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")
})
})