@basictech/cli
Version:
Basic CLI for creating & managing your projects
76 lines ⢠3.11 kB
JavaScript
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