import * as path from 'node:path' /** * 会话终端直接由登录 shell 拉起目标程序,而不是开个空 shell 再往里打字: * - `-il` 照常加载 .zprofile / .zshrc,PATH、mise 等环境与手动开终端一致 * - `-c` 执行的命令不进 shell 历史,超长的启动命令不污染历史补全 * - argv 走 `"$@"` 原样传进去,空格、引号、`$`、换行都不经 shell 解析 * - `exec` 让程序顶替 shell,程序退出时终端标签页随之关闭 */ export interface SessionLaunch { shellPath: string shellArgs: string[] } const FALLBACK_SHELL = '/bin/zsh' export function buildSessionLaunch(shell: string, argv: readonly string[]): SessionLaunch { const shellPath = shell || FALLBACK_SHELL // `-c` 后的第一个参数成为 $0,占位用 shell 自己的名字,后面的才是 "$@"。 return { shellPath, shellArgs: ['-ilc', 'exec "$@"', path.basename(shellPath), ...argv], } }