ConceptCreator/app/storage/storing/ScriptAccountStoring.ts
Sebastian Böckelmann 0884709603
All checks were successful
E2E Testing / test (push) Successful in 1m41s
Persist Characters
2024-03-22 10:01:47 +01:00

38 lines
1.4 KiB
TypeScript

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;
FileUtils.prepareDirectoryFroWriting(this.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')
}
}