1
- import { execSync } from "node:child_process"
2
1
import fs from 'node:fs'
3
2
import os from "node:os" ;
4
- import { handleSpacesInPath } from "../tools.js" ;
3
+ import { handleSpacesInPath , invokeCommand } from "../tools.js" ;
5
4
import path from 'node:path'
6
5
import Sbom from '../sbom.js'
7
6
import { PackageURL } from 'packageurl-js'
@@ -29,10 +28,16 @@ export default class Base_javascript {
29
28
throw new TypeError ( "_cmdName must be implemented" ) ;
30
29
}
31
30
31
+ /**
32
+ * @returns {Array<string> }
33
+ */
32
34
_listCmdArgs ( ) {
33
35
throw new TypeError ( "_listCmdArgs must be implemented" ) ;
34
36
}
35
37
38
+ /**
39
+ * @returns {Array<string> }
40
+ */
36
41
_updateLockFileCmdArgs ( ) {
37
42
throw new TypeError ( "_updateLockFileCmdArgs must be implemented" ) ;
38
43
}
@@ -97,8 +102,7 @@ export default class Base_javascript {
97
102
if ( parts . length === 2 ) {
98
103
purlNs = parts [ 0 ] ;
99
104
purlName = parts [ 1 ] ;
100
- }
101
- else {
105
+ } else {
102
106
purlName = parts [ 0 ] ;
103
107
}
104
108
return new PackageURL ( 'npm' , purlNs , purlName , version , undefined , undefined ) ;
@@ -154,8 +158,7 @@ export default class Base_javascript {
154
158
Object . entries ( dependencies )
155
159
. filter ( entry => entry [ 1 ] . version !== undefined )
156
160
. forEach ( entry => {
157
- let name , artifact ;
158
- [ name , artifact ] = entry ;
161
+ let [ name , artifact ] = entry ;
159
162
let purl = this . #toPurl( name , artifact . version ) ;
160
163
sbom . addDependency ( from , purl )
161
164
let transitiveDeps = artifact . dependencies
@@ -168,22 +171,20 @@ export default class Base_javascript {
168
171
#executeListCmd( includeTransitive , manifestDir ) {
169
172
const listArgs = this . _listCmdArgs ( includeTransitive , manifestDir ) ;
170
173
try {
171
- return execSync ( listArgs , {
172
- encoding : 'utf-8'
173
- } ) ;
174
- } catch ( err ) {
175
- throw new Error ( `failed to execute command ${ listArgs } - Error: ${ err } ` ) ;
174
+ invokeCommand ( this . _cmdName ( ) , listArgs )
175
+ } catch ( error ) {
176
+ throw new Error ( `failed to list dependencies via "${ this . _cmdName ( ) } ${ listArgs . join ( ' ' ) } " - Error: ${ error } ` , { cause : error } ) ;
176
177
}
177
178
}
178
179
179
180
#version( ) {
180
181
try {
181
- execSync ( ` ${ handleSpacesInPath ( this . _cmdName ( ) ) } --version` , {
182
- stdio : 'ignore' ,
183
- encoding : "utf8" ,
184
- } ) ;
185
- } catch ( err ) {
186
- throw new Error ( `${ this . _cmdName ( ) } is not accessible: ${ err } ` ) ;
182
+ invokeCommand ( this . _cmdName ( ) , [ ' --version' ] , { stdio : 'ignore' } ) ;
183
+ } catch ( error ) {
184
+ if ( error . code === 'ENOENT' ) {
185
+ throw new Error ( ` ${ this . _cmdName ( ) } is not accessible` ) ;
186
+ }
187
+ throw new Error ( `failed to check for package manager binary at ${ this . _cmdName ( ) } ` , { cause : error } )
187
188
}
188
189
}
189
190
@@ -194,21 +195,17 @@ export default class Base_javascript {
194
195
if ( os . platform ( ) === 'win32' ) {
195
196
process . chdir ( manifestDir )
196
197
}
197
- const args = this . _updateLockFileCmdArgs ( manifestDir ) ;
198
198
199
199
try {
200
- return execSync ( args , {
201
- encoding : 'utf-8'
202
- } ) ;
203
- } catch ( err ) {
204
- throw new Error ( `failed to execute command ${ args } - Error: ${ err } ` ) ;
200
+ const args = this . _updateLockFileCmdArgs ( manifestDir ) ;
201
+ invokeCommand ( this . _cmdName ( ) , args )
202
+ } catch ( error ) {
203
+ throw new Error ( `failed to create lockfile "${ args } " - Error: ${ error } ` , { cause : error } ) ;
205
204
} finally {
206
205
if ( os . platform ( ) === 'win32' ) {
207
206
process . chdir ( originalDir )
208
207
}
209
208
}
210
-
211
-
212
209
}
213
210
}
214
211
0 commit comments