import fs = require ( 'fs-extra' ) ; import path = require ( 'path' ) ; export class FileStore { type { string } root = './data' ; constructor ( root ? ) { if ( root ) { this . root = root ; } if ( ! fs . pathExistsSync ( this . root ) ) { fs . mkdirSync ( this . root ) ; } } author CaoMeiYouRen
     *  date param { string } key param { string } filename returns { Promise < any > } async get ( key , filename = 'global' ) { let file = { } ; const filepath = path . join ( this . root , `${ filename }.json` ) ; if ( await fs . pathExists ( filepath ) ) { try { file = await fs . readJSON ( filepath ) ; } catch ( error ) { } } return file [ key ] ; } author CaoMeiYouRen
     *  date param { string } key param { * } data param { string } filename returns { Promise < boolean > } async set ( key , data , filename = 'global' ) { try { let file = { } ; const filepath = path . join ( this . root , `${ filename }.json` ) ; if ( await fs . pathExists ( filepath ) ) { try { file = await fs . readJSON ( filepath ) ; } catch ( error ) { } } file [ key ] = data ; await fs . writeFile ( filepath , JSON . stringify ( file , null , 4 ) ) ; return true ; } catch ( error ) { console . error ( error ) ; return false ; } } } export const filestore = new FileStore ( ) ;