Skip to content

Commit cb95607

Browse files
Strum355ruromero
authored andcommitted
chore: use invokeCommand instead of execSync
1 parent 50c9ffc commit cb95607

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

src/providers/base_javascript.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { execSync } from "node:child_process"
21
import fs from 'node:fs'
32
import os from "node:os";
4-
import { handleSpacesInPath } from "../tools.js";
3+
import { handleSpacesInPath, invokeCommand } from "../tools.js";
54
import path from 'node:path'
65
import Sbom from '../sbom.js'
76
import { 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

src/providers/javascript_npm.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ export default class Javascript_npm extends Base_javascript {
1111
}
1212

1313
_listCmdArgs(includeTransitive, manifestDir) {
14-
const depthArg = includeTransitive ? "--all" : "--depth=0";
15-
const manifestArg = manifestDir ? `--prefix ${manifestDir}` : "";
16-
17-
return `${this._cmdName()} ls ${depthArg} --package-lock-only --omit=dev --json ${manifestArg}`;
14+
const args = ['ls', includeTransitive ? '--all' : '--depth=0', '--package-lock-only', '--omit=dev', '--json']
15+
if (manifestDir) {
16+
args.push('--prefix', manifestDir)
17+
}
18+
return args
1819
}
1920

2021
_updateLockFileCmdArgs(manifestDir) {
21-
const manifestArg = manifestDir ? `--dir ${manifestDir}` : "";
22-
return `${this._cmdName()} install --package-lock-only ${manifestArg}`;
22+
const args = ['install', '--package-lock-only']
23+
if (manifestDir) {
24+
args.push('--dir', manifestDir)
25+
}
26+
return args;
2327
}
24-
2528
}

src/providers/javascript_pnpm.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ export default class Javascript_pnpm extends Base_javascript {
1111
}
1212

1313
_listCmdArgs(includeTransitive, manifestDir) {
14-
const depthArg = includeTransitive ? "--depth=Infinity" : "--depth=0";
15-
const manifestArg = manifestDir ? `--dir ${manifestDir}` : "";
16-
return `${this._cmdName()} ls ${depthArg} ${manifestArg} --prod --json`;
14+
const args = ['ls', includeTransitive ? '--all' : '--depth=0', '--prod', '--json'];
15+
if (manifestDir) {
16+
args.push('--prefix', manifestDir);
17+
}
18+
return args;
1719
}
1820

1921
_updateLockFileCmdArgs(manifestDir) {
20-
const manifestArg = manifestDir ? `--dir ${manifestDir}` : "";
21-
return `${this._cmdName()} install --frozen-lockfile ${manifestArg}`;
22+
const args = ['install', '--frozen-lockfile'];
23+
if (manifestDir) {
24+
args.push('--prefix', manifestDir)
25+
}
26+
args.push(...[])
27+
return args;
2228
}
2329

2430
_buildDependencyTree(includeTransitive, manifest) {

0 commit comments

Comments
 (0)