@@ -3,86 +3,87 @@ const path = require('path');
3
3
const childProcess = require ( 'child_process' ) ;
4
4
const isWsl = require ( 'is-wsl' ) ;
5
5
6
- module . exports = ( target , opts ) => {
6
+ module . exports = async ( target , options ) => {
7
7
if ( typeof target !== 'string' ) {
8
- return Promise . reject ( new Error ( 'Expected a `target`' ) ) ;
8
+ throw new TypeError ( 'Expected a `target`' ) ;
9
9
}
10
10
11
- opts = Object . assign ( { wait : true } , opts ) ;
11
+ options = {
12
+ wait : true ,
13
+ ...options
14
+ } ;
12
15
13
- let cmd ;
14
- let appArgs = [ ] ;
15
- let args = [ ] ;
16
- const cpOpts = { } ;
16
+ let command ;
17
+ let appArguments = [ ] ;
18
+ const cliArguments = [ ] ;
19
+ const childProcessOptions = { } ;
17
20
18
- if ( Array . isArray ( opts . app ) ) {
19
- appArgs = opts . app . slice ( 1 ) ;
20
- opts . app = opts . app [ 0 ] ;
21
+ if ( Array . isArray ( options . app ) ) {
22
+ appArguments = options . app . slice ( 1 ) ;
23
+ options . app = options . app [ 0 ] ;
21
24
}
22
25
23
26
if ( process . platform === 'darwin' ) {
24
- cmd = 'open' ;
27
+ command = 'open' ;
25
28
26
- if ( opts . wait ) {
27
- args . push ( '-W' ) ;
29
+ if ( options . wait ) {
30
+ cliArguments . push ( '-W' ) ;
28
31
}
29
32
30
- if ( opts . app ) {
31
- args . push ( '-a' , opts . app ) ;
33
+ if ( options . app ) {
34
+ cliArguments . push ( '-a' , options . app ) ;
32
35
}
33
36
} else if ( process . platform === 'win32' || isWsl ) {
34
- cmd = 'cmd' + ( isWsl ? '.exe' : '' ) ;
35
- args . push ( '/c' , 'start' , '""' , '/b' ) ;
37
+ command = 'cmd' + ( isWsl ? '.exe' : '' ) ;
38
+ cliArguments . push ( '/c' , 'start' , '""' , '/b' ) ;
36
39
target = target . replace ( / & / g, '^&' ) ;
37
40
38
- if ( opts . wait ) {
39
- args . push ( '/wait' ) ;
41
+ if ( options . wait ) {
42
+ cliArguments . push ( '/wait' ) ;
40
43
}
41
44
42
- if ( opts . app ) {
43
- args . push ( opts . app ) ;
45
+ if ( options . app ) {
46
+ cliArguments . push ( options . app ) ;
44
47
}
45
48
46
- if ( appArgs . length > 0 ) {
47
- args = args . concat ( appArgs ) ;
49
+ if ( appArguments . length > 0 ) {
50
+ cliArguments . push ( ... appArguments ) ;
48
51
}
49
52
} else {
50
- if ( opts . app ) {
51
- cmd = opts . app ;
53
+ if ( options . app ) {
54
+ command = options . app ;
52
55
} else {
53
56
const useSystemXdgOpen = process . versions . electron || process . platform === 'android' ;
54
- cmd = useSystemXdgOpen ? 'xdg-open' : path . join ( __dirname , 'xdg-open' ) ;
57
+ command = useSystemXdgOpen ? 'xdg-open' : path . join ( __dirname , 'xdg-open' ) ;
55
58
}
56
59
57
- if ( appArgs . length > 0 ) {
58
- args = args . concat ( appArgs ) ;
60
+ if ( appArguments . length > 0 ) {
61
+ cliArguments . push ( ... appArguments ) ;
59
62
}
60
63
61
- if ( ! opts . wait ) {
62
- // `xdg-open` will block the process unless
63
- // stdio is ignored and it's detached from the parent
64
- // even if it's unref'd
65
- cpOpts . stdio = 'ignore' ;
66
- cpOpts . detached = true ;
64
+ if ( ! options . wait ) {
65
+ // `xdg-open` will block the process unless stdio is ignored
66
+ // and it's detached from the parent even if it's unref'd.
67
+ childProcessOptions . stdio = 'ignore' ;
68
+ childProcessOptions . detached = true ;
67
69
}
68
70
}
69
71
70
- args . push ( target ) ;
72
+ cliArguments . push ( target ) ;
71
73
72
- if ( process . platform === 'darwin' && appArgs . length > 0 ) {
73
- args . push ( '--args' ) ;
74
- args = args . concat ( appArgs ) ;
74
+ if ( process . platform === 'darwin' && appArguments . length > 0 ) {
75
+ cliArguments . push ( '--args' , ...appArguments ) ;
75
76
}
76
77
77
- const cp = childProcess . spawn ( cmd , args , cpOpts ) ;
78
+ const cp = childProcess . spawn ( command , cliArguments , childProcessOptions ) ;
78
79
79
- if ( opts . wait ) {
80
+ if ( options . wait ) {
80
81
return new Promise ( ( resolve , reject ) => {
81
82
cp . once ( 'error' , reject ) ;
82
83
83
- cp . once ( 'close' , code => {
84
- if ( code > 0 ) {
85
- reject ( new Error ( ' Exited with code ' + code ) ) ;
84
+ cp . once ( 'close' , exitCode => {
85
+ if ( exitCode > 0 ) {
86
+ reject ( new Error ( ` Exited with code ${ exitCode } ` ) ) ;
86
87
return ;
87
88
}
88
89
0 commit comments