import {StoreComponent} from "../StoreComponent"; import {FileUtils} from "../FileUtils"; import * as path from "node:path"; import * as fs from "fs"; export class GamesystemStorage { private gamesystemRootDir: string constructor(gamesystemRootDir: string) { this.gamesystemRootDir = gamesystemRootDir; } public storeGamesystems(gamesystems: StoreComponent[]) { this.detectUnusedGamesystemFiles(this.gamesystemRootDir, gamesystems) gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem)) } private detectUnusedGamesystemFiles(gamesystemDir: string, gamesystems: StoreComponent[]) { const gamesystemFiles = FileUtils.listFilesInDirectory(gamesystemDir); gamesystemFiles.forEach(gamesystemFile => { if(fs.lstatSync(gamesystemFile).isDirectory()) { this.detectUnusedGamesystemFiles(gamesystemFile, gamesystems) } else { const currentFileName = path.parse(path.basename(gamesystemFile)).name const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === currentFileName); if(searchedGamesystem != undefined) { if(path.dirname(searchedGamesystem.fileName) === currentFileName) { //Aus Simple wurde Product => Delete .json fs.unlinkSync(gamesystemFile) } } else { fs.unlinkSync(gamesystemFile) } } }) } private storeGamesystem(gamesystem: StoreComponent) { const gamesystemFile = path.join(... gamesystem.fileName.split("/")) console.log(gamesystem.jsonString) const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile) FileUtils.prepareFileForWriting(completeGamesystemFile) fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8') } }