@intlayer/chokidar 5.7.7 → 5.8.0-canary.0

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.
Files changed (32) hide show
  1. package/dist/cjs/loadDictionaries/loadDictionaries.cjs +2 -1
  2. package/dist/cjs/loadDictionaries/loadDictionaries.cjs.map +1 -1
  3. package/dist/cjs/mergeDictionaries.cjs +67 -3
  4. package/dist/cjs/mergeDictionaries.cjs.map +1 -1
  5. package/dist/cjs/reduceDictionaryContent/applyMask.cjs +16 -2
  6. package/dist/cjs/reduceDictionaryContent/applyMask.cjs.map +1 -1
  7. package/dist/cjs/writeContentDeclaration/formatCode.cjs +3 -1
  8. package/dist/cjs/writeContentDeclaration/formatCode.cjs.map +1 -1
  9. package/dist/cjs/writeContentDeclaration/writeJSFile.cjs +101 -148
  10. package/dist/cjs/writeContentDeclaration/writeJSFile.cjs.map +1 -1
  11. package/dist/esm/loadDictionaries/loadDictionaries.mjs +2 -1
  12. package/dist/esm/loadDictionaries/loadDictionaries.mjs.map +1 -1
  13. package/dist/esm/mergeDictionaries.mjs +67 -3
  14. package/dist/esm/mergeDictionaries.mjs.map +1 -1
  15. package/dist/esm/reduceDictionaryContent/applyMask.mjs +16 -2
  16. package/dist/esm/reduceDictionaryContent/applyMask.mjs.map +1 -1
  17. package/dist/esm/writeContentDeclaration/formatCode.mjs +3 -1
  18. package/dist/esm/writeContentDeclaration/formatCode.mjs.map +1 -1
  19. package/dist/esm/writeContentDeclaration/writeJSFile.mjs +101 -148
  20. package/dist/esm/writeContentDeclaration/writeJSFile.mjs.map +1 -1
  21. package/dist/types/loadDictionaries/loadDictionaries.d.ts.map +1 -1
  22. package/dist/types/mergeDictionaries.d.ts.map +1 -1
  23. package/dist/types/reduceDictionaryContent/applyMask.d.ts.map +1 -1
  24. package/dist/types/writeContentDeclaration/formatCode.d.ts.map +1 -1
  25. package/dist/types/writeContentDeclaration/writeJSFile.d.ts.map +1 -1
  26. package/package.json +17 -17
  27. package/dist/cjs/utils/resolveObjectPromises.test.cjs +0 -107
  28. package/dist/cjs/utils/resolveObjectPromises.test.cjs.map +0 -1
  29. package/dist/esm/utils/resolveObjectPromises.test.mjs +0 -106
  30. package/dist/esm/utils/resolveObjectPromises.test.mjs.map +0 -1
  31. package/dist/types/utils/resolveObjectPromises.test.d.ts +0 -2
  32. package/dist/types/utils/resolveObjectPromises.test.d.ts.map +0 -1
