@intlayer/chokidar 5.7.7 → 5.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/mergeDictionaries.cjs +67 -3
- package/dist/cjs/mergeDictionaries.cjs.map +1 -1
- package/dist/cjs/reduceDictionaryContent/applyMask.cjs +16 -2
- package/dist/cjs/reduceDictionaryContent/applyMask.cjs.map +1 -1
- package/dist/cjs/writeContentDeclaration/formatCode.cjs +3 -1
- package/dist/cjs/writeContentDeclaration/formatCode.cjs.map +1 -1
- package/dist/cjs/writeContentDeclaration/writeJSFile.cjs +101 -148
- package/dist/cjs/writeContentDeclaration/writeJSFile.cjs.map +1 -1
- package/dist/esm/mergeDictionaries.mjs +67 -3
- package/dist/esm/mergeDictionaries.mjs.map +1 -1
- package/dist/esm/reduceDictionaryContent/applyMask.mjs +16 -2
- package/dist/esm/reduceDictionaryContent/applyMask.mjs.map +1 -1
- package/dist/esm/writeContentDeclaration/formatCode.mjs +3 -1
- package/dist/esm/writeContentDeclaration/formatCode.mjs.map +1 -1
- package/dist/esm/writeContentDeclaration/writeJSFile.mjs +101 -148
- package/dist/esm/writeContentDeclaration/writeJSFile.mjs.map +1 -1
- package/dist/types/mergeDictionaries.d.ts.map +1 -1
- package/dist/types/reduceDictionaryContent/applyMask.d.ts.map +1 -1
- package/dist/types/writeContentDeclaration/formatCode.d.ts.map +1 -1
- package/dist/types/writeContentDeclaration/writeJSFile.d.ts.map +1 -1
- package/package.json +14 -14
- package/dist/cjs/utils/resolveObjectPromises.test.cjs +0 -107
- package/dist/cjs/utils/resolveObjectPromises.test.cjs.map +0 -1
- package/dist/esm/utils/resolveObjectPromises.test.mjs +0 -106
- package/dist/esm/utils/resolveObjectPromises.test.mjs.map +0 -1
- package/dist/types/utils/resolveObjectPromises.test.d.ts +0 -2
- package/dist/types/utils/resolveObjectPromises.test.d.ts.map +0 -1
|
@@ -41,7 +41,7 @@ const checkTypesMatch = (obj1, obj2, dictionaryKey, path = []) => {
|
|
|
41
41
|
const type2 = (0, import_core.getNodeType)(obj2);
|
|
42
42
|
if (type1 !== type2) {
|
|
43
43
|
appLogger(
|
|
44
|
-
`Error: Dictionary ${dictionaryKey} has a multiple content files with type mismatch at path "${path.join(".")}": Cannot merge ${type1} with ${type2}`,
|
|
44
|
+
`Error: Dictionary "${dictionaryKey}" has a multiple content files with type mismatch at path "${path.join(".")}": Cannot merge ${type1} with ${type2}`,
|
|
45
45
|
{
|
|
46
46
|
level: "error"
|
|
47
47
|
}
|
|
@@ -56,9 +56,65 @@ const checkTypesMatch = (obj1, obj2, dictionaryKey, path = []) => {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
|
+
const arrayMerge = (destinationArray, sourceArray) => {
|
|
60
|
+
const isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
61
|
+
const getKey = (item) => {
|
|
62
|
+
if (!isObject(item)) return void 0;
|
|
63
|
+
const key = item.key;
|
|
64
|
+
if (typeof key === "string" || typeof key === "number") return key;
|
|
65
|
+
return void 0;
|
|
66
|
+
};
|
|
67
|
+
const result = [];
|
|
68
|
+
const destKeyToIndex = /* @__PURE__ */ new Map();
|
|
69
|
+
const destUsed = new Array(destinationArray.length).fill(false);
|
|
70
|
+
for (let i = 0; i < destinationArray.length; i++) {
|
|
71
|
+
const k = getKey(destinationArray[i]);
|
|
72
|
+
if (k !== void 0 && !destKeyToIndex.has(k)) {
|
|
73
|
+
destKeyToIndex.set(k, i);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (let i = 0; i < sourceArray.length; i++) {
|
|
77
|
+
const sourceItem = sourceArray[i];
|
|
78
|
+
const sourceKey = getKey(sourceItem);
|
|
79
|
+
if (sourceKey !== void 0 && destKeyToIndex.has(sourceKey)) {
|
|
80
|
+
const destIndex = destKeyToIndex.get(sourceKey);
|
|
81
|
+
const destItem2 = destinationArray[destIndex];
|
|
82
|
+
destUsed[destIndex] = true;
|
|
83
|
+
if (isObject(destItem2) && isObject(sourceItem)) {
|
|
84
|
+
result.push((0, import_deepmerge.default)(sourceItem, destItem2, { arrayMerge }));
|
|
85
|
+
} else {
|
|
86
|
+
result.push(destItem2 !== void 0 ? destItem2 : sourceItem);
|
|
87
|
+
}
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const destItem = destinationArray[i];
|
|
91
|
+
if (destItem !== void 0 && !destUsed[i]) {
|
|
92
|
+
destUsed[i] = true;
|
|
93
|
+
if (isObject(destItem) && isObject(sourceItem)) {
|
|
94
|
+
result.push((0, import_deepmerge.default)(sourceItem, destItem, { arrayMerge }));
|
|
95
|
+
} else if (destItem !== void 0) {
|
|
96
|
+
result.push(destItem);
|
|
97
|
+
} else {
|
|
98
|
+
result.push(sourceItem);
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
result.push(sourceItem);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (let i = 0; i < destinationArray.length; i++) {
|
|
105
|
+
if (!destUsed[i]) {
|
|
106
|
+
result.push(destinationArray[i]);
|
|
107
|
+
destUsed[i] = true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
};
|
|
59
112
|
const mergeDictionaries = (dictionaries) => {
|
|
60
113
|
const { editor } = import_built.default;
|
|
61
114
|
let mergedDictionaries = dictionaries[0];
|
|
115
|
+
const mergeOptions = {
|
|
116
|
+
arrayMerge
|
|
117
|
+
};
|
|
62
118
|
for (let i = 1; i < dictionaries.length; i++) {
|
|
63
119
|
const currentDictionary = dictionaries[i];
|
|
64
120
|
checkTypesMatch(
|
|
@@ -69,9 +125,17 @@ const mergeDictionaries = (dictionaries) => {
|
|
|
69
125
|
);
|
|
70
126
|
const isDistant = currentDictionary.location === "distant";
|
|
71
127
|
if (editor.dictionaryPriorityStrategy === "distant_first" && isDistant) {
|
|
72
|
-
mergedDictionaries = (0, import_deepmerge.default)(
|
|
128
|
+
mergedDictionaries = (0, import_deepmerge.default)(
|
|
129
|
+
mergedDictionaries,
|
|
130
|
+
currentDictionary,
|
|
131
|
+
mergeOptions
|
|
132
|
+
);
|
|
73
133
|
} else {
|
|
74
|
-
mergedDictionaries = (0, import_deepmerge.default)(
|
|
134
|
+
mergedDictionaries = (0, import_deepmerge.default)(
|
|
135
|
+
currentDictionary,
|
|
136
|
+
mergedDictionaries,
|
|
137
|
+
mergeOptions
|
|
138
|
+
);
|
|
75
139
|
}
|
|
76
140
|
}
|
|
77
141
|
return { ...mergedDictionaries, filePath: void 0 };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/mergeDictionaries.ts"],"sourcesContent":["import { getAppLogger } from '@intlayer/config';\nimport configuration from '@intlayer/config/built';\nimport type { Dictionary } from '@intlayer/core';\nimport { getNodeType } from '@intlayer/core';\nimport merge from 'deepmerge';\n\nconst checkTypesMatch = (\n obj1: any,\n obj2: any,\n dictionaryKey: string,\n path: string[] = []\n): void => {\n const appLogger = getAppLogger(configuration);\n const type1 = getNodeType(obj1);\n const type2 = getNodeType(obj2);\n\n if (type1 !== type2) {\n appLogger(\n `Error: Dictionary ${dictionaryKey} has a multiple content files with type mismatch at path \"${path.join('.')}\": Cannot merge ${type1} with ${type2}`,\n {\n level: 'error',\n }\n );\n }\n\n if (type1 === 'object' && obj1 && obj2) {\n const allKeys = new Set([...Object.keys(obj1), ...Object.keys(obj2)]);\n for (const key of allKeys) {\n if (key in obj1 && key in obj2) {\n checkTypesMatch(obj1[key], obj2[key], dictionaryKey, [...path, key]);\n }\n }\n }\n};\n\nexport const mergeDictionaries = (dictionaries: Dictionary[]): Dictionary => {\n const { editor } = configuration;\n\n let mergedDictionaries: Dictionary = dictionaries[0];\n\n for (let i = 1; i < dictionaries.length; i++) {\n const currentDictionary = dictionaries[i];\n\n // Check types before merging\n checkTypesMatch(\n mergedDictionaries,\n currentDictionary,\n currentDictionary.key,\n []\n );\n\n const isDistant = currentDictionary.location === 'distant';\n\n if (editor.dictionaryPriorityStrategy === 'distant_first' && isDistant) {\n mergedDictionaries = merge(mergedDictionaries
|
|
1
|
+
{"version":3,"sources":["../../src/mergeDictionaries.ts"],"sourcesContent":["import { getAppLogger } from '@intlayer/config';\nimport configuration from '@intlayer/config/built';\nimport type { Dictionary } from '@intlayer/core';\nimport { getNodeType } from '@intlayer/core';\nimport merge, { Options } from 'deepmerge';\n\nconst checkTypesMatch = (\n obj1: any,\n obj2: any,\n dictionaryKey: string,\n path: string[] = []\n): void => {\n const appLogger = getAppLogger(configuration);\n const type1 = getNodeType(obj1);\n const type2 = getNodeType(obj2);\n\n if (type1 !== type2) {\n appLogger(\n `Error: Dictionary \"${dictionaryKey}\" has a multiple content files with type mismatch at path \"${path.join('.')}\": Cannot merge ${type1} with ${type2}`,\n {\n level: 'error',\n }\n );\n }\n\n if (type1 === 'object' && obj1 && obj2) {\n const allKeys = new Set([...Object.keys(obj1), ...Object.keys(obj2)]);\n for (const key of allKeys) {\n if (key in obj1 && key in obj2) {\n checkTypesMatch(obj1[key], obj2[key], dictionaryKey, [...path, key]);\n }\n }\n }\n};\n\n// Custom array merge strategy that merges arrays by key when present, otherwise by index\nconst arrayMerge = (destinationArray: any[], sourceArray: any[]): any[] => {\n const isObject = (value: unknown): value is Record<string, any> =>\n !!value && typeof value === 'object' && !Array.isArray(value);\n\n const getKey = (item: any): string | number | undefined => {\n if (!isObject(item)) return undefined;\n const key = (item as any).key;\n if (typeof key === 'string' || typeof key === 'number') return key;\n return undefined;\n };\n\n const result: any[] = [];\n\n // Build a lookup for destination keyed items and track usage of all destination indices\n const destKeyToIndex = new Map<string | number, number>();\n const destUsed: boolean[] = new Array(destinationArray.length).fill(false);\n for (let i = 0; i < destinationArray.length; i++) {\n const k = getKey(destinationArray[i]);\n if (k !== undefined && !destKeyToIndex.has(k)) {\n destKeyToIndex.set(k, i);\n }\n }\n\n // First pass: respect source (already merged) order\n for (let i = 0; i < sourceArray.length; i++) {\n const sourceItem = sourceArray[i];\n const sourceKey = getKey(sourceItem);\n\n if (sourceKey !== undefined && destKeyToIndex.has(sourceKey)) {\n const destIndex = destKeyToIndex.get(sourceKey)!;\n const destItem = destinationArray[destIndex];\n destUsed[destIndex] = true;\n\n if (isObject(destItem) && isObject(sourceItem)) {\n result.push(merge(sourceItem, destItem, { arrayMerge }));\n } else {\n // Prefer destination item (later dictionary) when primitive\n result.push(destItem !== undefined ? destItem : sourceItem);\n }\n continue;\n }\n\n // Fallback to index-based merge when no key match\n const destItem = destinationArray[i];\n if (destItem !== undefined && !destUsed[i]) {\n destUsed[i] = true;\n if (isObject(destItem) && isObject(sourceItem)) {\n result.push(merge(sourceItem, destItem, { arrayMerge }));\n } else if (destItem !== undefined) {\n result.push(destItem);\n } else {\n result.push(sourceItem);\n }\n } else {\n result.push(sourceItem);\n }\n }\n\n // Second pass: append remaining unused destination items (including keyed-only in destination or extra by index)\n for (let i = 0; i < destinationArray.length; i++) {\n if (!destUsed[i]) {\n result.push(destinationArray[i]);\n destUsed[i] = true;\n }\n }\n\n return result;\n};\n\nexport const mergeDictionaries = (dictionaries: Dictionary[]): Dictionary => {\n const { editor } = configuration;\n\n let mergedDictionaries: Dictionary = dictionaries[0];\n\n // Configure deepmerge options with custom array merge strategy\n const mergeOptions: Options = {\n arrayMerge,\n };\n\n for (let i = 1; i < dictionaries.length; i++) {\n const currentDictionary = dictionaries[i];\n\n // Check types before merging\n checkTypesMatch(\n mergedDictionaries,\n currentDictionary,\n currentDictionary.key,\n []\n );\n\n const isDistant = currentDictionary.location === 'distant';\n\n if (editor.dictionaryPriorityStrategy === 'distant_first' && isDistant) {\n mergedDictionaries = merge(\n mergedDictionaries,\n currentDictionary,\n mergeOptions\n );\n } else {\n mergedDictionaries = merge(\n currentDictionary,\n mergedDictionaries,\n mergeOptions\n );\n }\n }\n\n return { ...mergedDictionaries, filePath: undefined };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;A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A2C;AAC3E,QAAM,EAAE,OAAO,IAAI,aAAAF;AAEnB,MAAI,qBAAiC,aAAa,CAAC;AAGnD,QAAM,eAAwB;AAAA,IAC5B;AAAA,EACF;AAEA,WAAS,IAAI,GAAG,IAAI,aAAa,QAAQ,KAAK;AAC5C,UAAM,oBAAoB,aAAa,CAAC;AAGxC;AAAA,MACE;AAAA,MACA;AAAA,MACA,kBAAkB;AAAA,MAClB,CAAC;AAAA,IACH;AAEA,UAAM,YAAY,kBAAkB,aAAa;AAEjD,QAAI,OAAO,+BAA+B,mBAAmB,WAAW;AACtE,+BAAqB,iBAAAE;AAAA,QACnB;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF,OAAO;AACL,+BAAqB,iBAAAA;AAAA,QACnB;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,GAAG,oBAAoB,UAAU,OAAU;AACtD;","names":["configuration","destItem","merge"]}
|
|
@@ -36,8 +36,22 @@ const applyMask = (full, mask) => {
|
|
|
36
36
|
}
|
|
37
37
|
if (mask && typeof mask === "object" && full && typeof full === "object") {
|
|
38
38
|
const out = {};
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
const maskEntries = Object.entries(mask);
|
|
40
|
+
const allChildrenAreArrays = maskEntries.every(
|
|
41
|
+
([, value]) => Array.isArray(value)
|
|
42
|
+
);
|
|
43
|
+
for (const [k, m] of maskEntries) {
|
|
44
|
+
const fullValue = full[k];
|
|
45
|
+
if (Array.isArray(m) && Array.isArray(fullValue)) {
|
|
46
|
+
const isTranslationNode = (val) => !!val && typeof val === "object" && "nodeType" in val;
|
|
47
|
+
const isArrayOfTranslationNodes = fullValue.every(
|
|
48
|
+
(item) => isTranslationNode(item)
|
|
49
|
+
);
|
|
50
|
+
if (!allChildrenAreArrays && !isArrayOfTranslationNodes) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
out[k] = applyMask(fullValue, m);
|
|
41
55
|
}
|
|
42
56
|
return out;
|
|
43
57
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/reduceDictionaryContent/applyMask.ts"],"sourcesContent":["import type { Dictionary } from '@intlayer/core';\n\nexport const applyMask = (full: Dictionary, mask: any): Dictionary => {\n // the mask \"true\" → we don't filter\n if (mask === true) {\n return full;\n }\n\n // arrays\n if (Array.isArray(mask) && Array.isArray(full)) {\n return mask.map((m, i) => applyMask(full[i], m)) as any;\n }\n\n // handle node with nodeType property\n if (full && typeof full === 'object' && 'nodeType' in full) {\n if (mask && typeof mask === 'object') {\n return full; // Keep the full object with nodeType intact\n }\n return full;\n }\n\n // generic object\n if (mask && typeof mask === 'object' && full && typeof full === 'object') {\n const out: any = {};\n for (const [k, m] of
|
|
1
|
+
{"version":3,"sources":["../../../src/reduceDictionaryContent/applyMask.ts"],"sourcesContent":["import type { Dictionary } from '@intlayer/core';\n\nexport const applyMask = (full: Dictionary, mask: any): Dictionary => {\n // the mask \"true\" → we don't filter\n if (mask === true) {\n return full;\n }\n\n // arrays\n if (Array.isArray(mask) && Array.isArray(full)) {\n return mask.map((m, i) => applyMask(full[i], m)) as any;\n }\n\n // handle node with nodeType property\n if (full && typeof full === 'object' && 'nodeType' in full) {\n if (mask && typeof mask === 'object') {\n return full; // Keep the full object with nodeType intact\n }\n return full;\n }\n\n // generic object\n if (mask && typeof mask === 'object' && full && typeof full === 'object') {\n const out: any = {};\n const maskEntries = Object.entries(mask);\n const allChildrenAreArrays = maskEntries.every(([, value]) =>\n Array.isArray(value)\n );\n\n for (const [k, m] of maskEntries) {\n const fullValue = (full as any)[k];\n\n // If this child is an array, decide preservation rules.\n // - Preserve when all children at this level are arrays in the mask\n // - Also preserve when the array is an array of translation nodes\n if (Array.isArray(m) && Array.isArray(fullValue)) {\n const isTranslationNode = (val: unknown): boolean =>\n !!val && typeof val === 'object' && 'nodeType' in (val as any);\n const isArrayOfTranslationNodes = fullValue.every((item: any) =>\n isTranslationNode(item)\n );\n\n if (!allChildrenAreArrays && !isArrayOfTranslationNodes) {\n continue; // skip incidental arrays when mixed with non-arrays\n }\n }\n\n out[k] = applyMask(fullValue, m);\n }\n return out;\n }\n\n // unexpected case: we return the original value\n return full;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,MAAM,YAAY,CAAC,MAAkB,SAA0B;AAEpE,MAAI,SAAS,MAAM;AACjB,WAAO;AAAA,EACT;AAGA,MAAI,MAAM,QAAQ,IAAI,KAAK,MAAM,QAAQ,IAAI,GAAG;AAC9C,WAAO,KAAK,IAAI,CAAC,GAAG,MAAM,UAAU,KAAK,CAAC,GAAG,CAAC,CAAC;AAAA,EACjD;AAGA,MAAI,QAAQ,OAAO,SAAS,YAAY,cAAc,MAAM;AAC1D,QAAI,QAAQ,OAAO,SAAS,UAAU;AACpC,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT;AAGA,MAAI,QAAQ,OAAO,SAAS,YAAY,QAAQ,OAAO,SAAS,UAAU;AACxE,UAAM,MAAW,CAAC;AAClB,UAAM,cAAc,OAAO,QAAQ,IAAI;AACvC,UAAM,uBAAuB,YAAY;AAAA,MAAM,CAAC,CAAC,EAAE,KAAK,MACtD,MAAM,QAAQ,KAAK;AAAA,IACrB;AAEA,eAAW,CAAC,GAAG,CAAC,KAAK,aAAa;AAChC,YAAM,YAAa,KAAa,CAAC;AAKjC,UAAI,MAAM,QAAQ,CAAC,KAAK,MAAM,QAAQ,SAAS,GAAG;AAChD,cAAM,oBAAoB,CAAC,QACzB,CAAC,CAAC,OAAO,OAAO,QAAQ,YAAY,cAAe;AACrD,cAAM,4BAA4B,UAAU;AAAA,UAAM,CAAC,SACjD,kBAAkB,IAAI;AAAA,QACxB;AAEA,YAAI,CAAC,wBAAwB,CAAC,2BAA2B;AACvD;AAAA,QACF;AAAA,MACF;AAEA,UAAI,CAAC,IAAI,UAAU,WAAW,CAAC;AAAA,IACjC;AACA,WAAO;AAAA,EACT;AAGA,SAAO;AACT;","names":[]}
|
|
@@ -33,6 +33,7 @@ __export(formatCode_exports, {
|
|
|
33
33
|
module.exports = __toCommonJS(formatCode_exports);
|
|
34
34
|
var import_config = require("@intlayer/config");
|
|
35
35
|
var import_built = __toESM(require("@intlayer/config/built"));
|
|
36
|
+
var import_path = require("path");
|
|
36
37
|
const formatCode = async (filePath, code) => {
|
|
37
38
|
const appLogger = (0, import_config.getAppLogger)(import_built.default);
|
|
38
39
|
let prettier;
|
|
@@ -48,7 +49,8 @@ const formatCode = async (filePath, code) => {
|
|
|
48
49
|
filepath: filePath
|
|
49
50
|
// Explicitly provide the filepath so Prettier can infer the parser
|
|
50
51
|
});
|
|
51
|
-
|
|
52
|
+
const relativePath = (0, import_path.relative)(import_built.default.content.baseDir, filePath);
|
|
53
|
+
appLogger(`Applied Prettier formatting to ${relativePath}`, {
|
|
52
54
|
level: "info",
|
|
53
55
|
isVerbose: true
|
|
54
56
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/writeContentDeclaration/formatCode.ts"],"sourcesContent":["import { getAppLogger } from '@intlayer/config';\nimport configuration from '@intlayer/config/built';\n\nexport const formatCode = async (filePath: string, code: string) => {\n const appLogger = getAppLogger(configuration);\n // Try to import prettier if it exists\n let prettier: any;\n try {\n prettier = require('prettier');\n } catch (error) {\n // Prettier is not installed, continue without it\n }\n\n // Apply Prettier formatting if it's available\n if (prettier) {\n try {\n // Try to find a prettier config file\n const prettierConfig = await prettier.resolveConfig(filePath ?? '');\n\n // Format the code with Prettier\n const formattedCode = await prettier.format(code, {\n ...prettierConfig,\n filepath: filePath, // Explicitly provide the filepath so Prettier can infer the parser\n });\n\n appLogger(`Applied Prettier formatting to ${
|
|
1
|
+
{"version":3,"sources":["../../../src/writeContentDeclaration/formatCode.ts"],"sourcesContent":["import { getAppLogger } from '@intlayer/config';\nimport configuration from '@intlayer/config/built';\nimport { relative } from 'path';\n\nexport const formatCode = async (filePath: string, code: string) => {\n const appLogger = getAppLogger(configuration);\n // Try to import prettier if it exists\n let prettier: any;\n try {\n prettier = require('prettier');\n } catch (error) {\n // Prettier is not installed, continue without it\n }\n\n // Apply Prettier formatting if it's available\n if (prettier) {\n try {\n // Try to find a prettier config file\n const prettierConfig = await prettier.resolveConfig(filePath ?? '');\n\n // Format the code with Prettier\n const formattedCode = await prettier.format(code, {\n ...prettierConfig,\n filepath: filePath, // Explicitly provide the filepath so Prettier can infer the parser\n });\n\n const relativePath = relative(configuration.content.baseDir, filePath);\n\n appLogger(`Applied Prettier formatting to ${relativePath}`, {\n level: 'info',\n isVerbose: true,\n });\n\n return formattedCode;\n } catch (error) {\n const err = error as Error;\n appLogger(\n `Failed to apply Prettier formatting to ${filePath}: ${err.message}`,\n {\n level: 'warn',\n isVerbose: true,\n }\n );\n // Continue with unformatted code on prettier error\n }\n }\n\n return code;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;AAC7B,mBAA0B;AAC1B,kBAAyB;AAElB,MAAM,aAAa,OAAO,UAAkB,SAAiB;AAClE,QAAM,gBAAY,4BAAa,aAAAA,OAAa;AAE5C,MAAI;AACJ,MAAI;AACF,eAAW,QAAQ,UAAU;AAAA,EAC/B,SAAS,OAAO;AAAA,EAEhB;AAGA,MAAI,UAAU;AACZ,QAAI;AAEF,YAAM,iBAAiB,MAAM,SAAS,cAAc,YAAY,EAAE;AAGlE,YAAM,gBAAgB,MAAM,SAAS,OAAO,MAAM;AAAA,QAChD,GAAG;AAAA,QACH,UAAU;AAAA;AAAA,MACZ,CAAC;AAED,YAAM,mBAAe,sBAAS,aAAAA,QAAc,QAAQ,SAAS,QAAQ;AAErE,gBAAU,kCAAkC,YAAY,IAAI;AAAA,QAC1D,OAAO;AAAA,QACP,WAAW;AAAA,MACb,CAAC;AAED,aAAO;AAAA,IACT,SAAS,OAAO;AACd,YAAM,MAAM;AACZ;AAAA,QACE,0CAA0C,QAAQ,KAAK,IAAI,OAAO;AAAA,QAClE;AAAA,UACE,OAAO;AAAA,UACP,WAAW;AAAA,QACb;AAAA,MACF;AAAA,IAEF;AAAA,EACF;AAEA,SAAO;AACT;","names":["configuration"]}
|
|
@@ -186,167 +186,120 @@ const writeJSFile = async (filePath, dictionary) => {
|
|
|
186
186
|
const contentObjectPath = contentPropertyPath.get(
|
|
187
187
|
"value"
|
|
188
188
|
);
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
189
|
+
const buildValueNodeFromData = (data) => {
|
|
190
|
+
if (data?.nodeType === import_core.NodeType.Translation) {
|
|
191
|
+
const translationContent = data;
|
|
192
|
+
if (isPerLocaleDeclarationFile && typeof locale === "string" && translationContent?.[import_core.NodeType.Translation]?.[locale] !== void 0) {
|
|
193
|
+
return buildValueNodeFromData(
|
|
194
|
+
translationContent[import_core.NodeType.Translation][locale]
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
const translationsObj = t.objectExpression(
|
|
198
|
+
Object.entries(translationContent?.[import_core.NodeType.Translation] ?? {}).map(
|
|
199
|
+
([langKey, langValue]) => {
|
|
200
|
+
const keyNode = t.isValidIdentifier(langKey) ? t.identifier(langKey) : t.stringLiteral(langKey);
|
|
201
|
+
return t.objectProperty(keyNode, buildValueNodeFromData(langValue));
|
|
202
|
+
}
|
|
203
|
+
)
|
|
204
|
+
);
|
|
205
|
+
return t.callExpression(t.identifier("t"), [translationsObj]);
|
|
206
|
+
}
|
|
207
|
+
if (Array.isArray(data)) {
|
|
208
|
+
return t.arrayExpression(data.map((el) => buildValueNodeFromData(el)));
|
|
209
|
+
}
|
|
210
|
+
if (data && typeof data === "object") {
|
|
211
|
+
const props = Object.entries(data).map(([k, v]) => {
|
|
212
|
+
const key = t.isValidIdentifier(k) ? t.identifier(k) : t.stringLiteral(k);
|
|
213
|
+
return t.objectProperty(key, buildValueNodeFromData(v));
|
|
214
|
+
});
|
|
215
|
+
return t.objectExpression(props);
|
|
216
|
+
}
|
|
217
|
+
switch (typeof data) {
|
|
218
|
+
case "string":
|
|
219
|
+
return t.stringLiteral(data);
|
|
220
|
+
case "number":
|
|
221
|
+
return t.numericLiteral(data);
|
|
222
|
+
case "boolean":
|
|
223
|
+
return t.booleanLiteral(data);
|
|
224
|
+
default:
|
|
225
|
+
return t.nullLiteral();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const ensureObjectProperty = (objPath, key) => {
|
|
229
|
+
const existing = objPath.get("properties").find((propPath) => {
|
|
193
230
|
if (!propPath.isObjectProperty()) return false;
|
|
194
231
|
const propNode = propPath.node;
|
|
195
232
|
const keyName = t.isIdentifier(propNode.key) ? propNode.key.name : t.isStringLiteral(propNode.key) ? propNode.key.value : null;
|
|
196
|
-
return keyName ===
|
|
233
|
+
return keyName === key;
|
|
197
234
|
});
|
|
198
|
-
if (
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
Object.entries(translationContent?.[import_core.NodeType.Translation]).map(
|
|
216
|
-
([langKey, langValue]) => {
|
|
217
|
-
const keyNode2 = t.isValidIdentifier(langKey) ? t.identifier(langKey) : t.stringLiteral(langKey);
|
|
218
|
-
return t.objectProperty(
|
|
219
|
-
keyNode2,
|
|
220
|
-
t.stringLiteral(String(langValue))
|
|
221
|
-
);
|
|
222
|
-
}
|
|
235
|
+
if (existing) return existing;
|
|
236
|
+
const keyNode = t.isValidIdentifier(key) ? t.identifier(key) : t.stringLiteral(key);
|
|
237
|
+
const newProp = t.objectProperty(keyNode, t.objectExpression([]));
|
|
238
|
+
objPath.node.properties.push(newProp);
|
|
239
|
+
const props = objPath.get("properties");
|
|
240
|
+
return props[props.length - 1];
|
|
241
|
+
};
|
|
242
|
+
const mergeValueIntoProperty = (propPath, value, propKeyForLogs) => {
|
|
243
|
+
const valuePath = propPath.get("value");
|
|
244
|
+
if (value?.nodeType === import_core.NodeType.Translation) {
|
|
245
|
+
const translationContent = value;
|
|
246
|
+
if (valuePath.isCallExpression() && t.isIdentifier(valuePath.node.callee) && valuePath.node.callee.name === "t") {
|
|
247
|
+
const translationsObj = t.objectExpression(
|
|
248
|
+
Object.entries(translationContent?.[import_core.NodeType.Translation] ?? {}).map(
|
|
249
|
+
([langKey, langValue]) => t.objectProperty(
|
|
250
|
+
t.isValidIdentifier(langKey) ? t.identifier(langKey) : t.stringLiteral(langKey),
|
|
251
|
+
buildValueNodeFromData(langValue)
|
|
223
252
|
)
|
|
224
|
-
)
|
|
225
|
-
valueNode = t.callExpression(t.identifier("t"), [translationsObj]);
|
|
226
|
-
}
|
|
227
|
-
} else if (typeof newEntryData === "string") {
|
|
228
|
-
valueNode = t.stringLiteral(newEntryData);
|
|
229
|
-
} else {
|
|
230
|
-
appLogger(
|
|
231
|
-
`Unsupported data type for new key '${entryKeyToUpdate}'. Using empty string.`,
|
|
232
|
-
{ level: "warn", isVerbose: true }
|
|
253
|
+
)
|
|
233
254
|
);
|
|
234
|
-
valueNode = t.stringLiteral("");
|
|
235
|
-
}
|
|
236
|
-
const keyNode = t.isValidIdentifier(entryKeyToUpdate) ? t.identifier(entryKeyToUpdate) : t.stringLiteral(entryKeyToUpdate);
|
|
237
|
-
const newProperty = t.objectProperty(keyNode, valueNode);
|
|
238
|
-
contentObjectPath.node.properties.push(newProperty);
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
const callExpressionPath = targetPropertyPath.get("value");
|
|
242
|
-
if (callExpressionPath.isCallExpression()) {
|
|
243
|
-
const calleeNode = callExpressionPath.node.callee;
|
|
244
|
-
const calleeName = t.isIdentifier(calleeNode) ? calleeNode.name : null;
|
|
245
|
-
if (newEntryData?.nodeType === "translation" && calleeName === "t") {
|
|
246
|
-
const args = callExpressionPath.node.arguments;
|
|
247
|
-
if (args.length === 0 || !t.isObjectExpression(args[0])) {
|
|
248
|
-
appLogger(
|
|
249
|
-
`'t' call for '${entryKeyToUpdate}' in ${filePath} does not have an object literal as its first argument. Skipping.`,
|
|
250
|
-
{
|
|
251
|
-
level: "warn",
|
|
252
|
-
isVerbose: true
|
|
253
|
-
}
|
|
254
|
-
);
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
255
|
if (isPerLocaleDeclarationFile && typeof locale === "string") {
|
|
258
|
-
const
|
|
259
|
-
if (
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
appLogger(
|
|
263
|
-
`Missing translation for locale '${locale}' in '${entryKeyToUpdate}'. Using first available translation.`,
|
|
264
|
-
{ level: "warn", isVerbose: true }
|
|
265
|
-
);
|
|
266
|
-
const firstValue = Object.values(translations)[0];
|
|
267
|
-
if (firstValue) {
|
|
268
|
-
targetPropertyPath.get("value").replaceWith(t.stringLiteral(String(firstValue)));
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
const translationsObjectAstNode = args[0];
|
|
274
|
-
const processedLangKeysInJsonUpdate = /* @__PURE__ */ new Set();
|
|
275
|
-
translationsObjectAstNode.properties.forEach((prop) => {
|
|
276
|
-
if (t.isObjectProperty(prop)) {
|
|
277
|
-
const langKeyNode = prop.key;
|
|
278
|
-
const astLangKeyName = t.isIdentifier(langKeyNode) ? langKeyNode.name : t.isStringLiteral(langKeyNode) ? langKeyNode.value : null;
|
|
279
|
-
if (astLangKeyName && newEntryData?.[import_core.NodeType.Translation].hasOwnProperty(astLangKeyName)) {
|
|
280
|
-
prop.value = t.stringLiteral(
|
|
281
|
-
String(
|
|
282
|
-
newEntryData?.[import_core.NodeType.Translation]?.[astLangKeyName]
|
|
283
|
-
)
|
|
284
|
-
);
|
|
285
|
-
processedLangKeysInJsonUpdate.add(astLangKeyName);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
for (const [jsonLangKey, jsonLangValue] of Object.entries(
|
|
290
|
-
newEntryData?.[import_core.NodeType.Translation]
|
|
291
|
-
)) {
|
|
292
|
-
if (!processedLangKeysInJsonUpdate.has(jsonLangKey)) {
|
|
293
|
-
const newKeyNode = t.isValidIdentifier(jsonLangKey) ? t.identifier(jsonLangKey) : t.stringLiteral(jsonLangKey);
|
|
294
|
-
translationsObjectAstNode.properties.push(
|
|
295
|
-
t.objectProperty(
|
|
296
|
-
newKeyNode,
|
|
297
|
-
t.stringLiteral(String(jsonLangValue))
|
|
298
|
-
)
|
|
299
|
-
);
|
|
256
|
+
const localized = translationContent?.[import_core.NodeType.Translation]?.[locale];
|
|
257
|
+
if (localized !== void 0) {
|
|
258
|
+
valuePath.replaceWith(buildValueNodeFromData(localized));
|
|
259
|
+
return;
|
|
300
260
|
}
|
|
301
261
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
`Unhandled callee '${calleeName || "unknown"}' for key '${entryKeyToUpdate}' in ${filePath}.`,
|
|
305
|
-
{ level: "warn", isVerbose: true }
|
|
306
|
-
);
|
|
262
|
+
valuePath.node.arguments = [translationsObj];
|
|
263
|
+
return;
|
|
307
264
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
appLogger(
|
|
319
|
-
`Unhandled data structure for string replacement at '${entryKeyToUpdate}' in ${filePath}.`,
|
|
320
|
-
{ level: "warn", isVerbose: true }
|
|
321
|
-
);
|
|
265
|
+
valuePath.replaceWith(buildValueNodeFromData(value));
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (Array.isArray(value)) {
|
|
269
|
+
valuePath.replaceWith(buildValueNodeFromData(value));
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (value && typeof value === "object") {
|
|
273
|
+
if (!valuePath.isObjectExpression()) {
|
|
274
|
+
valuePath.replaceWith(t.objectExpression([]));
|
|
322
275
|
}
|
|
323
|
-
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
{ level: "info", isVerbose: true }
|
|
328
|
-
);
|
|
329
|
-
if (typeof newEntryData === "string") {
|
|
330
|
-
targetPropertyPath.get("value").replaceWith(t.stringLiteral(newEntryData));
|
|
331
|
-
} else if (newEntryData?.[import_core.NodeType.Translation]) {
|
|
332
|
-
const translations = newEntryData[import_core.NodeType.Translation];
|
|
333
|
-
const localeMatch = filePath.match(/\.([a-z]{2})\.content\.(ts|js)$/i);
|
|
334
|
-
const locale2 = localeMatch ? localeMatch[1] : Object.keys(translations)[0];
|
|
335
|
-
if (translations[locale2]) {
|
|
336
|
-
targetPropertyPath.get("value").replaceWith(t.stringLiteral(String(translations[locale2])));
|
|
337
|
-
} else {
|
|
338
|
-
const firstValue = Object.values(translations)[0];
|
|
339
|
-
if (firstValue) {
|
|
340
|
-
targetPropertyPath.get("value").replaceWith(t.stringLiteral(String(firstValue)));
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
} else {
|
|
344
|
-
appLogger(
|
|
345
|
-
`Cannot update value of type ${valueType} for '${entryKeyToUpdate}' in ${filePath}. Unsupported data structure.`,
|
|
346
|
-
{ level: "warn", isVerbose: true }
|
|
347
|
-
);
|
|
276
|
+
const objPath = valuePath;
|
|
277
|
+
for (const [k, v] of Object.entries(value)) {
|
|
278
|
+
const childProp = ensureObjectProperty(objPath, k);
|
|
279
|
+
mergeValueIntoProperty(childProp, v, `${propKeyForLogs}.${k}`);
|
|
348
280
|
}
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
valuePath.replaceWith(buildValueNodeFromData(value));
|
|
284
|
+
return;
|
|
285
|
+
};
|
|
286
|
+
for (const [entryKeyToUpdate, newEntryData] of Object.entries(
|
|
287
|
+
updatesToApply
|
|
288
|
+
)) {
|
|
289
|
+
let targetPropertyPath = contentObjectPath.get("properties").find((propPath) => {
|
|
290
|
+
if (!propPath.isObjectProperty()) return false;
|
|
291
|
+
const propNode = propPath.node;
|
|
292
|
+
const keyName = t.isIdentifier(propNode.key) ? propNode.key.name : t.isStringLiteral(propNode.key) ? propNode.key.value : null;
|
|
293
|
+
return keyName === entryKeyToUpdate;
|
|
294
|
+
});
|
|
295
|
+
if (!targetPropertyPath) {
|
|
296
|
+
const keyNode = t.isValidIdentifier(entryKeyToUpdate) ? t.identifier(entryKeyToUpdate) : t.stringLiteral(entryKeyToUpdate);
|
|
297
|
+
const newProperty = t.objectProperty(keyNode, t.objectExpression([]));
|
|
298
|
+
contentObjectPath.node.properties.push(newProperty);
|
|
299
|
+
const props = contentObjectPath.get("properties");
|
|
300
|
+
targetPropertyPath = props[props.length - 1];
|
|
349
301
|
}
|
|
302
|
+
mergeValueIntoProperty(targetPropertyPath, newEntryData, entryKeyToUpdate);
|
|
350
303
|
}
|
|
351
304
|
const generatedCode = (0, import_generator.default)(ast, {
|
|
352
305
|
retainLines: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/writeContentDeclaration/writeJSFile.ts"],"sourcesContent":["import generator from '@babel/generator';\nimport * as babelParser from '@babel/parser';\nimport traverse, { NodePath } from '@babel/traverse';\nimport * as t from '@babel/types';\nimport { getAppLogger, logger } from '@intlayer/config';\nimport configuration from '@intlayer/config/built';\nimport {\n Dictionary,\n NodeType,\n TranslationContent,\n TypedNode,\n} from '@intlayer/core';\nimport { existsSync } from 'fs';\nimport { readFile, writeFile } from 'fs/promises';\nimport { extname } from 'path';\nimport { getContentDeclarationFileTemplate } from '../getContentDeclarationFileTemplate/getContentDeclarationFileTemplate';\nimport { formatCode } from './formatCode';\n\n/**\n * Updates a JavaScript/TypeScript file based on the provided JSON instructions.\n * It targets a specific dictionary object within the file (identified by its 'key' property)\n * and updates its 'content' entries. Currently, it focuses on modifying arguments\n * of 't' (translation) function calls.\n */\nexport const writeJSFile = async (\n filePath: string,\n dictionary: Dictionary\n): Promise<void> => {\n const appLogger = getAppLogger(configuration);\n\n const {\n key: dictionaryIdentifierKey,\n content: updatesToApply,\n locale,\n autoFilled,\n } = dictionary;\n const isPerLocaleDeclarationFile = typeof locale === 'string';\n\n // Check if the file exist\n if (!existsSync(filePath)) {\n const fileExtension = extname(filePath);\n\n let format = 'ts' as 'ts' | 'cjs' | 'esm';\n\n if (fileExtension === '.ts' || fileExtension === '.tsx') {\n format = 'ts';\n } else if (fileExtension === '.cjs' || fileExtension === '.cjsx') {\n format = 'cjs';\n } else {\n format = 'esm';\n }\n\n appLogger('File does not exist, creating it', {\n isVerbose: true,\n });\n const template = await getContentDeclarationFileTemplate(\n dictionaryIdentifierKey,\n format,\n { locale, autoFilled }\n );\n\n await writeFile(filePath, template, 'utf-8');\n }\n\n let sourceCode: string;\n try {\n sourceCode = await readFile(filePath, 'utf-8');\n } catch (error) {\n const err = error as Error;\n appLogger(`Failed to read file: ${filePath}`, {\n level: 'error',\n });\n throw new Error(`Failed to read file ${filePath}: ${err.message}`);\n }\n\n const ast = babelParser.parse(sourceCode, {\n sourceType: 'module',\n plugins: ['typescript', 'jsx'],\n tokens: true,\n });\n\n let dictionaryObjectPath: NodePath<t.ObjectExpression> | null = null;\n let dictionaryIdentifier: string | null = null;\n\n // First look for direct objects with the right key, regardless of variable assignments\n traverse(ast, {\n ObjectExpression(path) {\n if (dictionaryObjectPath) return; // Already found\n\n // Check if this object has a key property with the right value\n const keyProp = path.node.properties.find((prop) => {\n if (!t.isObjectProperty(prop)) return false;\n if (!t.isIdentifier(prop.key) && !t.isStringLiteral(prop.key))\n return false;\n\n const keyName = t.isIdentifier(prop.key)\n ? prop.key.name\n : prop.key.value;\n\n if (keyName !== 'key' || !t.isStringLiteral(prop.value)) return false;\n\n // Unescape the value for comparison\n const propValue = prop.value.value;\n // Compare actual string content, not just raw representation\n return propValue === dictionaryIdentifierKey;\n });\n\n if (keyProp) {\n dictionaryObjectPath = path;\n path.stop();\n }\n },\n });\n\n // If not found directly, look for variable declarations and exports\n if (!dictionaryObjectPath) {\n appLogger(`Looking for variable declarations`, {\n isVerbose: true,\n });\n\n // Collect all variable declarations with objects\n const candidateVars: { id: string; path: NodePath<t.ObjectExpression> }[] =\n [];\n\n traverse(ast, {\n VariableDeclarator(path) {\n const { node } = path;\n if (!t.isIdentifier(node.id)) return;\n\n let objPath: NodePath<t.ObjectExpression> | null = null;\n\n // Direct object assignment\n if (node.init && t.isObjectExpression(node.init)) {\n objPath = path.get('init') as NodePath<t.ObjectExpression>;\n }\n // Object with TS type assertion (satisfies/as)\n else if (\n node.init &&\n (t.isTSAsExpression(node.init) || t.isTSTypeAssertion(node.init)) &&\n t.isObjectExpression(node.init.expression)\n ) {\n objPath = path.get('init.expression') as NodePath<t.ObjectExpression>;\n }\n\n if (objPath) {\n candidateVars.push({ id: node.id.name, path: objPath });\n }\n },\n });\n\n appLogger(`Found ${candidateVars.length} candidate variables`, {\n isVerbose: true,\n });\n\n // Check each candidate for the key property\n for (const { id, path } of candidateVars) {\n const keyProp = path.node.properties.find((prop) => {\n if (!t.isObjectProperty(prop)) return false;\n if (!t.isIdentifier(prop.key) && !t.isStringLiteral(prop.key))\n return false;\n\n const keyName = t.isIdentifier(prop.key)\n ? prop.key.name\n : prop.key.value;\n return (\n keyName === 'key' &&\n t.isStringLiteral(prop.value) &&\n prop.value.value === dictionaryIdentifierKey\n );\n });\n\n if (keyProp) {\n appLogger(`Found match in variable: ${id}`);\n dictionaryObjectPath = path;\n dictionaryIdentifier = id;\n break;\n }\n }\n\n // If still not found, dump all object expressions for debugging\n if (!dictionaryObjectPath) {\n appLogger('Could not find dictionary object. Dumping all objects:', {\n isVerbose: true,\n });\n traverse(ast, {\n ObjectExpression(path) {\n const props = path.node.properties\n .map((prop) => {\n if (!t.isObjectProperty(prop)) return 'non-object-property';\n if (!t.isIdentifier(prop.key) && !t.isStringLiteral(prop.key))\n return 'complex-key';\n\n const keyName = t.isIdentifier(prop.key)\n ? prop.key.name\n : prop.key.value;\n let valueDesc = 'unknown-value';\n\n if (t.isStringLiteral(prop.value)) {\n valueDesc = `\"${prop.value.value}\"`;\n }\n\n return `${keyName}: ${valueDesc}`;\n })\n .join(', ');\n\n appLogger(`Object: { ${props} }`);\n },\n });\n }\n }\n\n if (!dictionaryObjectPath) {\n throw new Error(\n `Could not find dictionary object with key '${dictionaryIdentifierKey}' in ${filePath}`\n );\n }\n\n // Find the 'content' property within the identified dictionary object\n const contentPropertyPath = (\n (dictionaryObjectPath as any).get('properties') as NodePath<\n t.ObjectProperty | t.SpreadElement | t.ObjectMethod\n >[]\n ).find((propPath) => {\n if (!propPath.isObjectProperty()) return false;\n const propNode = propPath.node;\n const keyName = t.isIdentifier(propNode.key)\n ? propNode.key.name\n : t.isStringLiteral(propNode.key)\n ? propNode.key.value\n : null;\n return keyName === 'content';\n });\n\n if (\n !contentPropertyPath ||\n !contentPropertyPath.isObjectProperty() ||\n !(contentPropertyPath.get('value') as NodePath).isObjectExpression()\n ) {\n throw new Error(\n `Could not find 'content' object property, or it's not an object, in dictionary in ${filePath}`\n );\n }\n\n const contentObjectPath = contentPropertyPath.get(\n 'value'\n ) as NodePath<t.ObjectExpression>;\n\n // Apply updates to each entry specified in the JSON\n for (const [entryKeyToUpdate, newEntryData] of Object.entries(\n updatesToApply\n )) {\n const targetPropertyPath = (\n contentObjectPath.get('properties') as NodePath<\n t.ObjectProperty | t.SpreadElement | t.ObjectMethod\n >[]\n ).find((propPath) => {\n if (!propPath.isObjectProperty()) return false;\n const propNode = propPath.node;\n const keyName = t.isIdentifier(propNode.key)\n ? propNode.key.name\n : t.isStringLiteral(propNode.key)\n ? propNode.key.value\n : null;\n return keyName === entryKeyToUpdate;\n });\n\n if (!targetPropertyPath || !targetPropertyPath.isObjectProperty()) {\n appLogger(\n `Key '${entryKeyToUpdate}' not found in content object of ${filePath}. Adding the missing key.`,\n {\n level: 'info',\n isVerbose: true,\n }\n );\n\n // Create a new property for the missing key\n let valueNode: t.Expression;\n\n if ((newEntryData as TypedNode)?.nodeType === NodeType.Translation) {\n // Create a new t() call with the translations\n const translationContent = newEntryData as TranslationContent;\n\n if (\n isPerLocaleDeclarationFile &&\n typeof locale === 'string' &&\n translationContent?.[NodeType.Translation]?.[locale]\n ) {\n // For per-locale files, use the string value directly\n valueNode = t.stringLiteral(\n String(translationContent[NodeType.Translation][locale])\n );\n } else {\n // Otherwise create a t() call with translations object\n const translationsObj = t.objectExpression(\n Object.entries(translationContent?.[NodeType.Translation]).map(\n ([langKey, langValue]) => {\n const keyNode = t.isValidIdentifier(langKey)\n ? t.identifier(langKey)\n : t.stringLiteral(langKey);\n return t.objectProperty(\n keyNode,\n t.stringLiteral(String(langValue))\n );\n }\n )\n );\n valueNode = t.callExpression(t.identifier('t'), [translationsObj]);\n }\n } else if (typeof newEntryData === 'string') {\n // Create a string literal for string values\n valueNode = t.stringLiteral(newEntryData);\n } else {\n // Fallback to empty string if we don't know how to handle this type\n appLogger(\n `Unsupported data type for new key '${entryKeyToUpdate}'. Using empty string.`,\n { level: 'warn', isVerbose: true }\n );\n valueNode = t.stringLiteral('');\n }\n\n // Add the new property to the content object\n const keyNode = t.isValidIdentifier(entryKeyToUpdate)\n ? t.identifier(entryKeyToUpdate)\n : t.stringLiteral(entryKeyToUpdate);\n const newProperty = t.objectProperty(keyNode, valueNode);\n contentObjectPath.node.properties.push(newProperty);\n\n continue;\n }\n\n const callExpressionPath = targetPropertyPath.get('value') as NodePath; // Path to the value, e.g., t(...)\n\n // Handle different types of values\n if (callExpressionPath.isCallExpression()) {\n const calleeNode = (callExpressionPath.node as t.CallExpression).callee;\n const calleeName = t.isIdentifier(calleeNode) ? calleeNode.name : null;\n\n // Handle 't' function calls\n if (\n (newEntryData as TypedNode)?.nodeType === 'translation' &&\n calleeName === 't'\n ) {\n const args = (callExpressionPath.node as t.CallExpression).arguments;\n if (args.length === 0 || !t.isObjectExpression(args[0])) {\n appLogger(\n `'t' call for '${entryKeyToUpdate}' in ${filePath} does not have an object literal as its first argument. Skipping.`,\n {\n level: 'warn',\n isVerbose: true,\n }\n );\n continue;\n }\n\n if (isPerLocaleDeclarationFile && typeof locale === 'string') {\n // For per-locale files, replace t() call with direct string\n const translations = (newEntryData as TranslationContent)?.[\n NodeType.Translation\n ];\n\n if (translations[locale]) {\n targetPropertyPath\n .get('value')\n .replaceWith(t.stringLiteral(String(translations[locale])));\n } else {\n appLogger(\n `Missing translation for locale '${locale}' in '${entryKeyToUpdate}'. Using first available translation.`,\n { level: 'warn', isVerbose: true }\n );\n const firstValue = Object.values(translations)[0];\n if (firstValue) {\n targetPropertyPath\n .get('value')\n .replaceWith(t.stringLiteral(String(firstValue)));\n }\n }\n continue;\n }\n\n const translationsObjectAstNode = args[0] as t.ObjectExpression;\n const processedLangKeysInJsonUpdate = new Set<string>();\n\n // Update existing language properties in the AST node\n translationsObjectAstNode.properties.forEach((prop: any) => {\n if (t.isObjectProperty(prop)) {\n const langKeyNode = prop.key;\n const astLangKeyName = t.isIdentifier(langKeyNode)\n ? langKeyNode.name\n : t.isStringLiteral(langKeyNode)\n ? langKeyNode.value\n : null;\n\n if (\n astLangKeyName &&\n (newEntryData as TranslationContent)?.[\n NodeType.Translation\n ].hasOwnProperty(astLangKeyName)\n ) {\n prop.value = t.stringLiteral(\n String(\n (newEntryData as TranslationContent)?.[\n NodeType.Translation\n ]?.[astLangKeyName]\n )\n );\n processedLangKeysInJsonUpdate.add(astLangKeyName);\n }\n }\n });\n\n // Add new language properties from the JSON update that were not originally in the AST node\n for (const [jsonLangKey, jsonLangValue] of Object.entries(\n (newEntryData as TranslationContent)?.[NodeType.Translation]\n )) {\n if (!processedLangKeysInJsonUpdate.has(jsonLangKey)) {\n const newKeyNode = t.isValidIdentifier(jsonLangKey)\n ? t.identifier(jsonLangKey)\n : t.stringLiteral(jsonLangKey);\n translationsObjectAstNode.properties.push(\n t.objectProperty(\n newKeyNode,\n t.stringLiteral(String(jsonLangValue))\n )\n );\n }\n }\n }\n // Handle other function calls in the future\n else {\n appLogger(\n `Unhandled callee '${calleeName || 'unknown'}' for key '${entryKeyToUpdate}' in ${filePath}.`,\n { level: 'warn', isVerbose: true }\n );\n }\n }\n // Handle direct string literals\n else if (callExpressionPath.isStringLiteral()) {\n // For string literals, directly replace with the new value\n if (typeof newEntryData === 'string') {\n targetPropertyPath\n .get('value')\n .replaceWith(t.stringLiteral(newEntryData));\n } else if ((newEntryData as any)?.[NodeType.Translation]) {\n // Handle translation content (use first available translation)\n const translations = (newEntryData as TranslationContent)[\n NodeType.Translation\n ];\n const firstValue = Object.values(translations)[0];\n if (firstValue) {\n targetPropertyPath\n .get('value')\n .replaceWith(t.stringLiteral(String(firstValue)));\n }\n } else {\n appLogger(\n `Unhandled data structure for string replacement at '${entryKeyToUpdate}' in ${filePath}.`,\n { level: 'warn', isVerbose: true }\n );\n }\n }\n // Handle other value types (objects, arrays, etc.)\n else {\n const valueType = callExpressionPath.node.type;\n appLogger(\n `Updating value of type ${valueType} for '${entryKeyToUpdate}' in ${filePath}`,\n { level: 'info', isVerbose: true }\n );\n\n // For simple values like strings, use direct replacement\n if (typeof newEntryData === 'string') {\n targetPropertyPath\n .get('value')\n .replaceWith(t.stringLiteral(newEntryData));\n }\n // For translation content, use a smart approach\n else if ((newEntryData as any)?.[NodeType.Translation]) {\n // Extract just the value relevant to this file's locale\n const translations = (newEntryData as TranslationContent)[\n NodeType.Translation\n ];\n\n // Try to determine locale from file path (assuming a pattern like .fr.content.ts)\n const localeMatch = filePath.match(/\\.([a-z]{2})\\.content\\.(ts|js)$/i);\n const locale = localeMatch\n ? localeMatch[1]\n : Object.keys(translations)[0];\n\n if (translations[locale]) {\n targetPropertyPath\n .get('value')\n .replaceWith(t.stringLiteral(String(translations[locale])));\n } else {\n // Fallback to first translation\n const firstValue = Object.values(translations)[0];\n if (firstValue) {\n targetPropertyPath\n .get('value')\n .replaceWith(t.stringLiteral(String(firstValue)));\n }\n }\n } else {\n appLogger(\n `Cannot update value of type ${valueType} for '${entryKeyToUpdate}' in ${filePath}. Unsupported data structure.`,\n { level: 'warn', isVerbose: true }\n );\n }\n }\n }\n\n // Generate JavaScript/TypeScript code from the modified AST\n const generatedCode = generator(ast, {\n retainLines: true,\n comments: true,\n jsescOption: {\n minimal: true, // This ensures Unicode characters are not escaped\n },\n }).code;\n\n let finalCode = generatedCode;\n\n finalCode = await formatCode(filePath, finalCode);\n\n // Write the modified code back to the file\n try {\n await writeFile(filePath, finalCode, 'utf-8');\n logger(`Successfully updated ${filePath}`, {\n level: 'info',\n isVerbose: true,\n });\n } catch (error) {\n const err = error as Error;\n logger(`Failed to write updated file: ${filePath}`, {\n level: 'error',\n });\n throw new Error(`Failed to write updated file ${filePath}: ${err.message}`);\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAsB;AACtB,kBAA6B;AAC7B,sBAAmC;AACnC,QAAmB;AACnB,oBAAqC;AACrC,mBAA0B;AAC1B,kBAKO;AACP,gBAA2B;AAC3B,sBAAoC;AACpC,kBAAwB;AACxB,+CAAkD;AAClD,wBAA2B;AAQpB,MAAM,cAAc,OACzB,UACA,eACkB;AAClB,QAAM,gBAAY,4BAAa,aAAAA,OAAa;AAE5C,QAAM;AAAA,IACJ,KAAK;AAAA,IACL,SAAS;AAAA,IACT;AAAA,IACA;AAAA,EACF,IAAI;AACJ,QAAM,6BAA6B,OAAO,WAAW;AAGrD,MAAI,KAAC,sBAAW,QAAQ,GAAG;AACzB,UAAM,oBAAgB,qBAAQ,QAAQ;AAEtC,QAAI,SAAS;AAEb,QAAI,kBAAkB,SAAS,kBAAkB,QAAQ;AACvD,eAAS;AAAA,IACX,WAAW,kBAAkB,UAAU,kBAAkB,SAAS;AAChE,eAAS;AAAA,IACX,OAAO;AACL,eAAS;AAAA,IACX;AAEA,cAAU,oCAAoC;AAAA,MAC5C,WAAW;AAAA,IACb,CAAC;AACD,UAAM,WAAW,UAAM;AAAA,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gB,iBAAAC,SAAU,KAAK;AAAA,IACnC,aAAa;AAAA,IACb,UAAU;AAAA,IACV,aAAa;AAAA,MACX,SAAS;AAAA;AAAA,IACX;AAAA,EACF,CAAC,EAAE;AAEH,MAAI,YAAY;AAEhB,cAAY,UAAM,8BAAW,UAAU,SAAS;AAGhD,MAAI;AACF,cAAM,2BAAU,UAAU,WAAW,OAAO;AAC5C,8BAAO,wBAAwB,QAAQ,IAAI;AAAA,MACzC,OAAO;AAAA,MACP,WAAW;AAAA,IACb,CAAC;AAAA,EACH,SAAS,OAAO;AACd,UAAM,MAAM;AACZ,8BAAO,iCAAiC,QAAQ,IAAI;AAAA,MAClD,OAAO;AAAA,IACT,CAAC;AACD,UAAM,IAAI,MAAM,gCAAgC,QAAQ,KAAK,IAAI,OAAO,EAAE;AAAA,EAC5E;AACF;","names":["configuration","traverse","keyNode","locale","generator"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/writeContentDeclaration/writeJSFile.ts"],"sourcesContent":["import generator from '@babel/generator';\nimport * as babelParser from '@babel/parser';\nimport traverse, { NodePath } from '@babel/traverse';\nimport * as t from '@babel/types';\nimport { getAppLogger, logger } from '@intlayer/config';\nimport configuration from '@intlayer/config/built';\nimport {\n Dictionary,\n NodeType,\n TranslationContent,\n TypedNode,\n} from '@intlayer/core';\nimport { existsSync } from 'fs';\nimport { readFile, writeFile } from 'fs/promises';\nimport { extname } from 'path';\nimport { getContentDeclarationFileTemplate } from '../getContentDeclarationFileTemplate/getContentDeclarationFileTemplate';\nimport { formatCode } from './formatCode';\n\n/**\n * Updates a JavaScript/TypeScript file based on the provided JSON instructions.\n * It targets a specific dictionary object within the file (identified by its 'key' property)\n * and updates its 'content' entries. Currently, it focuses on modifying arguments\n * of 't' (translation) function calls.\n */\nexport const writeJSFile = async (\n filePath: string,\n dictionary: Dictionary\n): Promise<void> => {\n const appLogger = getAppLogger(configuration);\n\n const {\n key: dictionaryIdentifierKey,\n content: updatesToApply,\n locale,\n autoFilled,\n } = dictionary;\n const isPerLocaleDeclarationFile = typeof locale === 'string';\n\n // Check if the file exist\n if (!existsSync(filePath)) {\n const fileExtension = extname(filePath);\n\n let format = 'ts' as 'ts' | 'cjs' | 'esm';\n\n if (fileExtension === '.ts' || fileExtension === '.tsx') {\n format = 'ts';\n } else if (fileExtension === '.cjs' || fileExtension === '.cjsx') {\n format = 'cjs';\n } else {\n format = 'esm';\n }\n\n appLogger('File does not exist, creating it', {\n isVerbose: true,\n });\n const template = await getContentDeclarationFileTemplate(\n dictionaryIdentifierKey,\n format,\n { locale, autoFilled }\n );\n\n await writeFile(filePath, template, 'utf-8');\n }\n\n let sourceCode: string;\n try {\n sourceCode = await readFile(filePath, 'utf-8');\n } catch (error) {\n const err = error as Error;\n appLogger(`Failed to read file: ${filePath}`, {\n level: 'error',\n });\n throw new Error(`Failed to read file ${filePath}: ${err.message}`);\n }\n\n const ast = babelParser.parse(sourceCode, {\n sourceType: 'module',\n plugins: ['typescript', 'jsx'],\n tokens: true,\n });\n\n let dictionaryObjectPath: NodePath<t.ObjectExpression> | null = null;\n let dictionaryIdentifier: string | null = null;\n\n // First look for direct objects with the right key, regardless of variable assignments\n traverse(ast, {\n ObjectExpression(path) {\n if (dictionaryObjectPath) return; // Already found\n\n // Check if this object has a key property with the right value\n const keyProp = path.node.properties.find((prop) => {\n if (!t.isObjectProperty(prop)) return false;\n if (!t.isIdentifier(prop.key) && !t.isStringLiteral(prop.key))\n return false;\n\n const keyName = t.isIdentifier(prop.key)\n ? prop.key.name\n : prop.key.value;\n\n if (keyName !== 'key' || !t.isStringLiteral(prop.value)) return false;\n\n // Unescape the value for comparison\n const propValue = prop.value.value;\n // Compare actual string content, not just raw representation\n return propValue === dictionaryIdentifierKey;\n });\n\n if (keyProp) {\n dictionaryObjectPath = path;\n path.stop();\n }\n },\n });\n\n // If not found directly, look for variable declarations and exports\n if (!dictionaryObjectPath) {\n appLogger(`Looking for variable declarations`, {\n isVerbose: true,\n });\n\n // Collect all variable declarations with objects\n const candidateVars: { id: string; path: NodePath<t.ObjectExpression> }[] =\n [];\n\n traverse(ast, {\n VariableDeclarator(path) {\n const { node } = path;\n if (!t.isIdentifier(node.id)) return;\n\n let objPath: NodePath<t.ObjectExpression> | null = null;\n\n // Direct object assignment\n if (node.init && t.isObjectExpression(node.init)) {\n objPath = path.get('init') as NodePath<t.ObjectExpression>;\n }\n // Object with TS type assertion (satisfies/as)\n else if (\n node.init &&\n (t.isTSAsExpression(node.init) || t.isTSTypeAssertion(node.init)) &&\n t.isObjectExpression(node.init.expression)\n ) {\n objPath = path.get('init.expression') as NodePath<t.ObjectExpression>;\n }\n\n if (objPath) {\n candidateVars.push({ id: node.id.name, path: objPath });\n }\n },\n });\n\n appLogger(`Found ${candidateVars.length} candidate variables`, {\n isVerbose: true,\n });\n\n // Check each candidate for the key property\n for (const { id, path } of candidateVars) {\n const keyProp = path.node.properties.find((prop) => {\n if (!t.isObjectProperty(prop)) return false;\n if (!t.isIdentifier(prop.key) && !t.isStringLiteral(prop.key))\n return false;\n\n const keyName = t.isIdentifier(prop.key)\n ? prop.key.name\n : prop.key.value;\n return (\n keyName === 'key' &&\n t.isStringLiteral(prop.value) &&\n prop.value.value === dictionaryIdentifierKey\n );\n });\n\n if (keyProp) {\n appLogger(`Found match in variable: ${id}`);\n dictionaryObjectPath = path;\n dictionaryIdentifier = id;\n break;\n }\n }\n\n // If still not found, dump all object expressions for debugging\n if (!dictionaryObjectPath) {\n appLogger('Could not find dictionary object. Dumping all objects:', {\n isVerbose: true,\n });\n traverse(ast, {\n ObjectExpression(path) {\n const props = path.node.properties\n .map((prop) => {\n if (!t.isObjectProperty(prop)) return 'non-object-property';\n if (!t.isIdentifier(prop.key) && !t.isStringLiteral(prop.key))\n return 'complex-key';\n\n const keyName = t.isIdentifier(prop.key)\n ? prop.key.name\n : prop.key.value;\n let valueDesc = 'unknown-value';\n\n if (t.isStringLiteral(prop.value)) {\n valueDesc = `\"${prop.value.value}\"`;\n }\n\n return `${keyName}: ${valueDesc}`;\n })\n .join(', ');\n\n appLogger(`Object: { ${props} }`);\n },\n });\n }\n }\n\n if (!dictionaryObjectPath) {\n throw new Error(\n `Could not find dictionary object with key '${dictionaryIdentifierKey}' in ${filePath}`\n );\n }\n\n // Find the 'content' property within the identified dictionary object\n const contentPropertyPath = (\n (dictionaryObjectPath as any).get('properties') as NodePath<\n t.ObjectProperty | t.SpreadElement | t.ObjectMethod\n >[]\n ).find((propPath) => {\n if (!propPath.isObjectProperty()) return false;\n const propNode = propPath.node;\n const keyName = t.isIdentifier(propNode.key)\n ? propNode.key.name\n : t.isStringLiteral(propNode.key)\n ? propNode.key.value\n : null;\n return keyName === 'content';\n });\n\n if (\n !contentPropertyPath ||\n !contentPropertyPath.isObjectProperty() ||\n !(contentPropertyPath.get('value') as NodePath).isObjectExpression()\n ) {\n throw new Error(\n `Could not find 'content' object property, or it's not an object, in dictionary in ${filePath}`\n );\n }\n\n const contentObjectPath = contentPropertyPath.get(\n 'value'\n ) as NodePath<t.ObjectExpression>;\n\n /**\n * Build a Babel Expression for any JSON-serializable value or TypedNode.\n * - Translation nodes become t({...}) or string literal in per-locale files\n * - Objects recurse to ObjectExpression\n * - Arrays recurse to ArrayExpression\n * - Strings/numbers/booleans/null to corresponding literals\n */\n const buildValueNodeFromData = (data: any): t.Expression => {\n // Translation typed node\n if ((data as TypedNode)?.nodeType === NodeType.Translation) {\n const translationContent = data as TranslationContent;\n if (\n isPerLocaleDeclarationFile &&\n typeof locale === 'string' &&\n translationContent?.[NodeType.Translation]?.[locale] !== undefined\n ) {\n return buildValueNodeFromData(\n translationContent[NodeType.Translation][locale]\n );\n }\n const translationsObj = t.objectExpression(\n Object.entries(translationContent?.[NodeType.Translation] ?? {}).map(\n ([langKey, langValue]) => {\n const keyNode = t.isValidIdentifier(langKey)\n ? t.identifier(langKey)\n : t.stringLiteral(langKey);\n return t.objectProperty(keyNode, buildValueNodeFromData(langValue));\n }\n )\n );\n return t.callExpression(t.identifier('t'), [translationsObj]);\n }\n\n // Arrays\n if (Array.isArray(data)) {\n return t.arrayExpression(data.map((el) => buildValueNodeFromData(el)));\n }\n\n // Objects (plain)\n if (data && typeof data === 'object') {\n const props = Object.entries(data).map(([k, v]) => {\n const key = t.isValidIdentifier(k)\n ? t.identifier(k)\n : t.stringLiteral(k);\n return t.objectProperty(key, buildValueNodeFromData(v));\n });\n return t.objectExpression(props);\n }\n\n // Primitives\n switch (typeof data) {\n case 'string':\n return t.stringLiteral(data);\n case 'number':\n return t.numericLiteral(data);\n case 'boolean':\n return t.booleanLiteral(data);\n default:\n return t.nullLiteral();\n }\n };\n\n /** Ensure an object property exists on a given object expression path. */\n const ensureObjectProperty = (\n objPath: NodePath<t.ObjectExpression>,\n key: string\n ): NodePath<t.ObjectProperty> => {\n const existing = (\n objPath.get('properties') as NodePath<\n t.ObjectProperty | t.SpreadElement | t.ObjectMethod\n >[]\n ).find((propPath) => {\n if (!propPath.isObjectProperty()) return false;\n const propNode = propPath.node;\n const keyName = t.isIdentifier(propNode.key)\n ? propNode.key.name\n : t.isStringLiteral(propNode.key)\n ? propNode.key.value\n : null;\n return keyName === key;\n }) as NodePath<t.ObjectProperty> | undefined;\n\n if (existing) return existing;\n\n const keyNode = t.isValidIdentifier(key)\n ? t.identifier(key)\n : t.stringLiteral(key);\n const newProp = t.objectProperty(keyNode, t.objectExpression([]));\n objPath.node.properties.push(newProp);\n\n // Return a fresh path for the newly pushed property\n const props = objPath.get('properties') as NodePath<\n t.ObjectProperty | t.SpreadElement | t.ObjectMethod\n >[];\n return props[props.length - 1] as NodePath<t.ObjectProperty>;\n };\n\n /** Recursively merge a JSON-like value into an ObjectExpression property path. */\n const mergeValueIntoProperty = (\n propPath: NodePath<t.ObjectProperty>,\n value: any,\n propKeyForLogs: string\n ) => {\n const valuePath = propPath.get('value') as NodePath;\n\n // Translation typed node → either update t() args or replace value\n if ((value as TypedNode)?.nodeType === NodeType.Translation) {\n const translationContent = value as TranslationContent;\n if (\n valuePath.isCallExpression() &&\n t.isIdentifier(valuePath.node.callee) &&\n valuePath.node.callee.name === 't'\n ) {\n // Replace argument with the full translations object (simpler and robust)\n const translationsObj = t.objectExpression(\n Object.entries(translationContent?.[NodeType.Translation] ?? {}).map(\n ([langKey, langValue]) =>\n t.objectProperty(\n t.isValidIdentifier(langKey)\n ? t.identifier(langKey)\n : t.stringLiteral(langKey),\n buildValueNodeFromData(langValue)\n )\n )\n );\n\n if (isPerLocaleDeclarationFile && typeof locale === 'string') {\n const localized =\n translationContent?.[NodeType.Translation]?.[locale];\n if (localized !== undefined) {\n valuePath.replaceWith(buildValueNodeFromData(localized));\n return;\n }\n }\n\n (valuePath.node as t.CallExpression).arguments = [translationsObj];\n return;\n }\n\n // Otherwise, replace with a fresh node\n valuePath.replaceWith(buildValueNodeFromData(value));\n return;\n }\n\n // Arrays → replace entirely\n if (Array.isArray(value)) {\n valuePath.replaceWith(buildValueNodeFromData(value));\n return;\n }\n\n // Objects → ensure object expression and recurse into each key\n if (value && typeof value === 'object') {\n if (!valuePath.isObjectExpression()) {\n valuePath.replaceWith(t.objectExpression([]));\n }\n const objPath = valuePath as NodePath<t.ObjectExpression>;\n for (const [k, v] of Object.entries(value)) {\n const childProp = ensureObjectProperty(objPath, k);\n mergeValueIntoProperty(childProp, v, `${propKeyForLogs}.${k}`);\n }\n return;\n }\n\n // Primitives → replace\n valuePath.replaceWith(buildValueNodeFromData(value));\n return;\n };\n\n // Apply updates to each entry specified in the JSON (now supports deep nesting)\n for (const [entryKeyToUpdate, newEntryData] of Object.entries(\n updatesToApply\n )) {\n let targetPropertyPath = (\n contentObjectPath.get('properties') as NodePath<\n t.ObjectProperty | t.SpreadElement | t.ObjectMethod\n >[]\n ).find((propPath) => {\n if (!propPath.isObjectProperty()) return false;\n const propNode = propPath.node;\n const keyName = t.isIdentifier(propNode.key)\n ? propNode.key.name\n : t.isStringLiteral(propNode.key)\n ? propNode.key.value\n : null;\n return keyName === entryKeyToUpdate;\n }) as NodePath<t.ObjectProperty> | undefined;\n\n if (!targetPropertyPath) {\n const keyNode = t.isValidIdentifier(entryKeyToUpdate)\n ? t.identifier(entryKeyToUpdate)\n : t.stringLiteral(entryKeyToUpdate);\n // By default create an empty object; merge will replace if needed\n const newProperty = t.objectProperty(keyNode, t.objectExpression([]));\n contentObjectPath.node.properties.push(newProperty);\n const props = contentObjectPath.get('properties') as NodePath<\n t.ObjectProperty | t.SpreadElement | t.ObjectMethod\n >[];\n targetPropertyPath = props[\n props.length - 1\n ] as NodePath<t.ObjectProperty>;\n }\n\n mergeValueIntoProperty(targetPropertyPath, newEntryData, entryKeyToUpdate);\n }\n\n // Generate JavaScript/TypeScript code from the modified AST\n const generatedCode = generator(ast, {\n retainLines: true,\n comments: true,\n jsescOption: {\n minimal: true, // This ensures Unicode characters are not escaped\n },\n }).code;\n\n let finalCode = generatedCode;\n\n finalCode = await formatCode(filePath, finalCode);\n\n // Write the modified code back to the file\n try {\n await writeFile(filePath, finalCode, 'utf-8');\n logger(`Successfully updated ${filePath}`, {\n level: 'info',\n isVerbose: true,\n });\n } catch (error) {\n const err = error as Error;\n logger(`Failed to write updated file: ${filePath}`, {\n level: 'error',\n });\n throw new Error(`Failed to write updated file ${filePath}: ${err.message}`);\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAsB;AACtB,kBAA6B;AAC7B,sBAAmC;AACnC,QAAmB;AACnB,oBAAqC;AACrC,mBAA0B;AAC1B,kBAKO;AACP,gBAA2B;AAC3B,sBAAoC;AACpC,kBAAwB;AACxB,+CAAkD;AAClD,wBAA2B;AAQpB,MAAM,cAAc,OACzB,UACA,eACkB;AAClB,QAAM,gBAAY,4BAAa,aAAAA,OAAa;AAE5C,QAAM;AAAA,IACJ,KAAK;AAAA,IACL,SAAS;AAAA,IACT;AAAA,IACA;AAAA,EACF,IAAI;AACJ,QAAM,6BAA6B,OAAO,WAAW;AAGrD,MAAI,KAAC,sBAAW,QAA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