ConceptCreator/app/storage/storing/GamesystemStorage.js

58 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-03-20 19:35:24 +01:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GamesystemStorage = void 0;
const FileUtils_1 = require("../FileUtils");
const path = require("node:path");
const fs = require("fs");
class GamesystemStorage {
constructor(gamesystemRootDir) {
this.gamesystemRootDir = gamesystemRootDir;
2024-03-22 10:01:47 +01:00
FileUtils_1.FileUtils.prepareDirectoryFroWriting(gamesystemRootDir);
2024-03-20 19:35:24 +01:00
}
storeGamesystems(gamesystems) {
2024-03-21 08:43:28 +01:00
const unreferencedFiles = this.detectUnusedGamesystemFiles(gamesystems);
FileUtils_1.FileUtils.removeFiles(unreferencedFiles);
2024-03-20 19:35:24 +01:00
gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem));
}
2024-03-21 08:43:28 +01:00
detectUnusedGamesystemFiles(gamesystems) {
const unreferencedFiles = [];
const gamesystemFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.gamesystemRootDir);
while (gamesystemFiles.length > 0) {
const currentGamesystemFile = gamesystemFiles.shift();
const referencedGamesystemName = path.parse(path.basename(currentGamesystemFile)).name;
const referencedGamesystem = this.findReferencedGamesystem(referencedGamesystemName, gamesystems);
if (referencedGamesystem == undefined) {
unreferencedFiles.push(currentGamesystemFile);
2024-03-20 19:35:24 +01:00
}
else {
2024-03-21 08:43:28 +01:00
const decodedJSONData = JSON.parse(referencedGamesystem.jsonString);
if (decodedJSONData.childsystems != undefined) {
//Check if current file is a directory. When it is a directory, everything is fine
if (fs.lstatSync(currentGamesystemFile).isDirectory()) {
}
else {
const parentDirName = path.basename(path.dirname(currentGamesystemFile));
if (parentDirName !== referencedGamesystemName) {
unreferencedFiles.push(currentGamesystemFile);
}
2024-03-20 19:35:24 +01:00
}
}
}
2024-03-21 08:43:28 +01:00
if (fs.lstatSync(currentGamesystemFile).isDirectory()) {
gamesystemFiles.push(...FileUtils_1.FileUtils.listFilesInDirectory(currentGamesystemFile));
}
}
return unreferencedFiles;
}
findReferencedGamesystem(referencedName, gamesystems) {
return gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === referencedName);
2024-03-20 19:35:24 +01:00
}
storeGamesystem(gamesystem) {
const gamesystemFile = path.join(...gamesystem.fileName.split("/"));
const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile);
FileUtils_1.FileUtils.prepareFileForWriting(completeGamesystemFile);
fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8');
}
}
exports.GamesystemStorage = GamesystemStorage;
//# sourceMappingURL=GamesystemStorage.js.map