|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +const { spawnSync } = require('child_process'); |
| 3 | +const os = require('os'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const allowedShells = ['zsh', 'bash']; |
| 8 | + |
| 9 | +// Helper function to find the first existing file among a list of paths |
| 10 | +function findExistingFile(possibleFiles) { |
| 11 | + for (const file of possibleFiles) { |
| 12 | + const fullPath = path.join(os.homedir(), file); |
| 13 | + if (fs.existsSync(fullPath)) { |
| 14 | + return fullPath; |
| 15 | + } |
| 16 | + } |
| 17 | + return null; |
| 18 | +} |
| 19 | + |
| 20 | +const shellConfigs = { |
| 21 | + zsh: { |
| 22 | + rcFile: path.join(os.homedir(), '.zshrc'), |
| 23 | + detectFile: path.join(os.homedir(), '.zshrc'), |
| 24 | + postMessage: 'Run: source ~/.zshrc', |
| 25 | + action: (output, rcFile) => { |
| 26 | + const configContent = fs.existsSync(rcFile) ? fs.readFileSync(rcFile, 'utf-8') : ''; |
| 27 | + |
| 28 | + if (configContent.includes(output.trim())) { |
| 29 | + console.log(`✅ Autocomplete is already configured in ${rcFile}. Skipping addition.`); |
| 30 | + } else { |
| 31 | + fs.appendFileSync(rcFile, `\n# AsyncAPI CLI Autocomplete\n${output}\n`); |
| 32 | + console.log(`✅ Autocomplete configuration added to ${rcFile}.`); |
| 33 | + } |
| 34 | + }, |
| 35 | + }, |
| 36 | + bash: { |
| 37 | + rcFile: findExistingFile(['.bashrc', '.bash_profile', '.profile']) || path.join(os.homedir(), '.bashrc'), |
| 38 | + detectFile: findExistingFile(['.bashrc', '.bash_profile', '.profile']), |
| 39 | + postMessage: '', // This will be set dynamically later |
| 40 | + action: (output, rcFile) => { |
| 41 | + const configContent = fs.existsSync(rcFile) ? fs.readFileSync(rcFile, 'utf-8') : ''; |
| 42 | + |
| 43 | + if (configContent.includes(output.trim())) { |
| 44 | + console.log(`✅ Autocomplete is already configured in ${rcFile}. Skipping addition.`); |
| 45 | + } else { |
| 46 | + fs.appendFileSync(rcFile, `\n# AsyncAPI CLI Autocomplete\n${output}\n`); |
| 47 | + console.log(`✅ Autocomplete configuration added to ${rcFile}.`); |
| 48 | + } |
| 49 | + }, |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +// Set correct postMessage dynamically |
| 54 | +if (shellConfigs.bash.detectFile) { |
| 55 | + shellConfigs.bash.postMessage = `Run: source ${shellConfigs.bash.detectFile}`; |
| 56 | +} else { |
| 57 | + shellConfigs.bash.postMessage = 'Run: source ~/.bashrc'; |
| 58 | +} |
| 59 | + |
| 60 | +function getShellConfig(shell) { |
| 61 | + if (!allowedShells.includes(shell)) { |
| 62 | + throw new Error(`Unsupported shell: ${shell}. Autocomplete only supports zsh and bash.`); |
| 63 | + } |
| 64 | + return shellConfigs[shell]; |
| 65 | +} |
| 66 | + |
| 67 | +function detectShell() { |
| 68 | + const detectedShells = []; |
| 69 | + for (const [shell, config] of Object.entries(shellConfigs)) { |
| 70 | + if (config.detectFile && fs.existsSync(config.detectFile)) { |
| 71 | + detectedShells.push(shell); |
| 72 | + } |
| 73 | + } |
| 74 | + return detectedShells; |
| 75 | +} |
| 76 | + |
| 77 | +function checkPotentialPath(potentialPath) { |
| 78 | + if (potentialPath.includes(path.sep)) { |
| 79 | + if (fs.existsSync(potentialPath)) { |
| 80 | + return potentialPath; |
| 81 | + } |
| 82 | + } else { |
| 83 | + const result = spawnSync('/bin/sh', ['-c', `command -v ${potentialPath}`], { |
| 84 | + encoding: 'utf-8', |
| 85 | + stdio: 'pipe', |
| 86 | + }); |
| 87 | + if (result.status === 0 && result.stdout) { |
| 88 | + return result.stdout.trim().split('\n')[0]; |
| 89 | + } |
| 90 | + } |
| 91 | + return null; |
| 92 | +} |
| 93 | + |
| 94 | +function findCliExecutable() { |
| 95 | + const possiblePaths = [ |
| 96 | + path.resolve('./bin/run'), |
| 97 | + path.resolve('../bin/run'), |
| 98 | + path.resolve('./node_modules/.bin/asyncapi'), |
| 99 | + 'asyncapi', |
| 100 | + ]; |
| 101 | + |
| 102 | + for (const potentialPath of possiblePaths) { |
| 103 | + try { |
| 104 | + const foundPath = checkPotentialPath(potentialPath); |
| 105 | + if (foundPath) { |
| 106 | + console.log(`Found CLI executable at: ${foundPath}`); |
| 107 | + return foundPath; |
| 108 | + } |
| 109 | + } catch (error) { |
| 110 | + console.warn(`⚠️ Ignored error while checking path ${potentialPath}: ${error.message}`); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + throw new Error('CLI executable not found. Ensure AsyncAPI CLI is installed.'); |
| 115 | +} |
| 116 | + |
| 117 | +function generateAutocompleteScript(shell) { |
| 118 | + const executablePath = findCliExecutable(); |
| 119 | + const result = spawnSync(executablePath, ['autocomplete', 'script', shell], { |
| 120 | + encoding: 'utf-8', |
| 121 | + stdio: 'pipe', |
| 122 | + }); |
| 123 | + if (result.status !== 0 || result.error) { |
| 124 | + throw new Error( |
| 125 | + `Autocomplete setup for ${shell} failed: ${result.stderr || result.error?.message || 'Unknown error'}` |
| 126 | + ); |
| 127 | + } |
| 128 | + const output = result.stdout; |
| 129 | + if (!output || output.trim() === '') { |
| 130 | + throw new Error(`No autocomplete script generated for ${shell}.`); |
| 131 | + } |
| 132 | + return output; |
| 133 | +} |
| 134 | + |
| 135 | +function setupAutocomplete(shell) { |
| 136 | + if (!allowedShells.includes(shell)) { |
| 137 | + console.error(`❌ Autocomplete only supports zsh and bash. Skipping setup for ${shell}.`); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + try { |
| 142 | + const config = getShellConfig(shell); |
| 143 | + console.log(`🔧 Generating autocomplete script for ${shell}...`); |
| 144 | + const output = generateAutocompleteScript(shell); |
| 145 | + config.action(output, config.rcFile); |
| 146 | + console.log(`✅ Autocomplete configured for ${shell}. ${config.postMessage}`); |
| 147 | + } catch (error) { |
| 148 | + console.error(`❌ Failed to setup autocomplete for ${shell}: ${error.message}`); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Start |
| 153 | +const shells = detectShell(); |
| 154 | +if (shells.length) { |
| 155 | + for (const shell of shells) { |
| 156 | + setupAutocomplete(shell); |
| 157 | + } |
| 158 | +} else { |
| 159 | + console.log('⚠️ Shell not detected or unsupported. Autocomplete setup skipped.'); |
| 160 | +} |
0 commit comments