-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.js
54 lines (46 loc) · 1.36 KB
/
filter.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
const debug = require('debug')('cleaner:filter');
const utils = require('./utils');
const BINARY_MARKER = process.env.BINARY_MARKER || '.tar.gz';
exports.filter = (files) => {
const binaries = filterBinaries(files);
return setHierarchy(binaries)
.then(sortOnTimestamp)
.then((sorted) => {
return {
allFiles : files,
sortedHierarchy: sorted
};
});
};
function setHierarchy(files) {
return new Promise((resolve) => {
debug(`filter ${files.length} files`);
const hierarchy = files.reduce((acc, file) => {
const fileWithTs = addTimestamp(file);
const filePath = utils.getFilePath(fileWithTs.Key);
acc[filePath.module] = acc[filePath.module] || [];
acc[filePath.module].push(fileWithTs);
return acc;
}, {});
resolve(hierarchy);
})
}
function filterBinaries(files) {
return files.filter((file) => file.Key.endsWith(BINARY_MARKER));
}
function addTimestamp(file) {
file.timestamp=new Date(file.LastModified).getTime();
return file;
}
function sortOnTimestamp(hierarchy) {
return new Promise((resolve) => {
const sortHierarchy = {};
const modules = Object.keys(hierarchy);
modules.map((module) => {
sortHierarchy[module] = hierarchy[module].sort((a, b) => {
return a.timestamp < b.timestamp; //latest first
});
});
resolve(sortHierarchy);
});
}