-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzize.js
More file actions
138 lines (121 loc) · 5.31 KB
/
Copy pathzize.js
File metadata and controls
138 lines (121 loc) · 5.31 KB
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
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const {
getParam,
hasOption,
hasOptionWithValue,
formatSize,
autoFormatSize,
} = require('./utils');
const [, assets, ...args] = process.argv;
const p_path = getParam(args, 0);
const o_abort = hasOption(args, 'abort', 'A');
const o_extra_verbose = hasOption(args, 'extra-verbose', 'E');
const o_help = hasOption(args, 'help', 'H');
const o_large_dirs = hasOption(args, 'large-dirs', 'D');
const o_large_dirs_count = parseInt(hasOptionWithValue(args, 'large-dirs-count', 'N', '10'));
const o_large_files = hasOption(args, 'large-files', 'F');
const o_large_files_count = parseInt(hasOptionWithValue(args, 'large-files-count', 'M', '10'));
const o_verbose = hasOption(args, 'verbose', 'V');
if (o_help) {
console.log(fs.readFileSync(path.resolve(assets, '../help.txt'), { encoding: 'ascii' }));
return;
}
const getSize = require('.');
let lastDirCount = 0;
let lastFileCount = 0;
const options = {
onUpdateCount: (dirCount, fileCount) => {
lastDirCount = dirCount;
lastFileCount = fileCount;
if (process.stdout.cursorTo) {
process.stdout.cursorTo(0);
process.stdout.write(chalk.yellow(`${dirCount.toLocaleString()} directories counted, ${fileCount.toLocaleString()} files counted`));
}
},
};
if (o_verbose) {
options.onStatDir = (dir, size) => {
if (process.stdout.clearLine) {
process.stdout.clearLine();
}
if (process.stdout.cursorTo) {
process.stdout.cursorTo(0);
}
process.stdout.write(`${chalk.yellow(`[${autoFormatSize(size, false)}]`.padEnd(20, ' '))}${chalk.cyan(dir)}\n`);
options.onUpdateCount(lastDirCount, lastFileCount);
};
}
if (o_extra_verbose) {
options.onStatFile = (file, size) => {
if (process.stdout.clearLine) {
process.stdout.clearLine();
}
if (process.stdout.cursorTo) {
process.stdout.cursorTo(0);
}
process.stdout.write(`${chalk.yellow(`[${autoFormatSize(size, false)}]`.padEnd(20, ' '))}${chalk.gray(file)}\n`);
options.onUpdateCount(lastDirCount, lastFileCount);
};
}
if (o_abort) {
options.abort = true;
} else {
options.abort = false;
options.onError = (err) => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(chalk.red(err));
process.stdout.write('\n');
};
}
if (o_large_dirs) {
options.collectDirs = true;
}
if (o_large_files) {
options.collectFiles = true;
}
getSize(path.resolve(process.cwd(), p_path || ''), options)
.then(({ dirSize, dirSizePairs, fileSizePairs }) => {
if (process.stdout.cursorTo) {
process.stdout.cursorTo(0);
}
if (process.stdout.clearLine) {
process.stdout.clearLine();
}
process.stdout.write(chalk.yellow(`${lastDirCount.toLocaleString()} directories counted, ${lastFileCount.toLocaleString()} files counted\n`));
if (o_large_dirs) {
process.stdout.write(chalk.green('=== Largest directories ===\n'));
const sortedDirSizePairs = dirSizePairs.sort(([, sizeA], [, sizeB]) => sizeB - sizeA);
for (let i = 0; i < Math.min(o_large_dirs_count, sortedDirSizePairs.length); i++) {
const [dir, size] = sortedDirSizePairs[i];
process.stdout.write(`#${(i + 1).toString().padEnd(4, ' ')}${chalk.yellow(`[${autoFormatSize(size, false)}]`.padEnd(20, ' '))}${chalk.cyan(dir)}\n`);
}
}
if (o_large_files) {
process.stdout.write(chalk.green('=== Largest files ===\n'));
const sortedFileSizePairs = fileSizePairs.sort(([, sizeA], [, sizeB]) => sizeB - sizeA);
for (let i = 0; i < Math.min(o_large_files_count, sortedFileSizePairs.length); i++) {
const [file, size] = sortedFileSizePairs[i];
process.stdout.write(`#${(i + 1).toString().padEnd(4, ' ')}${chalk.yellow(`[${autoFormatSize(size, false)}]`.padEnd(20, ' '))}${chalk.cyan(file)}\n`);
}
}
process.stdout.write(chalk.green('=== Summary ===\n'));
const gb = formatSize(dirSize, 'gb');
const mb = formatSize(dirSize, 'mb');
const kb = formatSize(dirSize, 'kb');
const gib = formatSize(dirSize, 'gib');
const mib = formatSize(dirSize, 'mib');
const kib = formatSize(dirSize, 'kib');
const bytes = formatSize(dirSize);
const lhsLength = Math.max('Decimal'.length, gb.length, mb.length, kb.length, bytes.length);
const rhsLength = Math.max('Binary'.length, gib.length, mib.length, kib.length, '--'.length);
process.stdout.write(`${chalk.cyan('Decimal'.padEnd(lhsLength, ' '))} | ${chalk.cyan('Binary')}\n`);
process.stdout.write(`${''.padEnd(lhsLength, '-')} | ${''.padEnd(rhsLength, '-')}\n`);
process.stdout.write(`${gb.padEnd(lhsLength, ' ')} | ${gib}\n`);
process.stdout.write(`${mb.padEnd(lhsLength, ' ')} | ${mib}\n`);
process.stdout.write(`${kb.padEnd(lhsLength, ' ')} | ${kib}\n`);
process.stdout.write(`${bytes.padEnd(lhsLength, ' ')} | --\n`);
})
.catch((err) => console.error(chalk.red(err)));