Skip to content
Merged
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
29 changes: 23 additions & 6 deletions src/shared/command-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ import { existsSync } from "fs"
import { homedir } from "os"

const DEFAULT_ZSH_PATHS = ["/bin/zsh", "/usr/bin/zsh", "/usr/local/bin/zsh"]
const DEFAULT_BASH_PATHS = ["/bin/bash", "/usr/bin/bash", "/usr/local/bin/bash"]

function getHomeDir(): string {
return process.env.HOME || process.env.USERPROFILE || homedir()
}

function findZshPath(customZshPath?: string): string | null {
if (customZshPath && existsSync(customZshPath)) {
return customZshPath
function findShellPath(defaultPaths: string[], customPath?: string): string | null {
if (customPath && existsSync(customPath)) {
return customPath
}
for (const path of DEFAULT_ZSH_PATHS) {
for (const path of defaultPaths) {
if (existsSync(path)) {
return path
}
}
return null
}

function findZshPath(customZshPath?: string): string | null {
return findShellPath(DEFAULT_ZSH_PATHS, customZshPath)
}

function findBashPath(): string | null {
return findShellPath(DEFAULT_BASH_PATHS)
}

const execAsync = promisify(exec)

export interface CommandResult {
Expand Down Expand Up @@ -55,10 +64,18 @@ export async function executeHookCommand(
let finalCommand = expandedCommand

if (options?.forceZsh) {
const zshPath = options.zshPath || findZshPath()
// Always verify shell exists before using it
const zshPath = findZshPath(options.zshPath)
const escapedCommand = expandedCommand.replace(/'/g, "'\\''")
if (zshPath) {
const escapedCommand = expandedCommand.replace(/'/g, "'\\''")
finalCommand = `${zshPath} -lc '${escapedCommand}'`
} else {
// Fall back to bash login shell to preserve PATH from user profile
const bashPath = findBashPath()
if (bashPath) {
finalCommand = `${bashPath} -lc '${escapedCommand}'`
}
// If neither zsh nor bash found, fall through to spawn with shell: true
}
}

Expand Down