|
| 1 | +import archiver from 'archiver' |
| 2 | +import { createWriteStream, createReadStream } from 'fs' |
| 3 | +import { readdir, stat, unlink } from 'fs/promises' |
| 4 | +import path from 'path' |
| 5 | +import os from 'os' |
| 6 | +import { logger } from '../utils/logger' |
| 7 | + |
| 8 | +async function getIgnoredPaths(repoPath: string, paths: string[]): Promise<Set<string>> { |
| 9 | + if (paths.length === 0) return new Set() |
| 10 | + |
| 11 | + try { |
| 12 | + const { spawn } = await import('child_process') |
| 13 | + |
| 14 | + return new Promise((resolve) => { |
| 15 | + const ignored = new Set<string>() |
| 16 | + const proc = spawn('git', ['check-ignore', '--stdin'], { |
| 17 | + cwd: repoPath, |
| 18 | + shell: false |
| 19 | + }) |
| 20 | + |
| 21 | + let stdout = '' |
| 22 | + |
| 23 | + proc.stdout?.on('data', (data: Buffer) => { |
| 24 | + stdout += data.toString() |
| 25 | + }) |
| 26 | + |
| 27 | + proc.stdin?.write(paths.join('\n')) |
| 28 | + proc.stdin?.end() |
| 29 | + |
| 30 | + proc.on('close', () => { |
| 31 | + const ignoredPaths = stdout.split('\n').filter(p => p.trim()) |
| 32 | + for (const p of ignoredPaths) { |
| 33 | + ignored.add(p) |
| 34 | + } |
| 35 | + resolve(ignored) |
| 36 | + }) |
| 37 | + |
| 38 | + proc.on('error', () => { |
| 39 | + resolve(new Set()) |
| 40 | + }) |
| 41 | + }) |
| 42 | + } catch { |
| 43 | + return new Set() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async function collectFiles( |
| 48 | + repoPath: string, |
| 49 | + relativePath: string = '' |
| 50 | +): Promise<string[]> { |
| 51 | + const fullPath = path.join(repoPath, relativePath) |
| 52 | + const entries = await readdir(fullPath, { withFileTypes: true }) |
| 53 | + const files: string[] = [] |
| 54 | + |
| 55 | + for (const entry of entries) { |
| 56 | + const entryRelPath = relativePath ? path.join(relativePath, entry.name) : entry.name |
| 57 | + |
| 58 | + if (entry.name === '.git') continue |
| 59 | + |
| 60 | + if (entry.isDirectory()) { |
| 61 | + files.push(entryRelPath + '/') |
| 62 | + const subFiles = await collectFiles(repoPath, entryRelPath) |
| 63 | + files.push(...subFiles) |
| 64 | + } else { |
| 65 | + files.push(entryRelPath) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return files |
| 70 | +} |
| 71 | + |
| 72 | +async function filterIgnoredPaths(repoPath: string, allPaths: string[]): Promise<string[]> { |
| 73 | + const batchSize = 1000 |
| 74 | + const ignoredSet = new Set<string>() |
| 75 | + |
| 76 | + for (let i = 0; i < allPaths.length; i += batchSize) { |
| 77 | + const batch = allPaths.slice(i, i + batchSize) |
| 78 | + const ignored = await getIgnoredPaths(repoPath, batch) |
| 79 | + for (const p of ignored) { |
| 80 | + ignoredSet.add(p) |
| 81 | + if (p.endsWith('/')) { |
| 82 | + ignoredSet.add(p.slice(0, -1)) |
| 83 | + } else { |
| 84 | + ignoredSet.add(p + '/') |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const filteredPaths: string[] = [] |
| 90 | + const ignoredDirs = new Set<string>() |
| 91 | + |
| 92 | + for (const p of allPaths) { |
| 93 | + const isDir = p.endsWith('/') |
| 94 | + const cleanPath = isDir ? p.slice(0, -1) : p |
| 95 | + |
| 96 | + let isUnderIgnoredDir = false |
| 97 | + for (const ignoredDir of ignoredDirs) { |
| 98 | + if (cleanPath.startsWith(ignoredDir + '/')) { |
| 99 | + isUnderIgnoredDir = true |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (isUnderIgnoredDir) continue |
| 105 | + |
| 106 | + if (ignoredSet.has(p) || ignoredSet.has(cleanPath)) { |
| 107 | + if (isDir) { |
| 108 | + ignoredDirs.add(cleanPath) |
| 109 | + } |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + filteredPaths.push(p) |
| 114 | + } |
| 115 | + |
| 116 | + return filteredPaths |
| 117 | +} |
| 118 | + |
| 119 | +export async function createRepoArchive(repoPath: string): Promise<string> { |
| 120 | + const repoName = path.basename(repoPath) |
| 121 | + const tempFile = path.join(os.tmpdir(), `${repoName}-${Date.now()}.zip`) |
| 122 | + |
| 123 | + logger.info(`Creating archive for ${repoPath} at ${tempFile}`) |
| 124 | + |
| 125 | + const allPaths = await collectFiles(repoPath) |
| 126 | + const filteredPaths = await filterIgnoredPaths(repoPath, allPaths) |
| 127 | + |
| 128 | + const output = createWriteStream(tempFile) |
| 129 | + const archive = archiver('zip', { zlib: { level: 5 } }) |
| 130 | + |
| 131 | + return new Promise((resolve, reject) => { |
| 132 | + output.on('close', () => { |
| 133 | + logger.info(`Archive created: ${tempFile} (${archive.pointer()} bytes)`) |
| 134 | + resolve(tempFile) |
| 135 | + }) |
| 136 | + |
| 137 | + archive.on('error', (err) => { |
| 138 | + logger.error('Archive error:', err) |
| 139 | + reject(err) |
| 140 | + }) |
| 141 | + |
| 142 | + archive.pipe(output) |
| 143 | + |
| 144 | + for (const relativePath of filteredPaths) { |
| 145 | + if (relativePath.endsWith('/')) continue |
| 146 | + |
| 147 | + const fullPath = path.join(repoPath, relativePath) |
| 148 | + const archivePath = path.join(repoName, relativePath) |
| 149 | + archive.file(fullPath, { name: archivePath }) |
| 150 | + } |
| 151 | + |
| 152 | + archive.finalize() |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +export async function deleteArchive(filePath: string): Promise<void> { |
| 157 | + try { |
| 158 | + await unlink(filePath) |
| 159 | + logger.info(`Deleted temp archive: ${filePath}`) |
| 160 | + } catch (error) { |
| 161 | + logger.warn(`Failed to delete temp archive: ${filePath}`, error) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +export function getArchiveStream(filePath: string) { |
| 166 | + return createReadStream(filePath) |
| 167 | +} |
| 168 | + |
| 169 | +export async function getArchiveSize(filePath: string): Promise<number> { |
| 170 | + const stats = await stat(filePath) |
| 171 | + return stats.size |
| 172 | +} |
0 commit comments