Skip to content

Commit 24d7d7c

Browse files
committed
feat: use custom path for npm/pnpm
Signed-off-by: Ruben Romero Montes <[email protected]>
1 parent 8a7e65d commit 24d7d7c

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/providers/base_javascript.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import os from "node:os";
33
import path from 'node:path'
44
import { PackageURL } from 'packageurl-js'
55

6-
import { invokeCommand } from "../tools.js";
6+
import { getCustomPath, invokeCommand } from "../tools.js";
77
import Sbom from '../sbom.js'
88

99
/** @typedef {import('../provider.js').Provider} */
@@ -15,6 +15,9 @@ const defaultVersion = 'v0.0.0'
1515

1616
export default class Base_javascript {
1717

18+
// Resolved cmd to use
19+
#cmd;
20+
1821
/**
1922
* @returns {string} the name of the lock file name for the specific implementation
2023
*/
@@ -30,15 +33,15 @@ export default class Base_javascript {
3033
}
3134

3235
/**
33-
* @returns {Array<string>}
34-
*/
36+
* @returns {Array<string>}
37+
*/
3538
_listCmdArgs() {
3639
throw new TypeError("_listCmdArgs must be implemented");
3740
}
3841

3942
/**
40-
* @returns {Array<string>}
41-
*/
43+
* @returns {Array<string>}
44+
*/
4245
_updateLockFileCmdArgs() {
4346
throw new TypeError("_updateLockFileCmdArgs must be implemented");
4447
}
@@ -91,12 +94,11 @@ export default class Base_javascript {
9194
}
9295

9396
/**
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+
*/
100102
#toPurl(name, version) {
101103
let parts = name.split("/");
102104
var purlNs, purlName;
@@ -126,7 +128,8 @@ export default class Base_javascript {
126128
* @private
127129
*/
128130
#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);
130133
let rootName = depsObject["name"]
131134
let rootVersion = depsObject["version"]
132135
if (!rootVersion) {
@@ -171,22 +174,11 @@ export default class Base_javascript {
171174

172175
#executeListCmd(includeTransitive, manifestDir) {
173176
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);
179178
}
180179

181180
#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' });
190182
}
191183

192184
#createLockFile(manifestDir) {
@@ -198,14 +190,25 @@ export default class Base_javascript {
198190
}
199191
const args = this._updateLockFileCmdArgs(manifestDir);
200192
try {
201-
invokeCommand(this._cmdName(), args)
193+
this.#invokeCommand(args);
202194
} 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 });
204196
} finally {
205197
if (os.platform() === 'win32') {
206198
process.chdir(originalDir)
207199
}
208200
}
209201
}
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+
}
210213
}
211214

0 commit comments

Comments
 (0)