@@ -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: ${fileP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,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,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,MACrB;AAAA,MACA;AAAA,MACA,EAAE,QAAQ,WAAW;AAAA,IACvB;AAEA,cAAM,2BAAU,UAAU,UAAU,OAAO;AAA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WAAW;AAClD,YAAM,QAAQ,kBAAkB,IAAI,YAAY;AAGhD,2BAAqB,MACnB,MAAM,SAAS,CACjB;AAAA,IACF;AAEA,2BAAuB,oBAAoB,cAAc,gBAAgB;AAAA,EAC3E;AAGA,QAAM,oBAAgB,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","generator"]}
@@ -3,6 +3,7 @@ import {
3
3
  getAppLogger,
4
4
  getConfiguration
5
5
  } from "@intlayer/config";
6
+ import { relative } from "node:path";
6
7
  import { fetchDistantDictionaryKeys } from "../fetchDistantDictionaryKeys.mjs";
7
8
  import { logger } from "../log.mjs";
8
9
  import { sortAlphabetically } from "../utils/sortAlphabetically.mjs";
@@ -24,7 +25,7 @@ const loadDictionaries = async (contentDeclarationsPaths, configuration = getCon
24
25
  if (!hasContent) {
25
26
  console.error(
26
27
  "Content declaration has no exported content",
27
- dict.filePath
28
+ dict.filePath ? relative(configuration.content.baseDir, dict.filePath) : ""
28
29
  );
29
30
  } else if (!hasKey) {
30
31
  console.error("Content declaration has no key", dict.filePath);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/loadDictionaries/loadDictionaries.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport {\n ESMxCJSRequire,\n getAppLogger,\n getConfiguration,\n} from '@intlayer/config';\nimport type { Dictionary } from '@intlayer/core';\nimport { fetchDistantDictionaryKeys } from '../fetchDistantDictionaryKeys';\nimport { logger } from '../log';\nimport { sortAlphabetically } from '../utils/sortAlphabetically';\nimport { loadContentDeclarations } from './loadContentDeclaration';\nimport { loadDistantDictionaries } from './loadDistantDictionaries';\n\nexport const loadDictionaries = async (\n contentDeclarationsPaths: string[] | string,\n configuration = getConfiguration(),\n projectRequire = ESMxCJSRequire\n): Promise<Dictionary[]> => {\n try {\n const appLogger = getAppLogger(configuration);\n const { editor } = configuration;\n\n appLogger('Dictionaries:', { isVerbose: true });\n\n const files = Array.isArray(contentDeclarationsPaths)\n ? contentDeclarationsPaths\n : [contentDeclarationsPaths];\n\n const localDictionaries: Dictionary[] = await loadContentDeclarations(\n files,\n projectRequire\n );\n\n const filteredLocalDictionaries = localDictionaries.filter((dict) => {\n const hasKey = Boolean(dict.key);\n const hasContent = Boolean(dict.content);\n\n if (!hasContent) {\n console.error(\n 'Content declaration has no exported content',\n dict.filePath\n );\n } else if (!hasKey) {\n console.error('Content declaration has no key', dict.filePath);\n }\n\n return hasKey && hasContent;\n });\n\n const localDictionaryKeys = filteredLocalDictionaries\n .map((dict) => dict.key)\n .filter(Boolean); // Remove empty or undefined keys\n\n // Initialize the logger with both local and distant dictionaries\n logger.init(localDictionaryKeys, []);\n\n // Update logger statuses for local dictionaries\n logger.updateStatus(\n filteredLocalDictionaries.map((dict) => ({\n dictionaryKey: dict.key,\n type: 'local',\n status: { status: 'built' },\n }))\n );\n\n let distantDictionaries: DictionaryAPI[] = [];\n let distantDictionaryKeys: string[] = [];\n\n if (editor.clientId && editor.clientSecret) {\n try {\n // Fetch distant dictionary keys\n distantDictionaryKeys = await fetchDistantDictionaryKeys();\n\n const orderedDistantDictionaryKeys =\n distantDictionaryKeys.sort(sortAlphabetically);\n\n // Add distant dictionaries to the logger\n logger.addDictionaryKeys('distant', orderedDistantDictionaryKeys);\n\n // Fetch distant dictionaries\n distantDictionaries = await loadDistantDictionaries({\n dictionaryKeys: orderedDistantDictionaryKeys,\n });\n } catch (_error) {\n appLogger('Error during fetching distant dictionaries', {\n level: 'error',\n });\n }\n }\n\n // Ensure the logger is stopped\n logger.stop();\n\n return [...filteredLocalDictionaries, ...distantDictionaries];\n } catch (error) {\n // Ensure the logger is stopped\n logger.stop();\n\n throw error; // Re-throw the error after logging\n }\n};\n"],"mappings":"AAEA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,kCAAkC;AAC3C,SAAS,cAAc;AACvB,SAAS,0BAA0B;AACnC,SAAS,+BAA+B;AACxC,SAAS,+BAA+B;AAEjC,MAAM,mBAAmB,OAC9B,0BACA,gBAAgB,iBAAiB,GACjC,iBAAiB,mBACS;AAC1B,MAAI;AACF,UAAM,YAAY,aAAa,aAAa;AAC5C,UAAM,EAAE,OAAO,IAAI;AAEnB,cAAU,iBAAiB,EAAE,WAAW,KAAK,CAAC;AAE9C,UAAM,QAAQ,MAAM,QAAQ,wBAAwB,IAChD,2BACA,CAAC,wBAAwB;AAE7B,UAAM,oBAAkC,MAAM;AAAA,MAC5C;AAAA,MACA;AAAA,IACF;AAEA,UAAM,4BAA4B,kBAAkB,OAAO,CAAC,SAAS;AACnE,YAAM,SAAS,QAAQ,KAAK,GAAG;AAC/B,YAAM,aAAa,QAAQ,KAAK,OAAO;AAEvC,UAAI,CAAC,YAAY;AACf,gBAAQ;AAAA,UACN;AAAA,UACA,KAAK;AAAA,QACP;AAAA,MACF,WAAW,CAAC,QAAQ;AAClB,gBAAQ,MAAM,kCAAkC,KAAK,QAAQ;AAAA,MAC/D;AAEA,aAAO,UAAU;AAAA,IACnB,CAAC;AAED,UAAM,sBAAsB,0BACzB,IAAI,CAAC,SAAS,KAAK,GAAG,EACtB,OAAO,OAAO;AAGjB,WAAO,KAAK,qBAAqB,CAAC,CAAC;AAGnC,WAAO;AAAA,MACL,0BAA0B,IAAI,CAAC,UAAU;AAAA,QACvC,eAAe,KAAK;AAAA,QACpB,MAAM;AAAA,QACN,QAAQ,EAAE,QAAQ,QAAQ;AAAA,MAC5B,EAAE;AAAA,IACJ;AAEA,QAAI,sBAAuC,CAAC;AAC5C,QAAI,wBAAkC,CAAC;AAEvC,QAAI,OAAO,YAAY,OAAO,cAAc;AAC1C,UAAI;AAEF,gCAAwB,MAAM,2BAA2B;AAEzD,cAAM,+BACJ,sBAAsB,KAAK,kBAAkB;AAG/C,eAAO,kBAAkB,WAAW,4BAA4B;AAGhE,8BAAsB,MAAM,wBAAwB;AAAA,UAClD,gBAAgB;AAAA,QAClB,CAAC;AAAA,MACH,SAAS,QAAQ;AACf,kBAAU,8CAA8C;AAAA,UACtD,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACF;AAGA,WAAO,KAAK;AAEZ,WAAO,CAAC,GAAG,2BAA2B,GAAG,mBAAmB;AAAA,EAC9D,SAAS,OAAO;AAEd,WAAO,KAAK;AAEZ,UAAM;AAAA,EACR;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/loadDictionaries/loadDictionaries.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport {\n ESMxCJSRequire,\n getAppLogger,\n getConfiguration,\n} from '@intlayer/config';\nimport type { Dictionary } from '@intlayer/core';\nimport { relative } from 'node:path';\nimport { fetchDistantDictionaryKeys } from '../fetchDistantDictionaryKeys';\nimport { logger } from '../log';\nimport { sortAlphabetically } from '../utils/sortAlphabetically';\nimport { loadContentDeclarations } from './loadContentDeclaration';\nimport { loadDistantDictionaries } from './loadDistantDictionaries';\n\nexport const loadDictionaries = async (\n contentDeclarationsPaths: string[] | string,\n configuration = getConfiguration(),\n projectRequire = ESMxCJSRequire\n): Promise<Dictionary[]> => {\n try {\n const appLogger = getAppLogger(configuration);\n const { editor } = configuration;\n\n appLogger('Dictionaries:', { isVerbose: true });\n\n const files = Array.isArray(contentDeclarationsPaths)\n ? contentDeclarationsPaths\n : [contentDeclarationsPaths];\n\n const localDictionaries: Dictionary[] = await loadContentDeclarations(\n files,\n projectRequire\n );\n\n const filteredLocalDictionaries = localDictionaries.filter((dict) => {\n const hasKey = Boolean(dict.key);\n const hasContent = Boolean(dict.content);\n\n if (!hasContent) {\n console.error(\n 'Content declaration has no exported content',\n dict.filePath\n ? relative(configuration.content.baseDir, dict.filePath)\n : ''\n );\n } else if (!hasKey) {\n console.error('Content declaration has no key', dict.filePath);\n }\n\n return hasKey && hasContent;\n });\n\n const localDictionaryKeys = filteredLocalDictionaries\n .map((dict) => dict.key)\n .filter(Boolean); // Remove empty or undefined keys\n\n // Initialize the logger with both local and distant dictionaries\n logger.init(localDictionaryKeys, []);\n\n // Update logger statuses for local dictionaries\n logger.updateStatus(\n filteredLocalDictionaries.map((dict) => ({\n dictionaryKey: dict.key,\n type: 'local',\n status: { status: 'built' },\n }))\n );\n\n let distantDictionaries: DictionaryAPI[] = [];\n let distantDictionaryKeys: string[] = [];\n\n if (editor.clientId && editor.clientSecret) {\n try {\n // Fetch distant dictionary keys\n distantDictionaryKeys = await fetchDistantDictionaryKeys();\n\n const orderedDistantDictionaryKeys =\n distantDictionaryKeys.sort(sortAlphabetically);\n\n // Add distant dictionaries to the logger\n logger.addDictionaryKeys('distant', orderedDistantDictionaryKeys);\n\n // Fetch distant dictionaries\n distantDictionaries = await loadDistantDictionaries({\n dictionaryKeys: orderedDistantDictionaryKeys,\n });\n } catch (_error) {\n appLogger('Error during fetching distant dictionaries', {\n level: 'error',\n });\n }\n }\n\n // Ensure the logger is stopped\n logger.stop();\n\n return [...filteredLocalDictionaries, ...distantDictionaries];\n } catch (error) {\n // Ensure the logger is stopped\n logger.stop();\n\n throw error; // Re-throw the error after logging\n }\n};\n"],"mappings":"AAEA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS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,sBAAsB,KAAK,kBAAkB;AAG/C,eAAO,kBAAkB,WAAW,4BAA4B;AAGhE,8BAAsB,MAAM,wBAAwB;AAAA,UAClD,gBAAgB;AAAA,QAClB,CAAC;AAAA,MACH,SAAS,QAAQ;AACf,kBAAU,8CAA8C;AAAA,UACtD,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACF;AAGA,WAAO,KAAK;AAEZ,WAAO,CAAC,GAAG,2BAA2B,GAAG,mBAAmB;AAAA,EAC9D,SAAS,OAAO;AAEd,WAAO,KAAK;AAEZ,UAAM;AAAA,EACR;AACF;","names":[]}
@@ -8,7 +8,7 @@ const checkTypesMatch = (obj1, obj2, dictionaryKey, path = []) => {
8
8
  const type2 = getNodeType(obj2);
9
9
  if (type1 !== type2) {
10
10
  appLogger(
11
- `Error: Dictionary ${dictionaryKey} has a multiple content files with type mismatch at path "${path.join(".")}": Cannot merge ${type1} with ${type2}`,
11
+ `Error: Dictionary "${dictionaryKey}" has a multiple content files with type mismatch at path "${path.join(".")}": Cannot merge ${type1} with ${type2}`,
12
12
  {
13
13
  level: "error"
14
14
  }
@@ -23,9 +23,65 @@ const checkTypesMatch = (obj1, obj2, dictionaryKey, path = []) => {
23
23
  }
24
24
  }
25
25
  };
26
+ const arrayMerge = (destinationArray, sourceArray) => {
27
+ const isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
28
+ const getKey = (item) => {
29
+ if (!isObject(item)) return void 0;
30
+ const key = item.key;
31
+ if (typeof key === "string" || typeof key === "number") return key;
32
+ return void 0;
33
+ };
34
+ const result = [];
35
+ const destKeyToIndex = /* @__PURE__ */ new Map();
36
+ const destUsed = new Array(destinationArray.length).fill(false);
37
+ for (let i = 0; i < destinationArray.length; i++) {
38
+ const k = getKey(destinationArray[i]);
39
+ if (k !== void 0 && !destKeyToIndex.has(k)) {
40
+ destKeyToIndex.set(k, i);
41
+ }
42
+ }
43
+ for (let i = 0; i < sourceArray.length; i++) {
44
+ const sourceItem = sourceArray[i];
45
+ const sourceKey = getKey(sourceItem);
46
+ if (sourceKey !== void 0 && destKeyToIndex.has(sourceKey)) {
47
+ const destIndex = destKeyToIndex.get(sourceKey);
48
+ const destItem2 = destinationArray[destIndex];
49
+ destUsed[destIndex] = true;
50
+ if (isObject(destItem2) && isObject(sourceItem)) {
51
+ result.push(merge(sourceItem, destItem2, { arrayMerge }));
52
+ } else {
53
+ result.push(destItem2 !== void 0 ? destItem2 : sourceItem);
54
+ }
55
+ continue;
56
+ }
57
+ const destItem = destinationArray[i];
58
+ if (destItem !== void 0 && !destUsed[i]) {
59
+ destUsed[i] = true;
60
+ if (isObject(destItem) && isObject(sourceItem)) {
61
+ result.push(merge(sourceItem, destItem, { arrayMerge }));
62
+ } else if (destItem !== void 0) {
63
+ result.push(destItem);
64
+ } else {
65
+ result.push(sourceItem);
66
+ }
67
+ } else {
68
+ result.push(sourceItem);
69
+ }
70
+ }
71
+ for (let i = 0; i < destinationArray.length; i++) {
72
+ if (!destUsed[i]) {
73
+ result.push(destinationArray[i]);
74
+ destUsed[i] = true;
75
+ }
76
+ }
77
+ return result;
78
+ };
26
79
  const mergeDictionaries = (dictionaries) => {
27
80
  const { editor } = configuration;
28
81
  let mergedDictionaries = dictionaries[0];
82
+ const mergeOptions = {
83
+ arrayMerge
84
+ };
29
85
  for (let i = 1; i < dictionaries.length; i++) {
30
86
  const currentDictionary = dictionaries[i];
31
87
  checkTypesMatch(
@@ -36,9 +92,17 @@ const mergeDictionaries = (dictionaries) => {
36
92
  );
37
93
  const isDistant = currentDictionary.location === "distant";
38
94
  if (editor.dictionaryPriorityStrategy === "distant_first" && isDistant) {
39
- mergedDictionaries = merge(mergedDictionaries, currentDictionary);
95
+ mergedDictionaries = merge(
96
+ mergedDictionaries,
97
+ currentDictionary,
98
+ mergeOptions
99
+ );
40
100
  } else {
41
- mergedDictionaries = merge(currentDictionary, mergedDictionaries);
101
+ mergedDictionaries = merge(
102
+ currentDictionary,
103
+ mergedDictionaries,
104
+ mergeOptions
105
+ );
42
106
  }
43
107
  }
44
108
  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, currentDictionary);\n } else {\n mergedDictionaries = merge(currentDictionary, mergedDictionaries);\n }\n }\n\n return { ...mergedDictionaries, filePath: undefined };\n};\n"],"mappings":"AAAA,SAAS,oBAAoB;AAC7B,OAAO,mBAAmB;AAE1B,SAAS,mBAAmB;AAC5B,OAAO,WAAW;AAElB,MAAM,kBAAkB,CACtB,MACA,MACA,eACA,OAAiB,CAAC,MACT;AACT,QAAM,YAAY,aAAa,aAAa;AAC5C,QAAM,QAAQ,YAAY,IAAI;AAC9B,QAAM,QAAQ,YAAY,IAAI;AAE9B,MAAI,UAAU,OAAO;AACnB;AAAA,MACE,qBAAqB,aAAa,6DAA6D,KAAK,KAAK,GAAG,CAAC,mBAAmB,KAAK,SAAS,KAAK;AAAA,MACnJ;AAAA,QACE,OAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AAEA,MAAI,UAAU,YAAY,QAAQ,MAAM;AACtC,UAAM,UAAU,oBAAI,IAAI,CAAC,GAAG,OAAO,KAAK,IAAI,GAAG,GAAG,OAAO,KAAK,IAAI,CAAC,CAAC;AACpE,eAAW,OAAO,SAAS;AACzB,UAAI,OAAO,QAAQ,OAAO,MAAM;AAC9B,wBAAgB,KAAK,GAAG,GAAG,KAAK,GAAG,GAAG,eAAe,CAAC,GAAG,MAAM,GAAG,CAAC;AAAA,MACrE;AAAA,IACF;AAAA,EACF;AACF;AAEO,MAAM,oBAAoB,CAAC,iBAA2C;AAC3E,QAAM,EAAE,OAAO,IAAI;AAEnB,MAAI,qBAAiC,aAAa,CAAC;AAEnD,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,2BAAqB,MAAM,oBAAoB,iBAAiB;AAAA,IAClE,OAAO;AACL,2BAAqB,MAAM,mBAAmB,kBAAkB;AAAA,IAClE;AAAA,EACF;AAEA,SAAO,EAAE,GAAG,oBAAoB,UAAU,OAAU;AACtD;","names":[]}
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,SAAS,oBAAoB;AAC7B,OAAO,mBAAmB;AAE1B,SAAS,mBAAmB;AAC5B,OAAO,WAAwB;AAE/B,MAAM,kBAAkB,CACtB,MACA,MACA,eACA,OAAiB,CAAC,MACT;AACT,QAAM,YAAY,aAAa,aAAa;AAC5C,QAAM,QAAQ,YAAY,IAAI;AAC9B,QAAM,QAAQ,YAAY,IAAI;AAE9B,MAAI,UAA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AC;AAAA,IACH;AAEA,UAAM,YAAY,kBAAkB,aAAa;AAEjD,QAAI,OAAO,+BAA+B,mBAAmB,WAAW;AACtE,2BAAqB;AAAA,QACnB;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF,OAAO;AACL,2BAAqB;AAAA,QACnB;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,GAAG,oBAAoB,UAAU,OAAU;AACtD;","names":["destItem"]}
@@ -13,8 +13,22 @@ const applyMask = (full, mask) => {
13
13
  }
14
14
  if (mask && typeof mask === "object" && full && typeof full === "object") {
15
15
  const out = {};
16
- for (const [k, m] of Object.entries(mask)) {
17
- out[k] = applyMask(full[k], m);
16
+ const maskEntries = Object.entries(mask);
17
+ const allChildrenAreArrays = maskEntries.every(
18
+ ([, value]) => Array.isArray(value)
19
+ );
20
+ for (const [k, m] of maskEntries) {
21
+ const fullValue = full[k];
22
+ if (Array.isArray(m) && Array.isArray(fullValue)) {
23
+ const isTranslationNode = (val) => !!val && typeof val === "object" && "nodeType" in val;
24
+ const isArrayOfTranslationNodes = fullValue.every(
25
+ (item) => isTranslationNode(item)
26
+ );
27
+ if (!allChildrenAreArrays && !isArrayOfTranslationNodes) {
28
+ continue;
29
+ }
30
+ }
31
+ out[k] = applyMask(fullValue, m);
18
32
  }
19
33
  return out;
20
34
  }
@@ -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 Object.entries(mask)) {\n out[k] = applyMask((full as any)[k], m);\n }\n return out;\n }\n\n // unexpected case: we return the original value\n return full;\n};\n"],"mappings":"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,eAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,IAAI,GAAG;AACzC,UAAI,CAAC,IAAI,UAAW,KAAa,CAAC,GAAG,CAAC;AAAA,IACxC;AACA,WAAO;AAAA,EACT;AAGA,SAAO;AACT;","names":[]}
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":"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":[]}
@@ -1,5 +1,6 @@
1
1
  import { getAppLogger } from "@intlayer/config";
2
2
  import configuration from "@intlayer/config/built";
3
+ import { relative } from "path";
3
4
  const formatCode = async (filePath, code) => {
4
5
  const appLogger = getAppLogger(configuration);
5
6
  let prettier;
@@ -15,7 +16,8 @@ const formatCode = async (filePath, code) => {
15
16
  filepath: filePath
16
17
  // Explicitly provide the filepath so Prettier can infer the parser
17
18
  });
18
- appLogger(`Applied Prettier formatting to ${filePath}`, {
19
+ const relativePath = relative(configuration.content.baseDir, filePath);
20
+ appLogger(`Applied Prettier formatting to ${relativePath}`, {
19
21
  level: "info",
20
22
  isVerbose: true
21
23
  });
@@ -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 ${filePath}`, {\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,SAAS,oBAAoB;AAC7B,OAAO,mBAAmB;AAEnB,MAAM,aAAa,OAAO,UAAkB,SAAiB;AAClE,QAAM,YAAY,aAAa,aAAa;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,gBAAU,kCAAkC,QAAQ,IAAI;AAAA,QACtD,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":[]}
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,SAAS,oBAAoB;AAC7B,OAAO,mBAAmB;AAC1B,SAAS,gBAAgB;AAElB,MAAM,aAAa,OAAO,UAAkB,SAAiB;AAClE,QAAM,YAAY,aAAa,aAAa;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,eAAe,SAAS,cAAc,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":[]}