-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathutils.ts
More file actions
173 lines (156 loc) · 4.35 KB
/
Copy pathutils.ts
File metadata and controls
173 lines (156 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import path from "path";
import { diffLines } from "diff";
/**
* Calculate lines of code added/removed from a file diff
*/
export function calculateLinesOfCodeDiff(
oldContent: string,
newContent: string,
): { added: number; removed: number } {
const diff = diffLines(oldContent, newContent);
let added = 0;
let removed = 0;
for (const part of diff) {
if (part.added) {
added += part.count || 0;
} else if (part.removed) {
removed += part.count || 0;
}
}
return { added, removed };
}
/**
* Extract programming language from file path
*/
export function getLanguageFromFilePath(filePath: string): string {
const ext = path.extname(filePath).toLowerCase();
const languageMap: Record<string, string> = {
".js": "JavaScript",
".jsx": "JavaScript",
".ts": "TypeScript",
".tsx": "TypeScript",
".py": "Python",
".java": "Java",
".cpp": "C++",
".c": "C",
".cs": "C#",
".php": "PHP",
".rb": "Ruby",
".go": "Go",
".rs": "Rust",
".swift": "Swift",
".kt": "Kotlin",
".scala": "Scala",
".clj": "Clojure",
".hs": "Haskell",
".ml": "OCaml",
".elm": "Elm",
".dart": "Dart",
".lua": "Lua",
".r": "R",
".m": "Objective-C",
".mm": "Objective-C++",
".sh": "Shell",
".bash": "Bash",
".zsh": "Zsh",
".fish": "Fish",
".ps1": "PowerShell",
".html": "HTML",
".css": "CSS",
".scss": "SCSS",
".sass": "Sass",
".less": "Less",
".vue": "Vue",
".svelte": "Svelte",
".md": "Markdown",
".json": "JSON",
".xml": "XML",
".yaml": "YAML",
".yml": "YAML",
".toml": "TOML",
".ini": "INI",
".cfg": "Config",
".conf": "Config",
".sql": "SQL",
".dockerfile": "Dockerfile",
};
return languageMap[ext] || "unknown";
}
/**
* Extract the first word (command type) from a shell command
*/
export function extractCommandType(command: string): string {
return command.trim().split(/\s+/)[0] || "unknown";
}
/**
* Check if a command is a git commit command
*/
export function isGitCommitCommand(command: string): boolean {
const trimmed = command.trim().toLowerCase();
return trimmed.startsWith("git commit") || trimmed.startsWith("git-commit");
}
/**
* Check if a command is a pull request creation command
*/
export function isPullRequestCommand(command: string): boolean {
const trimmed = command.trim().toLowerCase();
return (
trimmed.includes("gh pr create") ||
(trimmed.includes("git push") && trimmed.includes("pull-request")) ||
trimmed.includes("hub pull-request") ||
trimmed.includes("gitlab mr create")
);
}
/**
* Check if a command is a comment command (PR or issue comment)
*/
export function isCommentCommand(command: string): boolean {
const trimmed = command.trim().toLowerCase();
return (
trimmed.includes("gh pr comment") || trimmed.includes("gh issue comment")
);
}
/**
* Check if a command is a git push command (but not PR creation)
*/
export function isGitPushCommand(command: string): boolean {
const trimmed = command.trim().toLowerCase();
return trimmed.startsWith("git push") && !trimmed.includes("pull-request");
}
/**
* Check if a command is an issue close command
*/
export function isIssueCloseCommand(command: string): boolean {
const trimmed = command.trim().toLowerCase();
return trimmed.includes("gh issue close");
}
/**
* Check if a command is a PR review command
*/
export function isReviewCommand(command: string): boolean {
const trimmed = command.trim().toLowerCase();
return trimmed.includes("gh pr review");
}
/**
* Check if command is a gh api call to reply to a PR review comment
* Pattern: gh api -X POST repos/{owner}/{repo}/pulls/{pr}/comments/{id}/replies
*/
export function isCommentReplyCommand(command: string): boolean {
return /gh api\s+.*repos\/[^\/]+\/[^\/]+\/pulls\/\d+\/comments\/\d+\/replies/i.test(
command,
);
}
/**
* Check if command is a gh api graphql call to resolve a review thread
* Pattern: gh api graphql ... resolveReviewThread
*/
export function isResolveThreadCommand(command: string): boolean {
return /gh api graphql.*resolveReviewThread/i.test(command);
}
/**
* Get file type from extension for metrics
*/
export function getFileTypeFromPath(filePath: string): string {
const ext = path.extname(filePath).toLowerCase();
return ext.startsWith(".") ? ext.substring(1) : "unknown";
}