ConceptCreator/app/storage/storing/GamesystemStorage.ts
Sebastian Böckelmann e6073d4f2f
Some checks failed
E2E Testing / test (push) Failing after 1m41s
Save Gamesystems
2024-03-20 19:35:24 +01:00

48 lines
1.8 KiB
TypeScript

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')
}
}