@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
115 lines • 4.59 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var fetchDistantDictionaries_exports = {};
__export(fetchDistantDictionaries_exports, {
fetchDistantDictionaries: () => fetchDistantDictionaries
});
module.exports = __toCommonJS(fetchDistantDictionaries_exports);
var import_api = require("@intlayer/api");
var import_config = require("@intlayer/config");
var import_p_limit = __toESM(require("p-limit"));
var import_log = require('./log.cjs');
const fetchDistantDictionaries = async (options) => {
try {
const config = (0, import_config.getConfiguration)();
const { clientId, clientSecret } = config.editor;
const intlayerAPI = (0, import_api.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 = (0, import_p_limit.default)(5);
const processDictionary = async (dictionaryKey) => {
import_log.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`);
}
import_log.logger.updateStatus([
{ dictionaryKey, type: "distant", status: { status: "imported" } }
]);
return distantDictionary;
} catch (error) {
import_log.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 = import_log.logger.getStatuses();
for (const statusObj of statuses) {
const currentState = statusObj.state.find((s) => s.type === "distant");
if (currentState && currentState.errorMessage) {
(0, import_config.appLogger)(currentState.errorMessage, { level: "error" });
}
}
const filteredResult = result.filter(
(dict) => dict !== void 0
);
return filteredResult;
} catch (error) {
(0, import_config.appLogger)(error, { level: "error" });
return [];
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fetchDistantDictionaries
});
//# sourceMappingURL=fetchDistantDictionaries.cjs.map