|
1 | 1 | /* inspired by http://procbits.com/2011/10/29/a-node-js-experiment-thinking-asynchronously-recursion-calculate-file-size-directory */ |
2 | | -(function(){ |
3 | | - 'use strict'; |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const EventEmitter = require('events').EventEmitter; |
| 7 | + |
| 8 | +const format = require('format-io'); |
| 9 | + |
| 10 | +/* The lstat() function shall be equivalent to stat(), |
| 11 | + except when path refers to a symbolic link. In that case lstat() |
| 12 | + shall return information about the link, while stat() shall return |
| 13 | + information about the file the link references. |
| 14 | +*/ |
| 15 | +const stat = fs.lstat; |
| 16 | + |
| 17 | +module.exports = (dir, options, callback) => { |
| 18 | + let type; |
| 19 | + const ERROR_EMPTY = 'could not be empty!'; |
| 20 | + const emitter = new EventEmitter(); |
| 21 | + let total = 0; |
4 | 22 |
|
5 | | - var fs = require('fs'), |
6 | | - path = require('path'), |
7 | | - |
8 | | - format = require('format-io'), |
9 | | - |
10 | | - EventEmitter= require('events').EventEmitter, |
11 | | - |
12 | | - /* The lstat() function shall be equivalent to stat(), |
13 | | - except when path refers to a symbolic link. In that case lstat() |
14 | | - shall return information about the link, while stat() shall return |
15 | | - information about the file the link references. |
16 | | - */ |
17 | | - stat = fs.lstat; |
| 23 | + if (!callback) { |
| 24 | + callback = options; |
| 25 | + options = {}; |
| 26 | + } else { |
| 27 | + type = options.type; |
| 28 | + } |
18 | 29 |
|
19 | | - module.exports = function(dir, options, callback) { |
20 | | - var type, stopOnError, |
21 | | - ERROR_EMPTY = 'could not be empty!', |
22 | | - emitter = new EventEmitter(), |
23 | | - total = 0; |
24 | | - |
25 | | - if (!callback) { |
26 | | - callback = options; |
27 | | - options = {}; |
28 | | - } else { |
29 | | - type = options.type; |
30 | | - stopOnError = options.stopOnError; |
31 | | - } |
32 | | - |
33 | | - if (!dir) |
34 | | - throw(Error('dir ' + ERROR_EMPTY)); |
35 | | - |
36 | | - if (!callback) |
37 | | - throw(Error('callback' + ERROR_EMPTY)); |
38 | | - |
39 | | - emitter.on('file', function(file, stat) { |
40 | | - total += stat.size; |
41 | | - }); |
| 30 | + if (!dir) |
| 31 | + throw Error('dir ' + ERROR_EMPTY); |
| 32 | + |
| 33 | + if (!callback) |
| 34 | + throw Error('callback' + ERROR_EMPTY); |
| 35 | + |
| 36 | + emitter.on('file', (file, stat) => { |
| 37 | + total += stat.size; |
| 38 | + }); |
| 39 | + |
| 40 | + emitter.on('error', (error) => { |
| 41 | + callback(error); |
| 42 | + }); |
| 43 | + |
| 44 | + emitter.on('end', () => { |
| 45 | + let result; |
42 | 46 |
|
43 | | - emitter.on('error', function(error) { |
44 | | - callback(error); |
45 | | - }); |
| 47 | + if (type !== 'raw') |
| 48 | + result = format.size(total); |
| 49 | + else |
| 50 | + result = total; |
46 | 51 |
|
47 | | - emitter.on('end', function() { |
48 | | - var result; |
49 | | - |
50 | | - if (type !== 'raw') |
51 | | - result = format.size(total); |
52 | | - else |
53 | | - result = total; |
54 | | - |
55 | | - callback(null, result); |
56 | | - }); |
| 52 | + callback(null, result); |
| 53 | + }); |
| 54 | + |
| 55 | + processDir(dir, options, emitter); |
| 56 | +}; |
| 57 | + |
| 58 | +function processDir(dir, options, emitter) { |
| 59 | + var stopOnError = options.stopOnError, |
| 60 | + wasError = false, |
| 61 | + asyncRunning = 0, |
| 62 | + fileCounter = 1, |
57 | 63 |
|
58 | | - processDir(dir, options, emitter); |
59 | | - }; |
60 | | - |
61 | | - function processDir(dir, options, emitter) { |
62 | | - var stopOnError = options.stopOnError, |
63 | | - wasError = false, |
64 | | - asyncRunning = 0, |
65 | | - fileCounter = 1, |
66 | | - |
67 | | - execCallBack = function () { |
68 | | - var noErrors = !wasError || !stopOnError, |
69 | | - yesAllDone = !fileCounter && !asyncRunning; |
70 | | - |
71 | | - if (yesAllDone && noErrors) |
72 | | - emitter.emit('end'); |
73 | | - }, |
| 64 | + execCallBack = function () { |
| 65 | + let noErrors = !wasError || !stopOnError; |
| 66 | + let yesAllDone = !fileCounter && !asyncRunning; |
74 | 67 |
|
75 | | - getDirInfo = function(dir) { |
76 | | - stat(dir, getStat.bind(null, dir)); |
77 | | - }; |
| 68 | + if (yesAllDone && noErrors) |
| 69 | + emitter.emit('end'); |
| 70 | + }, |
78 | 71 |
|
79 | | - getDirInfo(dir); |
| 72 | + getDirInfo = (dir) => { |
| 73 | + stat(dir, getStat.bind(null, dir)); |
| 74 | + }; |
| 75 | + |
| 76 | + getDirInfo(dir); |
| 77 | + |
| 78 | + function getStat(dir, error, stat) { |
| 79 | + --fileCounter; |
80 | 80 |
|
81 | | - function getStat(dir, error, stat) { |
82 | | - var isDir; |
83 | | - |
84 | | - --fileCounter; |
85 | | - |
86 | | - if (!wasError || !stopOnError) { |
87 | | - if (error && stopOnError) { |
88 | | - wasError = true; |
89 | | - emitter.emmit('error', error); |
90 | | - } else if (!error) { |
91 | | - isDir = stat.isDirectory(); |
| 81 | + if (!wasError || !stopOnError) { |
| 82 | + if (error && stopOnError) { |
| 83 | + wasError = true; |
| 84 | + emitter.emmit('error', error); |
| 85 | + } else if (!error) { |
| 86 | + let isDir = stat.isDirectory(); |
| 87 | + |
| 88 | + if (!isDir) |
| 89 | + return emitter.emit('file', dir, stat); |
| 90 | + |
| 91 | + ++asyncRunning; |
| 92 | + |
| 93 | + fs.readdir(dir, (error, files) => { |
| 94 | + asyncRunning--; |
92 | 95 |
|
93 | | - if (!isDir) { |
94 | | - emitter.emit('file', dir, stat); |
95 | | - } else { |
96 | | - ++asyncRunning; |
97 | | - |
98 | | - fs.readdir(dir, function(error, files) { |
99 | | - asyncRunning--; |
100 | | - |
101 | | - if (!error) { |
102 | | - onReaddir(dir, files); |
103 | | - } else if (error && stopOnError) { |
104 | | - wasError = true; |
105 | | - emitter.emit('error', error); |
106 | | - } |
107 | | - }); |
| 96 | + if (!error) { |
| 97 | + onReaddir(dir, files); |
| 98 | + } else if (error && stopOnError) { |
| 99 | + wasError = true; |
| 100 | + emitter.emit('error', error); |
108 | 101 | } |
109 | | - } |
110 | | - |
111 | | - execCallBack(); |
| 102 | + }); |
112 | 103 | } |
113 | | - } |
114 | | - |
115 | | - function onReaddir(dir, files) { |
116 | | - var n = files.length; |
117 | | - |
118 | | - fileCounter += n; |
119 | 104 |
|
120 | | - if (!n) |
121 | | - execCallBack(); |
122 | | - else |
123 | | - files.forEach(function(file) { |
124 | | - var dirPath = path.join(dir, file); |
125 | | - |
126 | | - process.nextTick(function() { |
127 | | - getDirInfo(dirPath); |
128 | | - }); |
129 | | - }); |
| 105 | + execCallBack(); |
130 | 106 | } |
131 | 107 | } |
132 | 108 |
|
133 | | -})(); |
| 109 | + function onReaddir(dir, files) { |
| 110 | + let n = files.length; |
| 111 | + |
| 112 | + fileCounter += n; |
| 113 | + |
| 114 | + if (!n) |
| 115 | + return execCallBack(); |
| 116 | + |
| 117 | + files.forEach((file) => { |
| 118 | + const dirPath = path.join(dir, file); |
| 119 | + |
| 120 | + process.nextTick(() => { |
| 121 | + getDirInfo(dirPath); |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
| 126 | + |
0 commit comments