-
Notifications
You must be signed in to change notification settings - Fork 2
/
dedup.js
228 lines (193 loc) · 6.12 KB
/
dedup.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
#!/usr/bin/env node
(function() {
var FAST_READ_BUFFER_SIZE, Promise, Queue, createReadStream, crypto, duplicates, fast, fileHashes, fileQueue, hashFileAsync, i, ignore, ignoreList, logHelp, only, outputDuplicate, parameter, parameters, path, readFileAsync, readdirAsync, recursive, ref, removeDuplicate, scanDir, scanFile, searchDirs, searches, startTime, statAsync, storeHash, unlinkAsync, unsafe, verbose, veryverbose;
Promise = require('bluebird');
startTime = Date.now();
ref = Promise.promisifyAll(require('fs')), readdirAsync = ref.readdirAsync, readFileAsync = ref.readFileAsync, statAsync = ref.statAsync, unlinkAsync = ref.unlinkAsync, createReadStream = ref.createReadStream;
crypto = require('crypto');
path = require('path');
Queue = require('promise-queue');
parameters = Array.prototype.slice.call(process.argv, 2);
fileQueue = new Queue(10);
recursive = false;
removeDuplicate = false;
ignoreList = {};
verbose = false;
veryverbose = false;
fast = false;
searchDirs = [];
unsafe = false;
duplicates = [];
only = [];
FAST_READ_BUFFER_SIZE = 10000;
logHelp = function() {
console.log('dedup <options> <directories>');
console.log(' -r, --recursive search in directories recursively');
console.log(' -d, --delete delete duplicates when found');
console.log(' -i, --ignore <path> ignore directories or files by relative path');
console.log(' -v, --verbose log both duplicate files not just the first');
console.log(' --logging developer output shows recursion and scanning');
console.log(' -f, --fast build had from first 10kb of files for faster');
console.log(' performance however hash conditions are possible');
console.log(' --unsafe must be set when deleting files base on fast');
console.log(' hash');
console.log(' --only <suffix> only find duplicates with files matching the');
console.log(' suffix');
return console.log(' -h, --help show this message');
};
i = 0;
while (i < parameters.length) {
parameter = parameters[i];
switch (parameter) {
case '-r':
case '--recursive':
recursive = true;
break;
case '--delete':
removeDuplicate = true;
break;
case '-i':
case '--ignore':
ignore = parameters[++i];
ignoreList[ignore] = true;
break;
case '-v':
case '--verbose':
verbose = true;
break;
case '-f':
case '--fast':
fast = true;
break;
case '--logging':
veryverbose = true;
break;
case '--unsafe':
unsafe = true;
break;
case '--only':
if (only == null) {
only = [];
}
only.push(parameters[++i]);
break;
case '-h':
case '--help':
logHelp();
process.exit(0);
break;
default:
searchDirs.push(parameter);
}
i++;
}
fileHashes = {};
if (fast && removeDuplicate && !unsafe) {
console.log('bailing because you are trying to run fast and remove duplicates');
console.log('without setting --unsafe flag');
process.exit(1);
}
if (veryverbose) {
console.log({
recursive: recursive,
removeDuplicate: removeDuplicate,
ignoreList: ignoreList,
verbose: verbose,
searchDirs: searchDirs
});
}
hashFileAsync = function(filePath, maxBytes) {
return new Promise(function(resolve, reject) {
var bytesRead, finish, hash, readStream;
hash = crypto.createHash('md5');
finish = function() {
readStream.close();
return resolve(hash.digest('hex'));
};
readStream = createReadStream(filePath);
bytesRead = 0;
readStream.on('data', function(data) {
if (maxBytes && (bytesRead + data.length) > maxBytes) {
hash.update(data.slice(0, maxBytes - bytesRead));
return finish();
} else {
bytesRead += data.length;
return hash.update(data);
}
});
readStream.on('end', finish);
return readStream.on('error', reject);
});
};
outputDuplicate = function(duplicates) {
if (verbose) {
return console.log.apply(console, duplicates);
} else {
return console.log(duplicates[duplicates.length - 1]);
}
};
scanDir = function(dir) {
var scanSubpath;
scanSubpath = function(file) {
var filePath;
filePath = path.join(dir, file);
return statAsync(filePath).then(function(stats) {
if (ignoreList[filePath]) {
} else if (stats.isDirectory()) {
if (recursive) {
return scanDir(filePath);
}
} else {
return scanFile(filePath);
}
});
};
return readdirAsync(dir).then(function(files) {
return Promise.all(files.map(scanSubpath));
});
};
scanFile = function(filePath) {
var match;
if (only != null ? only.length : void 0) {
match = only.find(function(suffix) {
return filePath.endsWith(suffix);
});
if (!match) {
return;
}
}
return fileQueue.add(function() {
return hashFileAsync(filePath, fast ? FAST_READ_BUFFER_SIZE : void 0);
}).then(function(hash) {
return storeHash(hash, filePath);
});
};
storeHash = function(hash, filePath) {
var existing;
existing = fileHashes[hash] || [];
existing.push(filePath);
fileHashes[hash] = existing;
if (existing.length > 1) {
duplicates.push(filePath);
outputDuplicate(existing);
if (removeDuplicate) {
return fileQueue.add(function() {
return unlinkAsync(filePath);
});
}
}
};
searches = Promise.resolve();
searchDirs.forEach(function(dir) {
return searches = searches.then(function() {
return scanDir(dir);
});
});
searches.then(function() {
if (veryverbose) {
console.log(((Date.now() - startTime) / 1000) + " seconds");
console.log(duplicates.length + " duplicates");
return console.log(duplicates);
}
});
}).call(this);