ConceptCreator/app/storage/storing/ScriptAccountStoring.ts

37 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-03-20 16:23:37 +01:00
import {StoreComponent} from "../StoreComponent";
import {FileUtils} from "../FileUtils";
import * as path from "node:path";
import * as fs from "fs";
export class ScriptAccountStorage {
private scriptAccountDir: string
constructor(scriptAccountDir: string) {
this.scriptAccountDir = scriptAccountDir;
}
storeScriptAccounts(scriptAccounts: StoreComponent[]) {
this.persistDeletedScriptAccounts(scriptAccounts)
scriptAccounts.forEach(scriptAccount => {
this.storeScriptAccount(scriptAccount)
})
}
private persistDeletedScriptAccounts(existingScriptAccount: StoreComponent[]) {
const scriptAccountFiles = FileUtils.listFilesInDirectory(this.scriptAccountDir);
scriptAccountFiles.forEach(scriptAccountFile => {
const scriptAccountFileName = path.parse(path.basename(scriptAccountFile)).name
if(existingScriptAccount.find(scriptAccount => scriptAccount.fileName === scriptAccountFileName) == undefined) {
//No scriptAccountFile was found with that nae of the file. So the scriptAccount was deleted. Remove file
fs.unlinkSync(scriptAccountFile)
}
})
}
private storeScriptAccount(scriptAccount: StoreComponent) {
const completeScriptAccountFile = path.join(this.scriptAccountDir, scriptAccount.fileName + ".json")
fs.writeFileSync(completeScriptAccountFile, scriptAccount.jsonString, 'utf-8')
}
}