@@ -3,7 +3,7 @@ import os from "node:os";
3
3
import path from 'node:path'
4
4
import { PackageURL } from 'packageurl-js'
5
5
6
- import { invokeCommand } from "../tools.js" ;
6
+ import { getCustomPath , invokeCommand } from "../tools.js" ;
7
7
import Sbom from '../sbom.js'
8
8
9
9
/** @typedef {import('../provider.js').Provider } */
@@ -15,6 +15,9 @@ const defaultVersion = 'v0.0.0'
15
15
16
16
export default class Base_javascript {
17
17
18
+ // Resolved cmd to use
19
+ #cmd;
20
+
18
21
/**
19
22
* @returns {string } the name of the lock file name for the specific implementation
20
23
*/
@@ -30,15 +33,15 @@ export default class Base_javascript {
30
33
}
31
34
32
35
/**
33
- * @returns {Array<string> }
34
- */
36
+ * @returns {Array<string> }
37
+ */
35
38
_listCmdArgs ( ) {
36
39
throw new TypeError ( "_listCmdArgs must be implemented" ) ;
37
40
}
38
41
39
42
/**
40
- * @returns {Array<string> }
41
- */
43
+ * @returns {Array<string> }
44
+ */
42
45
_updateLockFileCmdArgs ( ) {
43
46
throw new TypeError ( "_updateLockFileCmdArgs must be implemented" ) ;
44
47
}
@@ -91,12 +94,11 @@ export default class Base_javascript {
91
94
}
92
95
93
96
/**
94
- * Utility function for creating Purl String
95
-
96
- * @param name the name of the artifact, can include a namespace(group) or not - namespace/artifactName.
97
- * @param version the version of the artifact
98
- * @returns {PackageURL|null } PackageUrl Object ready to be used in SBOM
99
- */
97
+ * Utility function for creating Purl String
98
+ * @param name the name of the artifact, can include a namespace(group) or not - namespace/artifactName.
99
+ * @param version the version of the artifact
100
+ * @returns {PackageURL|null } PackageUrl Object ready to be used in SBOM
101
+ */
100
102
#toPurl( name , version ) {
101
103
let parts = name . split ( "/" ) ;
102
104
var purlNs , purlName ;
@@ -126,7 +128,8 @@ export default class Base_javascript {
126
128
* @private
127
129
*/
128
130
#getSBOM( manifest , opts = { } , includeTransitive ) {
129
- const depsObject = this . _buildDependencyTree ( includeTransitive , manifest ) ;
131
+ this . #cmd = getCustomPath ( this . _cmdName ( ) , opts ) ;
132
+ const depsObject = this . _buildDependencyTree ( includeTransitive , manifest , opts ) ;
130
133
let rootName = depsObject [ "name" ]
131
134
let rootVersion = depsObject [ "version" ]
132
135
if ( ! rootVersion ) {
@@ -171,22 +174,11 @@ export default class Base_javascript {
171
174
172
175
#executeListCmd( includeTransitive , manifestDir ) {
173
176
const listArgs = this . _listCmdArgs ( includeTransitive , manifestDir ) ;
174
- try {
175
- return invokeCommand ( this . _cmdName ( ) , listArgs )
176
- } catch ( error ) {
177
- throw new Error ( `failed to list dependencies via "${ this . _cmdName ( ) } ${ listArgs . join ( ' ' ) } " - Error: ${ error } ` , { cause : error } ) ;
178
- }
177
+ return this . #invokeCommand( listArgs ) ;
179
178
}
180
179
181
180
#version( ) {
182
- try {
183
- invokeCommand ( this . _cmdName ( ) , [ '--version' ] , { stdio : 'ignore' } ) ;
184
- } catch ( error ) {
185
- if ( error . code === 'ENOENT' ) {
186
- throw new Error ( `${ this . _cmdName ( ) } is not accessible` ) ;
187
- }
188
- throw new Error ( `failed to check for package manager binary at ${ this . _cmdName ( ) } ` , { cause : error } )
189
- }
181
+ this . #invokeCommand( [ '--version' ] , { stdio : 'ignore' } ) ;
190
182
}
191
183
192
184
#createLockFile( manifestDir ) {
@@ -198,14 +190,25 @@ export default class Base_javascript {
198
190
}
199
191
const args = this . _updateLockFileCmdArgs ( manifestDir ) ;
200
192
try {
201
- invokeCommand ( this . _cmdName ( ) , args )
193
+ this . #invokeCommand ( args ) ;
202
194
} catch ( error ) {
203
- throw new Error ( `failed to create lockfile "${ args } " - Error: ${ error } ` , { cause : error } ) ;
195
+ throw new Error ( `failed to create lockfile "${ args } "` , { cause : error } ) ;
204
196
} finally {
205
197
if ( os . platform ( ) === 'win32' ) {
206
198
process . chdir ( originalDir )
207
199
}
208
200
}
209
201
}
202
+
203
+ #invokeCommand( args , opts = { } ) {
204
+ try {
205
+ return invokeCommand ( this . #cmd, args , opts ) ;
206
+ } catch ( error ) {
207
+ if ( error . code === 'ENOENT' ) {
208
+ throw new Error ( `${ this . #cmd} is not accessible` ) ;
209
+ }
210
+ throw new Error ( `failed to execute ${ this . #cmd} ${ args } ` , { cause : error } )
211
+ }
212
+ }
210
213
}
211
214
0 commit comments