Inital move (minus WIP tests)

This commit is contained in:
Logan Cusano
2024-05-12 12:55:00 -04:00
parent 132d974b89
commit 580513997d
21 changed files with 4687 additions and 0 deletions

39
modules/cliHandler.mjs Normal file
View File

@@ -0,0 +1,39 @@
import { spawn } from "child_process";
/**
* Executes a command and retrieves its output.
* @param {string} command - The command to execute.
* @param {string[]} args - The arguments to pass to the command.
* @returns {Promise<string>} A promise that resolves with the output of the command.
*/
export const executeCommand = (command, args) => {
return new Promise((resolve, reject) => {
const childProcess = spawn(command, args);
let commandOutput = '';
childProcess.stdout.on('data', (data) => {
commandOutput += data.toString();
});
childProcess.stderr.on('data', (data) => {
// Log any errors to stderr
console.error(data.toString());
});
childProcess.on('error', (error) => {
// Reject the promise if there's an error executing the command
reject(error);
});
childProcess.on('close', (code) => {
if (code === 0) {
// Resolve the promise with the command output if it exits successfully
resolve(commandOutput.trim());
} else {
// Reject the promise if the command exits with a non-zero code
reject(new Error(`Command '${command}' exited with code ${code}`));
}
});
});
};