Skip to content

Commit 2c56928

Browse files
committed
fix: sub processes with spaces work
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 47f7d93 commit 2c56928

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/builders.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,28 @@ export class BomBuilder {
113113
)
114114
}
115115

116-
private getNpmCommand (process: NodeJS.Process): string {
116+
private getNpmCommand (process: NodeJS.Process): string | 'npm' {
117117
// `npm_execpath` will be whichever cli script has called this application by npm.
118118
// This can be `npm`, `npx`, or `undefined` if called by `node` directly.
119119
const execPath = process.env.npm_execpath ?? ''
120+
if (execPath === '') {
121+
return 'npm'
122+
}
120123

121124
if (this.npxMatcher.test(execPath)) {
122125
// `npm` must be used for executing `ls`.
123126
this.console.debug('DEBUG | command: npx-cli.js usage detected, checking for npm-cli.js ...')
124127
// Typically `npm-cli.js` is alongside `npx-cli.js`, as such we attempt to use this and validate it exists.
125128
// Replace the script in the path, and normalise it with resolve (eliminates any extraneous path separators).
126129
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
131135
}
132136

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}`)
135138
}
136139

137140
private fetchNpmLs (projectDir: string, process: NodeJS.Process): any {
@@ -160,8 +163,7 @@ export class BomBuilder {
160163
this.console.info('INFO | gather dependency tree ...')
161164
this.console.debug('DEBUG | npm-ls: run %s with %j in %s', command, args, projectDir)
162165
const npmLsReturns = spawnSync(command, args, {
163-
// must use a shell for Windows systems in order to work
164-
shell: true,
166+
shell: command === 'npm',
165167
cwd: projectDir,
166168
env: process.env,
167169
encoding: 'buffer',

tests/integration/synthetics/cli.run.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('cli.run()', () => {
110110
try {
111111
expect(() => {
112112
cli.run(mockProcess)
113-
}).toThrow(/^npm-ls exited with errors: \?\?\? [1-9]\d* noSignal$/i)
113+
}).toThrow(/^unexpected npm execpath/i)
114114
} finally {
115115
closeSync(stdout.fd)
116116
stderr.close()

0 commit comments

Comments
 (0)