-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
471 lines (398 loc) · 14 KB
/
Copy pathscript.js
File metadata and controls
471 lines (398 loc) · 14 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import { cacheManager } from "./cache.js";
import {
clearToken,
config,
loadToken,
saveToken,
} from "./authentication.js";
import {
githubFetch,
RateLimitError,
showError,
updateRateLimitDisplay,
} from "./helpers.js";
import {
countPhysicalLines,
getFileExtension,
isBinaryExtension,
} from "./line-utils.js";
const MAX_FILE_SIZE_BYTES = 1_000_000;
function storageGet(keys) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(keys, (result) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
return;
}
resolve(result);
});
});
}
function storageSet(items) {
return new Promise((resolve, reject) => {
chrome.storage.local.set(items, () => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
return;
}
resolve();
});
});
}
async function fetchGithubRepoData(owner, repo) {
const cacheKey = `cache_repo_${owner}_${repo}`;
const cachedData = await cacheManager.get(cacheKey);
if (cachedData) return cachedData;
const response = await githubFetch(
`https://api.github.com/repos/${owner}/${repo}`,
);
if (!response.ok) {
if (response.status === 404) {
throw new Error(
"Repository not found. For a private repository, save a token with access to that repository.",
);
}
throw new Error(
`Could not read repository information (${response.status} ${response.statusText}).`,
);
}
const data = await response.json();
await cacheManager.set(cacheKey, data);
return data;
}
async function fetchRepositoryTree(owner, repo, defaultBranch) {
const cacheKey = `cache_tree_${owner}_${repo}_${defaultBranch}`;
const cachedData = await cacheManager.get(cacheKey);
if (cachedData) return cachedData;
const response = await githubFetch(
`https://api.github.com/repos/${owner}/${repo}/git/trees/${encodeURIComponent(defaultBranch)}?recursive=1`,
);
if (!response.ok) {
throw new Error(
`Could not read the repository tree (${response.status} ${response.statusText}).`,
);
}
const data = await response.json();
if (data.truncated) {
throw new Error(
"GitHub truncated this repository tree because the repository is too large. No partial result was shown.",
);
}
await cacheManager.set(cacheKey, data);
return data;
}
async function getFileExclusions() {
const result = await storageGet(["fileExclusions"]);
return new Set(result.fileExclusions ?? []);
}
function getSkipReason(file, extension, exclusions) {
if (exclusions.has(extension)) return "excluded by user";
if (isBinaryExtension(extension)) return "binary file";
if (file.size > MAX_FILE_SIZE_BYTES) return "larger than 1 MB";
if (extension === "no-extension") return "unknown extensionless file";
return null;
}
async function getFileContent(owner, repo, sha) {
const response = await githubFetch(
`https://api.github.com/repos/${owner}/${repo}/git/blobs/${sha}`,
);
if (!response.ok) {
throw new Error(
`Could not fetch file content (${response.status} ${response.statusText}).`,
);
}
const data = await response.json();
if (data.encoding !== "base64" || typeof data.content !== "string") {
throw new Error("GitHub returned an unsupported blob encoding.");
}
return atob(data.content.replace(/\n/g, ""));
}
async function countLinesInFile(owner, repo, file, extension) {
const lineCacheKey = `cache_lines_${file.sha}`;
const cachedLines = await cacheManager.get(lineCacheKey);
if (Number.isInteger(cachedLines) && cachedLines >= 0) {
return cachedLines;
}
const fileContent = await getFileContent(owner, repo, file.sha);
const lines = countPhysicalLines(fileContent);
await cacheManager.set(lineCacheKey, lines);
return lines;
}
function addCountedFile(stats, file, extension, lines) {
stats.totalLines += lines;
stats.countedFiles += 1;
stats.byFile.push({ path: file.path, lines });
if (!stats.byExtension[extension]) {
stats.byExtension[extension] = { files: 0, lines: 0 };
}
stats.byExtension[extension].files += 1;
stats.byExtension[extension].lines += lines;
}
function addSkippedFile(stats, filePath, reason) {
stats.numFilesSkipped += 1;
stats.filesSkipped.push(`${filePath} (${reason})`);
}
async function countLinesOfCode(owner, repo) {
const stats = {
totalLines: 0,
totalFiles: 0,
countedFiles: 0,
byExtension: {},
byFile: [],
numFilesSkipped: 0,
filesSkipped: [],
};
const repoData = await fetchGithubRepoData(owner, repo);
const treeData = await fetchRepositoryTree(
owner,
repo,
repoData.default_branch,
);
const files = treeData.tree.filter((item) => item.type === "blob");
const exclusions = await getFileExclusions();
stats.totalFiles = files.length;
let processedFiles = 0;
const batchSize = config.github.useAuth ? 4 : 1;
for (let index = 0; index < files.length; index += batchSize) {
const batch = files.slice(index, index + batchSize);
await Promise.all(
batch.map(async (file) => {
const extension = getFileExtension(file.path);
const skipReason = getSkipReason(file, extension, exclusions);
if (skipReason) {
addSkippedFile(stats, file.path, skipReason);
return;
}
try {
const lines = await countLinesInFile(owner, repo, file, extension);
addCountedFile(stats, file, extension, lines);
} catch (error) {
if (error instanceof RateLimitError || error.name === "RateLimitError") {
throw error;
}
console.warn(`Could not process ${file.path}:`, error);
addSkippedFile(stats, file.path, error.message);
}
}),
);
processedFiles += batch.length;
updateProgress(processedFiles, files.length);
}
stats.byFile.sort((a, b) => b.lines - a.lines || a.path.localeCompare(b.path));
displayStats(stats);
return stats;
}
async function analyzeRepository(owner, repo) {
try {
return await countLinesOfCode(owner, repo);
} catch (error) {
if (error instanceof RateLimitError || error.name === "RateLimitError") {
showError(error.message);
return null;
}
showError(`Analysis failed: ${error.message}`);
return null;
} finally {
console.log("Finished counting lines of code.");
}
}
function updateProgress(processedFiles, totalFiles) {
const progressBar = document.getElementById("progress-bar");
const progressText = document.getElementById("progress-text");
const percentage =
totalFiles === 0
? processedFiles > 0
? 100
: 0
: Math.round((processedFiles / totalFiles) * 100);
if (progressBar) progressBar.style.width = `${percentage}%`;
if (progressText) {
progressText.textContent = `${percentage}% (${processedFiles}/${totalFiles} files)`;
}
}
function displayStats(stats) {
const resultsDiv = document.getElementById("results");
const loadingDiv = document.getElementById("loading");
document.getElementById("totalLines").textContent =
stats.totalLines.toLocaleString();
document.getElementById("totalFiles").textContent =
stats.totalFiles.toLocaleString();
document.getElementById("countedFiles").textContent =
stats.countedFiles.toLocaleString();
document.getElementById("totalSkippedFiles").textContent =
stats.numFilesSkipped.toLocaleString();
const skippedFilesList = document.getElementById("skippedFiles");
skippedFilesList.innerHTML = "";
const skippedToShow = stats.filesSkipped.slice(0, 20);
if (skippedToShow.length === 0) {
const item = document.createElement("li");
item.textContent = "None";
skippedFilesList.appendChild(item);
} else {
for (const file of skippedToShow) {
const item = document.createElement("li");
item.textContent = file;
skippedFilesList.appendChild(item);
}
if (stats.filesSkipped.length > skippedToShow.length) {
const item = document.createElement("li");
item.textContent = `...and ${stats.filesSkipped.length - skippedToShow.length} more`;
skippedFilesList.appendChild(item);
}
}
const extensionsDiv = document.getElementById("extensions");
extensionsDiv.innerHTML = "";
const sortedExtensions = Object.entries(stats.byExtension).sort(
(a, b) => b[1].lines - a[1].lines,
);
for (const [extension, data] of sortedExtensions) {
const percentage =
stats.totalLines === 0
? "0.0"
: ((data.lines / stats.totalLines) * 100).toFixed(1);
const row = document.createElement("div");
row.className = "extension-row";
const name = document.createElement("span");
name.textContent = extension;
const values = document.createElement("span");
values.textContent = `${data.files.toLocaleString()} ${data.files === 1 ? "file" : "files"}, ${data.lines.toLocaleString()} lines (${percentage}%)`;
row.append(name, values);
extensionsDiv.appendChild(row);
}
const filesDiv = document.getElementById("files");
filesDiv.innerHTML = "";
for (const file of stats.byFile) {
const row = document.createElement("div");
row.className = "file-row";
const path = document.createElement("span");
path.className = "file-path";
path.textContent = file.path;
path.title = file.path;
const lineCount = document.createElement("span");
lineCount.textContent = `${file.lines.toLocaleString()} lines`;
row.append(path, lineCount);
filesDiv.appendChild(row);
}
if (loadingDiv) loadingDiv.style.display = "none";
if (resultsDiv) resultsDiv.style.display = "block";
}
function showLoadingState() {
const loadingDiv = document.getElementById("loading");
const resultsDiv = document.getElementById("results");
const errorDiv = document.getElementById("error");
if (loadingDiv) loadingDiv.style.display = "block";
if (resultsDiv) resultsDiv.style.display = "none";
if (errorDiv) errorDiv.style.display = "none";
updateProgress(0, 0);
}
function renderExclusions(exclusions) {
const list = document.getElementById("exclusionsList");
list.innerHTML = "";
if (exclusions.length === 0) {
const item = document.createElement("li");
item.textContent = "No exclusions added";
list.appendChild(item);
return;
}
for (const extension of exclusions) {
const item = document.createElement("li");
const label = document.createElement("span");
label.textContent = `.${extension}`;
const removeButton = document.createElement("button");
removeButton.type = "button";
removeButton.textContent = "×";
removeButton.className = "remove-exclusion";
removeButton.setAttribute("aria-label", `Remove .${extension} exclusion`);
removeButton.addEventListener("click", async () => {
const updated = exclusions.filter((value) => value !== extension);
await storageSet({ fileExclusions: updated });
exclusions.splice(0, exclusions.length, ...updated);
renderExclusions(exclusions);
});
item.append(label, removeButton);
list.appendChild(item);
}
}
document.addEventListener("DOMContentLoaded", async () => {
const tokenInput = document.getElementById("githubToken");
const saveTokenButton = document.getElementById("saveToken");
const clearTokenButton = document.getElementById("clearToken");
const countButton = document.getElementById("countLoc");
const currentRepoDiv = document.getElementById("currentRepo");
const exclusionInput = document.getElementById("exclusionInput");
const addExclusionButton = document.getElementById("addExclusion");
const tokenHelpLink = document.getElementById("tokenHelpLink");
let exclusions = [...(await getFileExclusions())];
renderExclusions(exclusions);
try {
await loadToken();
} catch (error) {
showError(`Could not load the saved token: ${error.message}`);
}
await updateRateLimitDisplay();
tokenHelpLink.addEventListener("click", (event) => {
event.preventDefault();
chrome.tabs.create({
url: "https://docs.github.com/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens",
});
});
saveTokenButton.addEventListener("click", async () => {
try {
saveTokenButton.disabled = true;
await saveToken(tokenInput.value);
tokenInput.value = "";
await updateRateLimitDisplay();
} catch (error) {
showError(error.message);
} finally {
saveTokenButton.disabled = false;
}
});
clearTokenButton.addEventListener("click", async () => {
try {
await clearToken();
await cacheManager.clear();
await updateRateLimitDisplay();
} catch (error) {
showError(`Could not clear token: ${error.message}`);
}
});
addExclusionButton.addEventListener("click", async () => {
const enteredValue = exclusionInput.value.trim().toLowerCase();
const extension = enteredValue.startsWith(".")
? enteredValue.slice(1)
: enteredValue;
if (!extension || exclusions.includes(extension)) return;
exclusions.push(extension);
exclusions.sort();
await storageSet({ fileExclusions: exclusions });
renderExclusions(exclusions);
exclusionInput.value = "";
});
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const match = tab?.url?.match(
/^https?:\/\/github\.com\/([^/]+)\/([^/?#]+)(?:[/?#]|$)/,
);
if (!match) {
currentRepoDiv.textContent = "No GitHub repository detected";
countButton.disabled = true;
return;
}
const owner = match[1];
const repo = match[2].replace(/\.git$/, "");
currentRepoDiv.textContent = `Repository: ${owner}/${repo}`;
countButton.disabled = false;
countButton.addEventListener("click", async () => {
showLoadingState();
countButton.disabled = true;
const originalText = countButton.textContent;
countButton.textContent = "Counting…";
await analyzeRepository(owner, repo);
countButton.textContent = originalText;
countButton.disabled = false;
});
} catch (error) {
showError(`Could not detect the current repository: ${error.message}`);
}
});