Skip to content

Commit 84c182c

Browse files
committed
Optimize zipDirectory to exclude node_modules and build files
Refactored the zipDirectory handler in app.js to use archiver and stream-buffers for zipping, excluding node_modules and common build artifacts for faster processing. Added warnings when node_modules is detected and updated dependencies to include archiver and stream-buffers.
1 parent e3adb04 commit 84c182c

File tree

3 files changed

+218
-195
lines changed

3 files changed

+218
-195
lines changed

app.js

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,69 @@ ipcMain.handle("zipDirectory", async() => {
8080
console.log('[Electron] Starting zip of:', zipPaths[0]);
8181
console.log('[Electron] Output path:', zipPaths[1]);
8282

83-
// Dynamic import for ES module
83+
// Check if node_modules exists and warn about size
84+
const nodeModulesPath = path.join(zipPaths[0], 'node_modules');
85+
const hasNodeModules = fs.existsSync(nodeModulesPath);
86+
87+
if (hasNodeModules) {
88+
console.log('[Electron] WARNING: node_modules detected - excluding from zip for faster processing');
89+
console.log('[Electron] OSI will run npm install automatically if needed');
90+
}
91+
92+
// Dynamic import for ES modules
8493
const { zip } = await import("zip-a-folder");
85-
await zip(zipPaths[0], zipPaths[1]);
86-
console.log('[Electron] Zip complete! Reading file...');
94+
const archiver = await import("archiver");
95+
const streamBuffers = await import("stream-buffers");
96+
97+
// Create a buffer to store the zip
98+
const outputStreamBuffer = new streamBuffers.default.WritableStreamBuffer({
99+
initialSize: (100 * 1024),
100+
incrementAmount: (10 * 1024)
101+
});
102+
103+
// Create archive with exclusions
104+
const archive = archiver.default('zip', {
105+
zlib: { level: 5 } // Moderate compression for speed
106+
});
107+
108+
archive.on('error', (err) => {
109+
console.error('[Electron] Archive error:', err);
110+
reject(err);
111+
});
112+
113+
archive.on('end', async () => {
114+
console.log('[Electron] Archive finalized, size:', archive.pointer(), 'bytes');
115+
const buffer = outputStreamBuffer.getContents();
116+
117+
// Write to temp file
118+
await fs.promises.writeFile(zipPaths[1], buffer);
119+
console.log('[Electron] Zip file written to:', zipPaths[1]);
120+
121+
return resolve(buffer);
122+
});
123+
124+
// Pipe archive to buffer
125+
archive.pipe(outputStreamBuffer);
126+
127+
// Add directory with exclusions
128+
archive.glob('**/*', {
129+
cwd: zipPaths[0],
130+
ignore: [
131+
'**/node_modules/**',
132+
'**/.git/**',
133+
'**/dist/**',
134+
'**/build/**',
135+
'**/.next/**',
136+
'**/coverage/**',
137+
'**/.cache/**',
138+
'**/tmp/**',
139+
'**/*.log'
140+
]
141+
});
87142

88-
const fileData = await fs.promises.readFile(zipPaths[1]);
89-
console.log('[Electron] File read complete, size:', fileData.length, 'bytes');
143+
console.log('[Electron] Creating optimized zip (excluding node_modules and build artifacts)...');
144+
await archive.finalize();
90145

91-
return resolve(fileData);
92146
} catch(error) {
93147
console.error('[Electron] Zip error:', error);
94148
return reject(error);

0 commit comments

Comments
 (0)