UNPKG

@rr0/data

Version:
30 lines (29 loc) 1.07 kB
import path from "path"; import { findDirsContaining } from "@javarome/fileutil"; import { AbstractDataFactory } from "./AbstractDataFactory.js"; /** * A RR0Data factory which can read either <someType>.json files of index.json with a "type": "<someType>" property. */ export class TypedDataFactory extends AbstractDataFactory { constructor(eventFactory, type, fileNames = [type]) { super(eventFactory); this.type = type; this.fileNames = fileNames; } createFromFile(file) { const dataJson = JSON.parse(file.contents); const basename = path.basename(file.name); if (!dataJson.type) { // Guess type fromfile name dataJson.type = basename.substring(0, basename.indexOf(".")).toLowerCase(); } let datum; if (dataJson.type === this.type) { dataJson.dirName = path.dirname(file.name); datum = this.parse(dataJson); } return datum; } async getFiles() { return findDirsContaining(this.fileNames[0] + ".json", "out"); } }