UNPKG

@rr0/cms

Version:

RR0 Content Management System (CMS)

81 lines (80 loc) 4.38 kB
import path from "path"; import { RR0ContextImpl } from "../RR0Context.js"; import { HtmlFileContents } from "ssg-api"; import { cities, CityService, CmsOrganizationFactory, countries, departments, DepartmentService, OrganizationService, regions, RegionService } from "../org/index.js"; import { CaseFactory } from "../science/index.js"; import { APIFactory } from "../tech/index.js"; import { TimeTestUtil } from "../time/TimeTestUtil.js"; import { FileContents } from "@javarome/fileutil"; import { AllDataService, EventDataFactory, PeopleFactory, RR0EventFactory, TypedDataFactory } from "@rr0/data"; import { CountryService } from "../org/country/CountryService.js"; import { TimeContext } from "../time/TimeContext.mjs"; export class CMSTestUtil { constructor(rootDir = "test", outDir = "out", orgFiles = []) { this.rootDir = rootDir; this.outDir = outDir; this.config = { getOutputPath: (context) => { return path.join(this.outDir, context.file.name); } }; this.intlOptions = { year: "numeric", month: "long", day: "numeric", weekday: "long", hour: "2-digit", minute: "2-digit", timeZoneName: "short" }; const eventFactory = new RR0EventFactory(); const sightingFactory = new EventDataFactory(eventFactory, ["sighting"], ["index"]); const orgFactory = this.orgFactory = new CmsOrganizationFactory(eventFactory); this.caseFactory = new CaseFactory(eventFactory); this.peopleFactory = new PeopleFactory(eventFactory); const apiFactory = new APIFactory(eventFactory); const bookFactory = new TypedDataFactory(eventFactory, "book"); const articleFactory = new TypedDataFactory(eventFactory, "article"); const dataService = this.dataService = new AllDataService([orgFactory, this.caseFactory, this.peopleFactory, bookFactory, articleFactory, sightingFactory, apiFactory]); dataService.getFromDir("", ["people", "case"]).then(data => { // console.debug(data) }); const orgConfig = { rootDir: this.filePath("org"), files: orgFiles }; this.orgService = new OrganizationService(dataService, orgFactory, orgConfig, undefined, []); this.time = new TimeTestUtil(this); const countryService = this.countryService = new CountryService(dataService, orgFactory, orgConfig, undefined, countries); const regionService = this.regionService = new RegionService(dataService, orgFactory, orgConfig, countryService, regions); const departmentService = this.departmentService = new DepartmentService(dataService, orgFactory, orgConfig, regionService, departments); this.cityService = new CityService(dataService, orgFactory, orgConfig, departmentService, cities); } newContext(inputFileName, contents, locale = "fr") { const context = new RR0ContextImpl(locale, new TimeContext(), this.config); if (contents !== undefined && contents != null) { const langInfo = FileContents.getLang(inputFileName); context.file = new FileContents(inputFileName, "utf8", contents, new Date(), langInfo); } else { context.file = FileContents.read(inputFileName); } context.file = context.file; // By default return context; } filePath(inputFileName) { return path.join(this.rootDir, inputFileName); } newHtmlContext(inputFileName, contents, locale = "fr") { const context = this.newContext(this.filePath(inputFileName), contents, locale); const titleExec = /<title>(.*)<\/title>/.exec(contents); const title = titleExec && titleExec.length > 0 ? titleExec[1].trim() : undefined; const currentFile = context.file; const lang = currentFile.lang; const htmlFile = new HtmlFileContents(currentFile.name, currentFile.encoding, currentFile.contents, currentFile.lastModified, lang); context.file = htmlFile; htmlFile.title = title; const htmlContext = context; const timeContext = this.time.getService().contextFromFileName(htmlContext, inputFileName); Object.assign(htmlContext.time, timeContext); return htmlContext; } } export const cmsTestUtil = new CMSTestUtil();