import fs , { promises as fsAsync } from 'fs' ; const logsDirectory = '.logs' ; export const logToFile = async ( fileName , object , options = { } ) => { const { mode = 'append' , timestamp = false } = options ; const path = __dirname . split ( '/' ) . slice ( 1 , - 1 ) . join ( '/' ) ; const logsDirPath = `/${ path }/${ logsDirectory }` ; if ( ! fs . existsSync ( logsDirPath ) ) { fs . mkdirSync ( logsDirPath , { recursive : true } ) ; } const fullPath = `${ logsDirPath }/${ fileName }` ; const contents = typeof object === 'string' ? object : JSON . stringify ( object , null , 2 ) ; const now = new Date ( ) ; const text = timestamp ? now . toString ( ) + '\n---\n' + contents : contents ; if ( mode === 'append' ) { const stream = fs . createWriteStream ( fullPath , { flags : 'a' } ) ; stream . write ( text + '\n' ) ; stream . end ( ) ; } else { fs . readFile ( fullPath , ( error , data ) => { let shouldWrite = false ; if ( error && error . code === 'ENOENT' ) { shouldWrite = true ; } else if ( error ) { throw error ; } else { const fileContent = data . toString ( ) ; if ( fileContent !== text ) { shouldWrite = true ; } } if ( shouldWrite ) { fs . writeFile ( fullPath , text , error => { if ( error ) throw error ; console . log ( `Log saved to ${ fullPath }` ) ; } ) ; } } ) ; } } ;