import * as Bluebird from "bluebird" ; import { ICacheItem , IStorage } from "node-ts-cache" ; const Fs = Bluebird . promisifyAll ( require ( "fs" ) ) ; export class NodeFsStorage implements IStorage { constructor ( public jsonFilePath ) { if ( ! Fs . existsSync ( this . jsonFilePath ) ) { this . createEmptyCache ( ) ; } } public async getItem ( key ) { return ( await this . getCacheObject ( ) ) [ key ] ; } public async setItem ( key , content ) { const cache = await this . getCacheObject ( ) ; cache [ key ] = content ; await this . setCache ( cache ) ; } public async clear ( ) { await this . createEmptyCache ( ) ; } private createEmptyCache ( ) { Fs . writeFileSync ( this . jsonFilePath , JSON . stringify ( { } ) ) ; } private async setCache ( newCache ) { await Fs . writeFileAsync ( this . jsonFilePath , JSON . stringify ( newCache ) ) ; } private async getCacheObject ( ) { return JSON . parse ( ( await Fs . readFileAsync ( this . jsonFilePath ) ) . toString ( ) ) ; } }