Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions packages/cli-kit/src/public/node/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ function parseCommand(command: string): string[] {
* ```
*/
export async function captureCommandWithExitCode(command: string, options?: ExecOptions): Promise<CaptureOutputResult> {
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}
Expand All @@ -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<void> {
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')
Expand Down Expand Up @@ -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,
Expand All @@ -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, {
Expand Down
Loading