diff --git a/src/commands/update-command.ts b/src/commands/update-command.ts index 4201443f..c875b8bd 100644 --- a/src/commands/update-command.ts +++ b/src/commands/update-command.ts @@ -6,11 +6,10 @@ */ import { spawn } from 'child_process'; -import * as path from 'path'; -import * as fs from 'fs'; import { colored } from '../utils/helpers'; import { detectInstallationMethod, detectPackageManager } from '../utils/package-manager-detector'; import { compareVersionsWithPrerelease } from '../utils/update-checker'; +import { getVersion } from '../utils/version'; /** * Options for the update command @@ -20,10 +19,8 @@ export interface UpdateOptions { beta?: boolean; } -// Version (sync with package.json) -const CCS_VERSION = JSON.parse( - fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf8') -).version; +// Version (from centralized utility) +const CCS_VERSION = getVersion(); /** * Handle the update command @@ -169,19 +166,17 @@ function handleCheckFailed( * Handle no update available */ function handleNoUpdate(reason: string | undefined): void { - const CCS_VERSION_LOCAL = JSON.parse( - fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf8') - ).version; + const version = getVersion(); - let message = `You are already on the latest version (${CCS_VERSION_LOCAL})`; + let message = `You are already on the latest version (${version})`; switch (reason) { case 'dismissed': - message = `Update dismissed. You are on version ${CCS_VERSION_LOCAL}`; + message = `Update dismissed. You are on version ${version}`; console.log(colored(`[i] ${message}`, 'yellow')); break; case 'cached': - message = `No updates available (cached result). You are on version ${CCS_VERSION_LOCAL}`; + message = `No updates available (cached result). You are on version ${version}`; console.log(colored(`[i] ${message}`, 'cyan')); break; default: diff --git a/src/commands/version-command.ts b/src/commands/version-command.ts index fb5bbb52..50395af2 100644 --- a/src/commands/version-command.ts +++ b/src/commands/version-command.ts @@ -9,17 +9,13 @@ import * as fs from 'fs'; import * as os from 'os'; import { colored } from '../utils/helpers'; import { getConfigPath } from '../utils/config-manager'; - -// Get version from package.json -const CCS_VERSION = JSON.parse( - fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf8') -).version; +import { getVersion } from '../utils/version'; /** * Handle version command */ export function handleVersionCommand(): void { - console.log(colored(`CCS (Claude Code Switch) v${CCS_VERSION}`, 'bold')); + console.log(colored(`CCS (Claude Code Switch) v${getVersion()}`, 'bold')); console.log(''); console.log(colored('Installation:', 'cyan')); diff --git a/src/utils/version.ts b/src/utils/version.ts new file mode 100644 index 00000000..ef232bd4 --- /dev/null +++ b/src/utils/version.ts @@ -0,0 +1,26 @@ +/** + * Version Utility + * + * Centralized version management for CCS. + * Reads version from package.json at runtime. + */ + +import * as path from 'path'; +import * as fs from 'fs'; + +// Get version from package.json (relative to dist/ at runtime) +let cachedVersion: string | null = null; + +export function getVersion(): string { + if (cachedVersion) return cachedVersion; + + try { + const packageJsonPath = path.join(__dirname, '../../package.json'); + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); + cachedVersion = packageJson.version || '0.0.0'; + } catch { + cachedVersion = '0.0.0'; + } + + return cachedVersion ?? '0.0.0'; +}