-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.js
150 lines (129 loc) · 5.48 KB
/
fetcher.js
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
import { Octokit } from "https://cdn.skypack.dev/@octokit/rest";
document.addEventListener("DOMContentLoaded", () => {
const repoInput = document.getElementById("repoInput");
const tokenInput = document.getElementById("tokenInput");
const fetchBtn = document.getElementById("fetchBtn");
const statusLog = document.getElementById("statusLog");
const statusOutput = document.getElementById("statusOutput");
const fileListElement = document.getElementById("fileList");
const fileListWrapper = document.getElementById("fileListWrapper");
const toggleTokenBtn = document.getElementById("toggleTokenBtn");
const tokenInfo = document.getElementById("tokenInfo");
let fetchedFiles = [];
let progressPercentage = 0;
const updateStatus = (message) => {
statusLog.textContent = message;
statusOutput.style.display = "block";
};
const updateProgress = (percent) => {
progressPercentage = percent;
statusLog.style.background = `linear-gradient(to right, rgba(100, 255, 218, 0.3) ${percent}%, transparent ${percent}%)`;
statusLog.innerHTML = `${statusLog.textContent} <span style="color: #64FFDA;">${percent}% Complete</span>`;
};
const fetchFolderContents = async (octokit, owner, repo, path) => {
const response = await octokit.rest.repos.getContent({
owner,
repo,
path,
});
return response.data;
};
const fetchFileContent = async (octokit, owner, repo, path, fileName, totalSize) => {
const response = await octokit.rest.repos.getContent({
owner,
repo,
path,
mediaType: {
format: "raw",
},
});
updateStatus(`Fetching ${fileName}`);
updateProgress(100);
return response.data;
};
const processFolderContents = async (octokit, owner, repo, folder, zip, path = "") => {
for (const item of folder) {
if (item.type === "file") {
updateStatus(`Queueing fetch of ${item.name}`);
const fileContent = await fetchFileContent(octokit, owner, repo, item.path, item.name, item.size);
fetchedFiles.push(item);
updateFileList();
zip.file(item.name, fileContent);
} else if (item.type === "dir") {
const subFolder = await fetchFolderContents(octokit, owner, repo, item.path);
const subFolderZip = zip.folder(item.name);
await processFolderContents(octokit, owner, repo, subFolder, subFolderZip, item.path);
}
}
};
const fetchRepositoryContents = async (octokit, owner, repo, path) => {
try {
const folderData = await fetchFolderContents(octokit, owner, repo, path);
const zip = new JSZip();
await processFolderContents(octokit, owner, repo, folderData, zip, path);
return zip;
} catch (error) {
console.error(`Error: ${error}`);
throw error;
}
};
const updateFileList = () => {
fileListElement.innerHTML = "";
fetchedFiles.forEach((file) => {
const li = document.createElement("li");
li.textContent = file.name;
fileListElement.appendChild(li);
});
fileListWrapper.style.display = "block";
fileListElement.scrollTop = fileListElement.scrollHeight;
};
const fetchRepo = async () => {
updateStatus("");
fetchedFiles = [];
fileListWrapper.style.display = "none";
const repoUrl = repoInput.value;
const token = tokenInput.value.trim();
if (!repoUrl.includes("github.com")) {
updateStatus("Invalid URL. Please enter a valid GitHub repository URL.");
return;
}
const [, , , owner, repo, , , ...dirParts] = repoUrl.split("/");
const path = dirParts.join("/");
const octokit = new Octokit({ auth: token });
updateStatus("Fetching repository contents...");
try {
const zip = await fetchRepositoryContents(octokit, owner, repo, path);
updateStatus("Compressing files...");
const content = await zip.generateAsync({ type: "blob" });
saveAs(content, `${path ? path.replace(/\/|%20/g, "-") : repo}.zip`);
const fileList = zip.file(/.*/);
updateProgress(0);
updateStatus(
`Fetched ${fileList.length} files\nUser: ${owner}\nRepository: ${repoUrl}\nFolder: ${path}\nSize: ${(
content.size /
1024 /
1024
).toFixed(2)} MB`
);
} catch (error) {
if (error.status === 403) {
updateStatus("Rate limit exceeded. Please try again in a few minutes or use a GitHub Personal Access Token.");
} else {
updateStatus(`Error: ${error.message}`);
}
}
};
fetchBtn.addEventListener("click", fetchRepo);
repoInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
fetchRepo();
}
});
toggleTokenBtn.addEventListener("click", () => {
const isHidden = tokenInput.style.display === "none";
tokenInput.style.display = isHidden ? "block" : "none";
tokenInfo.style.display = isHidden ? "block" : "none";
toggleTokenBtn.textContent = isHidden ? "Hide Token" : "Use Token";
});
repoInput.focus();
});