-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (56 loc) · 1.72 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
module.exports = function () {
const fs = require('fs');
const path = require('path');
const param = {};
const size = {'B': 1, 'K': 1024, 'M': 1024 ** 2, 'G': 1024 ** 3};
const arg = process.argv.slice(2);
const toBytes = (str) => +str.match(/\d{1,}/g) * size[str.match(/B|K|M|G/g)];
const filter = (str) => {
if (/--/.test(str)) {
return param[(str.match(/--(.*)=/)[1]).toLowerCase()] = str.replace(/(.*)=/g, '$`');
}
};
arg.map((i) => filter(i));
if (param['min-size']) {
param['min-size'] = toBytes(param['min-size']);
}
if (param['max-size']) {
param['max-size'] = toBytes(param['max-size']);
}
const func = (err, arr) => err ? console.log(err) : arr.map((i) => console.log(i));
const getFiles = function (dir, callback) {
let results = [];
fs.readdir(dir, function (err, files) {
if (err) return callback(err);
let counter = files.length;
if (!counter) return callback(null, results);
files.forEach(function (item) {
let essence = item;
item = path.resolve(dir, item);
fs.stat(item, function (err, stat) {
if (stat && stat.isDirectory()) {
param.type !== 'F' ?
!param.pattern || (essence.indexOf(param.pattern) >= 0) ? results.push(item)
: null
: null;
getFiles(item, function (err, res) {
results = results.concat(res);
if (!--counter) callback(null, results);
});
} else {
param.type !== 'D' ?
!param['max-size'] || stat.size < param['max-size'] ?
!param['min-size'] || stat.size > param['min-size'] ?
!param.pattern || (essence.indexOf(param.pattern) >= 0) ? results.push(item)
: null
: null
: null
: null;
if (!--counter) callback(null, results);
}
});
});
});
};
getFiles(param.dir, func);
};