|
| 1 | +import { command } from 'cleye'; |
| 2 | +import { execSync, exec } from 'child_process'; |
| 3 | +import { promisify } from 'util'; |
| 4 | +import { green, red, yellow, cyan } from 'kolorist'; |
| 5 | +import { outro, spinner } from '@clack/prompts'; |
| 6 | +import pkg from '../../package.json'; |
| 7 | +import { handleCommandError, KnownError } from '../utils/error.js'; |
| 8 | + |
| 9 | +const execAsync = promisify(exec); |
| 10 | + |
| 11 | +interface PackageManagerInfo { |
| 12 | + name: string; |
| 13 | + updateCommand: string; |
| 14 | +} |
| 15 | + |
| 16 | +// Determine the dist tag based on current version |
| 17 | +// Versions with prerelease (e.g., 2.0.0-develop.5) use 'develop' tag |
| 18 | +// Stable versions use 'latest' tag |
| 19 | +function getDistTag(version: string): string { |
| 20 | + // Skip for development/semantic-release versions |
| 21 | + if (version === '0.0.0-semantic-release' || version.includes('semantic-release')) { |
| 22 | + return 'latest'; |
| 23 | + } |
| 24 | + // If version has prerelease identifier (contains '-'), use 'develop' tag |
| 25 | + if (version.includes('-')) { |
| 26 | + return 'develop'; |
| 27 | + } |
| 28 | + return 'latest'; |
| 29 | +} |
| 30 | + |
| 31 | +function detectPackageManager(distTag: string): PackageManagerInfo { |
| 32 | + // Check if running from global installation |
| 33 | + try { |
| 34 | + const globalPath = execSync('npm root -g', { encoding: 'utf8' }).trim(); |
| 35 | + const { execPath } = process; |
| 36 | + |
| 37 | + // Check if running from global npm installation |
| 38 | + if (execPath.includes(globalPath) || execPath.includes('/usr/local') || execPath.includes('/usr/bin')) { |
| 39 | + return { name: 'npm', updateCommand: `npm install -g aicommits@${distTag}` }; |
| 40 | + } |
| 41 | + } catch { |
| 42 | + // Fall through to other detection methods |
| 43 | + } |
| 44 | + |
| 45 | + // Check for pnpm |
| 46 | + try { |
| 47 | + execSync('pnpm --version', { stdio: 'ignore' }); |
| 48 | + // Check if installed via pnpm global |
| 49 | + const pnpmList = execSync('pnpm list -g aicommits', { encoding: 'utf8' }); |
| 50 | + if (pnpmList.includes('aicommits')) { |
| 51 | + return { name: 'pnpm', updateCommand: `pnpm add -g aicommits@${distTag}` }; |
| 52 | + } |
| 53 | + } catch { |
| 54 | + // Not pnpm |
| 55 | + } |
| 56 | + |
| 57 | + // Check for yarn |
| 58 | + try { |
| 59 | + execSync('yarn --version', { stdio: 'ignore' }); |
| 60 | + // Check if installed via yarn global |
| 61 | + const yarnList = execSync('yarn global list', { encoding: 'utf8' }); |
| 62 | + if (yarnList.includes('aicommits')) { |
| 63 | + return { name: 'yarn', updateCommand: `yarn global add aicommits@${distTag}` }; |
| 64 | + } |
| 65 | + } catch { |
| 66 | + // Not yarn |
| 67 | + } |
| 68 | + |
| 69 | + // Check for bun |
| 70 | + try { |
| 71 | + execSync('bun --version', { stdio: 'ignore' }); |
| 72 | + // Check if installed via bun |
| 73 | + const bunList = execSync('bun pm bin -g', { encoding: 'utf8' }); |
| 74 | + if (process.execPath.includes('bun') || bunList.includes('aicommits')) { |
| 75 | + return { name: 'bun', updateCommand: `bun add -g aicommits@${distTag}` }; |
| 76 | + } |
| 77 | + } catch { |
| 78 | + // Not bun |
| 79 | + } |
| 80 | + |
| 81 | + // Default to npm |
| 82 | + return { name: 'npm', updateCommand: `npm install -g aicommits@${distTag}` }; |
| 83 | +} |
| 84 | + |
| 85 | +async function getLatestVersion(distTag: string): Promise<string | null> { |
| 86 | + try { |
| 87 | + const response = await fetch(`https://registry.npmjs.org/aicommits/${distTag}`, { |
| 88 | + headers: { Accept: 'application/json' }, |
| 89 | + }); |
| 90 | + if (!response.ok) return null; |
| 91 | + const data = await response.json(); |
| 92 | + return data.version || null; |
| 93 | + } catch { |
| 94 | + return null; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export default command( |
| 99 | + { |
| 100 | + name: 'update', |
| 101 | + description: 'Update aicommits to the latest version', |
| 102 | + help: { |
| 103 | + description: 'Check for updates and install the latest version using your package manager', |
| 104 | + }, |
| 105 | + }, |
| 106 | + () => { |
| 107 | + (async () => { |
| 108 | + // Determine dist tag based on current version |
| 109 | + const distTag = getDistTag(pkg.version); |
| 110 | + const pm = detectPackageManager(distTag); |
| 111 | + |
| 112 | + console.log(`${cyan('ℹ')} Current version: ${pkg.version}`); |
| 113 | + console.log(`${cyan('ℹ')} Package manager detected: ${pm.name}`); |
| 114 | + if (distTag !== 'latest') { |
| 115 | + console.log(`${cyan('ℹ')} Using '${distTag}' distribution tag`); |
| 116 | + } |
| 117 | + |
| 118 | + const s = spinner(); |
| 119 | + s.start('Checking for updates...'); |
| 120 | + |
| 121 | + const latestVersion = await getLatestVersion(distTag); |
| 122 | + |
| 123 | + if (!latestVersion) { |
| 124 | + s.stop('Could not check for updates', 1); |
| 125 | + throw new KnownError('Failed to fetch latest version from npm registry'); |
| 126 | + } |
| 127 | + |
| 128 | + if (latestVersion === pkg.version) { |
| 129 | + s.stop(`${green('✔')} Already on the latest version (${pkg.version})`); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + s.stop(`${green('✔')} Update available: v${pkg.version} → v${latestVersion}`); |
| 134 | + |
| 135 | + const updateS = spinner(); |
| 136 | + updateS.start(`Updating via ${pm.name}...`); |
| 137 | + |
| 138 | + try { |
| 139 | + await execAsync(pm.updateCommand, { timeout: 120000 }); |
| 140 | + |
| 141 | + updateS.stop(`${green('✔')} Successfully updated to v${latestVersion}`); |
| 142 | + outro(`${green('✔')} Update complete! Run 'aic --version' to verify.`); |
| 143 | + } catch (error: any) { |
| 144 | + updateS.stop(`${red('✘')} Update failed`, 1); |
| 145 | + |
| 146 | + if (error.stderr?.includes('permission') || error.message?.includes('permission')) { |
| 147 | + console.error(`${red('✘')} Permission denied. Try running with sudo:`); |
| 148 | + console.error(` sudo ${pm.updateCommand}`); |
| 149 | + } else if (error.stderr?.includes('EACCES')) { |
| 150 | + console.error(`${red('✘')} Permission denied. Try running with sudo:`); |
| 151 | + console.error(` sudo ${pm.updateCommand}`); |
| 152 | + } else { |
| 153 | + console.error(`${red('✘')} Error: ${error.message || 'Unknown error'}`); |
| 154 | + console.error(`\n${yellow('You can manually update with:')}`); |
| 155 | + console.error(` ${pm.updateCommand}`); |
| 156 | + } |
| 157 | + |
| 158 | + process.exit(1); |
| 159 | + } |
| 160 | + })().catch(handleCommandError); |
| 161 | + } |
| 162 | +); |
0 commit comments