@@ -404,6 +404,40 @@ export const isWsl = async (
404
404
}
405
405
}
406
406
407
+ interface OpenOptions {
408
+ args : string [ ]
409
+ options : cp . SpawnOptions
410
+ command : string
411
+ urlSearch : string
412
+ }
413
+
414
+ /**
415
+ * A helper function to construct options for `open` function.
416
+ *
417
+ * Extract to make it easier to test.
418
+ *
419
+ * @param platform - platform on machine
420
+ * @param urlSearch - url.search
421
+ * @returns an object with args, command, options and urlSearch
422
+ */
423
+ export function constructOpenOptions ( platform : NodeJS . Platform | "wsl" , urlSearch : string ) : OpenOptions {
424
+ const args : string [ ] = [ ]
425
+ const options : cp . SpawnOptions = { }
426
+ let command = platform === "darwin" ? "open" : "xdg-open"
427
+ if ( platform === "win32" || platform === "wsl" ) {
428
+ command = platform === "wsl" ? "cmd.exe" : "cmd"
429
+ args . push ( "/c" , "start" , '""' , "/b" )
430
+ urlSearch = urlSearch . replace ( / & / g, "^&" )
431
+ }
432
+
433
+ return {
434
+ args,
435
+ command,
436
+ options,
437
+ urlSearch,
438
+ }
439
+ }
440
+
407
441
/**
408
442
* Try opening an address using whatever the system has set for opening URLs.
409
443
*/
@@ -416,15 +450,8 @@ export const open = async (address: URL | string): Promise<void> => {
416
450
if ( url . hostname === "0.0.0.0" ) {
417
451
url . hostname = "localhost"
418
452
}
419
- const args : string [ ] = [ ]
420
- const options : cp . SpawnOptions = { }
421
453
const platform = ( await isWsl ( process . platform , os . release ( ) , "/proc/version" ) ) ? "wsl" : process . platform
422
- let command = platform === "darwin" ? "open" : "xdg-open"
423
- if ( platform === "win32" || platform === "wsl" ) {
424
- command = platform === "wsl" ? "cmd.exe" : "cmd"
425
- args . push ( "/c" , "start" , '""' , "/b" )
426
- url . search = url . search . replace ( / & / g, "^&" )
427
- }
454
+ const { command, args, options } = constructOpenOptions ( platform , url . search )
428
455
const proc = cp . spawn ( command , [ ...args , url . toString ( ) ] , options )
429
456
await new Promise < void > ( ( resolve , reject ) => {
430
457
proc . on ( "error" , reject )
0 commit comments