toes/src/cli/index.ts
Chris Wanstrath 3a6ed5d546 Handle -c flag position for both shebang and compiled argv layouts
When run via shebang, bun prepends itself so -c appears at argv[2]
instead of argv[1]. Search both positions to support either mode.
2026-03-17 21:35:19 -07:00

22 lines
731 B
TypeScript
Executable File

#!/usr/bin/env bun
import { program } from './setup'
const isCliUser = process.env.USER === 'cli'
const noArgs = process.argv.length <= 2
const isTTY = !!process.stdin.isTTY
// SSH login shell passes commands as: toes -c "command args"
// With shebang, argv is [bun, script, -c, cmd]; compiled, it's [toes, -c, cmd]
const cIndex = process.argv[1] === '-c' ? 1 : process.argv[2] === '-c' ? 2 : -1
const shellExec = cIndex !== -1 ? process.argv.slice(cIndex + 1).join(' ') : null
if (shellExec) {
const tokens = shellExec.split(/\s+/).filter(Boolean)
program.parse(['node', 'toes', ...tokens])
} else if (isCliUser && noArgs && isTTY) {
const { shell } = await import('./shell')
await shell()
} else {
program.parse()
}