From 4b5a991c5c04e0b56777b1c84d29c639338c0249 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 7 Jun 2026 12:54:50 +0000 Subject: [PATCH] refactor(cli): extract duplicated getCurrentExecutablePath helper getCurrentExecutablePath() was copy-pasted verbatim in both src/cli/commands.ts and src/cli/updater.ts. Move it to a shared src/cli/executable.ts and import it in both call sites. No behavior change. --- src/cli/commands.ts | 11 +---------- src/cli/executable.ts | 18 ++++++++++++++++++ src/cli/updater.ts | 15 +-------------- 3 files changed, 20 insertions(+), 24 deletions(-) create mode 100644 src/cli/executable.ts diff --git a/src/cli/commands.ts b/src/cli/commands.ts index 1a50a58..6658707 100644 --- a/src/cli/commands.ts +++ b/src/cli/commands.ts @@ -21,6 +21,7 @@ import { analyzeCoverage, formatCoverageReport, type CoverageResult } from "../c import { checkForUpdate, performUpdate, isNpmInstall } from "./updater.js"; import { confirm } from "./prompts.js"; import { removeFromPath } from "./shell.js"; +import { getCurrentExecutablePath } from "./executable.js"; /** * Print version information. @@ -122,16 +123,6 @@ export const handleUpdateCommand = async (): Promise => { console.log("Please restart to use the new version."); }; -/** - * Get the path to the current executable. - */ -const getCurrentExecutablePath = (): string => { - if (process.execPath.includes("node") || process.execPath.includes("bun")) { - return process.argv[1]; - } - return process.execPath; -}; - /** * Handle --uninstall command. * Removes the binary and PATH entries from shell rc files. diff --git a/src/cli/executable.ts b/src/cli/executable.ts new file mode 100644 index 0000000..65030a1 --- /dev/null +++ b/src/cli/executable.ts @@ -0,0 +1,18 @@ +/** + * Shared helpers for locating the running CLI executable. + */ + +/** + * Get the path to the current executable. + * + * For Bun-compiled binaries, `process.execPath` points to the binary itself. + * For Node.js, `process.argv[1]` is the script path. + */ +export const getCurrentExecutablePath = (): string => { + if (process.execPath.includes("node") || process.execPath.includes("bun")) { + // Running via node/bun interpreter - use argv[1] + return process.argv[1]; + } + // Compiled binary - use execPath + return process.execPath; +}; diff --git a/src/cli/updater.ts b/src/cli/updater.ts index a233d01..18819e5 100644 --- a/src/cli/updater.ts +++ b/src/cli/updater.ts @@ -17,6 +17,7 @@ import { tmpdir } from "node:os"; import { join, dirname, basename } from "node:path"; import { spawn } from "node:child_process"; import { VERSION, GITHUB_REPO, BINARY_NAME } from "../version.js"; +import { getCurrentExecutablePath } from "./executable.js"; /** GitHub release information. */ interface GitHubRelease { @@ -198,20 +199,6 @@ const downloadFile = async (url: string, destPath: string): Promise => { }); }; -/** - * Get the path to the current executable. - */ -const getCurrentExecutablePath = (): string => { - // For Bun-compiled binaries, process.execPath points to the binary itself - // For Node.js, process.argv[1] is the script path - if (process.execPath.includes("node") || process.execPath.includes("bun")) { - // Running via node/bun interpreter - use argv[1] - return process.argv[1]; - } - // Compiled binary - use execPath - return process.execPath; -}; - /** * Generate a unique backup path with timestamp to avoid conflicts with locked files. * On Windows, previous backup files may still be locked by the old process.