UNPKG

void-cmd

Version:

AI-powered CLI tool that converts natural language to shell commands using Cerebras API

183 lines • 7.23 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateCommand = void 0; const utils_1 = require("../utils"); const child_process_1 = require("child_process"); const axios_1 = __importDefault(require("axios")); const fs = __importStar(require("fs")); const path = __importStar(require("path")); class UpdateCommand { constructor() { this.name = 'update'; this.description = 'Check for updates and update void-cmd'; } async execute(options) { try { (0, utils_1.log)('\nšŸ”„ Checking for updates...'); const currentVersion = this.getCurrentVersion(); const latestVersion = await this.getLatestVersion(); if (!latestVersion) { (0, utils_1.log)('āŒ Could not check for updates. Please check your internet connection.'); return; } (0, utils_1.log)(`\nšŸ“¦ Current version: ${currentVersion}`); (0, utils_1.log)(`šŸ“¦ Latest version: ${latestVersion}`); if (this.compareVersions(currentVersion, latestVersion) >= 0) { (0, utils_1.log)('\nāœ… You are already running the latest version!'); return; } (0, utils_1.log)('\nšŸ†™ A new version is available!'); (0, utils_1.log)('\nUpdate options:'); (0, utils_1.log)('1. Run: npm update -g void-cmd'); (0, utils_1.log)('2. Run: bun update -g void-cmd'); (0, utils_1.log)('3. Run: vcmd -update --auto (automatic update)'); if (options.auto || options._?.includes('--auto')) { await this.performUpdate(); } } catch (error) { (0, utils_1.errorHandler)(error); } } getCurrentVersion() { try { const possiblePaths = [ path.join(__dirname, '../../package.json'), path.join(__dirname, '../package.json'), path.join(process.cwd(), 'package.json') ]; for (const packagePath of possiblePaths) { if (fs.existsSync(packagePath)) { const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8')); return packageJson.version; } } return '1.0.0'; } catch (error) { return '1.0.0'; } } async getLatestVersion() { try { const response = await axios_1.default.get('https://registry.npmjs.org/void-cmd/latest', { timeout: 5000 }); return response.data.version; } catch (error) { try { const response = await axios_1.default.get('https://registry.npmjs.org/void-cmd', { timeout: 5000 }); return response.data['dist-tags'].latest; } catch (fallbackError) { return null; } } } 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 performUpdate() { (0, utils_1.log)('\n⚔ Starting automatic update...'); const updateCommands = [ 'npm update -g void-cmd', 'bun update -g void-cmd' ]; for (const command of updateCommands) { try { (0, utils_1.log)(`\nšŸ”„ Trying: ${command}`); const success = await this.runCommand(command); if (success) { (0, utils_1.log)('\nāœ… Update completed successfully!'); (0, utils_1.log)('šŸ”„ Please restart your terminal or run a new vcmd command to use the updated version.'); return; } } catch (error) { (0, utils_1.log)(`āŒ Failed with ${command.split(' ')[0]}`); continue; } } (0, utils_1.log)('\nāŒ Automatic update failed. Please update manually:'); (0, utils_1.log)(' npm update -g void-cmd'); (0, utils_1.log)(' or'); (0, utils_1.log)(' bun update -g void-cmd'); } async runCommand(command) { return new Promise((resolve) => { const [cmd, ...args] = command.split(' '); const childProcess = (0, child_process_1.spawn)(cmd, args, { stdio: ['pipe', 'pipe', 'pipe'], shell: true }); let hasError = false; childProcess.stdout.on('data', (data) => { process.stdout.write(data.toString()); }); childProcess.stderr.on('data', (data) => { const output = data.toString(); if (output.includes('error') || output.includes('Error')) { hasError = true; } process.stderr.write('\x1b[31m' + output + '\x1b[0m'); }); childProcess.on('close', (code) => { resolve(code === 0 && !hasError); }); childProcess.on('error', () => { resolve(false); }); }); } } exports.UpdateCommand = UpdateCommand; //# sourceMappingURL=update.js.map