fix(cli): detect direct execution through symlinked flynn binary

This commit is contained in:
William Valentin
2026-02-16 11:00:07 -08:00
parent a27f18d4f2
commit 16339eddd0
+19 -5
View File
@@ -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);
}