-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.js
executable file
·79 lines (61 loc) · 1.82 KB
/
cleanup.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
#!/usr/bin/env node
var fs = require('fs');
var sys = require('sys');
var readline = require('readline');
var exts=["liquid"];
var exceptions=["./cleanup.js"]
var cmd = "find . " + exts.map(function(e){return '-name "*.' + e + '" '}).join(" -o ");
// http://www.geedew.com/2012/10/24/remove-a-directory-that-is-not-empty-in-nodejs/
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
files = stdout.split("\n").filter(function(s){return s !== ""; });
//Filter out exceptions
exceptions.forEach(function(e) {
files = files.filter(function(f){
return f.indexOf(e) === -1;
});
});
files.forEach(function(f){console.log(f);});
if (files.length === 0) {
console.log("No files found. Exiting...");
return;
}
var txt = "\nAre you sure you want to delete these " + files.length + " paths? (type 'Yes' to confirm) ";
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(txt, function(answer) {
if (answer === "Yes") {
console.log("Deleting " + files.length + " paths ...");
files.forEach(function(f){
if (fs.lstatSync(f).isFile()) {
fs.unlink(f);
}
});
files.forEach(function(f){
if (fs.existsSync(f) && fs.lstatSync(f).isDirectory()) {
deleteFolderRecursive(f);
}
});
}
else {
console.log("Aborting");
}
rl.close();
});
}
exec(cmd, puts);