File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -182,6 +182,10 @@ Options object:
182
182
| ------ | ---------------- | ---------------------- |
183
183
| ` mode ` | number = 0o666 | Posix mode permissions |
184
184
185
+ ### ` fs.du(filepath, cb) `
186
+
187
+ Returns the size of a file or directory in bytes.
188
+
185
189
### ` fs.promises `
186
190
187
191
All the same functions as above, but instead of passing a callback they return a promise.
Original file line number Diff line number Diff line change @@ -444,4 +444,34 @@ describe("fs module", () => {
444
444
} ) ;
445
445
} ) ;
446
446
447
+ describe ( "du" , ( ) => {
448
+ it ( "du returns the total file size of a path" , done => {
449
+ fs . mkdir ( "/du" , ( ) => {
450
+ fs . writeFile ( "/du/a.txt" , "hello" , ( ) => {
451
+ fs . writeFile ( "/du/b.txt" , "hello" , ( ) => {
452
+ fs . mkdir ( "/du/sub" , ( ) => {
453
+ fs . writeFile ( "/du/sub/a.txt" , "hello" , ( ) => {
454
+ fs . writeFile ( "/du/sub/b.txt" , "hello" , ( ) => {
455
+ fs . du ( "/du/sub/a.txt" , ( err , size ) => {
456
+ expect ( err ) . toBe ( null )
457
+ expect ( size ) . toBe ( 5 )
458
+ fs . du ( "/du/sub" , ( err , size ) => {
459
+ expect ( err ) . toBe ( null )
460
+ expect ( size ) . toBe ( 10 )
461
+ fs . du ( "/du" , ( err , size ) => {
462
+ expect ( err ) . toBe ( null )
463
+ expect ( size ) . toBe ( 20 )
464
+ done ( ) ;
465
+ } ) ;
466
+ } ) ;
467
+ } ) ;
468
+ } ) ;
469
+ } ) ;
470
+ } ) ;
471
+ } ) ;
472
+ } ) ;
473
+ } ) ;
474
+ } ) ;
475
+ } ) ;
476
+
447
477
} ) ;
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ module.exports = class FS {
15
15
constructor ( ...args ) {
16
16
this . promises = new PromisifiedFS ( ...args )
17
17
// Needed so things don't break if you destructure fs and pass individual functions around
18
+ this . init = this . init . bind ( this )
18
19
this . readFile = this . readFile . bind ( this )
19
20
this . writeFile = this . writeFile . bind ( this )
20
21
this . unlink = this . unlink . bind ( this )
@@ -27,6 +28,7 @@ module.exports = class FS {
27
28
this . readlink = this . readlink . bind ( this )
28
29
this . symlink = this . symlink . bind ( this )
29
30
this . backFile = this . backFile . bind ( this )
31
+ this . du = this . du . bind ( this )
30
32
}
31
33
init ( name , options ) {
32
34
this . promises . init ( name , options )
@@ -79,4 +81,8 @@ module.exports = class FS {
79
81
const [ resolve , reject ] = wrapCallback ( opts , cb ) ;
80
82
this . promises . backFile ( filepath , opts ) . then ( resolve ) . catch ( reject ) ;
81
83
}
84
+ du ( filepath , cb ) {
85
+ const [ resolve , reject ] = wrapCallback ( cb ) ;
86
+ this . promises . du ( filepath ) . then ( resolve ) . catch ( reject ) ;
87
+ }
82
88
}
You can’t perform that action at this time.
0 commit comments