import * as fs from "fs-extra" ; import * as path from "path" ; export class StorageService { constructor ( public baseDirectory ) { } public initSync ( ) { return fs . ensureDirSync ( this . baseDirectory ) ; } public getItemSync < T > ( itemName ) { const filePath = path . resolve ( this . baseDirectory , itemName ) ; if ( ! fs . pathExistsSync ( filePath ) ) { return null ; } return fs . readJsonSync ( filePath ) ; } public async getItem < T > ( itemName ) { const filePath = path . resolve ( this . baseDirectory , itemName ) ; if ( ! await fs . pathExists ( filePath ) ) { return null ; } return await fs . readJson ( filePath ) ; } public setItemSync ( itemName , data ) { return fs . writeJsonSync ( path . resolve ( this . baseDirectory , itemName ) , data ) ; } public setItem ( itemName , data ) { return fs . writeJson ( path . resolve ( this . baseDirectory , itemName ) , data ) ; } public copyItem ( srcItemName , destItemName ) { return fs . copyFile ( path . resolve ( this . baseDirectory , srcItemName ) , path . resolve ( this . baseDirectory , destItemName ) ) ; } public copyItemSync ( srcItemName , destItemName ) { return fs . copyFileSync ( path . resolve ( this . baseDirectory , srcItemName ) , path . resolve ( this . baseDirectory , destItemName ) ) ; } public removeItemSync ( itemName ) { return fs . removeSync ( path . resolve ( this . baseDirectory , itemName ) ) ; } }