58 lines
2.9 KiB
JavaScript
58 lines
2.9 KiB
JavaScript
"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;
|
|
FileUtils_1.FileUtils.prepareDirectoryFroWriting(gamesystemRootDir);
|
|
}
|
|
storeGamesystems(gamesystems) {
|
|
const unreferencedFiles = this.detectUnusedGamesystemFiles(gamesystems);
|
|
FileUtils_1.FileUtils.removeFiles(unreferencedFiles);
|
|
gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem));
|
|
}
|
|
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);
|
|
}
|
|
else {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
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
|