Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

gitCloneProgress.ts

37 lines1.3 KB

1import type { ProjectCloneStage } from "@t3tools/contracts";
2​
3export interface GitCloneProgressLine {
4 readonly stage: ProjectCloneStage;
5 readonly percent: number | null;
6 readonly detail: string | null;
7}
8​
9const STAGE_PREFIXES: ReadonlyArray<readonly [RegExp, ProjectCloneStage]> = [
10 [/^remote: Enumerating objects/, "counting"],
11 [/^remote: Counting objects/, "counting"],
12 [/^remote: Compressing objects/, "counting"],
13 [/^Receiving objects/, "receiving"],
14 [/^Resolving deltas/, "resolving"],
15 [/^Updating files/, "checkout"],
16 [/^Checking out files/, "checkout"],
17];
18​
19const PERCENT = /:\s+(\d+)%\s+\((\d+)\/(\d+)\)(?:,\s*(.*?))?\s*(?:,\s*done\.)?\s*$/;
20​
21/** Pass each bare-CR progress redraw as a separate line. */
22export function parseGitCloneProgressLine(line: string): GitCloneProgressLine | null {
23 const trimmed = line.trim();
24 const stageEntry = STAGE_PREFIXES.find(([pattern]) => pattern.test(trimmed));
25 if (!stageEntry) return null;
26 const stage = stageEntry[1];
27 const match = PERCENT.exec(trimmed);
28 if (!match) return { stage, percent: null, detail: null };
29 const percent = Number(match[1]);
30 const rawDetail = match[4]?.trim() ?? "";
31 const detail = rawDetail.length > 0 && rawDetail !== "done." ? rawDetail : null;
32 return {
33 stage,
34 percent: Number.isFinite(percent) ? Math.max(0, Math.min(100, percent)) : null,
35 detail,
36 };