|
1 | | -import inquirer from 'inquirer'; |
2 | | -import { GitExecutor } from '../core/GitExecutor'; |
3 | | -import { Logger } from '../utils/Logger'; |
| 1 | +/** |
| 2 | + * 🏁 RepositoryManager.ts (Deep Version) |
| 3 | + * ------------------------------------------------------------- |
| 4 | + * Handles all repository-level operations including initialization, |
| 5 | + * configuration, inspection, remote management, and maintenance. |
| 6 | + * ------------------------------------------------------------- |
| 7 | + */ |
| 8 | + |
| 9 | +import inquirer from "inquirer"; |
| 10 | +import { GitExecutor } from "../core/GitExecutor"; |
| 11 | +import { Logger } from "../utils/Logger"; |
4 | 12 |
|
5 | 13 | export const RepositoryManager = { |
| 14 | + // 🏗 Initialize Repo |
6 | 15 | async initRepo() { |
7 | | - Logger.info('🌀 Initializing new Git repository...'); |
8 | | - await GitExecutor.run('git init'); |
| 16 | + const { bare } = await inquirer.prompt([ |
| 17 | + { |
| 18 | + type: "confirm", |
| 19 | + name: "bare", |
| 20 | + message: "Do you want to create a bare repository?", |
| 21 | + default: false, |
| 22 | + }, |
| 23 | + ]); |
| 24 | + |
| 25 | + const cmd = bare ? "git init --bare" : "git init"; |
| 26 | + Logger.info(`📦 Initializing repository (${bare ? "bare" : "standard"})...`); |
| 27 | + await GitExecutor.run(cmd); |
| 28 | + Logger.success("✅ Repository initialized successfully!"); |
9 | 29 | }, |
10 | 30 |
|
| 31 | + // 🌐 Clone Repo |
| 32 | + async cloneRepo() { |
| 33 | + const { url, folder } = await inquirer.prompt([ |
| 34 | + { type: "input", name: "url", message: "Enter repository URL to clone:" }, |
| 35 | + { type: "input", name: "folder", message: "Optional: Enter target folder name (leave empty for default):" }, |
| 36 | + ]); |
| 37 | + |
| 38 | + const cmd = folder ? `git clone ${url} ${folder}` : `git clone ${url}`; |
| 39 | + Logger.info(`🔁 Cloning repository from ${url}...`); |
| 40 | + await GitExecutor.run(cmd); |
| 41 | + Logger.success("✅ Repository cloned successfully!"); |
| 42 | + }, |
| 43 | + |
| 44 | + // 📂 Check Status |
11 | 45 | async status() { |
12 | | - Logger.info('📋 Checking repository status...'); |
13 | | - await GitExecutor.run('git status'); |
| 46 | + const { short } = await inquirer.prompt([ |
| 47 | + { |
| 48 | + type: "confirm", |
| 49 | + name: "short", |
| 50 | + message: "Show compact (short) status?", |
| 51 | + default: false, |
| 52 | + }, |
| 53 | + ]); |
| 54 | + |
| 55 | + const cmd = short ? "git status -s" : "git status"; |
| 56 | + Logger.info("📂 Checking repository status..."); |
| 57 | + await GitExecutor.run(cmd); |
| 58 | + }, |
| 59 | + |
| 60 | + // ⚙️ Git Config Management |
| 61 | + async showConfig() { |
| 62 | + const { scope } = await inquirer.prompt([ |
| 63 | + { |
| 64 | + type: "list", |
| 65 | + name: "scope", |
| 66 | + message: "Which config scope do you want to view?", |
| 67 | + choices: [ |
| 68 | + { name: "Local", value: "--local" }, |
| 69 | + { name: "Global", value: "--global" }, |
| 70 | + { name: "System", value: "--system" }, |
| 71 | + { name: "All combined", value: "--list" }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + ]); |
| 75 | + |
| 76 | + const cmd = scope === "--list" ? "git config --list" : `git config ${scope} --list`; |
| 77 | + Logger.info(`⚙️ Showing ${scope.replace("--", "")} configuration...`); |
| 78 | + await GitExecutor.run(cmd); |
| 79 | + }, |
| 80 | + |
| 81 | + // ✏️ Set Config |
| 82 | + async setConfig() { |
| 83 | + const { key, value, global } = await inquirer.prompt([ |
| 84 | + { type: "input", name: "key", message: "Enter config key (e.g. user.name):" }, |
| 85 | + { type: "input", name: "value", message: "Enter config value:" }, |
| 86 | + { type: "confirm", name: "global", message: "Apply globally?", default: false }, |
| 87 | + ]); |
| 88 | + |
| 89 | + const flag = global ? "--global" : ""; |
| 90 | + const cmd = `git config ${flag} ${key} "${value}"`; |
| 91 | + Logger.info(`🧩 Setting config: ${key}=${value}`); |
| 92 | + await GitExecutor.run(cmd); |
| 93 | + Logger.success("✅ Configuration updated!"); |
| 94 | + }, |
| 95 | + |
| 96 | + // 🔗 List Remotes |
| 97 | + async listRemotes() { |
| 98 | + Logger.info("🔗 Listing remotes..."); |
| 99 | + await GitExecutor.run("git remote -v"); |
14 | 100 | }, |
15 | 101 |
|
16 | | - |
| 102 | + // ➕ Add Remote |
| 103 | + async addRemote() { |
| 104 | + const { name, url } = await inquirer.prompt([ |
| 105 | + { type: "input", name: "name", message: "Enter remote name (e.g. origin):" }, |
| 106 | + { type: "input", name: "url", message: "Enter remote URL:" }, |
| 107 | + ]); |
| 108 | + |
| 109 | + const cmd = `git remote add ${name} ${url}`; |
| 110 | + Logger.info(`🔗 Adding remote '${name}' -> ${url}`); |
| 111 | + await GitExecutor.run(cmd); |
| 112 | + Logger.success(`✅ Remote '${name}' added successfully!`); |
| 113 | + }, |
| 114 | + |
| 115 | + // ✏️ Change Remote URL |
| 116 | + async updateRemote() { |
| 117 | + const { name, url } = await inquirer.prompt([ |
| 118 | + { type: "input", name: "name", message: "Enter remote name to update:" }, |
| 119 | + { type: "input", name: "url", message: "Enter new remote URL:" }, |
| 120 | + ]); |
| 121 | + |
| 122 | + const cmd = `git remote set-url ${name} ${url}`; |
| 123 | + Logger.info(`🔄 Updating remote '${name}' to ${url}`); |
| 124 | + await GitExecutor.run(cmd); |
| 125 | + Logger.success("✅ Remote URL updated!"); |
| 126 | + }, |
| 127 | + |
| 128 | + // 🗑️ Remove Remote |
| 129 | + async removeRemote() { |
| 130 | + const { name } = await inquirer.prompt([ |
| 131 | + { type: "input", name: "name", message: "Enter remote name to remove:" }, |
| 132 | + ]); |
| 133 | + |
| 134 | + const cmd = `git remote remove ${name}`; |
| 135 | + Logger.info(`🗑️ Removing remote '${name}'...`); |
| 136 | + await GitExecutor.run(cmd); |
| 137 | + Logger.success(`✅ Remote '${name}' removed successfully!`); |
| 138 | + }, |
| 139 | + |
| 140 | + // 🧠 Repository Info |
| 141 | + async showRepoInfo() { |
| 142 | + Logger.info("🔍 Getting repository HEAD and root info..."); |
| 143 | + await GitExecutor.run("git rev-parse --show-toplevel && git rev-parse --abbrev-ref HEAD"); |
| 144 | + }, |
| 145 | + |
| 146 | + // 🧹 Repository Cleanup / Maintenance |
| 147 | + async optimizeRepo() { |
| 148 | + Logger.info("🧹 Running garbage collection..."); |
| 149 | + await GitExecutor.run("git gc --prune=now --aggressive"); |
| 150 | + Logger.success("✅ Repository optimized successfully!"); |
| 151 | + }, |
| 152 | + |
| 153 | + // 🧾 Check Repository Integrity |
| 154 | + async verifyRepo() { |
| 155 | + Logger.info("🔎 Verifying repository integrity..."); |
| 156 | + await GitExecutor.run("git fsck --full"); |
| 157 | + }, |
17 | 158 | }; |
0 commit comments