Spaces:
Running
Running
File size: 15,110 Bytes
69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 99e84c5 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 99e84c5 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 f158af3 69fac21 |
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 |
import { displayDirectoryStructure, getSelectedFiles, formatRepoContents } from './utils.js';
let currentRepoInfo = {};
// Load saved token on page load
document.addEventListener('DOMContentLoaded', function() {
lucide.createIcons();
setupShowMoreInfoButton();
loadSavedToken();
});
// Load saved token from local storage
function loadSavedToken() {
const savedToken = localStorage.getItem('githubAccessToken');
if (savedToken) {
document.getElementById('accessToken').value = savedToken;
}
}
// Save token to local storage
function saveToken(token) {
if (token) {
localStorage.setItem('githubAccessToken', token);
} else {
localStorage.removeItem('githubAccessToken');
}
}
// Event listener for form submission
document.getElementById('repoForm').addEventListener('submit', async function (e) {
e.preventDefault();
const repoUrl = document.getElementById('repoUrl').value;
const accessToken = document.getElementById('accessToken').value;
saveToken(accessToken);
const outputText = document.getElementById('outputText');
outputText.value = 'Fetching repository structure...';
document.getElementById('directoryStructure').innerHTML = '';
document.getElementById('generateTextButton').style.display = 'none';
document.getElementById('downloadZipButton').style.display = 'none';
document.getElementById('copyButton').style.display = 'none';
document.getElementById('downloadButton').style.display = 'none';
try {
const repoInfo = parseRepoUrl(repoUrl);
currentRepoInfo = { ...repoInfo, accessToken }; // Store for later use
let tree;
if (repoInfo.source === 'github') {
const { owner, repo, lastString } = repoInfo;
let refFromUrl = '';
let pathFromUrl = '';
if (lastString) {
const references = await getGitHubReferences(owner, repo, accessToken);
const allRefs = [...references.branches, ...references.tags];
const matchingRef = allRefs.find(ref => lastString.startsWith(ref));
if (matchingRef) {
refFromUrl = matchingRef;
pathFromUrl = lastString.slice(matchingRef.length + 1);
} else {
refFromUrl = lastString;
}
}
const sha = await fetchRepoSha(owner, repo, refFromUrl, pathFromUrl, accessToken);
tree = await fetchGitHubRepoTree(owner, repo, sha, accessToken);
} else if (repoInfo.source === 'huggingface') {
const { owner, repo, repo_type, lastString } = repoInfo;
let refFromUrl = 'main'; // Default branch
let pathFromUrl = '';
if (lastString) {
const refs = await getHuggingFaceReferences(owner, repo, repo_type, accessToken);
const matchingRef = refs.find(ref => lastString.startsWith(ref + '/'));
if (matchingRef) {
refFromUrl = matchingRef;
pathFromUrl = lastString.slice(matchingRef.length + 1);
} else {
refFromUrl = lastString.split('/')[0];
pathFromUrl = lastString.substring(refFromUrl.length + 1);
}
}
currentRepoInfo.ref = refFromUrl;
tree = await fetchHuggingFaceTree(owner, repo, repo_type, refFromUrl, pathFromUrl, accessToken);
}
displayDirectoryStructure(tree);
document.getElementById('generateTextButton').style.display = 'flex';
document.getElementById('downloadZipButton').style.display = 'flex';
outputText.value = 'Select files and click "Generate Text File" or "Download Zip".';
} catch (error) {
outputText.value = `Error fetching repository contents: ${error.message}\n\n` +
"Please ensure:\n" +
"1. The repository URL is correct and accessible.\n" +
"2. You have the necessary permissions to access the repository.\n" +
"3. If it's a private repository, you've provided a valid access token.\n" +
"4. The specified branch/tag and path (if any) exist in the repository.";
}
});
// Event listener for generating text file
document.getElementById('generateTextButton').addEventListener('click', async function () {
const accessToken = document.getElementById('accessToken').value;
const outputText = document.getElementById('outputText');
outputText.value = 'Generating text file...';
saveToken(accessToken);
try {
const selectedFiles = getSelectedFiles();
if (selectedFiles.length === 0) {
throw new Error('No files selected');
}
const fileContents = await fetchFileContents(selectedFiles, accessToken, currentRepoInfo.source);
const formattedText = formatRepoContents(fileContents);
outputText.value = formattedText;
document.getElementById('copyButton').style.display = 'flex';
document.getElementById('downloadButton').style.display = 'flex';
} catch (error) {
outputText.value = `Error generating text file: ${error.message}\n\n` +
"Please ensure:\n" +
"1. You have selected at least one file from the directory structure.\n" +
"2. Your access token (if provided) is valid and has the necessary permissions.\n" +
"3. You have a stable internet connection.";
}
});
// Event listener for downloading zip file
document.getElementById('downloadZipButton').addEventListener('click', async function () {
const accessToken = document.getElementById('accessToken').value;
try {
const selectedFiles = getSelectedFiles();
if (selectedFiles.length === 0) {
throw new Error('No files selected');
}
const fileContents = await fetchFileContents(selectedFiles, accessToken, currentRepoInfo.source);
await createAndDownloadZip(fileContents);
} catch (error) {
const outputText = document.getElementById('outputText');
outputText.value = `Error generating zip file: ${error.message}\n\n` +
"Please ensure:\n" +
"1. You have selected at least one file from the directory structure.\n" +
"2. Your access token (if provided) is valid and has the necessary permissions.";
}
});
// Event listener for copying text to clipboard
document.getElementById('copyButton').addEventListener('click', function () {
const outputText = document.getElementById('outputText');
outputText.select();
navigator.clipboard.writeText(outputText.value)
.then(() => console.log('Text copied to clipboard'))
.catch(err => console.error('Failed to copy text: ', err));
});
// Event listener for downloading text file
document.getElementById('downloadButton').addEventListener('click', function () {
const outputText = document.getElementById('outputText').value;
if (!outputText.trim()) {
document.getElementById('outputText').value = 'Error: No content to download. Please generate the text file first.';
return;
}
const blob = new Blob([outputText], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'prompt.txt';
a.click();
URL.revokeObjectURL(url);
});
// Parse GitHub or Hugging Face repository URL
function parseRepoUrl(url) {
url = url.replace(/\/$/, '');
const githubPattern = /^https:\/\/github\.com\/([^\/]+)\/([^\/]+)(?:\/tree\/(.+))?$/;
const hfPattern = /^https:\/\/huggingface\.co\/(?:(datasets|spaces)\/)?([^\/]+)\/([^\/]+)(?:\/tree\/(.+))?$/;
let match = url.match(githubPattern);
if (match) {
return {
source: 'github',
owner: match[1],
repo: match[2],
lastString: match[4] || ''
};
}
match = url.match(hfPattern);
if (match) {
let repo_type = 'model';
if (match[1] === 'datasets') repo_type = 'dataset';
if (match[1] === 'spaces') repo_type = 'space';
return {
source: 'huggingface',
repo_type: repo_type,
owner: match[2],
repo: match[3],
lastString: match[4] || ''
};
}
throw new Error('Invalid GitHub or Hugging Face repository URL.');
}
// Fetch GitHub repository references
async function getGitHubReferences(owner, repo, token) {
const headers = { 'Accept': 'application/vnd.github+json' };
if (token) headers['Authorization'] = `token ${token}`;
const [branchesResponse, tagsResponse] = await Promise.all([
fetch(`https://api.github.com/repos/${owner}/${repo}/branches`, { headers }),
fetch(`https://api.github.com/repos/${owner}/${repo}/tags`, { headers })
]);
if (!branchesResponse.ok || !tagsResponse.ok) handleFetchError(branchesResponse, 'github');
const branches = await branchesResponse.json();
const tags = await tagsResponse.json();
return {
branches: branches.map(b => b.name),
tags: tags.map(t => t.name)
};
}
// Fetch Hugging Face repository references
async function getHuggingFaceReferences(owner, repo, repo_type, token) {
const repoId = `${owner}/${repo}`;
const typePath = repo_type === 'model' ? 'models' : repo_type === 'dataset' ? 'datasets' : 'spaces';
const url = `https://huggingface.co/api/${typePath}/${repoId}/refs`;
const headers = {};
if (token) headers['Authorization'] = `Bearer ${token}`;
const response = await fetch(url, { headers });
if (!response.ok) handleFetchError(response, 'huggingface');
const data = await response.json();
const branches = data.branches ? data.branches.map(b => b.name) : [];
const tags = data.tags ? data.tags.map(t => t.name) : [];
return [...branches, ...tags];
}
// Fetch repository SHA
async function fetchRepoSha(owner, repo, ref, path, token) {
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${path ? `${path}` : ''}${ref ? `?ref=${ref}` : ''}`;
const headers = { 'Accept': 'application/vnd.github.object+json' };
if (token) headers['Authorization'] = `token ${token}`;
const response = await fetch(url, { headers });
if (!response.ok) handleFetchError(response, 'github');
const data = await response.json();
return data.sha;
}
// Fetch GitHub repository tree
async function fetchGitHubRepoTree(owner, repo, sha, token) {
const url = `https://api.github.com/repos/${owner}/${repo}/git/trees/${sha}?recursive=1`;
const headers = { 'Accept': 'application/vnd.github+json' };
if (token) headers['Authorization'] = `token ${token}`;
const response = await fetch(url, { headers });
if (!response.ok) handleFetchError(response, 'github');
const data = await response.json();
return data.tree;
}
// Fetch Hugging Face repository tree
async function fetchHuggingFaceTree(owner, repo, repo_type, ref, path, token) {
const typePath = repo_type === 'model' ? 'models' : repo_type === 'dataset' ? 'datasets' : 'spaces';
const url = `https://huggingface.co/api/${typePath}/${owner}/${repo}/tree/${ref}?recursive=true`;
const headers = {};
if (token) headers['Authorization'] = `Bearer ${token}`;
const response = await fetch(url, { headers });
if (!response.ok) handleFetchError(response, 'huggingface');
let tree = await response.json();
if (path) {
tree = tree.filter(item => item.path.startsWith(path + '/') || item.path === path);
}
return tree.map(item => {
let repoIdForUrl;
switch (repo_type) {
case 'dataset':
repoIdForUrl = `datasets/${owner}/${repo}`;
break;
case 'space':
repoIdForUrl = `spaces/${owner}/${repo}`;
break;
default: // model
repoIdForUrl = `${owner}/${repo}`;
}
return {
path: item.path,
type: (item.type === 'file' || item.type === 'lfs') ? 'blob' : 'tree',
urlType: 'hf',
url: `https://huggingface.co/${repoIdForUrl}/raw/${ref}/${item.path}`
};
});
}
// Handle fetch errors
function handleFetchError(response, source = 'github') {
if (response.status === 403 && source === 'github' && response.headers.get('X-RateLimit-Remaining') === '0') {
throw new Error('GitHub API rate limit exceeded. Please try again later or provide a valid access token to increase your rate limit.');
}
if (response.status === 401) {
throw new Error(`Authentication error. Please check if your access token is valid and has the required permissions.`);
}
if (response.status === 404) {
throw new Error(`Repository, branch, or path not found. Please check that the URL, branch/tag, and path are correct and accessible.`);
}
throw new Error(`Failed to fetch repository data. Status: ${response.status}. Please check your input and try again.`);
}
// Fetch contents of selected files
async function fetchFileContents(files, token, source) {
const contents = await Promise.all(files.map(async file => {
let headers = {};
if (token) {
headers['Authorization'] = source === 'github' ? `token ${token}` : `Bearer ${token}`;
}
if (source === 'github') {
headers['Accept'] = 'application/vnd.github.v3.raw';
}
const response = await fetch(file.url, { headers });
if (!response.ok) handleFetchError(response, source);
const text = await response.text();
return { url: file.url, path: file.path, text };
}));
return contents;
}
function setupShowMoreInfoButton() {
const showMoreInfoButton = document.getElementById('showMoreInfo');
const tokenInfo = document.getElementById('tokenInfo');
showMoreInfoButton.addEventListener('click', function() {
tokenInfo.classList.toggle('hidden');
updateInfoIcon(this, tokenInfo);
});
}
function updateInfoIcon(button, tokenInfo) {
const icon = button.querySelector('[data-lucide]');
if (icon) {
icon.setAttribute('data-lucide', tokenInfo.classList.contains('hidden') ? 'info' : 'x');
lucide.createIcons();
}
}
// Create and download zip file
async function createAndDownloadZip(fileContents) {
const zip = new JSZip();
fileContents.forEach(file => {
const filePath = file.path.startsWith('/') ? file.path.slice(1) : file.path;
zip.file(filePath, file.text);
});
const content = await zip.generateAsync({type: "blob"});
const url = URL.createObjectURL(content);
const a = document.createElement('a');
a.href = url;
a.download = 'repo_contents.zip';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} |