|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import simpleGit from 'simple-git'; |
| 4 | + |
| 5 | +export interface FileData { |
| 6 | + name: string; |
| 7 | + path: string; |
| 8 | + type: 'file' | 'directory'; |
| 9 | + children?: FileData[]; |
| 10 | + content?: string; |
| 11 | +} |
| 12 | + |
| 13 | +async function cloneRepository(repoUrl: string, clonePath: string) { |
| 14 | + const git = simpleGit(); |
| 15 | + await git.clone(repoUrl, clonePath); |
| 16 | + console.log(`Repository cloned to ${clonePath}`); |
| 17 | +} |
| 18 | + |
| 19 | +function shouldIgnoreFile(fileName: string): boolean { |
| 20 | + const lowerCaseFileName = fileName.toLowerCase(); |
| 21 | + return ( |
| 22 | + lowerCaseFileName === 'package-lock.json' || |
| 23 | + lowerCaseFileName.endsWith('.pdf') || |
| 24 | + lowerCaseFileName.endsWith('.png') || |
| 25 | + lowerCaseFileName.endsWith('.jpg') || |
| 26 | + lowerCaseFileName.endsWith('.jpeg') || |
| 27 | + lowerCaseFileName.endsWith('.gif') || |
| 28 | + lowerCaseFileName.endsWith('.ico') || |
| 29 | + lowerCaseFileName.endsWith('.svg') || |
| 30 | + lowerCaseFileName.endsWith('.woff') || |
| 31 | + lowerCaseFileName.endsWith('.woff2') || |
| 32 | + lowerCaseFileName.endsWith('.eot') || |
| 33 | + lowerCaseFileName.endsWith('.ttf') || |
| 34 | + lowerCaseFileName.endsWith('.otf') || |
| 35 | + lowerCaseFileName.endsWith('.mp4') || |
| 36 | + lowerCaseFileName.endsWith('.avi') || |
| 37 | + lowerCaseFileName.endsWith('.webm') || |
| 38 | + lowerCaseFileName.endsWith('.mov') || |
| 39 | + lowerCaseFileName.endsWith('.mp3') || |
| 40 | + lowerCaseFileName.endsWith('.wav') || |
| 41 | + lowerCaseFileName.endsWith('.flac') || |
| 42 | + lowerCaseFileName.endsWith('.ogg') || |
| 43 | + lowerCaseFileName.endsWith('.webp') || |
| 44 | + lowerCaseFileName.startsWith('package-lock') || |
| 45 | + lowerCaseFileName.startsWith('yarn-lock') || |
| 46 | + lowerCaseFileName.startsWith('npm-debug') || |
| 47 | + lowerCaseFileName.startsWith('yarn-debug') || |
| 48 | + lowerCaseFileName.startsWith('yarn-error') || |
| 49 | + lowerCaseFileName.startsWith('tsconfig') || |
| 50 | + lowerCaseFileName.startsWith('jest.config') |
| 51 | + |
| 52 | + // Add more extensions as needed |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +function scrapeDirectoryToJson(dir: string, ignorePatterns: string[] = []): FileData[] { |
| 57 | + const files = fs.readdirSync(dir); |
| 58 | + return files.filter(file => { |
| 59 | + const filePath = path.join(dir, file); |
| 60 | + return ( |
| 61 | + !ignorePatterns.some(pattern => filePath.includes(pattern)) && |
| 62 | + !shouldIgnoreFile(file) |
| 63 | + ); |
| 64 | + }).map(file => { |
| 65 | + const filePath = path.join(dir, file); |
| 66 | + const stat = fs.statSync(filePath); |
| 67 | + |
| 68 | + if (stat.isDirectory()) { |
| 69 | + // Ignore the .git directory |
| 70 | + if (file === '.git') { |
| 71 | + return null; |
| 72 | + } |
| 73 | + return { |
| 74 | + name: file, |
| 75 | + path: filePath, |
| 76 | + type: 'directory', |
| 77 | + children: scrapeDirectoryToJson(filePath, ignorePatterns) |
| 78 | + }; |
| 79 | + } else { |
| 80 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 81 | + return { |
| 82 | + name: file, |
| 83 | + path: filePath, |
| 84 | + type: 'file', |
| 85 | + content: content |
| 86 | + }; |
| 87 | + } |
| 88 | + }).filter(item => item !== null) as FileData[]; |
| 89 | +} |
| 90 | + |
| 91 | +function scrapeDirectoryToPlainText(dir: string, ignorePatterns: string[] = [], prefix: string = ''): string { |
| 92 | + let result = ''; |
| 93 | + |
| 94 | + const files = fs.readdirSync(dir); |
| 95 | + files.forEach(file => { |
| 96 | + const filePath = path.join(dir, file); |
| 97 | + |
| 98 | + if (ignorePatterns.some(pattern => filePath.includes(pattern)) || shouldIgnoreFile(file)) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + const stat = fs.statSync(filePath); |
| 103 | + |
| 104 | + if (stat.isDirectory()) { |
| 105 | + // Ignore the .git directory |
| 106 | + if (file === '.git') { |
| 107 | + return; |
| 108 | + } |
| 109 | + result += scrapeDirectoryToPlainText(filePath, ignorePatterns, path.join(prefix, file)); |
| 110 | + } else { |
| 111 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 112 | + result += `${path.join(prefix, file)}\n${content}\n\n`; |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + return result; |
| 117 | +} |
| 118 | + |
| 119 | +export async function scrapeRepositoryToJson(repoUrl: string): Promise<FileData[]> { |
| 120 | + const repoName = repoUrl.split('/').pop()?.replace('.git', ''); |
| 121 | + if (!repoName) { |
| 122 | + throw new Error('Invalid repository URL'); |
| 123 | + } |
| 124 | + const clonePath = `./${repoName}`; // Directory where the repository will be cloned |
| 125 | + |
| 126 | + // Clone the repository |
| 127 | + await cloneRepository(repoUrl, clonePath); |
| 128 | + |
| 129 | + // Scrape the cloned repository directory |
| 130 | + const ignorePatterns = ['.git']; |
| 131 | + const result = scrapeDirectoryToJson(clonePath, ignorePatterns); |
| 132 | + |
| 133 | + // Clean up the cloned repository |
| 134 | + fs.rmdirSync(clonePath, { recursive: true }); |
| 135 | + console.log('Cloned repository directory removed'); |
| 136 | + |
| 137 | + return result; |
| 138 | +} |
| 139 | + |
| 140 | +export async function scrapeRepositoryToPlainText(repoUrl: string): Promise<string> { |
| 141 | + const repoName = repoUrl.split('/').pop()?.replace('.git', ''); |
| 142 | + if (!repoName) { |
| 143 | + throw new Error('Invalid repository URL'); |
| 144 | + } |
| 145 | + const clonePath = `./${repoName}`; // Directory where the repository will be cloned |
| 146 | + |
| 147 | + // Clone the repository |
| 148 | + await cloneRepository(repoUrl, clonePath); |
| 149 | + |
| 150 | + // Scrape the cloned repository directory |
| 151 | + const ignorePatterns = ['.git']; |
| 152 | + const result = scrapeDirectoryToPlainText(clonePath, ignorePatterns); |
| 153 | + |
| 154 | + // Clean up the cloned repository |
| 155 | + fs.rmdirSync(clonePath, { recursive: true }); |
| 156 | + console.log('Cloned repository directory removed'); |
| 157 | + |
| 158 | + return result; |
| 159 | +} |
0 commit comments