UNPKG

@basictech/cli

Version:

Basic CLI for creating & managing your projects

76 lines • 3.11 kB
import { spawn } from 'child_process'; import { getVersion } from '../lib/version.js'; import { ApiClient } from '../lib/api.js'; import { isOnline } from '../lib/platform.js'; import { MESSAGES } from '../lib/constants.js'; export async function UpdateCommand() { try { // Check if online if (!(await isOnline())) { console.error(MESSAGES.OFFLINE); process.exit(1); } const currentVersion = getVersion(); console.log(`Current version: ${currentVersion}`); console.log('Checking for updates...'); const apiClient = ApiClient.getInstance(); const latestVersion = await apiClient.checkLatestRelease(); if (latestVersion === currentVersion) { console.log('āœ… You are already running the latest version!'); return; } console.log(`šŸš€ New version available: ${latestVersion}`); console.log('Updating...'); // Use npm to update the package await updatePackage(); } catch (error) { console.error('āŒ Error checking for updates:', error instanceof Error ? error.message : 'Unknown error'); console.log('\nšŸ’” You can try updating manually:'); console.log(' npm update -g @basictech/cli'); console.log('\nšŸ“š Or visit: https://docs.basic.tech'); process.exit(1); } } async function updatePackage() { return new Promise((resolve, reject) => { const updateProcess = spawn('npm', ['update', '-g', '@basictech/cli'], { stdio: 'pipe', shell: true }); let output = ''; let errorOutput = ''; updateProcess.stdout?.on('data', (data) => { output += data.toString(); }); updateProcess.stderr?.on('data', (data) => { errorOutput += data.toString(); }); updateProcess.on('close', (code) => { if (code === 0) { console.log('āœ… Update successful!'); console.log('\nšŸŽ‰ Basic CLI has been updated to the latest version.'); console.log('šŸ’” Run `basic version` to verify the update.'); resolve(); } else { console.error('āŒ Error updating CLI'); console.log('\nšŸ’” Please try updating manually:'); console.log(' npm update -g @basictech/cli'); console.log('\nšŸ“š Or visit: https://docs.basic.tech'); if (errorOutput) { console.log('\nError details:', errorOutput); } reject(new Error(`Update process exited with code ${code}`)); } }); updateProcess.on('error', (error) => { console.error('āŒ Failed to start update process'); console.log('\nšŸ’” Please try updating manually:'); console.log(' npm update -g @basictech/cli'); console.log('\nšŸ“š Or visit: https://docs.basic.tech'); reject(error); }); }); } //# sourceMappingURL=update.js.map