refactor(execute,code-repository): allow ExecOptions in Execute.executeCommand and use cwd instead of 'cd'

- Extend Execute.executeCommand to accept ExecOptions and forward them to child_process.exec
- Log stderr on error and debug-log stderr when present
- Update CodeRepository to pass cwd to Execute.executeCommand instead of prefixing commands with "cd"
This commit is contained in:
Nawaz Dhandala
2025-10-28 17:37:06 +00:00
parent 2754657a6f
commit 444cf040a6
2 changed files with 28 additions and 7 deletions

View File

@@ -1,26 +1,47 @@
import { PromiseRejectErrorFunction } from "../../Types/FunctionTypes";
import { ExecException, exec, execFile } from "node:child_process";
import {
ExecException,
ExecOptions,
exec,
execFile,
} from "node:child_process";
import logger from "./Logger";
import CaptureSpan from "./Telemetry/CaptureSpan";
export default class Execute {
@CaptureSpan()
public static executeCommand(command: string): Promise<string> {
public static executeCommand(
command: string,
options?: ExecOptions,
): Promise<string> {
return new Promise(
(
resolve: (output: string) => void,
reject: PromiseRejectErrorFunction,
) => {
exec(`${command}`, (err: ExecException | null, stdout: string) => {
exec(
`${command}`,
{
...options,
},
(err: ExecException | null, stdout: string, stderr: string) => {
if (err) {
logger.error(`Error executing command: ${command}`);
logger.error(err);
logger.error(stdout);
if (stderr) {
logger.error(stderr);
}
return reject(err);
}
if (stderr) {
logger.debug(stderr);
}
return resolve(stdout);
});
},
);
},
);
}

View File

@@ -305,9 +305,9 @@ export default class CodeRepositoryUtil {
for (const command of commands) {
logger.info(`Executing command: ${command}`);
const commandResult: string = await Execute.executeCommand(
`cd ${this.getLocalRepositoryPath()} && ${command}`,
);
const commandResult: string = await Execute.executeCommand(command, {
cwd: this.getLocalRepositoryPath(),
});
if (commandResult) {
logger.info(`Command result: ${commandResult}`);
results.push(commandResult);