import { createWriteStream , WriteStream , mkdirSync } from 'fs' ; import { dirname } from 'path' ; import { RollingFileContext } from './rolling_file_context' ; /**
 * Delegate of the  RollingFileAppender used to manage the log file access
  export class RollingFileManager { private readonly filePath ; private outputStream ? : WriteStream ; constructor ( private readonly context ) { this . filePath = context . filePath ; } write ( chunk ) { const stream = this . ensureStreamOpen ( ) ; this . context . currentFileSize += Buffer . byteLength ( chunk , 'utf8' ) ; stream . write ( chunk ) ; } async closeStream ( ) { return new Promise < void > ( ( resolve ) => { if ( this . outputStream === undefined ) { return resolve ( ) ; } this . outputStream . end ( ( ) => { this . outputStream = undefined ; resolve ( ) ; } ) ; } ) ; } private ensureStreamOpen ( ) { if ( this . outputStream === undefined ) { this . ensureDirectory ( this . filePath ) ; this . outputStream = createWriteStream ( this . filePath , { encoding : 'utf8' , flags : 'a' , } ) ; this . context . refreshFileInfo ( ) ; } return this . outputStream ! ; } private ensureDirectory ( path ) { mkdirSync ( dirname ( path ) , { recursive : true } ) ; } }