UNPKG

claude-key-manager

Version:

🚀 Claude Key Manager 4.1.3 - 修复密钥验证问题,支持更多密钥格式

269 lines (223 loc) 7.75 kB
const https = require('https'); const { exec } = require('child_process'); const { promisify } = require('util'); const chalk = require('chalk'); const boxen = require('boxen'); const ora = require('ora'); const execAsync = promisify(exec); class VersionManager { constructor() { this.packageName = 'claude-key-manager'; this.currentVersion = require('../package.json').version; } /** * 获取当前版本 */ getCurrentVersion() { return this.currentVersion; } /** * 从npm registry获取最新版本 */ async getLatestVersion() { return new Promise((resolve, reject) => { const url = `https://registry.npmjs.org/${this.packageName}/latest`; https.get(url, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { const packageInfo = JSON.parse(data); resolve(packageInfo.version); } catch (error) { reject(new Error('解析版本信息失败')); } }); }).on('error', (error) => { reject(new Error(`获取版本信息失败: ${error.message}`)); }); }); } /** * 比较版本号 * @param {string} current - 当前版本 * @param {string} latest - 最新版本 * @returns {number} 0=相同, -1=当前版本较旧, 1=当前版本较新 */ compareVersions(current, latest) { const currentParts = current.split('.').map(Number); const latestParts = latest.split('.').map(Number); for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) { const currentPart = currentParts[i] || 0; const latestPart = latestParts[i] || 0; if (currentPart < latestPart) return -1; if (currentPart > latestPart) return 1; } return 0; } /** * 检查是否有更新 */ async checkForUpdates() { try { const latestVersion = await this.getLatestVersion(); const comparison = this.compareVersions(this.currentVersion, latestVersion); return { hasUpdate: comparison === -1, currentVersion: this.currentVersion, latestVersion: latestVersion, isNewer: comparison === 1 }; } catch (error) { throw new Error(`检查更新失败: ${error.message}`); } } /** * 显示版本信息 */ async showVersionInfo() { console.log(boxen( `🚀 ${chalk.bold('Claude Key Manager')}\n\n` + `📦 当前版本: ${chalk.green('v' + this.currentVersion)}\n` + `🔗 包名称: ${chalk.blue(this.packageName)}\n` + `📅 Node.js: ${process.version}\n` + `💻 平台: ${process.platform} ${process.arch}`, { padding: 1, borderColor: 'blue', borderStyle: 'round' } )); const spinner = ora('🔍 检查最新版本...').start(); try { const updateInfo = await this.checkForUpdates(); spinner.stop(); if (updateInfo.hasUpdate) { console.log(boxen( `🎉 ${chalk.yellow('发现新版本!')}\n\n` + `📍 当前版本: ${chalk.red('v' + updateInfo.currentVersion)}\n` + `🆕 最新版本: ${chalk.green('v' + updateInfo.latestVersion)}\n\n` + `${chalk.blue('更新命令:')}\n` + `${chalk.gray('npm install -g claude-key-manager@latest')}\n\n` + `${chalk.blue('或使用更新助手:')}\n` + `${chalk.gray('claude-key update')}`, { padding: 1, borderColor: 'yellow', borderStyle: 'round' } )); } else if (updateInfo.isNewer) { console.log(chalk.blue('🔬 你正在使用开发版本!')); } else { console.log(chalk.green('✅ 你已经使用最新版本!')); } } catch (error) { spinner.fail(chalk.red('❌ 检查更新失败')); console.log(chalk.gray(` ${error.message}`)); } } /** * 更新助手 - 提供多种更新选项 */ async updateHelper() { console.log(chalk.blue('🔄 Claude Key Manager 更新助手\n')); const spinner = ora('检查更新状态...').start(); try { const updateInfo = await this.checkForUpdates(); spinner.stop(); if (!updateInfo.hasUpdate) { console.log(chalk.green('✅ 已是最新版本,无需更新')); return; } console.log(boxen( `🎯 ${chalk.bold('发现新版本!')}\n\n` + `当前: v${updateInfo.currentVersion}\n` + `最新: v${updateInfo.latestVersion}\n\n` + `${chalk.green('正在自动更新...')}`, { padding: 1, borderColor: 'green' } )); // 直接自动更新,无需询问 (你要的功能!) await this.performUpdate(); } catch (error) { spinner.fail(chalk.red('❌ 检查更新失败')); console.log(chalk.gray(` ${error.message}`)); } } /** * 执行自动更新并重启 (你要的功能!) */ async performUpdate() { const spinner = ora('正在更新 claude-key-manager...').start(); try { // 尝试全局更新 const { stdout, stderr } = await execAsync('npm install -g claude-key-manager@latest'); spinner.succeed(chalk.green('✅ 更新成功!')); // 显示更新结果 if (stdout) { console.log(chalk.gray('\n更新输出:')); console.log(chalk.gray(stdout)); } console.log(chalk.blue('\n🚀 正在重启使用新版本...')); // 等待1秒让用户看到消息 await new Promise(resolve => setTimeout(resolve, 1000)); // 重新执行当前命令使用新版本 await this.restartWithNewVersion(); } catch (error) { spinner.fail(chalk.red('❌ 自动更新失败')); console.log(chalk.yellow('\n💡 请手动更新:')); console.log(chalk.gray(' npm install -g claude-key-manager@latest')); if (error.message.includes('EACCES') || error.message.includes('permission')) { console.log(chalk.yellow('\n🔒 权限问题? 尝试:')); console.log(chalk.gray(' sudo npm install -g claude-key-manager@latest')); } console.log(chalk.gray(`\n详细错误: ${error.message}`)); } } /** * 重启使用新版本 */ async restartWithNewVersion() { try { const { spawn } = require('child_process'); const originalArgs = process.argv.slice(2); // 获取原始参数 console.log(chalk.green('🔄 启动新版本...')); // 使用新版本重新执行相同的命令 const child = spawn('claude-key', originalArgs, { stdio: 'inherit', detached: true }); child.unref(); // 退出当前进程 process.exit(0); } catch (error) { console.log(chalk.yellow('⚠️ 自动重启失败,请手动重新运行命令')); console.log(chalk.gray(`错误: ${error.message}`)); } } /** * 获取更新提醒 (后台静默检查) */ async getUpdateNotification() { try { const updateInfo = await this.checkForUpdates(); if (updateInfo.hasUpdate) { return { show: true, message: `🎉 新版本 v${updateInfo.latestVersion} 可用! 当前: v${updateInfo.currentVersion}`, command: 'claude-key update', autoUpdate: true // 标记可以自动更新 }; } return { show: false }; } catch (error) { return { show: false }; // 静默失败 } } } module.exports = VersionManager;