@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
57 lines (55 loc) • 3.89 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
const require_utils_formatter = require('./utils/formatter.cjs');
let _intlayer_config_logger = require("@intlayer/config/logger");
let _intlayer_config_colors = require("@intlayer/config/colors");
_intlayer_config_colors = require_runtime.__toESM(_intlayer_config_colors);
let _intlayer_core_interpreter = require("@intlayer/core/interpreter");
//#region src/filterInvalidDictionaries.ts
const isInvalidDictionary = (dictionary, configuration, options) => {
const appLogger = (0, _intlayer_config_logger.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 ${(0, _intlayer_config_logger.colorizeKey)(dictionary.key)} at ${dictionary.filePath ? require_utils_formatter.formatPath(dictionary.filePath) : (0, _intlayer_config_logger.colorizePath)("Remote")} - dictionary filtered out`, { level: "error" });
return false;
}
if (!hasContent) {
appLogger(`${location} dictionary ${(0, _intlayer_config_logger.colorizeKey)(dictionary.key)} has no content - ${dictionary.filePath ? require_utils_formatter.formatPath(dictionary.filePath) : (0, _intlayer_config_logger.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 = (0, _intlayer_core_interpreter.getContent)(dictionary.content, {
dictionaryKey: dictionary.key,
keyPath: [],
locale
}, (0, _intlayer_core_interpreter.getBasePlugins)(locale, !isStrict));
const result = schema.safeParse(resolvedContent);
if (!result.success) {
appLogger(`${location} dictionary ${(0, _intlayer_config_logger.colorizeKey)(dictionary.key)} has invalid content according to schema ${(0, _intlayer_config_logger.colorize)(dictionary.schema, _intlayer_config_colors.ORANGE)} for locale ${require_utils_formatter.formatLocale(locale)} - ${dictionary.filePath ? require_utils_formatter.formatPath(dictionary.filePath) : (0, _intlayer_config_logger.colorizePath)("Remote")}`, { level: "error" });
result.error.issues.forEach((issue) => {
appLogger(`${_intlayer_config_logger.x} Error: ${(0, _intlayer_config_logger.colorizeKey)(dictionary.key)} - ${require_utils_formatter.formatLocale(locale)} - ${(0, _intlayer_config_logger.colorize)(`${issue.path.join(".")}:`, _intlayer_config_colors.BLUE)} ${(0, _intlayer_config_logger.colorize)(issue.message, _intlayer_config_colors.GREY)}`, { level: "error" });
});
return false;
}
}
}
}
return true;
};
const filterInvalidDictionaries = (dictionaries, configuration, options) => (dictionaries ?? [])?.filter((dictionary) => isInvalidDictionary(dictionary, configuration, options));
//#endregion
exports.filterInvalidDictionaries = filterInvalidDictionaries;
exports.isInvalidDictionary = isInvalidDictionary;
//# sourceMappingURL=filterInvalidDictionaries.cjs.map