@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
147 lines • 5.53 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 mergeDictionaries_exports = {};
__export(mergeDictionaries_exports, {
mergeDictionaries: () => mergeDictionaries
});
module.exports = __toCommonJS(mergeDictionaries_exports);
var import_config = require("@intlayer/config");
var import_built = __toESM(require("@intlayer/config/built"));
var import_core = require("@intlayer/core");
var import_deepmerge = __toESM(require("deepmerge"));
const checkTypesMatch = (obj1, obj2, dictionaryKey, path = []) => {
const appLogger = (0, import_config.getAppLogger)(import_built.default);
const type1 = (0, import_core.getNodeType)(obj1);
const type2 = (0, import_core.getNodeType)(obj2);
if (type1 !== type2) {
appLogger(
`Error: Dictionary "${dictionaryKey}" has a multiple content files with type mismatch at path "${path.join(".")}": Cannot merge ${type1} with ${type2}`,
{
level: "error"
}
);
}
if (type1 === "object" && obj1 && obj2) {
const allKeys = /* @__PURE__ */ new Set([...Object.keys(obj1), ...Object.keys(obj2)]);
for (const key of allKeys) {
if (key in obj1 && key in obj2) {
checkTypesMatch(obj1[key], obj2[key], dictionaryKey, [...path, key]);
}
}
}
};
const arrayMerge = (destinationArray, sourceArray) => {
const isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
const getKey = (item) => {
if (!isObject(item)) return void 0;
const key = item.key;
if (typeof key === "string" || typeof key === "number") return key;
return void 0;
};
const result = [];
const destKeyToIndex = /* @__PURE__ */ new Map();
const destUsed = new Array(destinationArray.length).fill(false);
for (let i = 0; i < destinationArray.length; i++) {
const k = getKey(destinationArray[i]);
if (k !== void 0 && !destKeyToIndex.has(k)) {
destKeyToIndex.set(k, i);
}
}
for (let i = 0; i < sourceArray.length; i++) {
const sourceItem = sourceArray[i];
const sourceKey = getKey(sourceItem);
if (sourceKey !== void 0 && destKeyToIndex.has(sourceKey)) {
const destIndex = destKeyToIndex.get(sourceKey);
const destItem2 = destinationArray[destIndex];
destUsed[destIndex] = true;
if (isObject(destItem2) && isObject(sourceItem)) {
result.push((0, import_deepmerge.default)(sourceItem, destItem2, { arrayMerge }));
} else {
result.push(destItem2 !== void 0 ? destItem2 : sourceItem);
}
continue;
}
const destItem = destinationArray[i];
if (destItem !== void 0 && !destUsed[i]) {
destUsed[i] = true;
if (isObject(destItem) && isObject(sourceItem)) {
result.push((0, import_deepmerge.default)(sourceItem, destItem, { arrayMerge }));
} else if (destItem !== void 0) {
result.push(destItem);
} else {
result.push(sourceItem);
}
} else {
result.push(sourceItem);
}
}
for (let i = 0; i < destinationArray.length; i++) {
if (!destUsed[i]) {
result.push(destinationArray[i]);
destUsed[i] = true;
}
}
return result;
};
const mergeDictionaries = (dictionaries) => {
const { editor } = import_built.default;
let mergedDictionaries = dictionaries[0];
const mergeOptions = {
arrayMerge
};
for (let i = 1; i < dictionaries.length; i++) {
const currentDictionary = dictionaries[i];
checkTypesMatch(
mergedDictionaries,
currentDictionary,
currentDictionary.key,
[]
);
const isDistant = currentDictionary.location === "distant";
if (editor.dictionaryPriorityStrategy === "distant_first" && isDistant) {
mergedDictionaries = (0, import_deepmerge.default)(
mergedDictionaries,
currentDictionary,
mergeOptions
);
} else {
mergedDictionaries = (0, import_deepmerge.default)(
currentDictionary,
mergedDictionaries,
mergeOptions
);
}
}
return { ...mergedDictionaries, filePath: void 0 };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
mergeDictionaries
});
//# sourceMappingURL=mergeDictionaries.cjs.map