-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add runCommand and runShellCommand functions to utils
The runCommand and runShellCommand functions were added to the utils module to handle running shell commands and streaming their output directly to the terminal. This change improves code organization and reusability. Generated by gpt-3.5-turbo
- Loading branch information
Showing
2 changed files
with
59 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { exec, spawn } from "child_process"; | ||
|
||
export function runShellCommand(command: string): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
exec(command, (error, stdout, stderr) => { | ||
if (error) { | ||
reject(`error: ${error.message}`); | ||
return; | ||
} | ||
if (stderr) { | ||
reject(`stderr: ${stderr}`); | ||
return; | ||
} | ||
resolve(stdout); | ||
}); | ||
}); | ||
} | ||
|
||
// Function to run a command and stream its stdout directly to the terminal | ||
export function runCommand(command: string): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
//const [cmd, ...args] = command.split(/\s+/); // Split the command and its arguments | ||
const child = spawn("bash", ["-c", command]); | ||
let stdoutData = ""; | ||
let stderrData = ""; | ||
|
||
child.stdout.on("data", (data: any) => { | ||
stdoutData += data; // Accumulate stdout data | ||
process.stdout.write(data); // Write stdout data to the terminal | ||
}); | ||
|
||
child.stderr.on("data", (data: any) => { | ||
stderrData += data; // Accumulate stderr data | ||
process.stderr.write(data); // Write stderr data to the terminal | ||
}); | ||
|
||
child.on("error", (error: any) => { | ||
console.error(`Execution Error: ${error.message}`); | ||
reject(error); // Reject the promise on spawn error | ||
}); | ||
|
||
child.on("close", (code: any) => { | ||
if (code === 2) { | ||
reject("No output: " + code); | ||
} else if (code !== 0) { | ||
console.log(`Process exited with code: ${code}`); | ||
// Check if both stdout and stderr are empty | ||
if (!stdoutData && !stderrData) { | ||
reject("No output: " + code); | ||
} else { | ||
reject(new Error(`Process exited with code: ${code}`)); | ||
} | ||
} else { | ||
resolve(); // Resolve the promise when the process closes successfully | ||
} | ||
}); | ||
}); | ||
} |