import { readdirSync , unlinkSync } from 'fs' ; import * as _ from 'lodash' ; import moment from 'moment' ; import { findLogPath } from 'root/app/log-rotate/find_log_path' ; const format = 'YYYY-MM-DD' ; interface YearMonthDayInterface { year : string ; month : string ; day : string ; } export function deleteLogFiles ( howManyDaysAgo , appName ) { const path = findLogPath ( appName ) ; let files = [ ] ; try { files = readdirSync ( path ) ; } catch ( e ) { return ; } _ . forEach ( files , ( file , index ) => { const yearMonthDay = getYearMonthDay ( file ) ; if ( ! yearMonthDay ) { return ; } if ( ! isBefore ( howManyDaysAgo , yearMonthDay ) ) { return ; } deleteLogFile ( path + file ) ; } ) ; } function getYearMonthDay ( file ) { const numberOfElements = 3 ; const split = _ . split ( file , '-' , numberOfElements ) ; if ( split . length < numberOfElements ) { return undefined ; } return { year : split [ 0 ] , month : split [ 1 ] , day : _ . split ( split [ 2 ] , '_' , 1 ) [ 0 ] , } as YearMonthDayInterface ; } function isBefore ( howManyDaysAgo , yearMonthDay ) { const agoDays = moment ( moment ( ) . format ( format ) ) . subtract ( howManyDaysAgo , 'days' ) ; const fileDays = moment ( [ yearMonthDay . year , yearMonthDay . month , yearMonthDay . day ] . join ( '-' ) ) ; return moment ( fileDays ) . isBefore ( agoDays ) ; } function deleteLogFile ( filePath ) { try { unlinkSync ( filePath ) ; } catch ( e ) { return ; } }