Skip to content

Commit 5ccf5e9

Browse files
committed
feature(trammel) drop support of node < 4
1 parent 86663db commit 5ccf5e9

File tree

5 files changed

+151
-129
lines changed

5 files changed

+151
-129
lines changed

.eslintrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true,
5+
},
6+
"parserOptions": {
7+
"ecmaVersion": 2017,
8+
},
9+
"rules": {
10+
"indent": ["error", 4],
11+
"semi": "error",
12+
"no-console": 0,
13+
"no-use-before-define": ["error", "nofunc"]
14+
},
15+
"extends": [
16+
"eslint:recommended",
17+
"plugin:node/recommended"
18+
],
19+
"plugins": [
20+
"node"
21+
]
22+
}

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 coderaiser
3+
Copyright (c) 2014-2018 coderaiser
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
SOFTWARE.

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@
22

33
Get directory size.
44

5-
If you want get realtime size updates use [dirsize](https://github.com/coderaiser/node-dirsize).
5+
If you want get realtime size updates use [dirsize](https://github.com/coderaiser/node-dirsize).
66

77
## Example
88

99
```js
10-
var trammel = require('trammel');
10+
const trammel = require('trammel');
1111

12-
trammel('.', function(error, size) {
12+
trammel('.', (error, size) => {
1313
console.log(error, size);
1414
//undefined '58.47kb'
1515
});
1616

17-
trammel('.', {type: 'raw'}, function(error, size) {
17+
trammel('.', {type: 'raw'}, (error, size) => {
1818
console.log(error, size);
1919
//undefined 59974
2020
});
2121

22-
trammel('do not exist', {stopOnError: true}, function(error, size) {
22+
trammel('do not exist', {stopOnError: true}, (error, size) => {
2323
if (error)
24-
console.error(error.message);
25-
else
26-
console.log(size);
24+
return console.error(error.message);
25+
26+
console.log(size);
2727
});
2828

2929
```
3030

3131
## License
3232

3333
MIT
34+

lib/size.js

Lines changed: 110 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,126 @@
11
/* 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;
422

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+
}
1829

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;
4246

43-
emitter.on('error', function(error) {
44-
callback(error);
45-
});
47+
if (type !== 'raw')
48+
result = format.size(total);
49+
else
50+
result = total;
4651

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,
5763

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;
7467

75-
getDirInfo = function(dir) {
76-
stat(dir, getStat.bind(null, dir));
77-
};
68+
if (yesAllDone && noErrors)
69+
emitter.emit('end');
70+
},
7871

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;
8080

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--;
9295

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);
108101
}
109-
}
110-
111-
execCallBack();
102+
});
112103
}
113-
}
114-
115-
function onReaddir(dir, files) {
116-
var n = files.length;
117-
118-
fileCounter += n;
119104

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();
130106
}
131107
}
132108

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+

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
"type": "git",
99
"url": "git://github.com/coderaiser/trammel.git"
1010
},
11+
"scripts": {
12+
"lint": "eslint lib"
13+
},
1114
"dependencies": {
1215
"format-io": "~0.9.5"
1316
},
14-
"devDependencies": {},
17+
"devDependencies": {
18+
"eslint": "^4.19.1",
19+
"eslint-plugin-node": "^6.0.1"
20+
},
1521
"license": "MIT",
1622
"engines": {
17-
"node": ">=0.4.x"
23+
"node": ">=4"
1824
},
1925
"main": "lib/size.js"
2026
}

0 commit comments

Comments
 (0)