-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (83 loc) · 2.75 KB
/
index.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
import path from 'node:path';
import alfy from 'alfy';
import {globby} from 'globby';
import dialog from 'dialog';
import del from 'del';
import {moveFile} from 'move-file';
import PQueue from 'p-queue';
async function processFile(filePath) {
const q = new PQueue({concurrency: 1});
filePath = filePath.trim();
const parentFolder = path.resolve(`${filePath}/..`);
const filePathResolved = path.resolve(filePath);
console.log(`Processing "${filePathResolved}"`);
console.log('↳ filePathResolved:', filePathResolved);
const foundFiles = await globby(filePathResolved + '/**/*');
console.log('↳ foundFiles:', foundFiles);
q.addAll(
foundFiles.map(filePath => () => moveFoundFiles(filePath, parentFolder)),
);
await q.onIdle();
await del(filePath, {force: true});
}
function moveFoundFiles(filePath, parentFolder) {
filePath = filePath.trim();
const filePathResolved = path.resolve(filePath);
console.log('\t↳ moving:', filePathResolved);
const fileDirectory = path.dirname(filePathResolved);
const fileName = filePathResolved.replace(fileDirectory, '');
const destinationFilePath = `${parentFolder}${fileName}`;
console.log('\t↳ destinationFilePath:', destinationFilePath);
return moveFileToDestination(filePathResolved, destinationFilePath);
}
async function moveFileToDestination(filePath, destPath, counter = 0) {
try {
await moveFile(filePath, (counter === 0) ? destPath : `${destPath} duplicate-filename ${counter}`, {
overwrite: false,
});
} catch {
// File already exists so put a counter on it
await moveFileToDestination(filePath, destPath, counter + 1);
}
}
async function processFiles(filePaths) {
const q = new PQueue({concurrency: 1});
return q.addAll(
filePaths.map(element => () => processFile(element)),
);
}
function dialogPromise(text, title, buttons, icon) {
return new Promise(resolve => {
buttons = `{"${buttons.join('","')}"}`; // '{"Cancel", "OK"}',
dialog.custom(text, title, buttons, icon, (element, result) => {
if (element === 1) {
return resolve('Cancel');
}
return resolve(result.slice('button returned:'.length));
});
});
}
async function main() {
const filePathsQuery = alfy.input;
const filePaths = filePathsQuery.split('\t');
console.log('filePaths:', filePaths);
if (filePaths.length <= 0) {
return dialog.info('Nothing to flatten.', 'Flatten Folder');
}
console.log(`Starting to flatten ${filePaths.length} Folders`);
const buttonResult = await dialogPromise(
'Do you really want to flatten this folder?',
'Flatten Folder',
['Cancel', 'OK'],
'note',
);
if (buttonResult === 'Cancel') {
return dialog.info('Canceled the flattening.', 'Flatten Folder');
}
await processFiles(filePaths);
dialog.info(
'Finished flattening Folders',
'Flatten Folder',
);
}
await main();