-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
520 lines (447 loc) · 16.2 KB
/
script.js
File metadata and controls
520 lines (447 loc) · 16.2 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import { cacheManager } from "./cache.js"; // Import cache manager
import {
config,
getHeaders,
saveToken,
loadToken,
clearToken,
setGitHubToken,
initializeAuthChecks,
updateAuthStatus,
} from "./authentication.js"; // Import authentication functions
import {
isBinaryExtension,
getFileExtension,
showError,
updateRateLimitDisplay,
} from "./helpers.js"; // Import helper functions
// FETCHING REPO DATA AND COUNTING LINES OF CODE
async function fetchGithubRepoData(owner, repo) {
// all cache keys will be prefixed with "cache_"
const cacheKey = `cache_repo_${owner}_${repo}`;
// Check cache first
const cachedData = await cacheManager.get(cacheKey);
if (cachedData) return cachedData;
// Make API request if not in cache
const url = `https://api.github.com/repos/${owner}/${repo}`;
const response = await fetch(url, { headers: getHeaders() });
// Update rate limit display after API call
updateRateLimitDisplay();
if (!response.ok) {
// Handle rate limiting specifically
if (
response.status === 403 &&
response.headers.get("X-RateLimit-Remaining") === "0"
) {
const resetTime = new Date(
parseInt(response.headers.get("X-RateLimit-Reset")) * 1000
);
throw new Error(
`GitHub API rate limit exceeded. Resets at ${resetTime.toLocaleTimeString()}`
);
}
throw new Error(`Error fetching data: ${response.statusText}`);
}
const data = await response.json();
// Cache the result
await cacheManager.set(cacheKey, data);
return data;
}
async function countLinesOfCode(owner, repo) {
const stats = {
totalLines: 0,
totalFiles: 0,
byExtension: {},
numFilesSkipped: 0,
filesSkipped: [],
};
try {
const repoData = await fetchGithubRepoData(owner, repo);
const defaultBranch = repoData.default_branch;
// Getting root tree - check cache first
const treeCacheKey = `cache_tree_${owner}_${repo}_${defaultBranch}`;
let treeData = await cacheManager.get(treeCacheKey);
try {
if (!treeData) {
const url = `https://api.github.com/repos/${owner}/${repo}/git/trees/${defaultBranch}?recursive=1`;
const treeResponse = await fetch(url, { headers: getHeaders() });
// Update rate limit display after API call
updateRateLimitDisplay();
if (!treeResponse.ok) {
// Handle rate limiting
if (
treeResponse.status === 403 &&
treeResponse.headers.get("X-RateLimit-Remaining") === "0"
) {
const resetTime = new Date(
parseInt(treeResponse.headers.get("X-RateLimit-Reset")) * 1000
);
throw new Error(
`GitHub API rate limit exceeded. Resets at ${resetTime.toLocaleTimeString()}`
);
}
throw new Error(`Error fetching tree: ${treeResponse.statusText}`);
}
treeData = await treeResponse.json();
await cacheManager.set(treeCacheKey, treeData);
if (treeData.truncated) {
showError("Repository is too large. Response will be partial.");
}
}
} catch (error) {
showError("Error fetching tree data. Please try again later.");
return;
}
const files = treeData.tree.filter((item) => item.type === "blob");
const fileCount = files.length;
let processedFiles = 0;
const batchSize = 4; // number of files to process in each batch
for (let i = 0; i < files.length; i += batchSize) {
const batch = files.slice(i, i + batchSize);
await Promise.all(
batch.map((file) => {
return countLinesInFile(owner, repo, file, stats);
})
);
processedFiles += batch.length;
updateProgress(processedFiles, fileCount);
// Update rate limit every few batches to avoid too many requests
if (i % (batchSize * 5) === 0) {
updateRateLimitDisplay();
}
const delay = config.github.useAuth ? 50 : 100; // Less delay if authenticated
await new Promise((resolve) => setTimeout(resolve, delay)); // Delay to avoid hitting rate limits
}
displayStats(stats);
// Final rate limit update when finished
updateRateLimitDisplay();
} catch (error) {
showError(`Error counting lines of code: ${error.message}`);
} finally {
console.log("Finished counting lines of code.");
}
}
async function countLinesInFile(owner, repo, file, stats) {
const filePath = file.path;
const fileExtension = getFileExtension(filePath);
// Get exclusions from storage
const exclusionsResult = await new Promise((resolve) => {
chrome.storage.local.get("fileExclusions", (result) => {
resolve(result.fileExclusions || []);
});
});
// Skip if the file extension is in the exclusions list
if (exclusionsResult.includes(fileExtension)) {
stats.totalFiles++;
stats.numFilesSkipped++;
stats.filesSkipped.push(`${filePath} (excluded by user)`);
return;
}
// Skip binary files and large files
if (
isBinaryExtension(fileExtension) ||
file.size > 1000000 ||
fileExtension === "no-extension"
) {
stats.totalFiles++;
stats.numFilesSkipped++;
stats.filesSkipped.push(filePath);
return;
}
try {
// Check cache for file content
const fileContentCacheKey = `content_${owner}_${repo}_${file.sha}`;
let fileContent = await cacheManager.get(fileContentCacheKey);
if (!fileContent) {
fileContent = await getFileContent(owner, repo, file.sha);
await cacheManager.set(fileContentCacheKey, fileContent);
}
const lines = fileContent.split("\n").length;
stats.totalLines += lines;
stats.totalFiles++;
if (!stats.byExtension[fileExtension]) {
stats.byExtension[fileExtension] = {
files: 0,
lines: 0,
};
}
// Update stats for the file extension
stats.byExtension[fileExtension].files++;
stats.byExtension[fileExtension].lines += lines;
} catch (error) {
console.warn(`Could not process file ${filePath}: ${error.message}`);
stats.totalFiles++;
stats.numFilesSkipped++;
stats.filesSkipped.push(filePath);
}
}
async function getFileContent(owner, repo, sha) {
const url = `https://api.github.com/repos/${owner}/${repo}/git/blobs/${sha}`;
const response = await fetch(url, { headers: getHeaders() });
if (!response.ok) {
// Handle rate limiting
if (
response.status === 403 &&
response.headers.get("X-RateLimit-Remaining") === "0"
) {
const resetTime = new Date(
parseInt(response.headers.get("X-RateLimit-Reset")) * 1000
);
throw new Error(
`GitHub API rate limit exceeded. Resets at ${resetTime.toLocaleTimeString()}`
);
}
throw new Error(`Error fetching file content: ${response.statusText}`);
}
const data = await response.json();
const decodedContent = atob(data.content); // decode base64 content
return decodedContent;
}
// Main execution function with authentication option
function analyzeRepository(owner, repo, token = null) {
if (token) {
setGitHubToken(token);
}
fetchGithubRepoData(owner, repo)
.then(() => {
countLinesOfCode(owner, repo);
})
.catch((error) => {
showError(`Error fetching repository data: ${error.message}`);
});
}
// UI updates
function updateProgress(processedFiles, totalFiles) {
const progressBar = document.getElementById("progress-bar");
const progressText = document.getElementById("progress-text");
if (progressBar && progressText) {
const percentage = Math.round((processedFiles / totalFiles) * 100);
progressBar.style.width = `${percentage}%`;
progressText.textContent = `${percentage}% (${processedFiles}/${totalFiles} files)`;
}
}
function displayStats(stats) {
// Get UI elements
const resultsDiv = document.getElementById("results");
const loadingDiv = document.getElementById("loading");
const totalLinesSpan = document.getElementById("totalLines");
const totalFilesSpan = document.getElementById("totalFiles");
const totalSkippedFilesSpan = document.getElementById("totalSkippedFiles");
const skippedFilesList = document.getElementById("skippedFiles");
const extensionsDiv = document.getElementById("extensions");
// Update UI with stats
if (totalLinesSpan)
totalLinesSpan.textContent = stats.totalLines.toLocaleString();
if (totalFilesSpan)
totalFilesSpan.textContent = stats.totalFiles.toLocaleString();
if (totalSkippedFilesSpan)
totalSkippedFilesSpan.textContent = stats.numFilesSkipped.toLocaleString();
// Update skipped files list
if (skippedFilesList) {
skippedFilesList.innerHTML = "";
if (stats.filesSkipped.length > 0) {
// Only show first 10 skipped files to avoid overflow
const filesToShow = stats.filesSkipped.slice(0, 10);
filesToShow.forEach((file) => {
const li = document.createElement("li");
li.textContent = file;
skippedFilesList.appendChild(li);
});
// Add indicator if there are more skipped files
if (stats.filesSkipped.length > 10) {
const li = document.createElement("li");
li.textContent = `...and ${stats.filesSkipped.length - 10} more`;
skippedFilesList.appendChild(li);
}
} else {
const li = document.createElement("li");
li.textContent = "None";
skippedFilesList.appendChild(li);
}
}
// Update extensions breakdown
if (extensionsDiv) {
extensionsDiv.innerHTML = "";
// Sort extensions by number of lines
const sortedExtensions = Object.entries(stats.byExtension).sort(
(a, b) => b[1].lines - a[1].lines
);
sortedExtensions.forEach(([ext, data]) => {
const percentage = ((data.lines / stats.totalLines) * 100).toFixed(1);
const row = document.createElement("div");
row.className = "extension-row";
const fileText = data.files === 1 ? "file" : "files";
row.innerHTML = `
<span>${ext}</span>
<span>${data.files.toLocaleString()} ${fileText}, ${data.lines.toLocaleString()} lines (${percentage}%)</span>
`;
extensionsDiv.appendChild(row);
});
}
// Show results and hide loading
if (loadingDiv) loadingDiv.style.display = "none";
if (resultsDiv) resultsDiv.style.display = "block";
}
document.addEventListener("DOMContentLoaded", () => {
// Get UI elements for token management
const tokenInput = document.getElementById("githubToken");
const saveTokenButton = document.getElementById("saveToken");
const button = document.getElementById("countLoc");
const currentRepoDiv = document.getElementById("currentRepo");
const loadingDiv = document.getElementById("loading");
const resultsDiv = document.getElementById("results");
const errorDiv = document.getElementById("error");
const exclusionInput = document.getElementById("exclusionInput");
const addExclusionButton = document.getElementById("addExclusion");
const exclusionsList =
document.getElementById("exclusionsList") || createExclusionsList();
// Add token help link handler
const tokenHelpLink = document.getElementById("tokenHelpLink");
if (tokenHelpLink) {
tokenHelpLink.addEventListener("click", (e) => {
e.preventDefault();
// Open GitHub token creation page in a new tab
chrome.tabs.create({
url: "https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token",
});
});
}
// Initialize exclusions array from storage
let fileExclusions = [];
// Load saved exclusions when popup opens
chrome.storage.local.get("fileExclusions", (result) => {
if (result.fileExclusions) {
fileExclusions = result.fileExclusions;
renderExclusions();
}
});
// Add exclusion when button is clicked
if (addExclusionButton) {
addExclusionButton.addEventListener("click", () => {
if (exclusionInput && exclusionInput.value.trim()) {
const extension = exclusionInput.value.trim().toLowerCase();
// Remove dot if user included it
const cleanExtension = extension.startsWith(".")
? extension.substring(1)
: extension;
if (!fileExclusions.includes(cleanExtension)) {
fileExclusions.push(cleanExtension); // Add new exclusion if not already present
saveExclusions();
renderExclusions();
}
exclusionInput.value = "";
}
});
}
// Create exclusions list element if it doesn't exist
function createExclusionsList() {
const container = document.getElementById("exclusions-section");
if (container) {
const list = document.createElement("ul");
list.id = "exclusionsList";
container.appendChild(list);
return list;
}
return null;
}
// Save exclusions to storage
function saveExclusions() {
chrome.storage.local.set({ fileExclusions });
}
// Render the list of exclusions
function renderExclusions() {
if (!exclusionsList) return;
exclusionsList.innerHTML = "";
if (fileExclusions.length === 0) {
const emptyItem = document.createElement("li");
emptyItem.textContent = "No exclusions added";
exclusionsList.appendChild(emptyItem);
} else {
fileExclusions.forEach((extension) => {
const item = document.createElement("li");
item.textContent = `.${extension}`;
// Add remove button
const removeBtn = document.createElement("button");
removeBtn.textContent = "×";
removeBtn.className = "remove-exclusion";
removeBtn.addEventListener("click", () => {
fileExclusions = fileExclusions.filter((ext) => ext !== extension);
saveExclusions();
renderExclusions();
});
item.appendChild(removeBtn);
exclusionsList.appendChild(item);
});
}
}
// Initially hide results and loading
if (loadingDiv) loadingDiv.style.display = "none";
if (resultsDiv) resultsDiv.style.display = "none";
if (errorDiv) errorDiv.style.display = "none";
updateRateLimitDisplay();
// Load token from storage and update API rate limit display
loadToken().then(() => {
// Initial rate limit check after token is loaded
updateRateLimitDisplay();
});
initializeAuthChecks(); // Initialize authentication checks
// Add token save button handler
if (saveTokenButton) {
saveTokenButton.addEventListener("click", () => {
const token = tokenInput.value.trim();
if (token) {
saveToken(token);
tokenInput.value = ""; // Clear input field after saving
} else {
clearToken();
}
});
}
// Check if we're on a GitHub repository page
const detectGitHubRepo = async () => {
try {
// Get current active tab
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
// Extract owner/repo from GitHub URL using regex
const match = tab.url.match(/github\.com\/([^\/]+)\/([^\/]+)/);
if (match) {
const [_, owner, repo] = match;
// Clean up repo name (remove any trailing slashes or query params)
const cleanRepo = repo.split(/[\/\?#]/)[0];
console.log(`Detected repo: ${owner}/${cleanRepo}`); // Debug log
// Update UI
if (currentRepoDiv)
currentRepoDiv.textContent = `Repository: ${owner}/${cleanRepo}`;
if (button) {
button.disabled = false;
button.replaceWith(button.cloneNode(true)); // Clone the button to remove old event listeners
// Reassign the button to the new cloned button
const newButton = document.getElementById("countLoc");
// Add click handler
newButton.addEventListener("click", () => {
// Show loading state
if (loadingDiv) loadingDiv.style.display = "block";
if (resultsDiv) resultsDiv.style.display = "none";
if (errorDiv) errorDiv.style.display = "none";
// Analyze the repository
analyzeRepository(owner, cleanRepo);
});
}
} else {
if (currentRepoDiv)
currentRepoDiv.textContent = "No GitHub repository detected";
if (button) button.disabled = true;
}
} catch (error) {
if (errorDiv) {
errorDiv.style.display = "block";
errorDiv.textContent = `Error: ${error.message}`;
}
}
};
// Start the detection process
detectGitHubRepo();
});