UNPKG

@intlayer/chokidar

Version:

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

81 lines 2.76 kB
import { getIntlayerAPI } from "@intlayer/api"; import { appLogger, getConfiguration } from "@intlayer/config"; import pLimit from "p-limit"; import { logger } from "./log.mjs"; const fetchDistantDictionaries = async (options) => { try { const config = getConfiguration(); const { clientId, clientSecret } = config.editor; const intlayerAPI = getIntlayerAPI(void 0, config); if (!clientId || !clientSecret) { throw new Error( "Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project." ); } const oAuth2TokenResult = await intlayerAPI.auth.getOAuth2AccessToken(); const oAuth2AccessToken = oAuth2TokenResult.data?.accessToken; const distantDictionariesKeys = options.dictionaryKeys; const limit = pLimit(5); const processDictionary = async (dictionaryKey) => { logger.updateStatus([ { dictionaryKey, type: "distant", status: { status: "fetching" } } ]); try { const getDictionaryResult = await intlayerAPI.dictionary.getDictionary( dictionaryKey, void 0, { headers: { Authorization: `Bearer ${oAuth2AccessToken}` } } ); const distantDictionary = getDictionaryResult.data; if (!distantDictionary) { throw new Error(`Dictionary ${dictionaryKey} not found on remote`); } logger.updateStatus([ { dictionaryKey, type: "distant", status: { status: "imported" } } ]); return distantDictionary; } catch (error) { logger.updateStatus([ { dictionaryKey, type: "distant", status: { status: "error", error, errorMessage: `${options?.logPrefix ?? ""}Error fetching dictionary ${dictionaryKey}: ${error}` } } ]); return void 0; } }; const fetchPromises = distantDictionariesKeys.map( (dictionaryKey) => limit(async () => await processDictionary(dictionaryKey)) ); const result = await Promise.all(fetchPromises); const statuses = logger.getStatuses(); for (const statusObj of statuses) { const currentState = statusObj.state.find((s) => s.type === "distant"); if (currentState && currentState.errorMessage) { appLogger(currentState.errorMessage, { level: "error" }); } } const filteredResult = result.filter( (dict) => dict !== void 0 ); return filteredResult; } catch (error) { appLogger(error, { level: "error" }); return []; } }; export { fetchDistantDictionaries }; //# sourceMappingURL=fetchDistantDictionaries.mjs.map