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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.getElementById('repoForm').addEventListener('submit', async function (e) {
|
| 2 |
+
e.preventDefault();
|
| 3 |
+
const repoUrl = document.getElementById('repoUrl').value;
|
| 4 |
+
const ref = document.getElementById('ref').value || '';
|
| 5 |
+
const path = document.getElementById('path').value || '';
|
| 6 |
+
const accessToken = document.getElementById('accessToken').value;
|
| 7 |
+
|
| 8 |
+
const outputText = document.getElementById('outputText');
|
| 9 |
+
outputText.value = '';
|
| 10 |
+
|
| 11 |
+
try {
|
| 12 |
+
const { owner, repo, refFromUrl, pathFromUrl } = parseRepoUrl(repoUrl);
|
| 13 |
+
const finalRef = ref || refFromUrl;
|
| 14 |
+
const finalPath = path || pathFromUrl;
|
| 15 |
+
|
| 16 |
+
const sha = await fetchRepoSha(owner, repo, finalRef, finalPath, accessToken);
|
| 17 |
+
const tree = await fetchRepoTree(owner, repo, sha, accessToken);
|
| 18 |
+
|
| 19 |
+
displayDirectoryStructure(tree);
|
| 20 |
+
document.getElementById('generateTextButton').style.display = 'flex';
|
| 21 |
+
} catch (error) {
|
| 22 |
+
outputText.value = `Error fetching repository contents: ${error.message}\n\nPlease ensure:\n1. The repository URL is correct and accessible.\n2. You have the necessary permissions to access the repository.\n3. If it's a private repository, you've provided a valid access token.\n4. The specified branch/tag and path (if any) exist in the repository.`;
|
| 23 |
+
}
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
document.getElementById('generateTextButton').addEventListener('click', async function () {
|
| 27 |
+
const accessToken = document.getElementById('accessToken').value;
|
| 28 |
+
const outputText = document.getElementById('outputText');
|
| 29 |
+
outputText.value = '';
|
| 30 |
+
|
| 31 |
+
try {
|
| 32 |
+
const selectedFiles = getSelectedFiles();
|
| 33 |
+
if (selectedFiles.length === 0) {
|
| 34 |
+
throw new Error('No files selected');
|
| 35 |
+
}
|
| 36 |
+
const fileContents = await fetchFileContents(selectedFiles, accessToken);
|
| 37 |
+
const formattedText = formatRepoContents(fileContents);
|
| 38 |
+
outputText.value = formattedText;
|
| 39 |
+
|
| 40 |
+
document.getElementById('copyButton').style.display = 'flex';
|
| 41 |
+
document.getElementById('downloadButton').style.display = 'flex';
|
| 42 |
+
} catch (error) {
|
| 43 |
+
outputText.value = `Error generating text file: ${error.message}\n\nPlease ensure:\n1. You have selected at least one file from the directory structure.\n2. Your access token (if provided) is valid and has the necessary permissions.\n3. You have a stable internet connection.\n4. The GitHub API is accessible and functioning normally.`;
|
| 44 |
+
}
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
document.getElementById('copyButton').addEventListener('click', function () {
|
| 48 |
+
const outputText = document.getElementById('outputText');
|
| 49 |
+
outputText.select();
|
| 50 |
+
navigator.clipboard.writeText(outputText.value).then(() => {
|
| 51 |
+
console.log('Text copied to clipboard');
|
| 52 |
+
}).catch(err => {
|
| 53 |
+
console.error('Failed to copy text: ', err);
|
| 54 |
+
});
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
+
document.getElementById('downloadButton').addEventListener('click', function () {
|
| 58 |
+
const outputText = document.getElementById('outputText').value;
|
| 59 |
+
if (!outputText.trim()) {
|
| 60 |
+
document.getElementById('outputText').value = 'Error: No content to download. Please generate the text file first.';
|
| 61 |
+
return;
|
| 62 |
+
}
|
| 63 |
+
const blob = new Blob([outputText], { type: 'text/plain' });
|
| 64 |
+
const url = URL.createObjectURL(blob);
|
| 65 |
+
const a = document.createElement('a');
|
| 66 |
+
a.href = url;
|
| 67 |
+
a.download = 'file.txt';
|
| 68 |
+
a.click();
|
| 69 |
+
URL.revokeObjectURL(url);
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
function parseRepoUrl(url) {
|
| 73 |
+
url = url.replace(/\/$/, '');
|
| 74 |
+
const urlPattern = /^https:\/\/github\.com\/([^\/]+)\/([^\/]+)(\/tree\/([^\/]+)(\/(.+))?)?$/;
|
| 75 |
+
const match = url.match(urlPattern);
|
| 76 |
+
if (!match) {
|
| 77 |
+
throw new Error('Invalid GitHub repository URL. Please ensure the URL is in the correct format: https://github.com/owner/repo or https://github.com/owner/repo/tree/branch/path');
|
| 78 |
+
}
|
| 79 |
+
return {
|
| 80 |
+
owner: match[1],
|
| 81 |
+
repo: match[2],
|
| 82 |
+
refFromUrl: match[4],
|
| 83 |
+
pathFromUrl: match[6]
|
| 84 |
+
};
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
async function fetchRepoSha(owner, repo, ref, path, token) {
|
| 88 |
+
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${path ? `${path}` : ''}${ref ? `?ref=${ref}` : ''}`;
|
| 89 |
+
const headers = {
|
| 90 |
+
'Accept': 'application/vnd.github.object+json'
|
| 91 |
+
};
|
| 92 |
+
if (token) {
|
| 93 |
+
headers['Authorization'] = `token ${token}`;
|
| 94 |
+
}
|
| 95 |
+
const response = await fetch(url, { headers });
|
| 96 |
+
if (!response.ok) {
|
| 97 |
+
if (response.status === 403 && response.headers.get('X-RateLimit-Remaining') === '0') {
|
| 98 |
+
throw new Error('GitHub API rate limit exceeded. Please try again later or provide a valid access token to increase your rate limit.');
|
| 99 |
+
}
|
| 100 |
+
if (response.status === 404) {
|
| 101 |
+
throw new Error(`Repository, branch, or path not found. Please check that the URL, branch/tag, and path are correct and accessible.`);
|
| 102 |
+
}
|
| 103 |
+
throw new Error(`Failed to fetch repository SHA. Status: ${response.status}. Please check your input and try again.`);
|
| 104 |
+
}
|
| 105 |
+
const data = await response.json();
|
| 106 |
+
return data.sha;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
async function fetchRepoTree(owner, repo, sha, token) {
|
| 110 |
+
const url = `https://api.github.com/repos/${owner}/${repo}/git/trees/${sha}?recursive=1`;
|
| 111 |
+
const headers = {
|
| 112 |
+
'Accept': 'application/vnd.github+json'
|
| 113 |
+
};
|
| 114 |
+
if (token) {
|
| 115 |
+
headers['Authorization'] = `token ${token}`;
|
| 116 |
+
}
|
| 117 |
+
const response = await fetch(url, { headers });
|
| 118 |
+
if (!response.ok) {
|
| 119 |
+
if (response.status === 403 && response.headers.get('X-RateLimit-Remaining') === '0') {
|
| 120 |
+
throw new Error('GitHub API rate limit exceeded. Please try again later or provide a valid access token to increase your rate limit.');
|
| 121 |
+
}
|
| 122 |
+
throw new Error(`Failed to fetch repository tree. Status: ${response.status}. Please check your input and try again.`);
|
| 123 |
+
}
|
| 124 |
+
const data = await response.json();
|
| 125 |
+
return data.tree;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
function displayDirectoryStructure(tree) {
|
| 129 |
+
tree = tree.filter(item => item.type === 'blob');
|
| 130 |
+
tree = sortContents(tree);
|
| 131 |
+
const container = document.getElementById('directoryStructure');
|
| 132 |
+
container.innerHTML = '';
|
| 133 |
+
const rootUl = document.createElement('ul');
|
| 134 |
+
container.appendChild(rootUl);
|
| 135 |
+
|
| 136 |
+
const directoryStructure = {};
|
| 137 |
+
|
| 138 |
+
tree.forEach(item => {
|
| 139 |
+
item.path = item.path.startsWith('/') ? item.path : '/' + item.path;
|
| 140 |
+
const pathParts = item.path.split('/');
|
| 141 |
+
let currentLevel = directoryStructure;
|
| 142 |
+
|
| 143 |
+
pathParts.forEach((part, index) => {
|
| 144 |
+
if (part === '') {
|
| 145 |
+
part = './';
|
| 146 |
+
}
|
| 147 |
+
if (!currentLevel[part]) {
|
| 148 |
+
currentLevel[part] = index === pathParts.length - 1 ? item : {};
|
| 149 |
+
}
|
| 150 |
+
currentLevel = currentLevel[part];
|
| 151 |
+
});
|
| 152 |
+
});
|
| 153 |
+
|
| 154 |
+
function createTreeNode(name, item, parentUl) {
|
| 155 |
+
const li = document.createElement('li');
|
| 156 |
+
const checkbox = document.createElement('input');
|
| 157 |
+
checkbox.type = 'checkbox';
|
| 158 |
+
const commonExtensions = ['.js', '.py', '.java', '.cpp', '.html', '.css', '.ts', '.jsx', '.tsx'];
|
| 159 |
+
const fileName = name.toLowerCase();
|
| 160 |
+
const isCommonFile = commonExtensions.some(ext => fileName.endsWith(ext));
|
| 161 |
+
checkbox.checked = isCommonFile;
|
| 162 |
+
checkbox.className = 'mr-2';
|
| 163 |
+
|
| 164 |
+
if (typeof item === 'object' && (!item.type || typeof item.type !== 'string')) {
|
| 165 |
+
// Directory
|
| 166 |
+
checkbox.classList.add('directory-checkbox');
|
| 167 |
+
li.appendChild(checkbox);
|
| 168 |
+
|
| 169 |
+
// Add collapse/expand button
|
| 170 |
+
const collapseButton = document.createElement('button');
|
| 171 |
+
collapseButton.innerHTML = '<i data-lucide="chevron-down" class="w-4 h-4"></i>';
|
| 172 |
+
collapseButton.className = 'mr-1 focus:outline-none';
|
| 173 |
+
li.appendChild(collapseButton);
|
| 174 |
+
|
| 175 |
+
const folderIcon = document.createElement('i');
|
| 176 |
+
folderIcon.setAttribute('data-lucide', 'folder');
|
| 177 |
+
folderIcon.className = 'inline-block w-4 h-4 mr-1';
|
| 178 |
+
li.appendChild(folderIcon);
|
| 179 |
+
li.appendChild(document.createTextNode(name));
|
| 180 |
+
const ul = document.createElement('ul');
|
| 181 |
+
ul.className = 'ml-6 mt-2';
|
| 182 |
+
li.appendChild(ul);
|
| 183 |
+
|
| 184 |
+
for (const [childName, childItem] of Object.entries(item)) {
|
| 185 |
+
createTreeNode(childName, childItem, ul);
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
checkbox.addEventListener('change', function() {
|
| 189 |
+
const childCheckboxes = li.querySelectorAll('input[type="checkbox"]');
|
| 190 |
+
childCheckboxes.forEach(childBox => {
|
| 191 |
+
childBox.checked = this.checked;
|
| 192 |
+
childBox.indeterminate = false;
|
| 193 |
+
});
|
| 194 |
+
});
|
| 195 |
+
|
| 196 |
+
// Add collapse/expand functionality
|
| 197 |
+
collapseButton.addEventListener('click', function() {
|
| 198 |
+
ul.classList.toggle('hidden');
|
| 199 |
+
const icon = this.querySelector('[data-lucide]');
|
| 200 |
+
if (ul.classList.contains('hidden')) {
|
| 201 |
+
icon.setAttribute('data-lucide', 'chevron-right');
|
| 202 |
+
} else {
|
| 203 |
+
icon.setAttribute('data-lucide', 'chevron-down');
|
| 204 |
+
}
|
| 205 |
+
lucide.createIcons();
|
| 206 |
+
});
|
| 207 |
+
} else {
|
| 208 |
+
// File
|
| 209 |
+
checkbox.value = JSON.stringify({ url: item.url, path: item.path });
|
| 210 |
+
li.appendChild(checkbox);
|
| 211 |
+
const fileIcon = document.createElement('i');
|
| 212 |
+
fileIcon.setAttribute('data-lucide', 'file');
|
| 213 |
+
fileIcon.className = 'inline-block w-4 h-4 mr-1';
|
| 214 |
+
li.appendChild(fileIcon);
|
| 215 |
+
li.appendChild(document.createTextNode(name));
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
li.className = 'my-2';
|
| 219 |
+
parentUl.appendChild(li);
|
| 220 |
+
updateParentCheckbox(checkbox);
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
for (const [name, item] of Object.entries(directoryStructure)) {
|
| 224 |
+
createTreeNode(name, item, rootUl);
|
| 225 |
+
}
|
| 226 |
+
// Add event listener to container for checkbox changes
|
| 227 |
+
container.addEventListener('change', function(event) {
|
| 228 |
+
if (event.target.type === 'checkbox') {
|
| 229 |
+
updateParentCheckbox(event.target);
|
| 230 |
+
}
|
| 231 |
+
});
|
| 232 |
+
|
| 233 |
+
function updateParentCheckbox(checkbox) {
|
| 234 |
+
if (!checkbox) return;
|
| 235 |
+
const li = checkbox.closest('li');
|
| 236 |
+
if (!li) return;
|
| 237 |
+
if (!li.parentElement) return;
|
| 238 |
+
const parentLi = li.parentElement.closest('li');
|
| 239 |
+
if (!parentLi) return;
|
| 240 |
+
|
| 241 |
+
const parentCheckbox = parentLi.querySelector(':scope > input[type="checkbox"]');
|
| 242 |
+
const siblingCheckboxes = parentLi.querySelectorAll(':scope > ul > li > input[type="checkbox"]');
|
| 243 |
+
|
| 244 |
+
const checkedCount = Array.from(siblingCheckboxes).filter(cb => cb.checked).length;
|
| 245 |
+
const indeterminateCount = Array.from(siblingCheckboxes).filter(cb => cb.indeterminate).length;
|
| 246 |
+
|
| 247 |
+
if (indeterminateCount !== 0) {
|
| 248 |
+
parentCheckbox.checked = false;
|
| 249 |
+
parentCheckbox.indeterminate = true;
|
| 250 |
+
} else if (checkedCount === 0) {
|
| 251 |
+
parentCheckbox.checked = false;
|
| 252 |
+
parentCheckbox.indeterminate = false;
|
| 253 |
+
} else if (checkedCount === siblingCheckboxes.length) {
|
| 254 |
+
parentCheckbox.checked = true;
|
| 255 |
+
parentCheckbox.indeterminate = false;
|
| 256 |
+
} else {
|
| 257 |
+
parentCheckbox.checked = false;
|
| 258 |
+
parentCheckbox.indeterminate = true;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
// Recursively update parent checkboxes
|
| 262 |
+
updateParentCheckbox(parentCheckbox);
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
lucide.createIcons();
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
function getSelectedFiles() {
|
| 269 |
+
const checkboxes = document.querySelectorAll('#directoryStructure input[type="checkbox"]:checked:not(.directory-checkbox)');
|
| 270 |
+
return Array.from(checkboxes).map(checkbox => JSON.parse(checkbox.value));
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
async function fetchFileContents(files, token) {
|
| 274 |
+
const headers = {
|
| 275 |
+
'Accept': 'application/vnd.github.v3.raw'
|
| 276 |
+
};
|
| 277 |
+
if (token) {
|
| 278 |
+
headers['Authorization'] = `token ${token}`;
|
| 279 |
+
}
|
| 280 |
+
const contents = await Promise.all(files.map(async file => {
|
| 281 |
+
const response = await fetch(file.url, { headers });
|
| 282 |
+
if (!response.ok) {
|
| 283 |
+
if (response.status === 403 && response.headers.get('X-RateLimit-Remaining') === '0') {
|
| 284 |
+
throw new Error(`GitHub API rate limit exceeded while fetching ${file.path}. Please try again later or provide a valid access token to increase your rate limit.`);
|
| 285 |
+
}
|
| 286 |
+
throw new Error(`Failed to fetch content for ${file.path}. Status: ${response.status}. Please check your permissions and try again.`);
|
| 287 |
+
}
|
| 288 |
+
const text = await response.text();
|
| 289 |
+
return { url: file.url, path: file.path, text };
|
| 290 |
+
}));
|
| 291 |
+
return contents;
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
function formatRepoContents(contents) {
|
| 295 |
+
let text = '';
|
| 296 |
+
let index = '';
|
| 297 |
+
|
| 298 |
+
contents = sortContents(contents);
|
| 299 |
+
|
| 300 |
+
// Create a directory tree structure
|
| 301 |
+
const tree = {};
|
| 302 |
+
contents.forEach(item => {
|
| 303 |
+
const parts = item.path.split('/');
|
| 304 |
+
let currentLevel = tree;
|
| 305 |
+
parts.forEach((part, i) => {
|
| 306 |
+
if (!currentLevel[part]) {
|
| 307 |
+
currentLevel[part] = i === parts.length - 1 ? null : {};
|
| 308 |
+
}
|
| 309 |
+
currentLevel = currentLevel[part];
|
| 310 |
+
});
|
| 311 |
+
});
|
| 312 |
+
|
| 313 |
+
// Function to recursively build the index
|
| 314 |
+
function buildIndex(node, prefix = '') {
|
| 315 |
+
let result = '';
|
| 316 |
+
const entries = Object.entries(node);
|
| 317 |
+
entries.forEach(([name, subNode], index) => {
|
| 318 |
+
const isLastItem = index === entries.length - 1;
|
| 319 |
+
const linePrefix = isLastItem ? 'βββ ' : 'βββ ';
|
| 320 |
+
const childPrefix = isLastItem ? ' ' : 'β ';
|
| 321 |
+
|
| 322 |
+
if (name === '') {
|
| 323 |
+
name = './';
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
result += `${prefix}${linePrefix}${name}\n`;
|
| 327 |
+
if (subNode) {
|
| 328 |
+
result += buildIndex(subNode, `${prefix}${childPrefix}`);
|
| 329 |
+
}
|
| 330 |
+
});
|
| 331 |
+
return result;
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
index = buildIndex(tree);
|
| 335 |
+
|
| 336 |
+
contents.forEach((item) => {
|
| 337 |
+
text += `\n\n---\nFile: ${item.path}\n---\n\n${item.text}\n`;
|
| 338 |
+
});
|
| 339 |
+
|
| 340 |
+
return `Directory Structure:\n\n${index}\n${text}`;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
function sortContents(contents) {
|
| 344 |
+
contents.sort((a, b) => {
|
| 345 |
+
const aPath = a.path.split('/');
|
| 346 |
+
const bPath = b.path.split('/');
|
| 347 |
+
const minLength = Math.min(aPath.length, bPath.length);
|
| 348 |
+
|
| 349 |
+
for (let i = 0; i < minLength; i++) {
|
| 350 |
+
if (aPath[i] !== bPath[i]) {
|
| 351 |
+
if (i === aPath.length - 1 && i < bPath.length - 1) return 1; // a is a directory, b is a file or subdirectory
|
| 352 |
+
if (i === bPath.length - 1 && i < aPath.length - 1) return -1; // b is a directory, a is a file or subdirectory
|
| 353 |
+
return aPath[i].localeCompare(bPath[i]);
|
| 354 |
+
}
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
return aPath.length - bPath.length;
|
| 358 |
+
});
|
| 359 |
+
return contents;
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 363 |
+
lucide.createIcons();
|
| 364 |
+
|
| 365 |
+
// Add event listener for the showMoreInfo button
|
| 366 |
+
const showMoreInfoButton = document.getElementById('showMoreInfo');
|
| 367 |
+
const tokenInfo = document.getElementById('tokenInfo');
|
| 368 |
+
|
| 369 |
+
showMoreInfoButton.addEventListener('click', function() {
|
| 370 |
+
tokenInfo.classList.toggle('hidden');
|
| 371 |
+
|
| 372 |
+
// Change the icon based on the visibility state
|
| 373 |
+
const icon = this.querySelector('[data-lucide]');
|
| 374 |
+
if (icon) {
|
| 375 |
+
if (tokenInfo.classList.contains('hidden')) {
|
| 376 |
+
icon.setAttribute('data-lucide', 'info');
|
| 377 |
+
} else {
|
| 378 |
+
icon.setAttribute('data-lucide', 'x');
|
| 379 |
+
}
|
| 380 |
+
lucide.createIcons();
|
| 381 |
+
}
|
| 382 |
+
});
|
| 383 |
+
});
|