ConceptCreator/app/storage/storing/ItemgroupStorage.ts
Sebastian Böckelmann 73edf03fa0
All checks were successful
E2E Testing / test (push) Successful in 1m37s
Load Itemgroups
2024-05-09 13:33:26 +02:00

36 lines
1.1 KiB
TypeScript

import {FileUtils} from "../FileUtils";
import {StoreComponent} from "../StoreComponent";
import * as path from "node:path";
import * as fs from "node:fs";
export class ItemgroupStorage {
private itemgroupDir: string
constructor(itemgroupDir: string) {
this.itemgroupDir = itemgroupDir;
FileUtils.prepareDirectoryFroWriting(this.itemgroupDir);
}
public storeItemgroups(itemgroups: StoreComponent[]) {
itemgroups.forEach(itemgroup => {
this.storeItemgroup(itemgroup)
})
}
private storeItemgroup(itemgroup: StoreComponent) {
const file = path.join(... itemgroup.fileName.split("/"));
const completeFileName = path.join(this.itemgroupDir, file);
const itemgroupDirectory = path.join(... itemgroup.fileName.split("/").slice(0, -1))
const completeItemgroupDirectory = path.join(this.itemgroupDir, itemgroupDirectory)
FileUtils.prepareDirectoryFroWriting(completeItemgroupDirectory);
fs.writeFileSync(completeFileName + ".json", itemgroup.jsonString, "utf-8")
}
storeItems(storedItems: StoreComponent[]) {
storedItems.forEach(item => this.storeItemgroup(item))
}
}