Skip to content

Commit 27fc4f3

Browse files
committed
implement async in promise
1 parent 290d646 commit 27fc4f3

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.txt
2+
13
.DS_Store
24
test.html
35

index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
'use strict';
22
const fs = require('fs');
3-
const pathExists = require('path-exists');
3+
const pify = require('pify');
44

5-
module.exports = filename => new Promise((resolve, reject) => {
6-
if (pathExists.sync(filename)) {
7-
const stats = fs.statSync(filename);
8-
resolve(stats.size);
9-
}
10-
11-
reject(new Error(`File "${filename}" not found`));
12-
});
5+
module.exports = filename => {
6+
return pify(fs.stat)(filename)
7+
.then(stats => stats.size)
8+
.catch(err => {
9+
throw err;
10+
});
11+
};
1312

1413
module.exports.sync = filename => {
15-
if (pathExists.sync(filename)) {
14+
try {
1615
const stats = fs.statSync(filename);
1716
return stats.size;
17+
} catch (err) {
18+
throw err;
1819
}
19-
throw new Error(`File "${filename}" not found`);
2020
};

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "file-bytes",
3-
"version": "1.1.1",
3+
"version": "2.0.0",
44
"description": "Get the file size of a file",
55
"license": "MIT",
66
"homepage": "https://github.com/dawsonbotsford/file-bytes#readme",
@@ -26,6 +26,9 @@
2626
"index.js"
2727
],
2828
"keywords": [
29+
"file",
30+
"size",
31+
"bytes",
2932
"lean",
3033
"minimal",
3134
"documented",
@@ -35,10 +38,10 @@
3538
"dawson"
3639
],
3740
"dependencies": {
38-
"path-exists": "^3.0.0",
39-
"shelljs": "^0.7.0"
41+
"pify": "^2.3.0"
4042
},
4143
"devDependencies": {
44+
"shelljs": "^0.7.0",
4245
"ava": "^0.14.0",
4346
"xo": "^0.14.0"
4447
},

test.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,47 @@ const sh = require('shelljs');
33
import test from 'ava';
44
import fileBytes from './';
55

6+
function getRand() {
7+
const min = 0;
8+
const max = 99999999999999999999;
9+
return Math.floor(Math.random() * (max - min) + min);
10+
}
11+
612
test('invalid args sync', t => {
713
t.throws(() => {
814
fileBytes.sync('invalidFileName.txt');
9-
}, Error);
15+
}, /ENOENT/);
1016
});
1117

1218
test('empty file sync', t => {
13-
sh.touch('empty.txt');
14-
t.is(fileBytes.sync('empty.txt'), 0);
15-
t.is(typeof fileBytes.sync('empty.txt'), 'number');
16-
sh.rm('empty.txt');
19+
const file = `empty${getRand()}.txt`;
20+
sh.touch(file);
21+
t.is(fileBytes.sync(file), 0);
22+
t.is(typeof fileBytes.sync(file), 'number');
23+
sh.rm(file);
1724
});
1825

1926
test('small file sync', t => {
20-
fs.writeFileSync('small.txt', '0123456789');
21-
t.is(fileBytes.sync('small.txt'), 10);
22-
sh.rm('small.txt');
27+
const file = `small${getRand()}.txt`;
28+
fs.writeFileSync(file, 'four');
29+
t.is(fileBytes.sync(file), 4);
30+
sh.rm(file);
2331
});
2432

25-
test('invalid args promise', async t => {
26-
try {
27-
await fileBytes('invalidFileName.txt');
28-
t.fail('Exception was not thrown');
29-
} catch (err) {
30-
t.truthy(err);
31-
}
33+
test('invalid args promise', t => {
34+
t.throws(fileBytes('invalidFileName'), /ENOENT/);
3235
});
3336

3437
test('empty file promise', async t => {
35-
sh.touch('empty.txt');
36-
t.is(await fileBytes('empty.txt'), 0);
37-
sh.rm('empty.txt');
38+
const file = `empty${getRand()}.txt`;
39+
sh.touch(file);
40+
t.is(await fileBytes(file), 0);
41+
sh.rm(file);
3842
});
3943

4044
test('small file promise', async t => {
41-
fs.writeFileSync('small.txt', '0123456789');
42-
t.is(await fileBytes('small.txt'), 10);
43-
sh.rm('small.txt');
45+
const file = `small${getRand()}.txt`;
46+
fs.writeFileSync(file, 'four');
47+
t.is(await fileBytes(file), 4);
48+
sh.rm(file);
4449
});

0 commit comments

Comments
 (0)