File size: 1,657 Bytes
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
// Function to extract files from a zip archive
async function extractZipContents(zipFile) {
    try {
        const zip = await JSZip.loadAsync(zipFile);
        const tree = [];
        const gitignoreContent = ['.git/**'];
        let pathZipMap = {};

        // Process each file in the zip
        for (const [relativePath, zipEntry] of Object.entries(zip.files)) {
            if (!zipEntry.dir) {
                tree.push({
                    path: relativePath,
                    type: 'blob',
                    urlType: 'zip', 
                    url: ''
                });
                pathZipMap[relativePath] = zipEntry;

                // Check for .gitignore file
                if (relativePath.endsWith('.gitignore')) {
                    const content = await zipEntry.async('text');
                    const lines = content.split('\n');
                    const gitignorePath = relativePath.split('/').slice(0, -1).join('/');
                    lines.forEach(line => {
                        line = line.trim();
                        if (line && !line.startsWith('#')) {
                            if (gitignorePath) {
                                gitignoreContent.push(`${gitignorePath}/${line}`);
                            } else {
                                gitignoreContent.push(line);
                            }
                        }
                    });
                }
            }
        }

        return { tree, gitignoreContent, pathZipMap };
    } catch (error) {
        throw new Error(`Failed to extract zip contents: ${error.message}`);
    }
}

export { extractZipContents };