-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatcher-shell.js
More file actions
29 lines (27 loc) · 1.06 KB
/
Copy pathdispatcher-shell.js
File metadata and controls
29 lines (27 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { exec as execCb } from 'child_process';
// Platform-aware shell defaults:
// - macOS: /bin/zsh
// - Linux/WSL: /bin/bash
// - Windows: cmd.exe
// Override with SCHEDULER_SHELL env var.
export const DEFAULT_SHELL = process.env.SCHEDULER_SHELL
|| (process.platform === 'darwin'
? '/bin/zsh'
: process.platform === 'win32'
? 'cmd.exe'
: '/bin/bash');
export function runShellCommand(cmd, timeoutMs = 300000, env = null) {
if (!cmd || typeof cmd !== 'string') throw new Error('Shell command must be a non-empty string');
const safeTimeout = (Number.isFinite(timeoutMs) && timeoutMs > 0) ? timeoutMs : 300_000;
return new Promise((resolve) => {
execCb(cmd, { timeout: safeTimeout, maxBuffer: 64 * 1024 * 1024, shell: DEFAULT_SHELL, env: env ? { ...process.env, ...env } : undefined }, (err, stdout, stderr) => {
resolve({
stdout: stdout || '',
stderr: stderr || '',
exitCode: Number.isInteger(err?.code) ? err.code : (err ? 1 : 0),
signal: err?.signal || null,
error: err || null,
});
});
});
}