|
1 | | -export const RemoteManager = {} |
| 1 | +/** |
| 2 | + * 🌎 RemoteManager.ts |
| 3 | + * ------------------------------------------------------------- |
| 4 | + * Handles all remote-related Git operations: |
| 5 | + * - Listing, adding, renaming, and removing remotes |
| 6 | + * - Pushing, pulling, and fetching |
| 7 | + * - Syncing remotes and updating URLs |
| 8 | + * ------------------------------------------------------------- |
| 9 | + */ |
| 10 | + |
| 11 | +import inquirer from 'inquirer'; |
| 12 | +import { execSync } from 'child_process'; |
| 13 | +import { GitExecutor } from '../core/GitExecutor'; |
| 14 | +import { Logger } from '../utils/Logger'; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Helper: get list of remote names |
| 19 | + */ |
| 20 | +function getRemoteList(): string[] { |
| 21 | + try { |
| 22 | + const raw = execSync('git remote', { encoding: 'utf-8' }); |
| 23 | + return raw |
| 24 | + .split('\n') |
| 25 | + .map((r) => r.trim()) |
| 26 | + .filter(Boolean); |
| 27 | + } catch { |
| 28 | + return []; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export const RemoteManager = { |
| 33 | + /** |
| 34 | + * 🔗 List all remotes |
| 35 | + */ |
| 36 | + async listRemotes() { |
| 37 | + Logger.info('🔗 Listing all configured remotes...'); |
| 38 | + await GitExecutor.run('git remote -v'); |
| 39 | + }, |
| 40 | + |
| 41 | + /** |
| 42 | + * ➕ Add a new remote |
| 43 | + */ |
| 44 | + async addRemote() { |
| 45 | + const { name, url } = await inquirer.prompt([ |
| 46 | + { type: 'input', name: 'name', message: 'Enter remote name (e.g. origin):' }, |
| 47 | + { type: 'input', name: 'url', message: 'Enter remote URL:' }, |
| 48 | + ]); |
| 49 | + |
| 50 | + Logger.info(`➕ Adding remote '${name}'...`); |
| 51 | + await GitExecutor.run(`git remote add ${name} ${url}`); |
| 52 | + Logger.success(`✅ Remote '${name}' added successfully!`); |
| 53 | + }, |
| 54 | + |
| 55 | + /** |
| 56 | + * ✏️ Rename an existing remote |
| 57 | + */ |
| 58 | + async renameRemote() { |
| 59 | + const remotes = getRemoteList(); |
| 60 | + if (!remotes.length) { |
| 61 | + Logger.info('⚠️ No remotes found.'); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const { oldName } = await inquirer.prompt([ |
| 66 | + { type: 'list', name: 'oldName', message: 'Select remote to rename:', choices: remotes }, |
| 67 | + ]); |
| 68 | + |
| 69 | + const { newName } = await inquirer.prompt([ |
| 70 | + { type: 'input', name: 'newName', message: `Enter new name for '${oldName}':` }, |
| 71 | + ]); |
| 72 | + |
| 73 | + Logger.info(`✏️ Renaming remote '${oldName}' → '${newName}'...`); |
| 74 | + await GitExecutor.run(`git remote rename ${oldName} ${newName}`); |
| 75 | + Logger.success(`✅ Remote renamed to '${newName}'.`); |
| 76 | + }, |
| 77 | + |
| 78 | + /** |
| 79 | + * 🗑️ Remove a remote |
| 80 | + */ |
| 81 | + async removeRemote() { |
| 82 | + const remotes = getRemoteList(); |
| 83 | + if (!remotes.length) { |
| 84 | + Logger.info('⚠️ No remotes found.'); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const { name } = await inquirer.prompt([ |
| 89 | + { type: 'list', name: 'name', message: 'Select remote to remove:', choices: remotes }, |
| 90 | + ]); |
| 91 | + |
| 92 | + Logger.info(`🗑️ Removing remote '${name}'...`); |
| 93 | + await GitExecutor.run(`git remote remove ${name}`); |
| 94 | + Logger.success(`✅ Remote '${name}' removed successfully!`); |
| 95 | + }, |
| 96 | + |
| 97 | + /** |
| 98 | + * ⚙️ Change a remote URL |
| 99 | + */ |
| 100 | + async updateRemoteUrl() { |
| 101 | + const remotes = getRemoteList(); |
| 102 | + if (!remotes.length) { |
| 103 | + Logger.info('⚠️ No remotes found.'); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const { name } = await inquirer.prompt([ |
| 108 | + { type: 'list', name: 'name', message: 'Select remote to update URL:', choices: remotes }, |
| 109 | + ]); |
| 110 | + |
| 111 | + const { url } = await inquirer.prompt([ |
| 112 | + { type: 'input', name: 'url', message: 'Enter new remote URL:' }, |
| 113 | + ]); |
| 114 | + |
| 115 | + Logger.info(`🔄 Updating URL for remote '${name}'...`); |
| 116 | + await GitExecutor.run(`git remote set-url ${name} ${url}`); |
| 117 | + Logger.success(`✅ Remote '${name}' URL updated!`); |
| 118 | + }, |
| 119 | + |
| 120 | + /** |
| 121 | + * 🚀 Push changes to remote |
| 122 | + */ |
| 123 | + async pushChanges() { |
| 124 | + const remotes = getRemoteList(); |
| 125 | + const { remote } = await inquirer.prompt([ |
| 126 | + { |
| 127 | + type: 'list', |
| 128 | + name: 'remote', |
| 129 | + message: 'Select remote to push to:', |
| 130 | + choices: remotes.length ? remotes : ['origin'], |
| 131 | + }, |
| 132 | + ]); |
| 133 | + |
| 134 | + Logger.info(`🚀 Pushing changes to '${remote}'...`); |
| 135 | + await GitExecutor.run(`git push ${remote}`); |
| 136 | + Logger.success(`✅ Changes pushed to '${remote}'!`); |
| 137 | + }, |
| 138 | + |
| 139 | + /** |
| 140 | + * 🚀 Push and set upstream |
| 141 | + */ |
| 142 | + async pushWithUpstream() { |
| 143 | + const remotes = getRemoteList(); |
| 144 | + const { remote, branch } = await inquirer.prompt([ |
| 145 | + { |
| 146 | + type: 'list', |
| 147 | + name: 'remote', |
| 148 | + message: 'Select remote:', |
| 149 | + choices: remotes.length ? remotes : ['origin'], |
| 150 | + }, |
| 151 | + { type: 'input', name: 'branch', message: 'Enter branch name to push:' }, |
| 152 | + ]); |
| 153 | + |
| 154 | + Logger.info(`🚀 Pushing '${branch}' to '${remote}' (set upstream)...`); |
| 155 | + await GitExecutor.run(`git push -u ${remote} ${branch}`); |
| 156 | + Logger.success(`✅ Upstream branch '${branch}' set to '${remote}'.`); |
| 157 | + }, |
| 158 | + |
| 159 | + /** |
| 160 | + * ⬇️ Pull changes from remote |
| 161 | + */ |
| 162 | + async pullChanges() { |
| 163 | + const remotes = getRemoteList(); |
| 164 | + const { remote } = await inquirer.prompt([ |
| 165 | + { |
| 166 | + type: 'list', |
| 167 | + name: 'remote', |
| 168 | + message: 'Select remote to pull from:', |
| 169 | + choices: remotes.length ? remotes : ['origin'], |
| 170 | + }, |
| 171 | + ]); |
| 172 | + |
| 173 | + Logger.info(`⬇️ Pulling changes from '${remote}'...`); |
| 174 | + await GitExecutor.run(`git pull ${remote}`); |
| 175 | + Logger.success(`✅ Pulled latest changes from '${remote}'.`); |
| 176 | + }, |
| 177 | + |
| 178 | + /** |
| 179 | + * 🔍 Fetch updates without merging |
| 180 | + */ |
| 181 | + async fetchUpdates() { |
| 182 | + const remotes = getRemoteList(); |
| 183 | + const { remote } = await inquirer.prompt([ |
| 184 | + { |
| 185 | + type: 'list', |
| 186 | + name: 'remote', |
| 187 | + message: 'Select remote to fetch from:', |
| 188 | + choices: [...remotes, 'all'], |
| 189 | + }, |
| 190 | + ]); |
| 191 | + |
| 192 | + const cmd = remote === 'all' ? 'git fetch --all' : `git fetch ${remote}`; |
| 193 | + Logger.info(`🔍 Fetching updates from ${remote}...`); |
| 194 | + await GitExecutor.run(cmd); |
| 195 | + Logger.success(`✅ Fetched updates from ${remote}.`); |
| 196 | + }, |
| 197 | + |
| 198 | + /** |
| 199 | + * 🌎 Show remote details |
| 200 | + */ |
| 201 | + async showRemoteInfo() { |
| 202 | + const remotes = getRemoteList(); |
| 203 | + if (!remotes.length) { |
| 204 | + Logger.info('⚠️ No remotes found.'); |
| 205 | + return; |
| 206 | + } |
| 207 | + |
| 208 | + const { remote } = await inquirer.prompt([ |
| 209 | + { type: 'list', name: 'remote', message: 'Select remote to view info:', choices: remotes }, |
| 210 | + ]); |
| 211 | + |
| 212 | + Logger.info(`🌎 Showing information for remote '${remote}'...`); |
| 213 | + await GitExecutor.run(`git remote show ${remote}`); |
| 214 | + }, |
| 215 | + |
| 216 | + /** |
| 217 | + * 🔄 Sync all remotes (fetch + pull) |
| 218 | + */ |
| 219 | + async syncAll() { |
| 220 | + Logger.info('🔄 Fetching and pulling from all remotes...'); |
| 221 | + await GitExecutor.run('git fetch --all && git pull --all'); |
| 222 | + Logger.success('✅ All remotes synchronized!'); |
| 223 | + }, |
| 224 | +}; |
0 commit comments