@@ -7,7 +7,8 @@ const yargs = require('yargs')
7
7
const usage = `$0 [--package|-p <package>] [--cache <path>] [--userconfig <path>] [-c <string>] [--shell <string>] [--shell-auto-fallback [<shell>]] [--ignore-existing] [--version|-v] [--] <command>[@version] [command-arg]...`
8
8
9
9
module . exports = parseArgs
10
- function parseArgs ( ) {
10
+ function parseArgs ( argv ) {
11
+ argv = argv || process . argv
11
12
const parser = yargs
12
13
. usage ( `Execute a binary from an npm package\n${ usage } ` )
13
14
. option ( 'package' , {
@@ -49,12 +50,11 @@ function parseArgs () {
49
50
50
51
const opts = parser . getOptions ( )
51
52
const bools = new Set ( opts . boolean )
52
- const raw = process . argv
53
53
54
54
let cmdIndex
55
55
let hasDashDash
56
- for ( let i = 2 ; i < raw . length ; i ++ ) {
57
- const opt = raw [ i ]
56
+ for ( let i = 2 ; i < argv . length ; i ++ ) {
57
+ const opt = argv [ i ]
58
58
if ( opt === '--' ) {
59
59
hasDashDash = true
60
60
break
@@ -68,19 +68,21 @@ function parseArgs () {
68
68
}
69
69
}
70
70
if ( cmdIndex ) {
71
- const parsed = parser . parse ( process . argv . slice ( 0 , cmdIndex ) )
72
- const parsedCmd = npa ( process . argv [ cmdIndex ] )
71
+ const parsed = parser . parse ( argv . slice ( 0 , cmdIndex ) )
72
+ const parsedCmd = npa ( argv [ cmdIndex ] )
73
73
parsed . command = parsed . package
74
- ? process . argv [ cmdIndex ]
74
+ ? argv [ cmdIndex ]
75
75
: guessCmdName ( parsedCmd )
76
- parsed . cmdOpts = process . argv . slice ( cmdIndex + 1 )
76
+ parsed . cmdOpts = argv . slice ( cmdIndex + 1 )
77
77
parsed . packageRequested = ! ! parsed . package
78
- parsed . cmdHadVersion = parsedCmd . name !== parsedCmd . raw
79
- const pkg = parsed . package || process . argv [ cmdIndex ]
78
+ parsed . cmdHadVersion = parsed . package
79
+ ? false
80
+ : parsedCmd . name !== parsedCmd . raw
81
+ const pkg = parsed . package || argv [ cmdIndex ]
80
82
parsed . p = parsed . package = npa ( pkg ) . toString ( )
81
83
return parsed
82
84
} else {
83
- const parsed = parser . argv
85
+ const parsed = parser . parse ( argv )
84
86
if ( parsed . call ) {
85
87
const splitCmd = parsed . call . trim ( ) . split ( / \s + / )
86
88
const parsedCmd = npa ( splitCmd [ 0 ] )
@@ -89,18 +91,24 @@ function parseArgs () {
89
91
: guessCmdName ( parsedCmd )
90
92
parsed . cmdOpts = splitCmd . slice ( 1 )
91
93
parsed . packageRequested = ! ! parsed . package
92
- parsed . cmdHadVersion = parsedCmd . name !== parsedCmd . raw
94
+ parsed . cmdHadVersion = parsed . package
95
+ ? false
96
+ : parsedCmd . name !== parsedCmd . raw
93
97
const pkg = parsed . package || splitCmd [ 0 ]
94
98
parsed . p = parsed . package = npa ( pkg ) . toString ( )
95
99
} else if ( hasDashDash ) {
96
- const splitCmd = parsed . _
100
+ const splitCmd = parsed . _ . slice ( 2 )
97
101
const parsedCmd = npa ( splitCmd [ 0 ] )
98
102
parsed . command = parsed . package
99
103
? splitCmd [ 0 ]
100
104
: guessCmdName ( parsedCmd )
101
105
parsed . cmdOpts = splitCmd . slice ( 1 )
102
106
parsed . packageRequested = ! ! parsed . package
103
- parsed . cmdHadVersion = parsedCmd . name !== parsedCmd . raw
107
+ parsed . cmdHadVersion = parsed . package
108
+ ? false
109
+ : parsedCmd . name !== parsedCmd . raw
110
+ const pkg = parsed . package || splitCmd [ 0 ]
111
+ parsed . p = parsed . package = npa ( pkg ) . toString ( )
104
112
}
105
113
return parsed
106
114
}
@@ -117,9 +125,7 @@ function guessCmdName (spec) {
117
125
return spec . hosted . project
118
126
} else if ( spec . type === 'git' ) {
119
127
const match = spec . fetchSpec . match ( / ( [ a - z 0 - 9 - ] + ) (?: \. g i t ) ? $ / i)
120
- if ( match && match [ 1 ] ) {
121
- return match [ 1 ]
122
- }
128
+ return match [ 1 ]
123
129
} else if ( spec . type === 'directory' ) {
124
130
return path . basename ( spec . fetchSpec )
125
131
} else if ( spec . type === 'file' || spec . type === 'remote' ) {
0 commit comments