import fs from 'fs' ; import path from 'path' ; import makeDir from 'make-dir' ; export function createHistorian ( filePath ) { const directoryPath = path . dirname ( filePath ) ; makeDir . sync ( directoryPath ) ; fs . closeSync ( fs . openSync ( filePath , 'a' ) ) ; const lines = fs . readFileSync ( filePath , 'utf-8' ) . trim ( ) . split ( '\n' ) ; if ( lines . length > 1000 ) { fs . writeFileSync ( filePath , lines . slice ( - 1000 ) . join ( '\n' ) + '\n' ) ; } const history = lines . slice ( - 1000 ) . reverse ( ) ; function commit ( line ) { history . unshift ( line ) ; const fd = fs . openSync ( filePath , 'a' ) ; fs . writeSync ( fd , `${ line }\n` ) ; fs . closeSync ( fd ) ; } return { commit , history } ; }