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 */ } try { process.loadEnvFile(); } catch { /* .env not found — that's fine */ }
import './suppressNodeWarnings.js'; import './suppressNodeWarnings.js';
import { realpathSync } from 'node:fs';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Command } from 'commander'; import { Command } from 'commander';
import { registerStartCommand } from './start.js'; import { registerStartCommand } from './start.js';
import { registerSendCommand } from './send.js'; import { registerSendCommand } from './send.js';
@@ -55,12 +58,23 @@ export function createProgram(): Command {
return program; return program;
} }
// Only run when executed directly (not imported in tests) // Only run when executed directly (not imported in tests).
const isDirectRun = process.argv[1] && // Resolve symlinks so installed shims (e.g. ~/.local/bin/flynn) still execute the CLI.
(process.argv[1].endsWith('/cli/index.js') || function isDirectRun(): boolean {
process.argv[1].endsWith('/cli/index.ts')); 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(); const program = createProgram();
program.parse(process.argv); program.parse(process.argv);
} }