@synet/net
Version:
Network abstraction layer for Synet. visit https://syntehtism.ai for more information.
42 lines (41 loc) • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeCommandExecutor = void 0;
const node_child_process_1 = require("node:child_process");
const node_util_1 = require("node:util");
const patterns_1 = require("@synet/patterns");
const execAsync = (0, node_util_1.promisify)(node_child_process_1.exec);
/**
* Node.js implementation of CommandExecutor
*/
class NodeCommandExecutor {
constructor(logger) {
this.logger = logger;
}
async execute(command, args = [], sudo) {
try {
let fullCommand = [command, ...args].join(" ");
if (sudo) {
// If sudo is true, prepend the command with 'sudo'
fullCommand = `sudo ${fullCommand}`;
}
this.logger?.debug("Command:", fullCommand);
const { stdout, stderr } = await execAsync(fullCommand);
return patterns_1.Result.success({
stdout: stdout.trim(),
stderr: stderr.trim(),
exitCode: 0, // exec doesn't provide exit code directly if successful
});
}
catch (error) {
if (error instanceof Error) {
return patterns_1.Result.fail(`Command execution failed: ${error.message}`, error);
}
return patterns_1.Result.fail("Command execution failed: Unknown error");
}
}
async executeAsSuperuser(command, args, sudo) {
return this.execute(`${command}`, args, sudo);
}
}
exports.NodeCommandExecutor = NodeCommandExecutor;