ConceptCreator/app/storage/storing/ItemStorage.js
Sebastian Böckelmann 8085b07264
All checks were successful
E2E Testing / test (push) Successful in 1m32s
Load Items
2024-04-20 10:55:50 +02:00

31 lines
1.2 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemStorage = void 0;
const FileUtils_1 = require("../FileUtils");
const path = require("node:path");
const fs = require("node:fs");
class ItemStorage {
constructor(itemDir) {
this.itemDir = itemDir;
FileUtils_1.FileUtils.prepareDirectoryFroWriting(this.itemDir);
}
storeItem(storedItems) {
this.persistDeletedItems(storedItems);
storedItems.forEach(item => this.storeSingleItem(item));
}
persistDeletedItems(existingItems) {
const itemFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.itemDir);
itemFiles.forEach(itemFile => {
const itemFileName = path.parse(path.basename(itemFile)).name;
if (existingItems.find(item => item.fileName === itemFileName) == undefined) {
fs.unlinkSync(itemFile);
}
});
}
storeSingleItem(item) {
const completeItemFile = path.join(this.itemDir, item.fileName + ".json");
fs.writeFileSync(completeItemFile, item.jsonString, 'utf-8');
}
}
exports.ItemStorage = ItemStorage;
//# sourceMappingURL=ItemStorage.js.map