Skip to content

Commit d388b0e

Browse files
committed
feature(trammel) drop support of node < 10 (#1)
1 parent d416016 commit d388b0e

File tree

8 files changed

+80
-101
lines changed

8 files changed

+80
-101
lines changed

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"rules": {
3+
"node/no-unsupported-features/node-builtins": "off"
4+
},
25
"extends": [
36
"plugin:putout/recommended",
47
"plugin:node/recommended"

.eslintrc.test

Lines changed: 0 additions & 15 deletions
This file was deleted.

.npmignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
test
33
yarn-error.log
44

5-
madrun.js
6-

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: node_js
22
node_js:
3+
- 14
34
- 12
45
- 10
5-
- 8
66

77
script:
88
- npm run lint

README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@ If you want get realtime size updates use [dirsize](https://github.com/coderaise
1616
## Example
1717

1818
```js
19-
const trammel = require('trammel');
19+
import trammel from 'trammel';
2020

21-
trammel('.', (error, size) => {
22-
console.log(error, size);
23-
//undefined '58.47kb'
24-
});
21+
await trammel('.');
22+
// returns
23+
'58.47kb'
2524

26-
trammel('.', {type: 'raw'}, (error, size) => {
27-
console.log(error, size);
28-
//undefined 59974
29-
});
30-
31-
trammel('do not exist', {stopOnError: true}, (error, size) => {
32-
if (error)
33-
return console.error(error.message);
34-
35-
console.log(size);
36-
});
25+
await trammel('.', {type: 'raw'});
26+
// returns
27+
59974
3728

29+
await trammel('do not exist', {stopOnError: true});
30+
// returns
31+
Error
3832
```
3933

4034
## License

lib/trammel.js

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
'use strict';
22

3-
const fs = require('fs');
3+
const {promisify} = require('util');
4+
const {lstat, readdir} = require('fs').promises;
45
const path = require('path');
56
const {EventEmitter} = require('events');
67

78
const format = require('format-io');
9+
const tryToCatch = require('try-to-catch');
810

911
/* The lstat() function shall be equivalent to stat(),
1012
except when path refers to a symbolic link. In that case lstat()
1113
shall return information about the link, while stat() shall return
1214
information about the file the link references.
1315
*/
14-
const stat = fs.lstat;
1516

16-
module.exports = (dir, options, callback) => {
17+
module.exports = promisify((dir, options, callback) => {
1718
const emitter = new EventEmitter();
1819

1920
let total = 0;
@@ -26,8 +27,6 @@ module.exports = (dir, options, callback) => {
2627
type = options.type;
2728
}
2829

29-
check(dir, callback);
30-
3130
emitter.on('file', (file, stat) => {
3231
total += stat.size;
3332
});
@@ -42,7 +41,7 @@ module.exports = (dir, options, callback) => {
4241
});
4342

4443
processDir(dir, options, emitter);
45-
};
44+
});
4645

4746
function processDir(dir, options, emitter) {
4847
const {stopOnError} = options;
@@ -60,13 +59,11 @@ function processDir(dir, options, emitter) {
6059
emitter.emit('end');
6160
};
6261

63-
const getDirInfo = (dir) => {
64-
stat(dir, getStat.bind(null, dir));
65-
};
66-
6762
getDirInfo(dir);
6863

69-
function getStat(dir, error, stat) {
64+
async function getDirInfo(dir) {
65+
const [error, stat] = await tryToCatch(lstat, dir);
66+
7067
--fileCounter;
7168

7269
if (error && stopOnError) {
@@ -90,19 +87,19 @@ function processDir(dir, options, emitter) {
9087

9188
execCallBack();
9289

93-
fs.readdir(dir, (error, files) => {
94-
asyncRunning--;
95-
96-
if (!error)
97-
return onReaddir(dir, files);
98-
99-
if (error && stopOnError) {
100-
wasError = true;
101-
emitter.emit('error', error);
102-
}
103-
104-
execCallBack();
105-
});
90+
const [readError, files] = await tryToCatch(readdir, dir);
91+
92+
asyncRunning--;
93+
94+
if (!readError)
95+
return onReaddir(dir, files);
96+
97+
if (readError && stopOnError) {
98+
wasError = true;
99+
emitter.emit('error', readError);
100+
}
101+
102+
execCallBack();
106103
}
107104

108105
function onReaddir(dir, files) {
@@ -120,11 +117,3 @@ function processDir(dir, options, emitter) {
120117
}
121118
}
122119

123-
function check(dir, callback) {
124-
if (!dir)
125-
throw Error('dir could not be empty!');
126-
127-
if (!callback)
128-
throw Error('callback could not be empty!');
129-
130-
}

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@
2121
"watcher": "madrun watcher"
2222
},
2323
"dependencies": {
24-
"format-io": "^2.0.0"
24+
"events.once": "^2.0.2",
25+
"format-io": "^2.0.0",
26+
"try-to-catch": "^3.0.0"
2527
},
2628
"devDependencies": {
2729
"coveralls": "^3.0.1",
2830
"eslint": "^6.4.0",
2931
"eslint-plugin-node": "^11.1.0",
3032
"eslint-plugin-putout": "^3.7.1",
3133
"madrun": "^5.5.0",
34+
"mock-require": "^3.0.3",
3235
"nodemon": "^1.17.5",
3336
"nyc": "^15.0.1",
3437
"putout": "^7.23.0",
35-
"supertape": "^1.2.3",
36-
"tape": "^4.9.1",
37-
"try-catch": "^3.0.0",
38-
"try-to-catch": "^3.0.0"
38+
"supertape": "^1.2.3"
3939
},
4040
"license": "MIT",
4141
"engines": {
42-
"node": ">=8"
42+
"node": ">=10"
4343
},
4444
"main": "lib/trammel.js",
4545
"publishConfig": {

test/trammel.js

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,31 @@ const fs = require('fs');
55

66
const test = require('supertape');
77
const tryToCatch = require('try-to-catch');
8-
const tryCatch = require('try-catch');
9-
const {promisify} = require('util');
8+
const {reRequire} = require('mock-require');
9+
1010
const trammel = require('..');
11-
const trammel_ = promisify(trammel);
1211

1312
const fixturePath = path.join(__dirname, 'fixture');
1413

1514
test('trammel: size of a file', async (t) => {
1615
const expected = '12b';
17-
const [, size] = await tryToCatch(trammel_, `${fixturePath}/file.txt`);
16+
const [, size] = await tryToCatch(trammel, `${fixturePath}/file.txt`);
1817

1918
t.equal(expected, size, 'should equal');
2019
t.end();
2120
});
2221

2322
test('trammel: size of a directory', async (t) => {
2423
const expected = '12b';
25-
const [, size] = await tryToCatch(trammel_, `${fixturePath}/dir`);
24+
const [, size] = await tryToCatch(trammel, `${fixturePath}/dir`);
2625

2726
t.equal(expected, size, 'should equal');
2827
t.end();
2928
});
3029

3130
test('trammel: size of a directory: empty dir: raw', async (t) => {
3231
const expected = 0;
33-
const [, size] = await tryToCatch(trammel_, `${fixturePath}/empty-dir`, {
32+
const [, size] = await tryToCatch(trammel, `${fixturePath}/empty-dir`, {
3433
type: 'raw',
3534
});
3635

@@ -40,67 +39,78 @@ test('trammel: size of a directory: empty dir: raw', async (t) => {
4039

4140
test('trammel: stopOnError: false', async (t) => {
4241
const expected = '0b';
43-
const [, size] = await tryToCatch(trammel_, 'abcef');
42+
const [, size] = await tryToCatch(trammel, 'abcef');
4443

4544
t.equal(size, expected, 'should equal');
4645
t.end();
4746
});
4847

4948
test('trammel: error', async (t) => {
50-
const [, size] = await tryToCatch(trammel_, 'abcd');
49+
const [, size] = await tryToCatch(trammel, 'abcd');
5150

5251
t.equal(size, '0b', 'should equal');
5352
t.end();
5453
});
5554

5655
test('trammel: stopOnError: true', async (t) => {
57-
const [e] = await tryToCatch(trammel_, 'abcd', {stopOnError: true});
56+
const [e] = await tryToCatch(trammel, 'abcd', {stopOnError: true});
5857

5958
t.equal(e.code, 'ENOENT', 'should equal');
6059
t.end();
6160
});
6261

6362
test('trammel: stopOnError: true: can not readdir', async (t) => {
6463
const error = Error('hello');
65-
const {readdir} = fs;
64+
const {readdir} = fs.promises;
65+
66+
fs.promises.readdir = async () => {
67+
throw error;
68+
};
6669

67-
fs.readdir = (dir, fn) => fn(error);
70+
const trammel = reRequire('..');
6871

69-
const [e] = await tryToCatch(trammel_, fixturePath, {
72+
const [e] = await tryToCatch(trammel, fixturePath, {
7073
stopOnError: true,
7174
});
7275

73-
fs.readdir = readdir;
76+
fs.promises.readdir = readdir;
7477

7578
t.equal(e.message, 'hello', 'should equal');
7679
t.end();
7780
});
7881

79-
test('trammel: can not readdir', async (t) => {
80-
const expected = '0b';
81-
const {readdir} = fs;
82+
test('trammel: readdir: empty', async (t) => {
83+
const {readdir} = fs.promises;
8284

83-
fs.readdir = (dir, fn) => fn(Error('hi'));
85+
fs.promises.readdir = async () => [];
8486

85-
const [, size] = await tryToCatch(trammel_, fixturePath);
87+
const trammel = reRequire('..');
8688

87-
fs.readdir = readdir;
89+
const size = await trammel(fixturePath, {
90+
type: 'raw',
91+
});
8892

89-
t.equal(size, expected, 'should equal');
90-
t.end();
91-
});
92-
93-
test('trammel: arguments: no', async (t) => {
94-
const [e] = tryCatch(trammel);
93+
fs.promises.readdir = readdir;
9594

96-
t.equal(e.message, 'dir could not be empty!', 'should equal');
95+
t.equal(size, 0, 'should equal');
9796
t.end();
9897
});
9998

100-
test('trammel: arguments: no callback', async (t) => {
101-
const [e] = tryCatch(trammel, '/');
99+
test('trammel: can not readdir', async (t) => {
100+
const expected = '0b';
101+
const {readdir} = fs.promises;
102+
103+
fs.promises.readdir = () => {
104+
throw Error('hi');
105+
};
102106

103-
t.equal(e.message, 'callback could not be empty!', 'should equal');
107+
const trammel = reRequire('..');
108+
109+
const [, size] = await tryToCatch(trammel, fixturePath);
110+
111+
fs.promises.readdir = readdir;
112+
113+
t.equal(size, expected, 'should equal');
104114
t.end();
105115
});
106116

0 commit comments

Comments
 (0)