11const debug = require ( "debug" ) ( "streamroller:RollingFileWriteStream" ) ;
2- const _ = require ( "lodash" ) ;
32const async = require ( "async" ) ;
43const fs = require ( "fs-extra" ) ;
54const zlib = require ( "zlib" ) ;
@@ -146,7 +145,7 @@ class RollingFileWriteStream extends Writable {
146145 keepFileExt : false ,
147146 alwaysIncludePattern : false
148147 } ;
149- const options = _ . defaults ( { } , rawOptions , defaultOptions ) ;
148+ const options = Object . assign ( { } , defaultOptions , rawOptions ) ;
150149 if ( options . maxSize <= 0 ) {
151150 throw new Error ( `options.maxSize (${ options . maxSize } ) should be > 0` ) ;
152151 }
@@ -198,11 +197,13 @@ class RollingFileWriteStream extends Writable {
198197 _getExistingFiles ( cb ) {
199198 fs . readdir ( this . fileObject . dir , ( e , files ) => {
200199 debug ( `_getExistingFiles: files=${ files } ` ) ;
201- const existingFileDetails = _ . chain ( files )
200+ const existingFileDetails = ( files || [ ] )
202201 . map ( n => this . fileNameParser ( n ) )
203- . compact ( )
204- . sortBy ( n => ( n . timestamp ? n . timestamp : newNow ( ) . getTime ( ) ) - n . index )
205- . value ( ) ;
202+ . filter ( n => n !== undefined && n !== null ) ;
203+
204+ const getKey = n => ( n . timestamp ? n . timestamp : newNow ( ) . getTime ( ) ) - n . index
205+ existingFileDetails . sort ( ( a , b ) => getKey ( a ) - getKey ( b ) )
206+
206207 cb ( null , existingFileDetails ) ;
207208 } ) ;
208209 }
@@ -274,7 +275,11 @@ class RollingFileWriteStream extends Writable {
274275 date : this . state . currentDate ,
275276 index : 0
276277 } ) ;
277- const ops = _ . pick ( this . options , [ "flags" , "encoding" , "mode" ] ) ;
278+ const ops = {
279+ flags : this . options . flags ,
280+ encoding : this . options . encoding ,
281+ mode : this . options . mode ,
282+ } ;
278283 this . currentFileStream = fs . createWriteStream ( filePath , ops ) ;
279284 this . currentFileStream . on ( "error" , e => {
280285 this . emit ( "error" , e ) ;
@@ -291,8 +296,7 @@ class RollingFileWriteStream extends Writable {
291296 this . options . numToKeep > 0 &&
292297 existingFileDetails . length > this . options . numToKeep
293298 ) {
294- const fileNamesToRemove = _ . slice (
295- existingFileDetails . map ( f => f . filename ) ,
299+ const fileNamesToRemove = existingFileDetails . map ( f => f . filename ) . slice (
296300 0 ,
297301 existingFileDetails . length - this . options . numToKeep - 1
298302 ) ;
@@ -306,7 +310,7 @@ class RollingFileWriteStream extends Writable {
306310 _deleteFiles ( fileNames , done ) {
307311 debug ( `files to delete: ${ fileNames } ` ) ;
308312 async . each (
309- _ . map ( fileNames , f => path . format ( { dir : this . fileObject . dir , base : f } ) ) ,
313+ fileNames . map ( f => path . format ( { dir : this . fileObject . dir , base : f } ) ) ,
310314 fs . unlink ,
311315 done
312316 ) ;
0 commit comments