Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (64 loc) · 1.76 KB
/
Copy pathindex.js
File metadata and controls
72 lines (64 loc) · 1.76 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const { app, BrowserWindow, ipcMain } = require('electron');
const fs = require('fs');
const { spawn } = require('child_process');
const path = require('path');
const { default: trash } = require('trash');
const { scanDirectory } = require('./scanner');
const { logDeletion } = require('./logger');
const TEST_DISK = path.join(__dirname, 'test-disk');
const LOG_DIR = path.join(__dirname, 'logs');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
ipcMain.handle('scan', async () => {
try {
return await scanDirectory(TEST_DISK);
} catch (err) {
return [];
}
});
ipcMain.handle('delete', async (event, paths) => {
for (const filePath of paths) {
try {
const stats = await fs.promises.stat(filePath);
await trash([filePath]);
await logDeletion({ path: filePath, size: stats.size });
} catch (err) {
console.error(err);
}
}
});
ipcMain.handle('listLogs', async () => {
try {
await fs.promises.mkdir(LOG_DIR, { recursive: true });
const files = await fs.promises.readdir(LOG_DIR);
return files.filter((f) => f.endsWith('.json'));
} catch {
return [];
}
});
ipcMain.handle('rollback', async (event, file) => {
return new Promise((resolve) => {
const logPath = path.join(LOG_DIR, file);
const child = spawn(process.execPath, [
path.join(__dirname, 'scripts', 'rollback.js'),
logPath,
]);
child.on('close', (code) => resolve(code === 0));
child.on('error', () => resolve(false));
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});