|
| 1 | +/** |
| 2 | + * Update command - updates eff-u-code to the latest version |
| 3 | + */ |
| 4 | + |
| 5 | +import { Command } from 'commander'; |
| 6 | +import { execSync } from 'node:child_process'; |
| 7 | +import { t } from '../../i18n/index.js'; |
| 8 | +import chalk from 'chalk'; |
| 9 | + |
| 10 | +interface NpmListOutput { |
| 11 | + dependencies?: { |
| 12 | + 'eff-u-code'?: { |
| 13 | + version: string; |
| 14 | + }; |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +export function createUpdateCommand(): Command { |
| 19 | + const command = new Command('update'); |
| 20 | + |
| 21 | + command |
| 22 | + .description(t('cmd_update_description')) |
| 23 | + .addHelpText( |
| 24 | + 'after', |
| 25 | + ` |
| 26 | +${t('cli_examples')} |
| 27 | + $ fuck-u-code update # ${t('cmd_update_example')} |
| 28 | +` |
| 29 | + ) |
| 30 | + .action(() => { |
| 31 | + try { |
| 32 | + console.log(chalk.blue('🔄 ' + t('update_checking'))); |
| 33 | + |
| 34 | + // Get current version |
| 35 | + const currentVersion = execSync('npm list -g eff-u-code --depth=0 --json', { |
| 36 | + encoding: 'utf-8', |
| 37 | + }); |
| 38 | + const current = JSON.parse(currentVersion) as NpmListOutput; |
| 39 | + const installedVersion = current.dependencies?.['eff-u-code']?.version ?? 'unknown'; |
| 40 | + |
| 41 | + // Get latest version from npm |
| 42 | + const latestVersionOutput = execSync('npm view eff-u-code version', { |
| 43 | + encoding: 'utf-8', |
| 44 | + }).trim(); |
| 45 | + |
| 46 | + console.log(chalk.gray(`${t('update_current_version')}: ${installedVersion}`)); |
| 47 | + console.log(chalk.gray(`${t('update_latest_version')}: ${latestVersionOutput}`)); |
| 48 | + |
| 49 | + if (installedVersion === latestVersionOutput) { |
| 50 | + console.log(chalk.green('✓ ' + t('update_already_latest'))); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + console.log(chalk.yellow('⬆ ' + t('update_updating'))); |
| 55 | + |
| 56 | + // Update the package |
| 57 | + execSync('npm install -g eff-u-code@latest', { |
| 58 | + stdio: 'inherit', |
| 59 | + }); |
| 60 | + |
| 61 | + console.log(chalk.green('✓ ' + t('update_success'))); |
| 62 | + console.log(chalk.gray(`${t('update_updated_to')}: ${latestVersionOutput}`)); |
| 63 | + } catch (error) { |
| 64 | + console.error(chalk.red('✗ ' + t('update_failed'))); |
| 65 | + if (error instanceof Error) { |
| 66 | + console.error(chalk.red(error.message)); |
| 67 | + } |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + return command; |
| 73 | +} |
0 commit comments