UNPKG

1.93 kBJavaScriptView Raw
1var fs, path, _$err, totalSize, totalBlockSize;
2fs = require('fs');
3function calcDirSize(path, callback) {
4 var dirSize, dirBlockSize, _$err, files, i, filename, stats, subDirSize, subDirBlockSize;
5 dirSize = 0;
6 dirBlockSize = 0;
7 fs.readdir(path, function () {
8 _$err = arguments[0];
9 files = arguments[1];
10 if (_$err)
11 throw _$err;
12 i = 0;
13 function _$loop_0(_$loop_0__$cont) {
14 if (i < files.length) {
15 filename = path + '/' + files[i];
16 fs.lstat(filename, function () {
17 _$err = arguments[0];
18 stats = arguments[1];
19 if (_$err)
20 throw _$err;
21 (function (_$cont) {
22 if (stats.isDirectory()) {
23 calcDirSize(filename, function () {
24 _$err = arguments[0];
25 subDirSize = arguments[1];
26 subDirBlockSize = arguments[2];
27 if (_$err)
28 throw _$err;
29 dirSize += subDirSize;
30 dirBlockSize += subDirBlockSize;
31 _$cont();
32 });
33 } else {
34 dirSize += stats.size;
35 dirBlockSize += 512 * stats.blocks;
36 _$cont();
37 }
38 }(function (_$err) {
39 if (_$err !== undefined)
40 return _$cont(_$err);
41 i++;
42 _$loop_0(_$loop_0__$cont);
43 }));
44 });
45 } else {
46 _$loop_0__$cont();
47 }
48 }
49 _$loop_0(function () {
50 callback(null, dirSize, dirBlockSize);
51 });
52 });
53}
54path = process.argv[2];
55if (!path) {
56 path = '.';
57}
58calcDirSize(path, function () {
59 _$err = arguments[0];
60 totalSize = arguments[1];
61 totalBlockSize = arguments[2];
62 if (_$err)
63 throw _$err;
64 console.log('Size:', Math.round(totalSize / 1024), 'KB');
65 console.log('Actual Size on Disk:', Math.round(totalBlockSize / 1024), 'KB');
66});
67/* Generated by Continuation.js v0.1.1 */
\No newline at end of file