diff --git a/src/cli/index.ts b/src/cli/index.ts index 97920c0..530de44 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -5,6 +5,9 @@ try { process.loadEnvFile(); } catch { /* .env not found — that's fine */ } import './suppressNodeWarnings.js'; +import { realpathSync } from 'node:fs'; +import { resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; import { Command } from 'commander'; import { registerStartCommand } from './start.js'; import { registerSendCommand } from './send.js'; @@ -55,12 +58,23 @@ export function createProgram(): Command { return program; } -// Only run when executed directly (not imported in tests) -const isDirectRun = process.argv[1] && - (process.argv[1].endsWith('/cli/index.js') || - process.argv[1].endsWith('/cli/index.ts')); +// Only run when executed directly (not imported in tests). +// Resolve symlinks so installed shims (e.g. ~/.local/bin/flynn) still execute the CLI. +function isDirectRun(): boolean { + if (!process.argv[1]) { + return false; + } -if (isDirectRun) { + try { + const currentFile = realpathSync(fileURLToPath(import.meta.url)); + const invokedFile = realpathSync(resolve(process.argv[1])); + return currentFile === invokedFile; + } catch { + return false; + } +} + +if (isDirectRun()) { const program = createProgram(); program.parse(process.argv); }