-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (82 loc) · 2.68 KB
/
app.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
/* deps */
const gm = require('gm');
const moment = require('moment');
const colors = require('colors');
const dir = require('node-dir');
const fs = require('fs');
const path = require('path');
/* config */
const srcPath = './src';
const threshold = 0.0009;
const suspectsPath = './suspects';
var movedCount = 0;
/* let's do this */
run();
/* run it! */
function run() {
let outcome;
let start = moment().format('x');
let count = 0;
// get an array of the input files and take the first one as the first comparison base
getSrcFiles().then(files => {
console.log(files);
// return false;
let lastFalse = files.shift();
console.log(lastFalse);
// return false;
compare(files, lastFalse, count);
}).catch(err => {
console.error('oh shit!', err);
});
}
/* get an array of input files */
function getSrcFiles() {
return new Promise((resolve, reject) => {
dir.paths(`${srcPath}/`, (err, files) => {
if (err) reject(err);
resolve(files.files.filter(file => {
return file !== 'src/.DS_Store';
}));
});
});
}
function compare(files, lastFalse, count, log = true) {
if (!files.length) return false;
count += 1;
let nextToCheck = files.shift();
gm.compare(lastFalse, nextToCheck, threshold, function (err, isEqual, equality, raw, path1, path2) {
if (err) {
console.log(err);
return false;
}
if (log) {
let color = isEqual ? 'green' : 'red';
console.log('');
console.log('- - - - - - - - - - - - - - - - - - - - - - - - - - -');
console.log('files processed:', count);
console.log('testing:', lastFalse, 'vs', nextToCheck);
console.log('equal:', colors[color](isEqual));
console.log('equality score:', equality);
console.log('- - - - - - - - - - - - - - - - - - - - - - - - - - -');
console.log('');
}
if (isEqual) {
// not found a car, current check becomes lastFalse
compare(files, nextToCheck, count);
} else {
// found a car, move nextToCheck to suspect dir, lastFalse remains the same
if (moveSuspect(nextToCheck))
compare(files, lastFalse, count);
}
});
}
/* move a suspect into the suspects directory */
function moveSuspect(suspect) {
if (movedCount > 4)
return false;
movedCount += 1;
let filename = path.basename(suspect);
console.log(colors.red('** Moving suspect', filename, '**'));
fs.renameSync(`${srcPath}/${filename}`, `${suspectsPath}/${filename}`);
return true;
}