UNPKG

@intlayer/chokidar

Version:

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

69 lines 2.56 kB
import { ESMxCJSRequire, getAppLogger, getConfiguration } from "@intlayer/config"; import { fetchDistantDictionaryKeys } from "../fetchDistantDictionaryKeys.mjs"; import { logger } from "../log.mjs"; import { sortAlphabetically } from "../utils/sortAlphabetically.mjs"; import { loadContentDeclarations } from "./loadContentDeclaration.mjs"; import { loadDistantDictionaries } from "./loadDistantDictionaries.mjs"; const loadDictionaries = async (contentDeclarationsPaths, configuration = getConfiguration(), projectRequire = ESMxCJSRequire) => { try { const appLogger = getAppLogger(configuration); const { editor } = configuration; appLogger("Dictionaries:", { isVerbose: true }); const files = Array.isArray(contentDeclarationsPaths) ? contentDeclarationsPaths : [contentDeclarationsPaths]; const localDictionaries = await loadContentDeclarations( files, projectRequire ); const filteredLocalDictionaries = localDictionaries.filter((dict) => { const hasKey = Boolean(dict.key); const hasContent = Boolean(dict.content); if (!hasContent) { console.error( "Content declaration has no exported content", dict.filePath ); } else if (!hasKey) { console.error("Content declaration has no key", dict.filePath); } return hasKey && hasContent; }); const localDictionaryKeys = filteredLocalDictionaries.map((dict) => dict.key).filter(Boolean); logger.init(localDictionaryKeys, []); logger.updateStatus( filteredLocalDictionaries.map((dict) => ({ dictionaryKey: dict.key, type: "local", status: { status: "built" } })) ); let distantDictionaries = []; let distantDictionaryKeys = []; if (editor.clientId && editor.clientSecret) { try { distantDictionaryKeys = await fetchDistantDictionaryKeys(); const orderedDistantDictionaryKeys = distantDictionaryKeys.sort(sortAlphabetically); logger.addDictionaryKeys("distant", orderedDistantDictionaryKeys); distantDictionaries = await loadDistantDictionaries({ dictionaryKeys: orderedDistantDictionaryKeys }); } catch (_error) { appLogger("Error during fetching distant dictionaries", { level: "error" }); } } logger.stop(); return [...filteredLocalDictionaries, ...distantDictionaries]; } catch (error) { logger.stop(); throw error; } }; export { loadDictionaries }; //# sourceMappingURL=loadDictionaries.mjs.map