@@ -113,25 +113,28 @@ export class BomBuilder {
113
113
)
114
114
}
115
115
116
- private getNpmCommand ( process : NodeJS . Process ) : string {
116
+ private getNpmCommand ( process : NodeJS . Process ) : string | 'npm' {
117
117
// `npm_execpath` will be whichever cli script has called this application by npm.
118
118
// This can be `npm`, `npx`, or `undefined` if called by `node` directly.
119
119
const execPath = process . env . npm_execpath ?? ''
120
+ if ( execPath === '' ) {
121
+ return 'npm'
122
+ }
120
123
121
124
if ( this . npxMatcher . test ( execPath ) ) {
122
125
// `npm` must be used for executing `ls`.
123
126
this . console . debug ( 'DEBUG | command: npx-cli.js usage detected, checking for npm-cli.js ...' )
124
127
// Typically `npm-cli.js` is alongside `npx-cli.js`, as such we attempt to use this and validate it exists.
125
128
// Replace the script in the path, and normalise it with resolve (eliminates any extraneous path separators).
126
129
const npmPath = resolve ( execPath . replace ( this . npxMatcher , '$1npm-cli.js' ) )
127
-
128
- return existsSync ( npmPath )
129
- ? npmPath // path detected
130
- : 'npm' // fallback to global npm
130
+ if ( existsSync ( npmPath ) ) {
131
+ return npmPath
132
+ }
133
+ } else if ( existsSync ( execPath ) ) {
134
+ return execPath
131
135
}
132
136
133
- /* eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/strict-boolean-expressions -- need to handle optional empty-string */
134
- return execPath || 'npm'
137
+ throw new Error ( `unexpected NPM execPath: ${ execPath } ` )
135
138
}
136
139
137
140
private fetchNpmLs ( projectDir : string , process : NodeJS . Process ) : any {
@@ -160,8 +163,7 @@ export class BomBuilder {
160
163
this . console . info ( 'INFO | gather dependency tree ...' )
161
164
this . console . debug ( 'DEBUG | npm-ls: run %s with %j in %s' , command , args , projectDir )
162
165
const npmLsReturns = spawnSync ( command , args , {
163
- // must use a shell for Windows systems in order to work
164
- shell : true ,
166
+ shell : command === 'npm' ,
165
167
cwd : projectDir ,
166
168
env : process . env ,
167
169
encoding : 'buffer' ,
0 commit comments