From 088a35a4590e5f816144920e93d62f5afc6dc241 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sat, 9 May 2026 00:25:37 +0000 Subject: [PATCH] [Refactor] Consolidate command execution in system.ts Refactors `captureCommandWithExitCode` and `execCommand` to delegate to `captureOutputWithExitCode` and `exec` respectively. This centralizes the logic for environment setup, working directory handling, and command safety checks, reducing code duplication and improving consistency across execution utilities. --- packages/cli-kit/src/public/node/system.ts | 42 ++-------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index e2d5cbeead..b4f1e8cc69 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -179,26 +179,11 @@ function parseCommand(command: string): string[] { * ``` */ export async function captureCommandWithExitCode(command: string, options?: ExecOptions): Promise { - const env = options?.env ?? process.env - if (shouldDisplayColors()) { - env.FORCE_COLOR = '1' - } - const executionCwd = options?.cwd ?? cwd() const [cmd, ...args] = parseCommand(command) if (!cmd) { return {stdout: '', stderr: 'Empty command', exitCode: 1} } - checkCommandSafety(cmd, {cwd: executionCwd}) - const result = await execa(cmd, args, { - env, - cwd: executionCwd, - reject: false, - }) - return { - stdout: result.stdout, - stderr: result.stderr, - exitCode: result.exitCode ?? 0, - } + return captureOutputWithExitCode(cmd, args, options) } /** @@ -208,34 +193,11 @@ export async function captureCommandWithExitCode(command: string, options?: Exec * @param options - Optional settings for how to run the command. */ export async function execCommand(command: string, options?: ExecOptions): Promise { - const env = options?.env ?? process.env - if (shouldDisplayColors()) { - env.FORCE_COLOR = '1' - } - const executionCwd = options?.cwd ?? cwd() const [cmd, ...args] = parseCommand(command) if (!cmd) { throw new AbortError('Empty command') } - checkCommandSafety(cmd, {cwd: executionCwd}) - try { - await execa(cmd, args, { - env, - cwd: executionCwd, - stdin: options?.stdin, - stdout: options?.stdout === 'inherit' ? 'inherit' : undefined, - stderr: options?.stderr === 'inherit' ? 'inherit' : undefined, - }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } catch (processError: any) { - if (options?.externalErrorHandler) { - await options.externalErrorHandler(processError) - } else { - const abortError = new ExternalError(processError.message, command, []) - abortError.stack = processError.stack - throw abortError - } - } + await exec(cmd, args, options) } /**