32 lines
1.5 KiB
JavaScript
32 lines
1.5 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.CharacterStorage = void 0;
|
||
|
const FileUtils_1 = require("../FileUtils");
|
||
|
const path = require("node:path");
|
||
|
const fs = require("fs");
|
||
|
class CharacterStorage {
|
||
|
constructor(characterDir) {
|
||
|
this.characterDir = characterDir;
|
||
|
FileUtils_1.FileUtils.prepareDirectoryFroWriting(characterDir);
|
||
|
}
|
||
|
storeCharacters(storedCharacters) {
|
||
|
this.persistDeletedCharacters(storedCharacters);
|
||
|
storedCharacters.forEach(storedCharacter => this.storeSingleCharacter(storedCharacter));
|
||
|
}
|
||
|
persistDeletedCharacters(existingCharacters) {
|
||
|
const scriptAccountFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.characterDir);
|
||
|
scriptAccountFiles.forEach(characterFile => {
|
||
|
const scriptAccountFileName = path.parse(path.basename(characterFile)).name;
|
||
|
if (existingCharacters.find(character => character.fileName === scriptAccountFileName) == undefined) {
|
||
|
//No scriptAccountFile was found with that nae of the file. So the scriptAccount was deleted. Remove file
|
||
|
fs.unlinkSync(characterFile);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
storeSingleCharacter(character) {
|
||
|
const completeScriptAccountFile = path.join(this.characterDir, character.fileName + ".json");
|
||
|
fs.writeFileSync(completeScriptAccountFile, character.jsonString, 'utf-8');
|
||
|
}
|
||
|
}
|
||
|
exports.CharacterStorage = CharacterStorage;
|
||
|
//# sourceMappingURL=CharacterStorage.js.map
|