-
Notifications
You must be signed in to change notification settings - Fork 0
/
moduleSize.js
62 lines (56 loc) · 2 KB
/
moduleSize.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
const fs = require('fs');
const getDirectorySize = path => new Promise((resolve, reject) => {
fs.readdir(path, (err, list) => {
if (err) reject(err);
Promise.all(
list
.filter(file => (file !== 'node_modules'))
.map(file => getFileSize(`${path}/${file}`))
).then(sizes => resolve(sizes.reduce((sum, size) => sum + size, 0)))
.catch(reject);
});
});
const getFileSize = path => new Promise((resolve, reject) => {
fs.stat(path, (err, data) => {
if (err) reject(err);
if (!data) resolve(0);
if (data.isDirectory()) {
getDirectorySize(path).then(size => resolve(size));
} else {
resolve(data.size);
}
});
});
const calculateDependenciesSizes = module => Promise.all(
module.dependencies.map(path => (path && getFileSize(path).then(fullSize => ({ path, fullSize }))))
);
const checkSharedDependencies = flatDependenciesTree => flatDependenciesTree
.map(d => d.dependencies)
.reduce((depArray, depList) => [...depArray, ...depList], [])
.reduce((result, path) => {
const currentValue = (result[path] || 0) + 1;
return {
...result,
[path]: currentValue
};
}, {});
module.exports = (flatDependenciesTree) => {
const sharedDependencies = checkSharedDependencies(flatDependenciesTree);
return Promise.all(
flatDependenciesTree
.map(module => calculateDependenciesSizes(module)
.then(sizes => sizes)
.then(sizes => ({
...module,
fullSize: sizes.reduce((sum, item) => sum + item.fullSize, 0),
sharedSize: sizes.reduce((sum, item) => (sum + parseInt(item.fullSize / sharedDependencies[item.path], 10)), 0),
ownSize: sizes.reduce((sum, item) => (sum + (sharedDependencies[item.path] > 1 ? 0 : item.fullSize)), 0)
}))
.then(mod => getFileSize(mod.path).then(moduleSize => ({
...mod,
fullSize: mod.fullSize + moduleSize,
sharedSize: mod.sharedSize + moduleSize,
ownSize: mod.ownSize + moduleSize
}))))
);
};