@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
63 lines • 2.48 kB
JavaScript
import { appLogger, ESMxCJSRequire, getConfiguration } from "@intlayer/config";
import merge from "deepmerge";
import { fetchDistantDictionaryKeys } from "../fetchDistantDictionaryKeys.mjs";
import { logger } from "../log.mjs";
import { mergeByKey } from "../mergeDictionaries.mjs";
import { sortAlphabetically } from "../utils.mjs";
import { loadContentDeclarations } from "./loadContentDeclaration.mjs";
import { loadDistantDictionaries } from "./loadDistantDictionaries.mjs";
const loadDictionaries = async (contentDeclarationsPaths, configuration = getConfiguration(), projectRequire = ESMxCJSRequire) => {
try {
const { editor } = configuration;
appLogger("Dictionaries:", { isVerbose: true });
const files = Array.isArray(contentDeclarationsPaths) ? contentDeclarationsPaths : [contentDeclarationsPaths];
const localDictionaries = await loadContentDeclarations(
files,
projectRequire
);
const localDictionaryKeys = localDictionaries.map((dict) => dict.key).filter(Boolean);
logger.init(localDictionaryKeys, []);
logger.updateStatus(
localDictionaries.map((dict) => ({
dictionaryKey: dict.key,
type: "local",
status: { status: "built" }
}))
);
let distantDictionaries = [];
let distantDictionaryKeys = [];
let mergedDictionaries = localDictionaries;
if (editor.clientId && editor.clientSecret) {
try {
distantDictionaryKeys = await fetchDistantDictionaryKeys();
const orderedDistantDictionaryKeys = distantDictionaryKeys.sort(sortAlphabetically);
logger.addDictionaryKeys("distant", orderedDistantDictionaryKeys);
distantDictionaries = await loadDistantDictionaries({
dictionaryKeys: orderedDistantDictionaryKeys
});
if (editor.dictionaryPriorityStrategy === "distant_first") {
mergedDictionaries = merge(localDictionaries, distantDictionaries, {
arrayMerge: mergeByKey("key")
});
} else {
mergedDictionaries = merge(distantDictionaries, localDictionaries, {
arrayMerge: mergeByKey("key")
});
}
} catch (_error) {
appLogger("Error during fetching distant dictionaries", {
level: "error"
});
}
}
logger.stop();
return mergedDictionaries;
} catch (error) {
logger.stop();
throw error;
}
};
export {
loadDictionaries
};
//# sourceMappingURL=loadDictionaries.mjs.map