UNPKG

@paroicms/server

Version:
56 lines 2.14 kB
import { jsonTypeValidator } from "@paroicms/public-server-lib"; import { readFile } from "node:fs/promises"; import { join } from "node:path"; import { packageDir } from "../../context.js"; import { appendToSchemaLocales, createEmptyLocalizedTranslations, } from "./read-locales.js"; import { checkSiteSchemaFormatVersion } 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, }); 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 }) { const file = join(dir, filename); const json = await readFile(file, { encoding: "utf8", }); const data = JSON.parse(json); checkSiteSchemaFormatVersion(data.ParoiCMSSiteSchemaFormatVersion, { file }); const result = jsonTypeValidator.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