|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const { spawn } = require("child_process"); |
| 4 | +const { Command } = require("commander"); |
| 5 | +const { fetch } = require("undici"); |
| 6 | +const chalk = require("chalk"); |
| 7 | +const inquirer = require("inquirer"); |
| 8 | +const { success, log, warn, error, hint, actionRunner, commandDescriptions } = require("../parser"); |
| 9 | +const { getLatestVersion, compareVersions } = require("../utils"); |
| 10 | +const { version } = require("../../package.json"); |
| 11 | + |
| 12 | +/** |
| 13 | + * Check if the CLI was installed via npm |
| 14 | + */ |
| 15 | +const isInstalledViaNpm = () => { |
| 16 | + try { |
| 17 | + const scriptPath = process.argv[1]; |
| 18 | + |
| 19 | + if (scriptPath.includes('node_modules') && scriptPath.includes('{{ language.params.npmPackage|caseDash }}')) { |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + if (scriptPath.includes('/usr/local/lib/node_modules/') || |
| 24 | + scriptPath.includes('/opt/homebrew/lib/node_modules/') || |
| 25 | + scriptPath.includes('/.npm-global/') || |
| 26 | + scriptPath.includes('/node_modules/.bin/')) { |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + return false; |
| 31 | + } catch (e) { |
| 32 | + return false; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Check if the CLI was installed via Homebrew |
| 38 | + */ |
| 39 | +const isInstalledViaHomebrew = () => { |
| 40 | + try { |
| 41 | + const scriptPath = process.argv[1]; |
| 42 | + return scriptPath.includes('/opt/homebrew/') || scriptPath.includes('/usr/local/Cellar/'); |
| 43 | + } catch (e) { |
| 44 | + return false; |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +/** |
| 51 | + * Execute command and return promise |
| 52 | + */ |
| 53 | +const execCommand = (command, args = [], options = {}) => { |
| 54 | + return new Promise((resolve, reject) => { |
| 55 | + const child = spawn(command, args, { |
| 56 | + stdio: 'inherit', |
| 57 | + shell: true, |
| 58 | + ...options |
| 59 | + }); |
| 60 | + |
| 61 | + child.on('close', (code) => { |
| 62 | + if (code === 0) { |
| 63 | + resolve(); |
| 64 | + } else { |
| 65 | + reject(new Error(`Command failed with exit code ${code}`)); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + child.on('error', (err) => { |
| 70 | + reject(err); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Update via npm |
| 77 | + */ |
| 78 | +const updateViaNpm = async () => { |
| 79 | + try { |
| 80 | + await execCommand('npm', ['install', '-g', '{{ language.params.npmPackage|caseDash }}@latest']); |
| 81 | + console.log(""); |
| 82 | + success("Updated to latest version via npm!"); |
| 83 | + hint("Run '{{ language.params.executableName|caseLower }} --version' to verify the new version."); |
| 84 | + } catch (e) { |
| 85 | + if (e.message.includes('EEXIST') || e.message.includes('file already exists')) { |
| 86 | + console.log(""); |
| 87 | + success("Latest version is already installed via npm!"); |
| 88 | + hint("The CLI is up to date. Run '{{ language.params.executableName|caseLower }} --version' to verify."); |
| 89 | + } else { |
| 90 | + console.log(""); |
| 91 | + error(`Failed to update via npm: ${e.message}`); |
| 92 | + hint("Try running: npm install -g {{ language.params.npmPackage|caseDash }}@latest --force"); |
| 93 | + } |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +/** |
| 98 | + * Update via Homebrew |
| 99 | + */ |
| 100 | +const updateViaHomebrew = async () => { |
| 101 | + try { |
| 102 | + await execCommand('brew', ['upgrade', '{{ language.params.executableName|caseLower }}']); |
| 103 | + console.log(""); |
| 104 | + success("Updated to latest version via Homebrew!"); |
| 105 | + hint("Run '{{ language.params.executableName|caseLower }} --version' to verify the new version."); |
| 106 | + } catch (e) { |
| 107 | + if (e.message.includes('already installed') || e.message.includes('up-to-date')) { |
| 108 | + console.log(""); |
| 109 | + success("Latest version is already installed via Homebrew!"); |
| 110 | + hint("The CLI is up to date. Run '{{ language.params.executableName|caseLower }} --version' to verify."); |
| 111 | + } else { |
| 112 | + console.log(""); |
| 113 | + error(`Failed to update via Homebrew: ${e.message}`); |
| 114 | + hint("Try running: brew upgrade {{ language.params.executableName|caseLower }}"); |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * Show manual update instructions |
| 121 | + */ |
| 122 | +const showManualInstructions = (latestVersion) => { |
| 123 | + log("Manual update options:"); |
| 124 | + console.log(""); |
| 125 | + |
| 126 | + log(`${chalk.bold("Option 1: NPM")}`); |
| 127 | + console.log(` npm install -g {{ language.params.npmPackage|caseDash }}@latest`); |
| 128 | + console.log(""); |
| 129 | + |
| 130 | + log(`${chalk.bold("Option 2: Homebrew")}`); |
| 131 | + console.log(` brew upgrade {{ language.params.executableName|caseLower }}`); |
| 132 | + console.log(""); |
| 133 | + |
| 134 | + log(`${chalk.bold("Option 3: Download Binary")}`); |
| 135 | + console.log(` Visit: https://github.com/{{ language.params.npmPackage|caseDash }}/releases/tag/${latestVersion}`); |
| 136 | +}; |
| 137 | + |
| 138 | +/** |
| 139 | + * Show interactive menu for choosing update method |
| 140 | + */ |
| 141 | +const chooseUpdateMethod = async (latestVersion) => { |
| 142 | + const choices = [ |
| 143 | + { name: 'NPM', value: 'npm' }, |
| 144 | + { name: 'Homebrew', value: 'homebrew' }, |
| 145 | + { name: 'Show manual instructions', value: 'manual' } |
| 146 | + ]; |
| 147 | + |
| 148 | + const { method } = await inquirer.prompt([ |
| 149 | + { |
| 150 | + type: 'list', |
| 151 | + name: 'method', |
| 152 | + message: 'Could not detect installation method. How would you like to update?', |
| 153 | + choices: choices |
| 154 | + } |
| 155 | + ]); |
| 156 | + |
| 157 | + switch (method) { |
| 158 | + case 'npm': |
| 159 | + await updateViaNpm(); |
| 160 | + break; |
| 161 | + case 'homebrew': |
| 162 | + await updateViaHomebrew(); |
| 163 | + break; |
| 164 | + case 'manual': |
| 165 | + showManualInstructions(latestVersion); |
| 166 | + break; |
| 167 | + } |
| 168 | +}; |
| 169 | + |
| 170 | +/** |
| 171 | + * Main update function |
| 172 | + */ |
| 173 | +const updateCli = async ({ manual } = {}) => { |
| 174 | + try { |
| 175 | + const latestVersion = await getLatestVersion(); |
| 176 | + |
| 177 | + const comparison = compareVersions(version, latestVersion); |
| 178 | + |
| 179 | + if (comparison === 0) { |
| 180 | + success(`You're already running the latest version (${chalk.bold(version)})!`); |
| 181 | + return; |
| 182 | + } else if (comparison < 0) { |
| 183 | + warn(`You're running a newer version (${chalk.bold(version)}) than the latest released version (${chalk.bold(latestVersion)}).`); |
| 184 | + hint("This might be a pre-release or development version."); |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + log(`Updating from ${chalk.blue(version)} to ${chalk.green(latestVersion)}...`); |
| 189 | + console.log(""); |
| 190 | + |
| 191 | + if (manual) { |
| 192 | + showManualInstructions(latestVersion); |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + if (isInstalledViaNpm()) { |
| 197 | + await updateViaNpm(); |
| 198 | + } else if (isInstalledViaHomebrew()) { |
| 199 | + await updateViaHomebrew(); |
| 200 | + } else { |
| 201 | + await chooseUpdateMethod(latestVersion); |
| 202 | + } |
| 203 | + |
| 204 | + } catch (e) { |
| 205 | + console.log(""); |
| 206 | + error(`Failed to check for updates: ${e.message}`); |
| 207 | + hint("You can manually check for updates at: https://github.com/{{ language.params.npmPackage|caseDash }}/releases"); |
| 208 | + } |
| 209 | +}; |
| 210 | + |
| 211 | +const update = new Command("update") |
| 212 | + .description("Update the {{ spec.title|caseUcfirst }} CLI to the latest version") |
| 213 | + .option("--manual", "Show manual update instructions instead of auto-updating") |
| 214 | + .action(actionRunner(updateCli)); |
| 215 | + |
| 216 | +module.exports = { |
| 217 | + update |
| 218 | +}; |
0 commit comments