|
| 1 | +const { app, BrowserWindow, ipcMain, dialog } = require('electron'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs/promises'); |
| 4 | +// Additional modules will be required for scanning logic |
| 5 | + |
| 6 | +function createWindow() { |
| 7 | + const win = new BrowserWindow({ |
| 8 | + width: 1000, |
| 9 | + height: 700, |
| 10 | + webPreferences: { |
| 11 | + nodeIntegration: false, |
| 12 | + contextIsolation: true, |
| 13 | + preload: path.join(__dirname, 'preload.js') |
| 14 | + }, |
| 15 | + backgroundColor: '#121212', // Dark mode background |
| 16 | + titleBarStyle: 'hiddenInset' // Premium feel for macOS |
| 17 | + }); |
| 18 | + |
| 19 | + win.loadFile('index.html'); |
| 20 | +} |
| 21 | + |
| 22 | +app.whenReady().then(() => { |
| 23 | + createWindow(); |
| 24 | + |
| 25 | + app.on('activate', () => { |
| 26 | + if (BrowserWindow.getAllWindows().length === 0) { |
| 27 | + createWindow(); |
| 28 | + } |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +app.on('window-all-closed', () => { |
| 33 | + if (process.platform !== 'darwin') { |
| 34 | + app.quit(); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +// IPC Handlers |
| 39 | +ipcMain.handle('select-directory', async () => { |
| 40 | + const result = await dialog.showOpenDialog({ |
| 41 | + properties: ['openDirectory'] |
| 42 | + }); |
| 43 | + return result.filePaths[0]; |
| 44 | +}); |
| 45 | + |
| 46 | +ipcMain.handle('scan-directory', async (event, rootPath) => { |
| 47 | + // Basic validation |
| 48 | + if (!rootPath) return []; |
| 49 | + |
| 50 | + // Helper to format size |
| 51 | + const formatSize = (bytes) => { |
| 52 | + if (bytes === 0) return '0 B'; |
| 53 | + const k = 1024; |
| 54 | + const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; |
| 55 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 56 | + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; |
| 57 | + }; |
| 58 | + |
| 59 | + const modules = []; |
| 60 | + |
| 61 | + // Recursive scanner |
| 62 | + async function scan(dir) { |
| 63 | + let entries; |
| 64 | + try { |
| 65 | + entries = await fs.readdir(dir, { withFileTypes: true }); |
| 66 | + } catch (e) { |
| 67 | + // Permission denied or other error, skip |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + for (const entry of entries) { |
| 72 | + const fullPath = path.join(dir, entry.name); |
| 73 | + if (entry.isDirectory()) { |
| 74 | + if (entry.name === 'node_modules') { |
| 75 | + // Found one! Calculate size and add to list. |
| 76 | + // Do not recurse INSIDE node_modules |
| 77 | + try { |
| 78 | + const size = await getDirSize(fullPath); |
| 79 | + modules.push({ |
| 80 | + path: fullPath, |
| 81 | + sizeBytes: size, |
| 82 | + sizeFormatted: formatSize(size) |
| 83 | + }); |
| 84 | + } catch (e) { |
| 85 | + console.error(`Error sizing ${fullPath}:`, e); |
| 86 | + } |
| 87 | + } else { |
| 88 | + // Recurse |
| 89 | + if (!entry.name.startsWith('.')) { // Skip hidden folders like .git |
| 90 | + await scan(fullPath); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Directory size calculator |
| 98 | + async function getDirSize(dir) { |
| 99 | + let size = 0; |
| 100 | + let entries; |
| 101 | + try { |
| 102 | + entries = await fs.readdir(dir, { withFileTypes: true }); |
| 103 | + } catch (e) { return 0; } |
| 104 | + |
| 105 | + for (const entry of entries) { |
| 106 | + const fullPath = path.join(dir, entry.name); |
| 107 | + if (entry.isDirectory()) { |
| 108 | + size += await getDirSize(fullPath); |
| 109 | + } else { |
| 110 | + try { |
| 111 | + const stats = await fs.stat(fullPath); |
| 112 | + size += stats.size; |
| 113 | + } catch (e) { } |
| 114 | + } |
| 115 | + } |
| 116 | + return size; |
| 117 | + } |
| 118 | + |
| 119 | + await scan(rootPath); |
| 120 | + return modules; |
| 121 | +}); |
| 122 | + |
| 123 | +ipcMain.handle('delete-directories', async (event, paths) => { |
| 124 | + for (const p of paths) { |
| 125 | + try { |
| 126 | + await fs.rm(p, { recursive: true, force: true }); |
| 127 | + } catch (error) { |
| 128 | + console.error(`Failed to delete ${p}:`, error); |
| 129 | + // Could return errors to UI, but for now simple log |
| 130 | + } |
| 131 | + } |
| 132 | + return true; |
| 133 | +}); |
0 commit comments