Skip to content

Commit

Permalink
Add runCommand and runShellCommand functions to utils
Browse files Browse the repository at this point in the history
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
joone committed Mar 3, 2024
1 parent 531aa60 commit a5c6a33
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
59 changes: 1 addition & 58 deletions src/loz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as path from "path";
import * as os from "os";
import * as readline from "readline";
import * as readlinePromises from "readline/promises";
import { exec, spawn } from "child_process";
import { OpenAiAPI, OllamaAPI, LLMSettings } from "./llm";
import { CommandLinePrompt } from "./prompt";
import { runCommand, runShellCommand } from "./utils";

import { ChatHistory } from "./history";
import {
Expand All @@ -24,63 +24,6 @@ const LOZ_SAFE = process.env.LOZ_SAFE === "true" ? true : false;
const HOME_PATH = os.homedir() || "";
const LOG_DEV_PATH = "logs";

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
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
}
});
});
}

const promptForGIT =
"Generate a Git commit message based on the following code changes. Ensure the message adheres to the following guidelines:\n\n" +
"1. Separate the subject from the body with a blank line.\n" +
Expand Down
58 changes: 58 additions & 0 deletions src/utils/index.ts
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
}
});
});
}

0 comments on commit a5c6a33

Please sign in to comment.