This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
460 lines (355 loc) · 12 KB
/
main.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
const { app, BrowserWindow, ipcMain, shell } = require('electron');
const url = require('url');
const path = require('path');
const FileTypes = require('./file-types.json');
const fs = require('fs');
const contextMenu = require('electron-context-menu');
const appVersion = app.getVersion();
const { autoUpdater } = require('electron-updater');
var mode = 1; // by default works on file
var handleDirs = true; // by default process directories with depth 1
// require('electron-reload')(path.join(__dirname, 'app','view'));
contextMenu({
menu: (actions, params, browserWindow) => [
{
label: 'Default Mode',
visible: true,
click: () => {
mode = 1;
handleDirs = true;
console.log("Switched to : Default Mode");
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('mode', "Default Mode");
}
},
actions.separator(),
{
label: 'Aggregate By Types',
visible: true,
click: () => {
mode = 1;
console.log("Switched to : Type Mode");
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('mode', "Aggregate Files By Type");
}
},
{
label: 'Aggregate By Extensions',
visible: true,
click: () => {
mode = 2;
console.log("Switched to : Extension Mode");
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('mode', "Aggregate Files by Extension");
}
},
actions.separator(),
// {
// label: 'Rename Without Space',
// visible: true,
// click: () => {
// mode = 3;
// console.log("Switched to : Rename Without Space Mode");
// let currWin = BrowserWindow.getFocusedWindow();
// currWin.webContents.send('mode', "Remove Spaces From Name");
// }
// },
{
label: 'Rename By Replacing Space With Dash',
visible: true,
click: () => {
mode = 3;
console.log("Switched to : Replace Space -> Dash Mode");
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('mode', "Replace Space With Dash");
}
},
{
label: 'Rename By Replacing Dash With Underscore',
visible: true,
click: () => {
mode = 4;
console.log("Switched to : Replace Dash -> Underscore Mode");
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('mode', "Replace Dash With Underscore");
}
},
{
label: 'Rename By Replacing Underscore With Space',
visible: true,
click: () => {
mode = 5;
console.log("Switched to : Replace underscore -> Space Mode");
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('mode', "Replace Underscore With Space");
}
},
actions.separator(),
{
label: 'Toggle Directories Handling',
visible: true,
click: () => {
handleDirs = !handleDirs;
console.log("Handle Dirs: " + handleDirs);
let currWin = BrowserWindow.getFocusedWindow();
if (handleDirs) {
currWin.webContents.send('mode', "Handle Directories");
}
else {
currWin.webContents.send('mode', "Don't Handle Directories");
}
}
},
actions.separator(),
{
label: 'About This App (Version: '+appVersion+")",
visible: true,
click: () => {
shell.openExternal(`https://github.com/deep5050/Mr.Dclutterer`);
}
}
]
});
function createWindow() {
const win = new BrowserWindow({
width: 298, // 300
height: 438, // 400
resizable: false,
frame: false,
maximizable: false,
fullscreenable: false,
webPreferences: {
nodeIntegration: true
}
})
win.loadURL(url.format({
pathname: path.join(__dirname, 'app', 'view', 'index.html'),
protocol: 'file:',
slashes: true
}));
// win.webContents.openDevTools()
win.once('ready-to-show', () => {
autoUpdater.checkForUpdatesAndNotify();
});
}
autoUpdater.on('update-available', () => {
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('mode', "App Update Available");
});
autoUpdater.on('update-downloaded', () => {
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('mode', "Restarting App To Update It");
autoUpdater.quitAndInstall();
});
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
///////////////////////// Aggregate by file types //////////////////////////////////
byType = (filePathArr, handleDirs) => {
var filesUnderAllDirs = [];
filePathArr.forEach(_path => {
if (fs.existsSync(_path)) {
if (fs.lstatSync(_path).isFile()) {
baseFileName = path.basename(_path);
// is file ??
var ext;
try {
ext = path.extname(_path).split('.')[1];
} catch (error) {
console.error("ERROR: " + _path + " no extension found");
}
if (ext && ext !== "") {
if (FileTypes[ext]) {
console.log(_path + " Is of Type " + FileTypes[ext]);
// logic goes here ..................
// extract the directory from the file path
currDirName = path.dirname(_path);
// create directory(UPPERCASE) if not exists based on the type
determinedDirName = FileTypes[ext].toUpperCase();
newDirPath = path.join(currDirName, determinedDirName);
movePath = path.join(newDirPath, baseFileName);
if (!fs.existsSync(newDirPath)) {
try {
fs.mkdirSync(newDirPath)
} catch (error) {
console.log("Error: create " + newDirPath);
}
}
// move file to the newly created directory
try {
fs.renameSync(_path, movePath);
} catch (error) {
console.log("Error: move " + movePath);
}
}
else {
console.log("ERROR: " + _path + " could not decide")
}
}
}
else if (fs.lstatSync(_path).isDirectory() && handleDirs == true) {
// if feature is enabled, list all the FILES ( exclude further directories) i.e. depth = 1 and add it to a list
console.log(_path + " is a Directory : Listing all the files to arrange them :)");
dirPath = _path;
entries = fs.readdirSync(dirPath);
entries.forEach(element => {
if (fs.lstatSync(path.join(dirPath, element)).isFile()) {
filesUnderAllDirs.push(path.join(dirPath, element)); // appending files only
}
});
}
}
});
// now call this function again with handleDirs=false
if (filesUnderAllDirs && filesUnderAllDirs.length != 0) {
byType(filesUnderAllDirs, false);
}
}
/////////////////////////////////////////////////////////////////////////////
///////////////////////// Aggregate by Extensions //////////////////////
byExtension = (filePathArr, handleDirs) => {
var filesUnderAllDirs = [];
filePathArr.forEach(_path => {
if (fs.existsSync(_path)) {
if (fs.lstatSync(_path).isFile()) {
baseFileName = path.basename(_path);
// is file ??
var ext;
try {
ext = path.extname(_path).split('.')[1];
} catch (error) {
console.error("ERROR: " + _path + " no extension found");
}
if (ext) {
if (ext !== "") {
console.log(_path + " Extension: " + ext);
// logic goes here ..................
// extract the directory from the file path
currDirName = path.dirname(_path);
// create directory(UPPERCASE) if not exists based on the type
determinedDirName = ext.toUpperCase();
newDirPath = path.join(currDirName, determinedDirName);
movePath = path.join(newDirPath, baseFileName);
if (!fs.existsSync(newDirPath)) {
try {
fs.mkdirSync(newDirPath)
} catch (error) {
console.log("Error: create " + newDirPath);
}
}
// move file to the newly created directory
try {
fs.renameSync(_path, movePath);
} catch (error) {
console.log("Error: move " + movePath);
}
}
else {
console.log("ERROR: " + _path + " could not decide")
}
}
}
else if (fs.lstatSync(_path).isDirectory() && handleDirs == true) {
// if feature is enabled, list all the FILES ( exclude further directories) i.e. depth = 1 and add it to a list
console.log(_path + " is a Directory : Listing all the files to arrange them :)");
dirPath = _path;
entries = fs.readdirSync(dirPath);
entries.forEach(element => {
if (fs.lstatSync(path.join(dirPath, element)).isFile()) {
filesUnderAllDirs.push(path.join(dirPath, element)); // appending files only
}
});
}
}
});
// now call this function again with handleDirs=false
if (filesUnderAllDirs && filesUnderAllDirs.length != 0) {
byExtension(filesUnderAllDirs, false);
}
}
///////////////////////////////////////////////////////////////////////
/////////////////////////// Renaming /////////////////////////
renameFiles = (filePathArr, handleDirs, exactMode) => {
var filesUnderAllDirs = [];
filePathArr.forEach(_path => {
if (fs.existsSync(_path)) {
if (fs.lstatSync(_path).isFile()) {
baseFileName = path.basename(_path);
// is file ??
currDirName = path.dirname(_path);
// check for the actual mode and set rules accordingly
newFileName = baseFileName;
if (exactMode === 3) {
// replace space with dash
newFileName = baseFileName.replace(/ /g, '-');
}
else if (exactMode === 4) {
// replace dash with underscore
newFileName = baseFileName.replace(/-/g, '_');
}
else if (exactMode === 5) {
// replace underscore with space
newFileName = baseFileName.replace(/_/g, ' ');
}
movePath = path.join(currDirName, newFileName);
if (fs.existsSync(movePath)) {
console.log("WARNING: " + movePath + " already exists");
}
else {
try {
// rename file accordingly
fs.renameSync(_path, movePath);
} catch (error) {
console.log("Error: move " + movePath);
}
}
}
else if (fs.lstatSync(_path).isDirectory() && handleDirs == true) {
// if feature is enabled, list all the FILES ( exclude further directories) i.e. depth = 1 and add it to a list
console.log(_path + " is a Directory : Listing all the files to arrange them :)");
dirPath = _path;
entries = fs.readdirSync(dirPath);
entries.forEach(element => {
if (fs.lstatSync(path.join(dirPath, element)).isFile()) {
filesUnderAllDirs.push(path.join(dirPath, element)); // appending files only
}
});
}
}
});
// now call this function again with handleDirs=false
if (filesUnderAllDirs && filesUnderAllDirs.length != 0) {
renameFiles(filesUnderAllDirs, false,exactMode);
}
}
///////////////////////////////////////////////////////////////
analyzeAndGo = async (filePathArr) => {
if (mode === 1) {
byType(filePathArr, handleDirs);
}
if (mode === 2) {
byExtension(filePathArr, handleDirs);
}
if (mode > 2) {
// renaming
renameFiles(filePathArr, handleDirs, mode);
}
}
ipcMain.on('go', (event, filePath) => {
// console.log(filePath);
var filePathArr = filePath.split("\n");
analyzeAndGo(filePathArr);
let currWin = BrowserWindow.getFocusedWindow();
currWin.webContents.send('done', 'DONE');
});
ipcMain.on('quit', (event) => {
app.quit();
})