UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

53 lines (51 loc) 3.07 kB
import { formatLocale, formatPath } from "./utils/formatter.mjs"; import { colorize, colorizeKey, colorizePath, getAppLogger, x } from "@intlayer/config/logger"; import * as ANSIColors from "@intlayer/config/colors"; import { getBasePlugins, getContent } from "@intlayer/core/interpreter"; //#region src/filterInvalidDictionaries.ts const isInvalidDictionary = (dictionary, configuration, options) => { const appLogger = getAppLogger(configuration); if (!dictionary) return false; const location = Boolean(dictionary.location === "local" || typeof dictionary.filePath === "string") ? "Local" : "Remote"; const hasKey = Boolean(dictionary.key); const hasContent = Boolean(dictionary.content); if (!hasKey) { appLogger(`${location} dictionary has no key`, { level: "error" }); return false; } if (!/^[\p{L}\p{N}]+([._-][\p{L}\p{N}]+)*$/u.test(dictionary.key)) { appLogger(`Insecure key ${colorizeKey(dictionary.key)} at ${dictionary.filePath ? formatPath(dictionary.filePath) : colorizePath("Remote")} - dictionary filtered out`, { level: "error" }); return false; } if (!hasContent) { appLogger(`${location} dictionary ${colorizeKey(dictionary.key)} has no content - ${dictionary.filePath ? formatPath(dictionary.filePath) : colorizePath("Remote")}`, { level: "error" }); return false; } if (dictionary.schema && options?.checkSchema) { if (!(typeof dictionary.content === "function" || typeof dictionary.content === "object" && dictionary.content !== null && typeof dictionary.content.then === "function")) { const locales = configuration?.internationalization?.locales ?? []; const isStrict = configuration?.internationalization.strictMode === "strict"; const schema = typeof dictionary.schema === "string" ? configuration?.schemas?.[dictionary.schema] : void 0; if (schema && typeof schema.safeParse === "function") for (const locale of locales) { const resolvedContent = getContent(dictionary.content, { dictionaryKey: dictionary.key, keyPath: [], locale }, getBasePlugins(locale, !isStrict)); const result = schema.safeParse(resolvedContent); if (!result.success) { appLogger(`${location} dictionary ${colorizeKey(dictionary.key)} has invalid content according to schema ${colorize(dictionary.schema, ANSIColors.ORANGE)} for locale ${formatLocale(locale)} - ${dictionary.filePath ? formatPath(dictionary.filePath) : colorizePath("Remote")}`, { level: "error" }); result.error.issues.forEach((issue) => { appLogger(`${x} Error: ${colorizeKey(dictionary.key)} - ${formatLocale(locale)} - ${colorize(`${issue.path.join(".")}:`, ANSIColors.BLUE)} ${colorize(issue.message, ANSIColors.GREY)}`, { level: "error" }); }); return false; } } } } return true; }; const filterInvalidDictionaries = (dictionaries, configuration, options) => (dictionaries ?? [])?.filter((dictionary) => isInvalidDictionary(dictionary, configuration, options)); //#endregion export { filterInvalidDictionaries, isInvalidDictionary }; //# sourceMappingURL=filterInvalidDictionaries.mjs.map