import { join } from 'path' ; import { readdir , rename , unlink , access } from 'fs/promises' ; import { getFileNameMatcher , getRollingFileName } from './pattern_matcher' ; export const shouldSkipRollout = async ( { logFilePath } ) => { try { await access ( logFilePath ) ; return false ; } catch ( e ) { return true ; } } ; /**
 * Returns the rolled file basenames, from the most recent to the oldest.
 */ export const getOrderedRolledFiles = async ( { logFileBaseName , logFileFolder , pattern , } ) : Promise < string [ ] > => { const matcher = getFileNameMatcher ( logFileBaseName , pattern ) ; const dirContent = await readdir ( logFileFolder ) ; return dirContent . map ( ( fileName ) => ( { fileName , index : matcher ( fileName ) , } ) ) . filter ( ( { index } ) => index !== undefined ) . sort ( ( a , b ) => a . index ! - b . index ! ) . map ( ( { fileName } ) => fileName ) ; } ; export const deleteFiles = async ( { logFileFolder , filesToDelete , } ) => { await Promise . all ( filesToDelete . map ( ( fileToDelete ) => unlink ( join ( logFileFolder , fileToDelete ) ) ) ) ; } ; export const rollPreviousFilesInOrder = async ( { filesToRoll , logFileFolder , logFileBaseName , pattern , } ) => { for ( let i = filesToRoll . length - 1 ; i >= 0 ; i -- ) { const oldFileName = filesToRoll [ i ] ; const newFileName = getRollingFileName ( logFileBaseName , pattern , i + 2 ) ; await rename ( join ( logFileFolder , oldFileName ) , join ( logFileFolder , newFileName ) ) ; } } ; export const rollCurrentFile = async ( { logFileFolder , logFileBaseName , pattern , } ) => { const rolledBaseName = getRollingFileName ( logFileBaseName , pattern , 1 ) ; await rename ( join ( logFileFolder , logFileBaseName ) , join ( logFileFolder , rolledBaseName ) ) ; } ;