-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (62 loc) · 1.91 KB
/
script.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
const fs = require('fs-extra');
const path = require('path')
const root = "../Takeout/Google_Photos";
const colors = require('colors');
start();
async function start() {
fs.readdir(root)
.then(elements => {
const directories = elements.filter(e => isDirectory(e));
directories.forEach(async directory => {
if (await dirIsEmpty(directory)) {
console.log(colors.blue(`${directory} is Empty !`));
removeDirectory(directory);
} else {
if (await dirContainsJson(directory)) {
// console.log(colors.blue.underline(directory), 'contains .json files');
removeJsonInDir(directory);
} else {
console.log(colors.green.underline(directory), 'doesn\'t contains .json files');
}
}
})
})
.catch(e => {
console.log(colors.red(e));
});
}
function isDirectory(element) {
return fs.lstatSync(`${root}/${element}`).isDirectory();
}
async function dirIsEmpty(file) {
return await fs.readdir(`${root}/${file}`)
.then(files => {
return !files.length
}).catch(e => console.log(colors.red(e)))
}
function removeDirectory(dir) {
fs.remove(`${root}/${dir}`)
.then(e => {
console.log(colors.green(dir, 'removed'));
})
.catch(e => console.log(colors.red(dir, e)))
}
async function dirContainsJson(dir) {
return fs.readdir(`${root}/${dir}`)
.then(files => {
return files.some(file => path.extname(file) === '.json');
})
}
async function removeJsonInDir(dir) {
const dirFiles = await fs.readdir(`${root}/${dir}`)
const jsonFiles = dirFiles.filter(file => path.extname(file) === '.json');
jsonFiles.forEach(jsonFile => {
fs.remove(`${root}/${dir}/${jsonFile}`)
.then(() => {
console.log(colors.green(`${dir}/${jsonFile} removed`));
})
.catch(e => {
console.log(colors.red(`Error removing file ${dir}/${jsonFile} e`));
})
})
}