UNPKG

qwen-code-updater

Version:

Auto-updater wrapper for Qwen Code that checks for updates before execution

143 lines (119 loc) • 5.19 kB
#!/usr/bin/env node const chalk = require('chalk'); const ShellDetector = require('../lib/shell-detector'); const Logger = require('../lib/logger'); const Config = require('../lib/config'); class Installer { constructor() { this.shellDetector = new ShellDetector(); this.logger = new Logger(); this.config = new Config(); } async install() { try { console.log(chalk.blue('šŸš€ Installing Qwen Code Updater...')); // Initialize configuration directory this.config.ensureConfigDir(); console.log(chalk.green('āœ… Configuration initialized')); // Setup shell alias await this.setupShellAlias(); // Log installation this.logger.info('Qwen Code Updater installed successfully'); console.log(chalk.green('\nšŸŽ‰ Installation completed successfully!')); console.log(chalk.cyan('\nUsage:')); console.log(chalk.white(' qwen [arguments] # Will auto-update and run Qwen Code')); console.log(chalk.white(' qwen --skip-update [arguments] # Skip update check')); console.log(chalk.cyan('\nConfiguration:')); console.log(chalk.white(` Config directory: ${this.config.configDir}`)); console.log(chalk.white(` Log directory: ${this.logger.logDir}`)); console.log(chalk.yellow('\nNote: You may need to restart your terminal or run:')); console.log(chalk.white(' source ~/.bashrc # or ~/.zshrc')); } catch (error) { console.error(chalk.red('āŒ Installation failed:'), error.message); this.logger.error('Installation failed:', error.message); process.exit(1); } } async setupShellAlias() { try { const systemInfo = this.shellDetector.getSystemInfo(); console.log(chalk.blue('šŸ”§ Setting up shell integration...')); console.log(chalk.gray(`Detected shell: ${systemInfo.shell}`)); // Get all available shells const shells = this.shellDetector.detectAllShells(); let aliasAdded = false; // Try to add alias for current shell and other detected shells for (const shell of shells) { try { const configPath = this.shellDetector.getShellConfigPath(shell.name); if (configPath) { const success = this.shellDetector.addToShellPath(shell.name, ''); // We don't need to add PATH, just alias // Manually add the alias since addToShellPath is for PATH modification const aliasCommand = this.getAliasCommand(shell.name); if (this.addAliasToFile(configPath, aliasCommand)) { console.log(chalk.green(`āœ… Added alias for ${shell.name}: ${configPath}`)); aliasAdded = true; } } } catch (error) { console.log(chalk.yellow(`āš ļø Could not add alias for ${shell.name}: ${error.message}`)); } } if (!aliasAdded) { console.log(chalk.yellow('āš ļø Could not automatically add shell alias. Please add manually:')); this.printManualSetupInstructions(); } } catch (error) { console.log(chalk.yellow('āš ļø Could not set up shell alias:'), error.message); this.printManualSetupInstructions(); } } getAliasCommand(shellName) { if (['bash', 'zsh', 'sh', 'ksh'].includes(shellName)) { return 'alias qwen="qwen-code-updater"'; } else if (shellName === 'fish') { return 'function qwen; qwen-code-updater $argv; end'; } else if (['tcsh', 'csh'].includes(shellName)) { return 'alias qwen qwen-code-updater'; } return 'alias qwen="qwen-code-updater"'; } addAliasToFile(configPath, aliasCommand) { try { const fs = require('fs'); // Check if file exists and read content let content = ''; if (fs.existsSync(configPath)) { content = fs.readFileSync(configPath, 'utf8'); } // Check if alias already exists if (content.includes('alias qwen=') || content.includes('function qwen')) { console.log(chalk.gray(`Alias already exists in ${configPath}`)); return true; } // Add the alias with comment const aliasBlock = `\n# Added by qwen-code-updater\n${aliasCommand}\n`; content += aliasBlock; fs.writeFileSync(configPath, content); return true; } catch (error) { console.log(chalk.yellow(`Could not write to ${configPath}: ${error.message}`)); return false; } } printManualSetupInstructions() { console.log(chalk.cyan('\nšŸ“– Manual Setup Instructions:')); console.log(chalk.white('Add the following alias to your shell configuration file:')); console.log(chalk.yellow('\n alias qwen="qwen-code-updater"')); console.log(chalk.white('\nShell configuration files:')); console.log(chalk.gray(' bash: ~/.bashrc or ~/.bash_profile')); console.log(chalk.gray(' zsh: ~/.zshrc')); console.log(chalk.gray(' fish: ~/.config/fish/config.fish (use: function qwen; qwen-code-updater $argv; end)')); } } // Run installation if this script is executed directly if (require.main === module) { const installer = new Installer(); installer.install(); } module.exports = Installer;