1- import { execSync } from "node:child_process"
21import fs from 'node:fs'
32import os from "node:os" ;
4- import { handleSpacesInPath } from "../tools.js" ;
3+ import { handleSpacesInPath , invokeCommand } from "../tools.js" ;
54import path from 'node:path'
65import Sbom from '../sbom.js'
76import { PackageURL } from 'packageurl-js'
@@ -29,10 +28,16 @@ export default class Base_javascript {
2928 throw new TypeError ( "_cmdName must be implemented" ) ;
3029 }
3130
31+ /**
32+ * @returns {Array<string> }
33+ */
3234 _listCmdArgs ( ) {
3335 throw new TypeError ( "_listCmdArgs must be implemented" ) ;
3436 }
3537
38+ /**
39+ * @returns {Array<string> }
40+ */
3641 _updateLockFileCmdArgs ( ) {
3742 throw new TypeError ( "_updateLockFileCmdArgs must be implemented" ) ;
3843 }
@@ -97,8 +102,7 @@ export default class Base_javascript {
97102 if ( parts . length === 2 ) {
98103 purlNs = parts [ 0 ] ;
99104 purlName = parts [ 1 ] ;
100- }
101- else {
105+ } else {
102106 purlName = parts [ 0 ] ;
103107 }
104108 return new PackageURL ( 'npm' , purlNs , purlName , version , undefined , undefined ) ;
@@ -154,8 +158,7 @@ export default class Base_javascript {
154158 Object . entries ( dependencies )
155159 . filter ( entry => entry [ 1 ] . version !== undefined )
156160 . forEach ( entry => {
157- let name , artifact ;
158- [ name , artifact ] = entry ;
161+ let [ name , artifact ] = entry ;
159162 let purl = this . #toPurl( name , artifact . version ) ;
160163 sbom . addDependency ( from , purl )
161164 let transitiveDeps = artifact . dependencies
@@ -168,22 +171,20 @@ export default class Base_javascript {
168171 #executeListCmd( includeTransitive , manifestDir ) {
169172 const listArgs = this . _listCmdArgs ( includeTransitive , manifestDir ) ;
170173 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 } ) ;
176177 }
177178 }
178179
179180 #version( ) {
180181 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 } )
187188 }
188189 }
189190
@@ -194,21 +195,17 @@ export default class Base_javascript {
194195 if ( os . platform ( ) === 'win32' ) {
195196 process . chdir ( manifestDir )
196197 }
197- const args = this . _updateLockFileCmdArgs ( manifestDir ) ;
198198
199199 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 } ) ;
205204 } finally {
206205 if ( os . platform ( ) === 'win32' ) {
207206 process . chdir ( originalDir )
208207 }
209208 }
210-
211-
212209 }
213210}
214211
0 commit comments