|
| 1 | +/** |
| 2 | + * Clone and Analyze command |
| 3 | + * 克隆 git 仓库并分析代码质量 |
| 4 | + */ |
| 5 | + |
| 6 | +import { Command } from 'commander'; |
| 7 | +import { resolve } from 'node:path'; |
| 8 | +import { loadConfig, createRuntimeConfig } from '../../config/index.js'; |
| 9 | +import { createAnalyzer } from '../../analyzer/index.js'; |
| 10 | +import { ConsoleOutput } from '../output/console.js'; |
| 11 | +import { MarkdownOutput } from '../output/markdown.js'; |
| 12 | +import { JsonOutput } from '../output/json.js'; |
| 13 | +import { HtmlOutput } from '../output/html.js'; |
| 14 | +import { createSpinner, ProgressBar } from '../../utils/progress.js'; |
| 15 | +import { exists, isDirectory } from '../../utils/fs.js'; |
| 16 | +import { t } from '../../i18n/index.js'; |
| 17 | +import { renderMarkdownToTerminal } from '../../utils/markdown.js'; |
| 18 | +import chalk from 'chalk'; |
| 19 | +import { |
| 20 | + gitClone, |
| 21 | + removeTempDir, |
| 22 | + isValidGitUrl, |
| 23 | + type GitCloneResult, |
| 24 | +} from '../../utils/git.js'; |
| 25 | + |
| 26 | +interface CloneAnalyzeOptions { |
| 27 | + verbose?: boolean; |
| 28 | + top?: number; |
| 29 | + format?: 'console' | 'markdown' | 'json' | 'html'; |
| 30 | + output?: string; |
| 31 | + exclude?: string[]; |
| 32 | + concurrency?: number; |
| 33 | + locale?: 'en' | 'zh' | 'ru'; |
| 34 | + keepTemp?: boolean; |
| 35 | +} |
| 36 | + |
| 37 | +export function createCloneAnalyzeCommand(): Command { |
| 38 | + const command = new Command('clone-and-analyze'); |
| 39 | + |
| 40 | + command |
| 41 | + .description(t('cmd_clone_analyze_description')) |
| 42 | + .argument('<git-url>', 'Git repository URL to clone and analyze') |
| 43 | + .option('-v, --verbose', 'Show verbose output') |
| 44 | + .option('-t, --top <number>', 'Show top N worst files (default: 10)', parseInt) |
| 45 | + .option( |
| 46 | + '-f, --format <format>', |
| 47 | + 'Output format: console, markdown, json, html (default: console)' |
| 48 | + ) |
| 49 | + .option('-o, --output <file>', 'Write output to file instead of stdout') |
| 50 | + .option('-e, --exclude <patterns...>', 'Additional glob patterns to exclude') |
| 51 | + .option('-c, --concurrency <number>', 'Number of concurrent workers (default: 8)', parseInt) |
| 52 | + .option('-l, --locale <locale>', 'Language: en, zh, ru (default: en)') |
| 53 | + .option('--keep-temp', 'Keep the temporary directory after analysis') |
| 54 | + .addHelpText( |
| 55 | + 'after', |
| 56 | + ` |
| 57 | +${t('cli_examples')} |
| 58 | + $ fuck-u-code clone-and-analyze https://github.com/user/repo.git # ${t('cmd_clone_analyze_example')} |
| 59 | + $ fuck-u-code clone-and-analyze git@github.com:user/repo.git # ${t('cmd_clone_analyze_example_url')} |
| 60 | + $ fuck-u-code clone-and-analyze https://github.com/user/repo.git -f markdown -o report.md # ${t('cmd_clone_analyze_example_output')} |
| 61 | + $ fuck-u-code clone-and-analyze https://github.com/user/repo.git --keep-temp # ${t('cmd_clone_analyze_example_keep')} |
| 62 | +` |
| 63 | + ) |
| 64 | + .action(async (gitUrl: string, options: CloneAnalyzeOptions) => { |
| 65 | + await runCloneAnalyze(gitUrl, options); |
| 66 | + }); |
| 67 | + |
| 68 | + return command; |
| 69 | +} |
| 70 | + |
| 71 | +async function runCloneAnalyze(gitUrl: string, options: CloneAnalyzeOptions): Promise<void> { |
| 72 | + // 验证 git URL |
| 73 | + if (!isValidGitUrl(gitUrl)) { |
| 74 | + console.error(chalk.red(t('error_invalid_git_url', { url: gitUrl }))); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | + |
| 78 | + const cloneSpinner = createSpinner(t('progress_cloning')); |
| 79 | + const discoverySpinner = createSpinner(t('progress_discovering')); |
| 80 | + const state: { progressBar: ProgressBar | null } = { progressBar: null }; |
| 81 | + let tempDir: string | undefined; |
| 82 | + let shouldCleanup = true; |
| 83 | + |
| 84 | + try { |
| 85 | + // 克隆仓库 |
| 86 | + cloneSpinner.start(); |
| 87 | + const cloneResult: GitCloneResult = await gitClone(gitUrl, { |
| 88 | + verbose: options.verbose, |
| 89 | + }); |
| 90 | + |
| 91 | + if (!cloneResult.success) { |
| 92 | + cloneSpinner.fail(t('progress_clone_failed')); |
| 93 | + console.error(chalk.red(cloneResult.error)); |
| 94 | + process.exit(1); |
| 95 | + } |
| 96 | + |
| 97 | + tempDir = cloneResult.targetDir; |
| 98 | + cloneSpinner.succeed(t('progress_clone_success')); |
| 99 | + |
| 100 | + if (options.verbose && tempDir) { |
| 101 | + console.log(chalk.green(t('info_temp_dir_created', { path: tempDir }))); |
| 102 | + } |
| 103 | + |
| 104 | + // 如果用户指定保留临时目录,则不清理 |
| 105 | + if (options.keepTemp) { |
| 106 | + shouldCleanup = false; |
| 107 | + if (tempDir) { |
| 108 | + console.log(chalk.yellow(t('info_temp_dir_kept', { path: tempDir }))); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + const resolvedPath = resolve(tempDir!); |
| 113 | + |
| 114 | + // 验证路径 |
| 115 | + if (!(await exists(resolvedPath))) { |
| 116 | + console.error(chalk.red(t('error_path_not_found', { path: resolvedPath }))); |
| 117 | + if (shouldCleanup && tempDir) { |
| 118 | + await removeTempDir(tempDir); |
| 119 | + } |
| 120 | + process.exit(1); |
| 121 | + } |
| 122 | + |
| 123 | + if (!(await isDirectory(resolvedPath))) { |
| 124 | + console.error(chalk.red(t('error_not_a_directory', { path: resolvedPath }))); |
| 125 | + if (shouldCleanup && tempDir) { |
| 126 | + await removeTempDir(tempDir); |
| 127 | + } |
| 128 | + process.exit(1); |
| 129 | + } |
| 130 | + |
| 131 | + // 加载配置并分析 |
| 132 | + const config = await loadConfig(resolvedPath); |
| 133 | + const runtimeConfig = createRuntimeConfig(resolvedPath, config, { |
| 134 | + verbose: options.verbose, |
| 135 | + concurrency: options.concurrency, |
| 136 | + exclude: options.exclude, |
| 137 | + output: { |
| 138 | + format: options.format ?? 'console', |
| 139 | + file: options.output, |
| 140 | + top: options.top ?? 10, |
| 141 | + maxIssues: 5, |
| 142 | + showDetails: true, |
| 143 | + }, |
| 144 | + }); |
| 145 | + |
| 146 | + const analyzer = createAnalyzer(runtimeConfig, { |
| 147 | + onDiscoveryStart: () => { |
| 148 | + discoverySpinner.start(); |
| 149 | + }, |
| 150 | + onDiscoveryComplete: (fileCount) => { |
| 151 | + discoverySpinner.succeed(t('progress_discovered', { count: fileCount })); |
| 152 | + if (fileCount > 0) { |
| 153 | + state.progressBar = new ProgressBar(fileCount, t('progress_analyzing')); |
| 154 | + state.progressBar.start(); |
| 155 | + } |
| 156 | + }, |
| 157 | + onAnalysisProgress: (current) => { |
| 158 | + state.progressBar?.update(current); |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const result = await analyzer.analyze(); |
| 163 | + |
| 164 | + state.progressBar?.succeed(t('analysisComplete')); |
| 165 | + |
| 166 | + // 输出结果 |
| 167 | + const outputFormat = runtimeConfig.output.format; |
| 168 | + const outputFile = runtimeConfig.output.file; |
| 169 | + |
| 170 | + switch (outputFormat) { |
| 171 | + case 'markdown': { |
| 172 | + const mdOutput = new MarkdownOutput(runtimeConfig); |
| 173 | + const markdown = mdOutput.render(result); |
| 174 | + if (outputFile) { |
| 175 | + const { writeFile } = await import('node:fs/promises'); |
| 176 | + await writeFile(outputFile, markdown, 'utf-8'); |
| 177 | + console.log(t('outputWritten', { file: outputFile })); |
| 178 | + } else { |
| 179 | + console.log(renderMarkdownToTerminal(markdown)); |
| 180 | + } |
| 181 | + break; |
| 182 | + } |
| 183 | + case 'json': { |
| 184 | + const jsonOutput = new JsonOutput(); |
| 185 | + const json = jsonOutput.render(result); |
| 186 | + if (outputFile) { |
| 187 | + const { writeFile } = await import('node:fs/promises'); |
| 188 | + await writeFile(outputFile, json, 'utf-8'); |
| 189 | + console.log(t('outputWritten', { file: outputFile })); |
| 190 | + } else { |
| 191 | + console.log(json); |
| 192 | + } |
| 193 | + break; |
| 194 | + } |
| 195 | + case 'html': { |
| 196 | + const htmlOutput = new HtmlOutput(runtimeConfig); |
| 197 | + const html = htmlOutput.render(result); |
| 198 | + if (outputFile) { |
| 199 | + const { writeFile } = await import('node:fs/promises'); |
| 200 | + await writeFile(outputFile, html, 'utf-8'); |
| 201 | + console.log(t('outputWritten', { file: outputFile })); |
| 202 | + } else { |
| 203 | + console.log(chalk.yellow(t('output_html_requires_file'))); |
| 204 | + const consoleOutputFallback = new ConsoleOutput(runtimeConfig); |
| 205 | + consoleOutputFallback.render(result); |
| 206 | + } |
| 207 | + break; |
| 208 | + } |
| 209 | + default: { |
| 210 | + const consoleOutput = new ConsoleOutput(runtimeConfig); |
| 211 | + consoleOutput.render(result); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + // 清理临时目录 |
| 216 | + if (shouldCleanup && tempDir) { |
| 217 | + const cleanSpinner = createSpinner(t('progress_cleaning')); |
| 218 | + cleanSpinner.start(); |
| 219 | + const removed = await removeTempDir(tempDir); |
| 220 | + if (removed) { |
| 221 | + cleanSpinner.succeed(t('progress_clean_complete')); |
| 222 | + if (options.verbose) { |
| 223 | + console.log(t('info_temp_dir_removed', { path: tempDir })); |
| 224 | + } |
| 225 | + } else { |
| 226 | + cleanSpinner.fail(t('progress_clean_failed')); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + process.exit(0); |
| 231 | + } catch (error) { |
| 232 | + cloneSpinner.fail(t('progress_clone_failed')); |
| 233 | + discoverySpinner.fail(t('analysisFailed')); |
| 234 | + state.progressBar?.fail(t('analysisFailed')); |
| 235 | + console.error(chalk.red(error instanceof Error ? error.message : String(error))); |
| 236 | + |
| 237 | + // 发生错误时也要清理临时目录 |
| 238 | + if (shouldCleanup && tempDir) { |
| 239 | + await removeTempDir(tempDir); |
| 240 | + } |
| 241 | + |
| 242 | + process.exit(1); |
| 243 | + } |
| 244 | +} |
0 commit comments