Spaces:
Configuration error
Configuration error
File size: 1,593 Bytes
03b405b | 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | const { exec, spawn } = require('child_process');
const { promisify } = require('util');
const execPromise = promisify(exec);
/**
* Safely verify if a CLI binary command is available
* @param {string} command
* @param {Array<string>} args
* @returns {Promise<boolean>}
*/
async function checkBinary(command, args = ['--version']) {
try {
const { stdout } = await execPromise(`${command} ${args.join(' ')}`);
return stdout && stdout.length > 0;
} catch (err) {
return false;
}
}
/**
* Execute a spawn process with automated timeout protection
* @param {string} command
* @param {Array<string>} args
* @param {object} options
* @param {number} timeoutMs
* @returns {Promise<{stdout: string, stderr: string, code: number}>}
*/
function spawnWithTimeout(command, args, options = {}, timeoutMs = 30 * 60 * 1000) {
return new Promise((resolve, reject) => {
const proc = spawn(command, args, options);
let stdout = '';
let stderr = '';
const timer = setTimeout(() => {
proc.kill('SIGKILL');
reject(new Error(`Process execution of ${command} timed out after ${timeoutMs}ms`));
}, timeoutMs);
proc.stdout?.on('data', (data) => {
stdout += data.toString();
});
proc.stderr?.on('data', (data) => {
stderr += data.toString();
});
proc.on('close', (code) => {
clearTimeout(timer);
resolve({ stdout, stderr, code });
});
proc.on('error', (err) => {
clearTimeout(timer);
reject(err);
});
});
}
module.exports = { execPromise, checkBinary, spawnWithTimeout };
|