ConceptCreator/app/storage/storing/ItemgroupStorage.ts
Sebastian Böckelmann fed0ca99bf
All checks were successful
E2E Testing / test (push) Successful in 1m33s
Persist Itemgroups
2024-05-09 09:12:46 +02:00

35 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[]) {
console.log("Store Itemgroups")
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)
console.log(completeItemgroupDirectory)
FileUtils.prepareDirectoryFroWriting(completeItemgroupDirectory);
fs.writeFileSync(completeFileName + ".json", itemgroup.jsonString, "utf-8")
}
}