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, {