Skip to content

Commit 10caf5b

Browse files
committed
refactor: standardize run-command.mjs error handling
Added proper error handling to all command execution functions: - runCommand: Added try/catch to handle non-zero exits from lib spawn - runCommandSync: Standardized shell options pattern - runCommandQuiet: Added try/catch for consistent error handling Changed shell option pattern from: shell: process.platform === 'win32' To: ...(process.platform === 'win32' && { shell: true }) This matches the pattern used in socket-sdk-js and socket-packageurl-js, ensuring consistent behavior across all socket-* repositories. Benefits: - Proper error handling prevents unhandled promise rejections - Consistent shell options pattern across repos - Better compatibility with @socketsecurity/lib/spawn behavior
1 parent 4d3c5cf commit 10caf5b

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

scripts/utils/run-command.mjs

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,21 @@ const logger = getDefaultLogger()
1414
* @returns {Promise<number>} Exit code
1515
*/
1616
export async function runCommand(command, args = [], options = {}) {
17-
const result = await spawn(command, args, {
18-
shell: process.platform === 'win32',
19-
stdio: 'inherit',
20-
...options,
21-
})
22-
return result.code
17+
try {
18+
const result = await spawn(command, args, {
19+
stdio: 'inherit',
20+
...(process.platform === 'win32' && { shell: true }),
21+
...options,
22+
})
23+
return result.code
24+
} catch (error) {
25+
// spawn() from @socketsecurity/lib throws on non-zero exit
26+
// Return the exit code from the error
27+
if (error && typeof error === 'object' && 'code' in error) {
28+
return error.code
29+
}
30+
throw error
31+
}
2332
}
2433

2534
/**
@@ -31,10 +40,11 @@ export async function runCommand(command, args = [], options = {}) {
3140
*/
3241
export function runCommandSync(command, args = [], options = {}) {
3342
const result = spawnSync(command, args, {
34-
shell: process.platform === 'win32',
3543
stdio: 'inherit',
44+
...(process.platform === 'win32' && { shell: true }),
3645
...options,
3746
})
47+
3848
return result.status || 0
3949
}
4050

@@ -85,17 +95,36 @@ export async function runParallel(commands) {
8595
* @returns {Promise<{exitCode: number, stdout: string, stderr: string}>}
8696
*/
8797
export async function runCommandQuiet(command, args = [], options = {}) {
88-
const result = await spawn(command, args, {
89-
shell: process.platform === 'win32',
90-
...options,
91-
stdio: ['inherit', 'pipe', 'pipe'],
92-
stdioString: true,
93-
})
98+
try {
99+
const result = await spawn(command, args, {
100+
...options,
101+
...(process.platform === 'win32' && { shell: true }),
102+
stdio: 'pipe',
103+
stdioString: true,
104+
})
94105

95-
return {
96-
exitCode: result.code,
97-
stderr: result.stderr,
98-
stdout: result.stdout,
106+
return {
107+
exitCode: result.code,
108+
stderr: result.stderr,
109+
stdout: result.stdout,
110+
}
111+
} catch (error) {
112+
// spawn() from @socketsecurity/lib throws on non-zero exit
113+
// Return the exit code and output from the error
114+
if (
115+
error &&
116+
typeof error === 'object' &&
117+
'code' in error &&
118+
'stdout' in error &&
119+
'stderr' in error
120+
) {
121+
return {
122+
exitCode: error.code,
123+
stderr: error.stderr,
124+
stdout: error.stdout,
125+
}
126+
}
127+
throw error
99128
}
100129
}
101130

0 commit comments

Comments
 (0)