-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·63 lines (47 loc) · 1.3 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
#! /usr/bin/env node
// Imports
const fs = require('fs');
const recursive = require('recursive-readdir');
const colors = require('colors');
// =======
// Helpers
// =======
const getFileSize = (filename) => fs.statSync(filename).size;
const getDirectorySize = (directory) => {
recursive(directory)
.then(files => {
const size = files.reduce((sum, file) => sum + getFileSize(file), 0);
formatResult(size, files.length);
});
}
const isMoreThanZero = (num = 0) => Number(num.toFixed(2)) > 0;
const formatResult = (size, files = 0) => {
const inKB = (size / 1024);
const inMB = (inKB / 1024);
const inGB = (inMB / 1024);
console.log();
if (isMoreThanZero(inGB)) {
console.log('\t', `${inGB.toFixed(2)}`.red, 'GB'.green);
}
if (isMoreThanZero(inMB)) {
console.log('\t', `${inMB.toFixed(2)}`.red, 'MB'.green);
}
if (isMoreThanZero(inKB)) {
console.log('\t', `${inKB.toFixed(2)}`.red, 'KB'.green);
}
console.log('\t', `${size}`.red, 'bytes'.green);
if (files) {
console.log('\t', `${files} total files inside.`.yellow)
}
console.log();
};
// ======
// Main
// ======
// Get dir or file name from arguments
const TARGET = process.argv[2];
if (fs.lstatSync(TARGET).isDirectory()) {
getDirectorySize(TARGET);
} else {
formatResult(getFileSize(TARGET));
}