feat(cli): Introduce version utility and command updates

This commit is contained in:
kaitranntt
2025-12-08 14:11:11 -05:00
parent b321edd99d
commit d77f07e093
3 changed files with 35 additions and 18 deletions
+7 -12
View File
@@ -6,11 +6,10 @@
*/ */
import { spawn } from 'child_process'; import { spawn } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import { colored } from '../utils/helpers'; import { colored } from '../utils/helpers';
import { detectInstallationMethod, detectPackageManager } from '../utils/package-manager-detector'; import { detectInstallationMethod, detectPackageManager } from '../utils/package-manager-detector';
import { compareVersionsWithPrerelease } from '../utils/update-checker'; import { compareVersionsWithPrerelease } from '../utils/update-checker';
import { getVersion } from '../utils/version';
/** /**
* Options for the update command * Options for the update command
@@ -20,10 +19,8 @@ export interface UpdateOptions {
beta?: boolean; beta?: boolean;
} }
// Version (sync with package.json) // Version (from centralized utility)
const CCS_VERSION = JSON.parse( const CCS_VERSION = getVersion();
fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf8')
).version;
/** /**
* Handle the update command * Handle the update command
@@ -169,19 +166,17 @@ function handleCheckFailed(
* Handle no update available * Handle no update available
*/ */
function handleNoUpdate(reason: string | undefined): void { function handleNoUpdate(reason: string | undefined): void {
const CCS_VERSION_LOCAL = JSON.parse( const version = getVersion();
fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf8')
).version;
let message = `You are already on the latest version (${CCS_VERSION_LOCAL})`; let message = `You are already on the latest version (${version})`;
switch (reason) { switch (reason) {
case 'dismissed': 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')); console.log(colored(`[i] ${message}`, 'yellow'));
break; break;
case 'cached': 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')); console.log(colored(`[i] ${message}`, 'cyan'));
break; break;
default: default:
+2 -6
View File
@@ -9,17 +9,13 @@ import * as fs from 'fs';
import * as os from 'os'; import * as os from 'os';
import { colored } from '../utils/helpers'; import { colored } from '../utils/helpers';
import { getConfigPath } from '../utils/config-manager'; import { getConfigPath } from '../utils/config-manager';
import { getVersion } from '../utils/version';
// Get version from package.json
const CCS_VERSION = JSON.parse(
fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf8')
).version;
/** /**
* Handle version command * Handle version command
*/ */
export function handleVersionCommand(): void { 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('');
console.log(colored('Installation:', 'cyan')); console.log(colored('Installation:', 'cyan'));
+26
View File
@@ -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';
}