Delete Unreferenced Gamesystems
Some checks failed
E2E Testing / test (push) Failing after 1m42s

This commit is contained in:
Sebastian Böckelmann 2024-03-21 08:43:28 +01:00
parent e6073d4f2f
commit c184c667fd
7 changed files with 89 additions and 35 deletions

View File

@ -5,6 +5,7 @@ import {GameModelLoader} from "./storage/loader/GameModelLoader";
import {StoredGameModel} from "./storage/StoredGameModel"; import {StoredGameModel} from "./storage/StoredGameModel";
import {ScriptAccountStorage} from "./storage/storing/ScriptAccountStoring"; import {ScriptAccountStorage} from "./storage/storing/ScriptAccountStoring";
import {ModelComponentFileDirectory} from "./storage/ModelComponentFileDirectory"; import {ModelComponentFileDirectory} from "./storage/ModelComponentFileDirectory";
import {GamesystemStorage} from "./storage/storing/GamesystemStorage";
let win: BrowserWindow | null = null; let win: BrowserWindow | null = null;
const args = process.argv.slice(1), const args = process.argv.slice(1),
@ -215,6 +216,9 @@ function saveProject() {
function recieveGameModelToStore(gameModel: StoredGameModel) { function recieveGameModelToStore(gameModel: StoredGameModel) {
const scriptAccountStorage = new ScriptAccountStorage(path.join(projectDirectory, ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME)) const scriptAccountStorage = new ScriptAccountStorage(path.join(projectDirectory, ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME))
scriptAccountStorage.storeScriptAccounts(gameModel.storedScriptAccounts) scriptAccountStorage.storeScriptAccounts(gameModel.storedScriptAccounts)
const gamesystemStorage = new GamesystemStorage(path.join(projectDirectory, ModelComponentFileDirectory.GAMESYSTEM_DIR_NAME))
gamesystemStorage.storeGamesystems(gameModel.storedGamesystems)
} }
/*function deleteComponent(component: DeleteModel) { /*function deleteComponent(component: DeleteModel) {

View File

@ -19,6 +19,16 @@ class FileUtils {
(0, fs_1.mkdirSync)(parentDirectory, { recursive: true }); (0, fs_1.mkdirSync)(parentDirectory, { recursive: true });
} }
} }
static removeFiles(files) {
files.forEach(file => {
if (fs.lstatSync(file).isDirectory()) {
fs.rmdirSync(file);
}
else {
fs.unlinkSync(file);
}
});
}
} }
exports.FileUtils = FileUtils; exports.FileUtils = FileUtils;
//# sourceMappingURL=FileUtils.js.map //# sourceMappingURL=FileUtils.js.map

View File

@ -1,6 +1,7 @@
import * as fs from "fs"; import * as fs from "fs";
import * as path from "node:path"; import * as path from "node:path";
import {mkdirSync} from "fs"; import {mkdirSync} from "fs";
import {lstatSync} from "node:fs";
export class FileUtils { export class FileUtils {
@ -19,4 +20,14 @@ export class FileUtils {
mkdirSync(parentDirectory, {recursive: true}) mkdirSync(parentDirectory, {recursive: true})
} }
} }
public static removeFiles(files: string[]) {
files.forEach(file => {
if(fs.lstatSync(file).isDirectory()) {
fs.rmdirSync(file)
} else {
fs.unlinkSync(file);
}
})
}
} }

View File

@ -9,33 +9,46 @@ class GamesystemStorage {
this.gamesystemRootDir = gamesystemRootDir; this.gamesystemRootDir = gamesystemRootDir;
} }
storeGamesystems(gamesystems) { storeGamesystems(gamesystems) {
this.detectUnusedGamesystemFiles(this.gamesystemRootDir, gamesystems); const unreferencedFiles = this.detectUnusedGamesystemFiles(gamesystems);
FileUtils_1.FileUtils.removeFiles(unreferencedFiles);
gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem)); gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem));
} }
detectUnusedGamesystemFiles(gamesystemDir, gamesystems) { detectUnusedGamesystemFiles(gamesystems) {
const gamesystemFiles = FileUtils_1.FileUtils.listFilesInDirectory(gamesystemDir); const unreferencedFiles = [];
gamesystemFiles.forEach(gamesystemFile => { const gamesystemFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.gamesystemRootDir);
if (fs.lstatSync(gamesystemFile).isDirectory()) { while (gamesystemFiles.length > 0) {
this.detectUnusedGamesystemFiles(gamesystemFile, gamesystems); 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 { else {
const currentFileName = path.parse(path.basename(gamesystemFile)).name; const decodedJSONData = JSON.parse(referencedGamesystem.jsonString);
const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === currentFileName); if (decodedJSONData.childsystems != undefined) {
if (searchedGamesystem != undefined) { //Check if current file is a directory. When it is a directory, everything is fine
if (path.dirname(searchedGamesystem.fileName) === currentFileName) { if (fs.lstatSync(currentGamesystemFile).isDirectory()) {
//Aus Simple wurde Product => Delete .json }
fs.unlinkSync(gamesystemFile); else {
const parentDirName = path.basename(path.dirname(currentGamesystemFile));
if (parentDirName !== referencedGamesystemName) {
unreferencedFiles.push(currentGamesystemFile);
}
} }
} }
else {
fs.unlinkSync(gamesystemFile);
}
} }
}); if (fs.lstatSync(currentGamesystemFile).isDirectory()) {
gamesystemFiles.push(...FileUtils_1.FileUtils.listFilesInDirectory(currentGamesystemFile));
}
}
return unreferencedFiles;
}
findReferencedGamesystem(referencedName, gamesystems) {
const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === referencedName);
return searchedGamesystem;
} }
storeGamesystem(gamesystem) { storeGamesystem(gamesystem) {
const gamesystemFile = path.join(...gamesystem.fileName.split("/")); const gamesystemFile = path.join(...gamesystem.fileName.split("/"));
console.log(gamesystem.jsonString);
const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile); const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile);
FileUtils_1.FileUtils.prepareFileForWriting(completeGamesystemFile); FileUtils_1.FileUtils.prepareFileForWriting(completeGamesystemFile);
fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8'); fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8');

View File

@ -13,33 +13,50 @@ export class GamesystemStorage {
} }
public storeGamesystems(gamesystems: StoreComponent[]) { public storeGamesystems(gamesystems: StoreComponent[]) {
this.detectUnusedGamesystemFiles(this.gamesystemRootDir, gamesystems) const unreferencedFiles = this.detectUnusedGamesystemFiles(gamesystems)
FileUtils.removeFiles(unreferencedFiles)
gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem)) gamesystems.forEach(gamesystem => this.storeGamesystem(gamesystem))
} }
private detectUnusedGamesystemFiles(gamesystemDir: string, gamesystems: StoreComponent[]) { private detectUnusedGamesystemFiles(gamesystems: StoreComponent[]) {
const gamesystemFiles = FileUtils.listFilesInDirectory(gamesystemDir); const unreferencedFiles: string[] = []
gamesystemFiles.forEach(gamesystemFile => { const gamesystemFiles = FileUtils.listFilesInDirectory(this.gamesystemRootDir);
if(fs.lstatSync(gamesystemFile).isDirectory()) { while(gamesystemFiles.length > 0) {
this.detectUnusedGamesystemFiles(gamesystemFile, gamesystems) 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 { } else {
const currentFileName = path.parse(path.basename(gamesystemFile)).name const decodedJSONData = JSON.parse(referencedGamesystem!.jsonString)
const searchedGamesystem = gamesystems.find(gamesystem => path.basename(gamesystem.fileName) === currentFileName); if(decodedJSONData.childsystems != undefined) {
if(searchedGamesystem != undefined) { //Check if current file is a directory. When it is a directory, everything is fine
if(path.dirname(searchedGamesystem.fileName) === currentFileName) { if(fs.lstatSync(currentGamesystemFile!).isDirectory()) {
//Aus Simple wurde Product => Delete .json
fs.unlinkSync(gamesystemFile) } else {
const parentDirName = path.basename(path.dirname(currentGamesystemFile!))
if(parentDirName !== referencedGamesystemName) {
unreferencedFiles.push(currentGamesystemFile!)
}
} }
} else {
fs.unlinkSync(gamesystemFile)
} }
} }
})
if(fs.lstatSync(currentGamesystemFile!).isDirectory()) {
gamesystemFiles.push(... FileUtils.listFilesInDirectory(currentGamesystemFile!))
}
}
return unreferencedFiles;
}
private findReferencedGamesystem(referencedName: string, gamesystems: StoreComponent[]): StoreComponent | undefined {
return gamesystems.find(gamesystem =>
path.basename(gamesystem.fileName) === referencedName)
} }
private storeGamesystem(gamesystem: StoreComponent) { private storeGamesystem(gamesystem: StoreComponent) {
const gamesystemFile = path.join(... gamesystem.fileName.split("/")) const gamesystemFile = path.join(... gamesystem.fileName.split("/"))
console.log(gamesystem.jsonString)
const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile) const completeGamesystemFile = path.join(this.gamesystemRootDir, gamesystemFile)
FileUtils.prepareFileForWriting(completeGamesystemFile) FileUtils.prepareFileForWriting(completeGamesystemFile)
fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8') fs.writeFileSync(completeGamesystemFile + ".json", gamesystem.jsonString, 'utf-8')

View File

@ -106,7 +106,7 @@ export class AppComponent implements OnInit{
this.gameModel!.removeScriptAccount(affectedModelComponent); this.gameModel!.removeScriptAccount(affectedModelComponent);
//this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.SCRIPTACCOUNT)) //this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.SCRIPTACCOUNT))
} else if(affectedModelComponent instanceof Gamesystem) { } else if(affectedModelComponent instanceof Gamesystem) {
//this.gameModel!.removeGamesystem(affectedModelComponent); this.gameModel!.removeGamesystem(affectedModelComponent);
//this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.GAMESYTEM)) //this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.GAMESYTEM))
this.gamesystemOverview!.refresh() this.gamesystemOverview!.refresh()
} }

View File

@ -39,7 +39,6 @@ export class GamesystemSerializer {
} }
}, SerializeConstants.JSON_INDENT) }, SerializeConstants.JSON_INDENT)
console.log(jsonString)
return new StoreComponent(jsonString, fileName, ModelComponentType.GAMESYTEM) return new StoreComponent(jsonString, fileName, ModelComponentType.GAMESYTEM)
} }