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:
182182| ------ | ---------------- | ---------------------- |
183183| ` mode ` | number = 0o666 | Posix mode permissions |
184184
185+ ### ` fs.du(filepath, cb) `
186+
187+ Returns the size of a file or directory in bytes.
188+
185189### ` fs.promises `
186190
187191All 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", () => {
444444 } ) ;
445445 } ) ;
446446
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+
447477} ) ;
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ module.exports = class FS {
1515 constructor ( ...args ) {
1616 this . promises = new PromisifiedFS ( ...args )
1717 // Needed so things don't break if you destructure fs and pass individual functions around
18+ this . init = this . init . bind ( this )
1819 this . readFile = this . readFile . bind ( this )
1920 this . writeFile = this . writeFile . bind ( this )
2021 this . unlink = this . unlink . bind ( this )
@@ -27,6 +28,7 @@ module.exports = class FS {
2728 this . readlink = this . readlink . bind ( this )
2829 this . symlink = this . symlink . bind ( this )
2930 this . backFile = this . backFile . bind ( this )
31+ this . du = this . du . bind ( this )
3032 }
3133 init ( name , options ) {
3234 this . promises . init ( name , options )
@@ -79,4 +81,8 @@ module.exports = class FS {
7981 const [ resolve , reject ] = wrapCallback ( opts , cb ) ;
8082 this . promises . backFile ( filepath , opts ) . then ( resolve ) . catch ( reject ) ;
8183 }
84+ du ( filepath , cb ) {
85+ const [ resolve , reject ] = wrapCallback ( cb ) ;
86+ this . promises . du ( filepath ) . then ( resolve ) . catch ( reject ) ;
87+ }
8288}
You can’t perform that action at this time.
0 commit comments