|
| 1 | +// check.ts — 检查项目仓库落后远程多少 git 提交 |
| 2 | +import chalk from 'chalk' |
| 3 | +import { existsSync } from 'fs' |
| 4 | +import { resolveProjectDir, getBackendDir, getFrontendDir } from '../lib/config.js' |
| 5 | +import { run } from '../lib/process.js' |
| 6 | +import { t } from '../lib/i18n.js' |
| 7 | + |
| 8 | +interface CheckOptions { |
| 9 | + project?: string |
| 10 | +} |
| 11 | + |
| 12 | +async function getRepoBehindCount(repoDir: string): Promise<{ behind: number; branch: string } | null> { |
| 13 | + if (!existsSync(repoDir)) return null |
| 14 | + |
| 15 | + // fetch latest from remote |
| 16 | + const fetchResult = await run('git', ['fetch'], { cwd: repoDir, spinner: true, label: `${t('checkFetching')} ${repoDir}`, showErrorOutput: false }) |
| 17 | + if (fetchResult.exitCode !== 0) return null |
| 18 | + |
| 19 | + // get current branch name |
| 20 | + const branchResult = await run('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: repoDir, stdio: 'pipe', showErrorOutput: false }) |
| 21 | + if (branchResult.exitCode !== 0) return null |
| 22 | + const branch = branchResult.stdout.trim() |
| 23 | + |
| 24 | + // check if upstream tracking branch exists |
| 25 | + const upstreamResult = await run('git', ['rev-parse', '--abbrev-ref', `${branch}@{upstream}`], { cwd: repoDir, stdio: 'pipe', showErrorOutput: false }) |
| 26 | + if (upstreamResult.exitCode !== 0) return null |
| 27 | + |
| 28 | + // count commits behind |
| 29 | + const countResult = await run('git', ['rev-list', '--count', `HEAD..${branch}@{upstream}`], { cwd: repoDir, stdio: 'pipe', showErrorOutput: false }) |
| 30 | + if (countResult.exitCode !== 0) return null |
| 31 | + |
| 32 | + return { behind: parseInt(countResult.stdout.trim(), 10), branch } |
| 33 | +} |
| 34 | + |
| 35 | +export async function checkAction(options: CheckOptions = {}) { |
| 36 | + const projectDir = resolveProjectDir(options.project) |
| 37 | + if (!projectDir || !existsSync(projectDir)) { |
| 38 | + console.log(chalk.red(t('projectDirNotExist'))) |
| 39 | + console.log(chalk.dim(t('hintRunCreate'))) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + const backendDir = getBackendDir(projectDir) |
| 44 | + const frontendDir = getFrontendDir(projectDir) |
| 45 | + |
| 46 | + console.log(chalk.bold(t('checkTitle'))) |
| 47 | + console.log() |
| 48 | + |
| 49 | + const dirs = [ |
| 50 | + { label: t('labelBackend'), dir: backendDir }, |
| 51 | + { label: t('labelFrontend'), dir: frontendDir }, |
| 52 | + ] |
| 53 | + |
| 54 | + for (const { label, dir } of dirs) { |
| 55 | + if (!existsSync(dir)) { |
| 56 | + console.log(` ${label}: ${chalk.dim(t('checkDirNotFound'))}`) |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + const result = await getRepoBehindCount(dir) |
| 61 | + if (result === null) { |
| 62 | + console.log(` ${label}: ${chalk.yellow(t('checkFetchFailed'))}`) |
| 63 | + } else if (result.behind === 0) { |
| 64 | + console.log(` ${label} ${chalk.dim(`(${result.branch})`)}: ${chalk.green(t('checkUpToDate'))}`) |
| 65 | + } else { |
| 66 | + console.log(` ${label} ${chalk.dim(`(${result.branch})`)}: ${chalk.yellow(`${t('checkBehind')} ${result.behind} ${t('checkCommits')}`)}`) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments