mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
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:
@@ -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);
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user