From 311ee45f7be2b5e555253edd57fdcf97451f6193 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Mon, 11 May 2026 00:35:31 +0000 Subject: [PATCH] [Refactor] Extract shared execution environment logic in system.ts Extract shared environment and CWD setup logic into a new private helper function `getExecutionEnvironment` in `packages/cli-kit/src/public/node/system.ts`. This refactor reduces duplication in `captureCommandWithExitCode`, `execCommand`, and `buildExec`. --- packages/cli-kit/src/public/node/system.ts | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index e2d5cbeead..841746f2db 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -179,11 +179,7 @@ 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 {env, executionCwd} = getExecutionEnvironment(options) const [cmd, ...args] = parseCommand(command) if (!cmd) { return {stdout: '', stderr: 'Empty command', exitCode: 1} @@ -208,11 +204,7 @@ 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 {env, executionCwd} = getExecutionEnvironment(options) const [cmd, ...args] = parseCommand(command) if (!cmd) { throw new AbortError('Empty command') @@ -305,11 +297,7 @@ function buildExec( options?: ExecOptions, execaOptions?: BuildExecOptions, ): ExecaChildProcess { - const env = options?.env ?? process.env - if (shouldDisplayColors()) { - env.FORCE_COLOR = '1' - } - const executionCwd = options?.cwd ?? cwd() + const {env, executionCwd} = getExecutionEnvironment(options) checkCommandSafety(command, {cwd: executionCwd}) const commandProcess = execa(command, args, { env, @@ -333,6 +321,18 @@ function buildExec( return commandProcess } +function getExecutionEnvironment(options?: ExecOptions): { + env: {[key: string]: string | undefined} + executionCwd: string +} { + const env = options?.env ?? process.env + if (shouldDisplayColors()) { + env.FORCE_COLOR = '1' + } + const executionCwd = options?.cwd ?? cwd() + return {env, executionCwd} +} + function checkCommandSafety(command: string, _options: {cwd: string}): void { const pathIncludingLocal = `${_options.cwd}${delimiter}${process.env.PATH}` const commandPath = which.sync(command, {