UNPKG

@paroicms/connector

Version:

An API to help build management tools for ParoiCMS instances

57 lines 2.22 kB
import { readFile } from "node:fs/promises"; import { join } from "node:path"; import { siteSchemaFormatVersion } from "../constants.js"; import { packageDir, typeValidator } from "../context.js"; import { appendToSchemaLocales, createEmptyLocalizedTranslations, } from "./read-locales.js"; import { checkSiteSchemaVersion } from "./site-schema-helpers.js"; import { readSiteSchemaLibrary } from "./site-schema-lib-reader.js"; const siteSchemaDirName = join(packageDir, "site-schema-lib"); export async function readCommonSchemaLibrary() { return { common: await readCommonSchema(), defaultLib: await readSiteSchemaLibrary(siteSchemaDirName), }; } async function readCommonSchema() { const schema = await readJtInternalCommonSchema({ filename: "common-schema.json", dir: siteSchemaDirName, siteSchemaVersion: siteSchemaFormatVersion, }); const l10n = createEmptyLocalizedTranslations(schema.languages); await appendToSchemaLocales({ dir: siteSchemaDirName, baseFileName: "common-schema.l10n", jsonTOnly: "JtInternalCommonSchemaTranslations", }, l10n); return { ...schema, l10n, languageLabels: makeCommonLanguageLabels(l10n), }; } function makeCommonLanguageLabels(l10n) { const labels = {}; for (const language of Object.keys(l10n)) { const label = l10n[language].languageLabel; if (typeof label !== "string") { throw new Error(`missing common 'languageLabel' for '${language}'`); } labels[language] = label; } return labels; } async function readJtInternalCommonSchema({ dir, filename, siteSchemaVersion, }) { const file = join(dir, filename); const json = await readFile(file, { encoding: "utf8", }); const data = JSON.parse(json); checkSiteSchemaVersion(data.ParoiCMSSiteSchemaFormatVersion, { file }, siteSchemaVersion); const result = typeValidator.validate("JtInternalCommonSchema", data); if (!result.valid) { throw new Error(`invalid common-schema file '${filename}': ${result.error ?? "(missing message)"}`); } return data; } //# sourceMappingURL=common-schema-reader.js.map