import type { CLIOptions } from "./list.ts"; import { filterProjects } from "../../scanner/index.ts"; import { bunGitService } from "../../services/git.ts"; import { batchPull, batchPush, batchFetch } from "../../operations/batch.ts"; import { formatBatchResult, formatProgress, formatWarning, formatInfo, formatScanning, formatOperationItem, formatOperationSummary, } from "../formatters.ts"; import { scanAllDirectories } from "../../scanner/index.ts"; /** * Pull all repositories */ export async function pullAll(options: CLIOptions): Promise { const { config, filter } = options; console.log(formatScanning()); const projects = await scanAllDirectories(config); let gitProjects = projects.filter((p) => p.type === "git" && p.status?.hasRemote); if (filter) { gitProjects = filterProjects(gitProjects, filter); } if (gitProjects.length === 0) { console.log(formatWarning("No repositories to pull.")); return; } console.log(formatInfo(`\nPulling ${gitProjects.length} repositories...\n`)); const result = await batchPull(gitProjects, { concurrency: config.scan.concurrency, onProgress: (completed, total) => { process.stdout.write(formatProgress(completed, total)); } }); console.log("\n"); console.log(formatBatchResult(result, "Pulled")); } /** * Push repositories with unpushed commits */ export async function pushAll(options: CLIOptions): Promise { const { config, filter } = options; console.log(formatScanning()); const projects = await scanAllDirectories(config); let unpushedProjects = projects.filter( (p) => p.type === "git" && p.status?.hasRemote && p.status?.isAhead ); if (filter) { unpushedProjects = filterProjects(unpushedProjects, filter); } if (unpushedProjects.length === 0) { console.log(formatWarning("No repositories with unpushed commits.")); return; } console.log(formatInfo(`\nPushing ${unpushedProjects.length} repositories...\n`)); const result = await batchPush(unpushedProjects, { concurrency: config.scan.concurrency, onProgress: (completed, total) => { process.stdout.write(formatProgress(completed, total)); } }); console.log("\n"); console.log(formatBatchResult(result, "Pushed")); } /** * Fetch all repositories */ export async function fetchAll(options: CLIOptions): Promise { const { config, filter } = options; console.log(formatScanning()); const projects = await scanAllDirectories(config); let gitProjects = projects.filter((p) => p.type === "git" && p.status?.hasRemote); if (filter) { gitProjects = filterProjects(gitProjects, filter); } if (gitProjects.length === 0) { console.log(formatWarning("No repositories to fetch.")); return; } console.log(formatInfo(`\nFetching ${gitProjects.length} repositories...\n`)); const result = await batchFetch(gitProjects, { concurrency: config.scan.concurrency, onProgress: (completed, total) => { process.stdout.write(formatProgress(completed, total)); } }); console.log("\n"); console.log(formatBatchResult(result, "Fetched")); } /** * Initialize git in non-git projects */ export async function initNonGit(options: CLIOptions): Promise { const { config, filter } = options; console.log(formatScanning()); const projects = await scanAllDirectories(config); let nonGitProjects = projects.filter((p) => p.type === "non-git"); if (filter) { nonGitProjects = filterProjects(nonGitProjects, filter); } if (nonGitProjects.length === 0) { console.log(formatWarning("No non-git projects found.")); return; } console.log(formatInfo(`\nInitializing git in ${nonGitProjects.length} projects...\n`)); let success = 0; for (const project of nonGitProjects) { const result = await bunGitService.init(project.path); console.log(formatOperationItem(project.name, result.success, result.error)); if (result.success) success++; } console.log(`\n${formatOperationSummary("Initialized", success, nonGitProjects.length)}`); }