advanced-patch-generator
Version:
Advanced patch manager using Xdelta for efficient patch creation and application with progress support, events, and error handling
69 lines • 2.25 kB
JavaScript
import { spawn } from 'child_process';
/**
* Utility functions for command execution
*/
class CommandUtils {
/**
* Executes a command and returns the result
* @param command - Command to execute
* @returns Promise with command result
*/
static async executeCommand(command) {
return new Promise(resolve => {
const startTime = Date.now();
// Use a single command string with shell to preserve quotes and spaces in paths
const child = spawn(command, {
stdio: ['pipe', 'pipe', 'pipe'],
shell: true,
windowsVerbatimArguments: true,
});
let stdout = '';
let stderr = '';
child.stdout?.on('data', (data) => {
stdout += data.toString();
});
child.stderr?.on('data', (data) => {
stderr += data.toString();
});
child.on('close', (code) => {
const duration = Date.now() - startTime;
const success = code === 0;
resolve({
success,
stdout: stdout.trim(),
stderr: stderr.trim(),
duration,
error: success
? undefined
: new Error(`Command failed with code ${code}`),
});
});
child.on('error', (error) => {
const duration = Date.now() - startTime;
resolve({
success: false,
stdout: '',
stderr: error.message,
duration,
error,
});
});
});
}
/**
* Checks if a command is available on the system
* @param command - Command to check
* @returns Promise with availability status
*/
static async isCommandAvailable(command) {
try {
const result = await this.executeCommand(`${command} --version`);
return result.success;
}
catch {
return false;
}
}
}
export default CommandUtils;
//# sourceMappingURL=commandUtils.js.map