@intlayer/chokidar 1.2.0 → 2.0.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 (102) hide show
  1. package/dist/cjs/chokidar/watcher.cjs +4 -4
  2. package/dist/cjs/chokidar/watcher.cjs.map +1 -1
  3. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.cjs +80 -0
  4. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.cjs.map +1 -0
  5. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.d.ts +6 -0
  6. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.cjs +86 -0
  7. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.cjs.map +1 -0
  8. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.d.ts +8 -0
  9. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.cjs +50 -0
  10. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.cjs.map +1 -0
  11. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.d.ts +3 -0
  12. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.cjs +23 -0
  13. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.cjs.map +1 -0
  14. package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.d.ts +1 -0
  15. package/dist/cjs/transpiler/declaration_file_to_dictionary/index.cjs +43 -0
  16. package/dist/cjs/transpiler/declaration_file_to_dictionary/index.cjs.map +1 -0
  17. package/dist/cjs/transpiler/declaration_file_to_dictionary/index.d.ts +3 -0
  18. package/dist/cjs/transpiler/{intlater_module_to_dictionary/transpileContentDeclaration.cjs → declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.cjs} +17 -11
  19. package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.cjs.map +1 -0
  20. package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.d.ts +6 -0
  21. package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.cjs +1 -1
  22. package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.cjs.map +1 -0
  23. package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/index.cjs +7 -7
  24. package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.cjs.map +1 -0
  25. package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.d.ts +4 -0
  26. package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.cjs.map +1 -0
  27. package/dist/cjs/transpiler/{intlater_module_to_dictionary/processModule.cjs → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.cjs} +5 -6
  28. package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.cjs.map +1 -0
  29. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs +12 -9
  30. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs.map +1 -1
  31. package/dist/cjs/transpiler/dictionary_to_type/createType.cjs +24 -12
  32. package/dist/cjs/transpiler/dictionary_to_type/createType.cjs.map +1 -1
  33. package/dist/esm/chokidar/watcher.mjs +4 -4
  34. package/dist/esm/chokidar/watcher.mjs.map +1 -1
  35. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.d.mts +6 -0
  36. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.mjs +61 -0
  37. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.mjs.map +1 -0
  38. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.d.mts +8 -0
  39. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.mjs +64 -0
  40. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.mjs.map +1 -0
  41. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.d.mts +3 -0
  42. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.mjs +26 -0
  43. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.mjs.map +1 -0
  44. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.d.mts +1 -0
  45. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.mjs +2 -0
  46. package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.mjs.map +1 -0
  47. package/dist/esm/transpiler/declaration_file_to_dictionary/index.d.mts +3 -0
  48. package/dist/esm/transpiler/declaration_file_to_dictionary/index.mjs +19 -0
  49. package/dist/esm/transpiler/declaration_file_to_dictionary/index.mjs.map +1 -0
  50. package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.d.mts +6 -0
  51. package/dist/esm/transpiler/{intlater_module_to_dictionary/transpileContentDeclaration.mjs → declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.mjs} +12 -6
  52. package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.mjs.map +1 -0
  53. package/dist/esm/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.mjs +1 -1
  54. package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.mjs.map +1 -0
  55. package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.d.mts +4 -0
  56. package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.mjs +4 -0
  57. package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.mjs.map +1 -0
  58. package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.mjs.map +1 -0
  59. package/dist/esm/transpiler/{intlater_module_to_dictionary/processModule.mjs → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.mjs} +2 -3
  60. package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.mjs.map +1 -0
  61. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs +12 -9
  62. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs.map +1 -1
  63. package/dist/esm/transpiler/dictionary_to_type/createType.mjs +24 -12
  64. package/dist/esm/transpiler/dictionary_to_type/createType.mjs.map +1 -1
  65. package/package.json +4 -4
  66. package/src/chokidar/watcher.ts +5 -14
  67. package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.ts +95 -0
  68. package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.ts +91 -0
  69. package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.ts +22 -0
  70. package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.ts +1 -0
  71. package/src/transpiler/declaration_file_to_dictionary/index.ts +21 -0
  72. package/src/transpiler/{intlater_module_to_dictionary/transpileContentDeclaration.ts → declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.ts} +12 -4
  73. package/src/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.ts +1 -1
  74. package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.ts +3 -0
  75. package/src/transpiler/{intlater_module_to_dictionary/processModule.ts → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.ts} +2 -3
  76. package/src/transpiler/dictionary_to_type/createModuleAugmentation.ts +21 -14
  77. package/src/transpiler/dictionary_to_type/createType.ts +36 -19
  78. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.cjs.map +0 -1
  79. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.cjs.map +0 -1
  80. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.d.ts +0 -4
  81. package/dist/cjs/transpiler/intlater_module_to_dictionary/loadContentDeclaration.cjs.map +0 -1
  82. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.cjs.map +0 -1
  83. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.cjs.map +0 -1
  84. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.d.ts +0 -6
  85. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.mjs.map +0 -1
  86. package/dist/esm/transpiler/intlater_module_to_dictionary/index.d.mts +0 -4
  87. package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs +0 -4
  88. package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs.map +0 -1
  89. package/dist/esm/transpiler/intlater_module_to_dictionary/loadContentDeclaration.mjs.map +0 -1
  90. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.mjs.map +0 -1
  91. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.d.mts +0 -6
  92. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.mjs.map +0 -1
  93. package/src/transpiler/intlater_module_to_dictionary/index.ts +0 -3
  94. /package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.d.ts +0 -0
  95. /package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.cjs +0 -0
  96. /package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.d.ts +0 -0
  97. /package/dist/cjs/transpiler/{intlater_module_to_dictionary/processModule.d.ts → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.d.ts} +0 -0
  98. /package/dist/esm/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.d.mts +0 -0
  99. /package/dist/esm/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.d.mts +0 -0
  100. /package/dist/esm/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.mjs +0 -0
  101. /package/dist/esm/transpiler/{intlater_module_to_dictionary/processModule.d.mts → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.d.mts} +0 -0
  102. /package/src/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.ts +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.ts"],"sourcesContent":["import { resolve } from 'path';\nimport type {\n Content,\n ContentModule,\n FlatContent,\n FlatContentValue,\n} from '@intlayer/core';\nimport { loadContentDeclaration } from './loadContentDeclaration';\n\n/**\n * Function to replace function and async function fields with their results in the object\n */\nconst processFunctionResults = async (entry: Content): Promise<FlatContent> => {\n if (entry && typeof entry === 'object') {\n const promises: Promise<void>[] = [];\n const result: FlatContent = {};\n\n for (const key of Object.keys(entry)) {\n const field = entry?.[key];\n\n if (typeof field === 'object') {\n result[key] = (await processFunctionResults(\n field as Content\n )) as FlatContentValue;\n } else if (typeof field === 'function') {\n // Wait for the function to resolve if it's an async function\n const promise = (async () => {\n // Execute the function and await the result if it's a Promise\n const value = await field();\n\n result[key] = value as FlatContentValue;\n })();\n promises.push(promise);\n } else {\n result[key] = field as FlatContentValue;\n }\n }\n\n // Wait for all async operations to complete\n await Promise.all(promises);\n\n return result;\n }\n\n return entry;\n};\n\n/**\n * Function to load, process the module and return the Intlayer ContentModule from the module file\n */\nexport const processContentDeclaration = async (file: string) => {\n try {\n const functionPath = resolve(file);\n const entry = loadContentDeclaration(functionPath);\n\n if (!entry) {\n console.error('No entry found in module:', functionPath);\n return;\n }\n\n return (await processFunctionResults(entry)) as ContentModule;\n } catch (error) {\n console.error('Error processing module:', error);\n }\n};\n"],"mappings":"AAAA,SAAS,eAAe;AAOxB,SAAS,8BAA8B;AAKvC,MAAM,yBAAyB,OAAO,UAAyC;AAC7E,MAAI,SAAS,OAAO,UAAU,UAAU;AACtC,UAAM,WAA4B,CAAC;AACnC,UAAM,SAAsB,CAAC;AAE7B,eAAW,OAAO,OAAO,KAAK,KAAK,GAAG;AACpC,YAAM,QAAQ,QAAQ,GAAG;AAEzB,UAAI,OAAO,UAAU,UAAU;AAC7B,eAAO,GAAG,IAAK,MAAM;AAAA,UACnB;AAAA,QACF;AAAA,MACF,WAAW,OAAO,UAAU,YAAY;AAEtC,cAAM,WAAW,YAAY;AAE3B,gBAAM,QAAQ,MAAM,MAAM;AAE1B,iBAAO,GAAG,IAAI;AAAA,QAChB,GAAG;AACH,iBAAS,KAAK,OAAO;AAAA,MACvB,OAAO;AACL,eAAO,GAAG,IAAI;AAAA,MAChB;AAAA,IACF;AAGA,UAAM,QAAQ,IAAI,QAAQ;AAE1B,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAKO,MAAM,4BAA4B,OAAO,SAAiB;AAC/D,MAAI;AACF,UAAM,eAAe,QAAQ,IAAI;AACjC,UAAM,QAAQ,uBAAuB,YAAY;AAEjD,QAAI,CAAC,OAAO;AACV,cAAQ,MAAM,6BAA6B,YAAY;AACvD;AAAA,IACF;AAEA,WAAQ,MAAM,uBAAuB,KAAK;AAAA,EAC5C,SAAS,OAAO;AACd,YAAQ,MAAM,4BAA4B,KAAK;AAAA,EACjD;AACF;","names":[]}
@@ -5,10 +5,10 @@ import { sync } from "glob";
5
5
  import { getFileHash, transformToCamelCase } from '../../utils.mjs';
6
6
  const { content, internationalization } = getConfiguration();
7
7
  const { typesDir, moduleAugmentationDir } = content;
8
- const { locales } = internationalization;
8
+ const { locales, strictMode } = internationalization;
9
9
  const getTypeName = (id) => transformToCamelCase(`${id}Content`);
10
10
  const generateTypeIndexContent = (typeFiles) => {
11
- let content2 = "/* eslint-disable */\nimport 'intlayer';\nimport { Locales } from '@intlayer/config'\n";
11
+ let content2 = "/* eslint-disable */\nimport { Locales } from 'intlayer'\n";
12
12
  const dictionariesRef = typeFiles.map((dictionaryPath) => ({
13
13
  relativePath: relative(moduleAugmentationDir, dictionaryPath),
14
14
  id: basename(dictionaryPath, ".d.ts"),
@@ -26,10 +26,11 @@ const generateTypeIndexContent = (typeFiles) => {
26
26
  const formatLocales = locales.map((locale) => {
27
27
  for (const key in Locales) {
28
28
  if (Locales[key] === locale) {
29
- return ` ${key} = '${locale}'`;
29
+ return `Locales.${key}`;
30
30
  }
31
31
  }
32
- }).join(",\n");
32
+ }).join(" | ");
33
+ const strictModeRecord = strictMode === "strict" ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content> {}` : strictMode === "required_only" ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}` : `interface IConfigLocales<Content> extends Partial<Record<Locales, Content>> {}`;
33
34
  content2 += `declare module 'intlayer' {
34
35
  `;
35
36
  content2 += ` interface IntlayerDictionaryTypesConnector {
@@ -37,14 +38,16 @@ ${formattedDictionaryMap}
37
38
  }
38
39
 
39
40
  `;
40
- content2 += ` enum ConfigLocales {
41
- ${formatLocales}
42
- };
41
+ content2 += ` type ConfigLocales = ${formatLocales};
42
+ `;
43
+ content2 += ` type ExtractedLocales = Extract<Locales, ConfigLocales>;
44
+ `;
45
+ content2 += ` type ExcludedLocales = Exclude<Locales, ConfigLocales>;
43
46
 
44
47
  `;
45
- content2 += ` interface IConfigLocales<Content> extends Record<ConfigLocales, Content> {}
48
+ content2 += ` ${strictModeRecord}
46
49
  `;
47
- content2 += `};`;
50
+ content2 += `}`;
48
51
  return content2;
49
52
  };
50
53
  const createModuleAugmentation = () => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/createModuleAugmentation.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { basename, join, relative } from 'path';\nimport { Locales, getConfiguration } from '@intlayer/config';\nimport { sync } from 'glob';\nimport { getFileHash, transformToCamelCase } from '../../utils';\n\nconst { content, internationalization } = getConfiguration();\nconst { typesDir, moduleAugmentationDir } = content;\nconst { locales } = internationalization;\n\nexport const getTypeName = (id: string): string =>\n transformToCamelCase(`${id}Content`);\n\n/**\n * This function generates the content of the module augmentation file\n */\nconst generateTypeIndexContent = (typeFiles: string[]): string => {\n let content =\n \"/* eslint-disable */\\nimport 'intlayer';\\nimport { Locales } from '@intlayer/config'\\n\";\n\n const dictionariesRef = typeFiles.map((dictionaryPath) => ({\n relativePath: relative(moduleAugmentationDir, dictionaryPath),\n id: basename(dictionaryPath, '.d.ts'), // Get the base name as the dictionary id\n hash: `_${getFileHash(dictionaryPath)}`, // Get the hash of the dictionary to avoid conflicts\n }));\n\n // Import all dictionaries\n dictionariesRef.forEach((dictionary) => {\n const typeName = getTypeName(dictionary.id);\n content += `import type { ${typeName} as ${dictionary.hash} } from '${dictionary.relativePath}';\\n`;\n });\n\n content += '\\n';\n\n // Format Dictionary Map\n const formattedDictionaryMap: string = dictionariesRef\n .map((dictionary) => ` \"${dictionary.id}\": ${dictionary.hash};`)\n .join('\\n');\n\n const formatLocales = locales\n .map((locale) => {\n for (const key in Locales) {\n if (Locales[key as keyof typeof Locales] === locale) {\n return ` ${key} = '${locale}'`;\n }\n }\n })\n .join(',\\n');\n\n /**\n * Write the module augmentation to extend the intlayer module with the dictionaries types\n * Will suggest the type resulting of the dictionaries\n *\n * declare module 'intlayer' {\n * interface IntlayerDictionaryTypesConnector = {\n * dictionaries: {\n * id: DictionaryType;\n * }\n * }\n *\n * enum ConfigLocales {\n * ENGLISH = 'en',\n * FRENCH = 'fr',\n * SPANISH = 'es',\n * }\n *\n * interface IConfigLocales<Content> extends Record<ConfigLocales, Content> {}\n *\n * }\n * See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation\n */\n content += `declare module 'intlayer' {\\n`;\n content += ` interface IntlayerDictionaryTypesConnector {\\n${formattedDictionaryMap}\\n }\\n\\n`;\n content += ` enum ConfigLocales {\\n${formatLocales}\\n };\\n\\n`;\n content += ` interface IConfigLocales<Content> extends Record<ConfigLocales, Content> {}\\n`;\n content += `};`;\n\n return content;\n};\n\n/**\n * This function generates a index file merging all the types\n */\nexport const createModuleAugmentation = () => {\n // Create main directory if it doesn't exist\n if (!existsSync(moduleAugmentationDir)) {\n mkdirSync(moduleAugmentationDir, { recursive: true });\n }\n\n const dictionaries: string[] = sync(`${typesDir}/**/*.d.ts`);\n // Create the dictionary list file\n\n const tsContent = generateTypeIndexContent(dictionaries);\n writeFileSync(join(moduleAugmentationDir, 'intlayer.d.ts'), tsContent);\n};\n"],"mappings":"AAAA,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,UAAU,MAAM,gBAAgB;AACzC,SAAS,SAAS,wBAAwB;AAC1C,SAAS,YAAY;AACrB,SAAS,aAAa,4BAA4B;AAElD,MAAM,EAAE,SAAS,qBAAqB,IAAI,iBAAiB;AAC3D,MAAM,EAAE,UAAU,sBAAsB,IAAI;AAC5C,MAAM,EAAE,QAAQ,IAAI;AAEb,MAAM,cAAc,CAAC,OAC1B,qBAAqB,GAAG,EAAE,SAAS;AAKrC,MAAM,2BAA2B,CAAC,cAAgC;AAChE,MAAIA,WACF;AAEF,QAAM,kBAAkB,UAAU,IAAI,CAAC,oBAAoB;AAAA,IACzD,cAAc,SAAS,uBAAuB,cAAc;AAAA,IAC5D,IAAI,SAAS,gBAAgB,OAAO;AAAA;AAAA,IACpC,MAAM,IAAI,YAAY,cAAc,CAAC;AAAA;AAAA,EACvC,EAAE;AAGF,kBAAgB,QAAQ,CAAC,eAAe;AACtC,UAAM,WAAW,YAAY,WAAW,EAAE;AAC1C,IAAAA,YAAW,iBAAiB,QAAQ,OAAO,WAAW,IAAI,YAAY,WAAW,YAAY;AAAA;AAAA,EAC/F,CAAC;AAED,EAAAA,YAAW;AAGX,QAAM,yBAAiC,gBACpC,IAAI,CAAC,eAAe,QAAQ,WAAW,EAAE,MAAM,WAAW,IAAI,GAAG,EACjE,KAAK,IAAI;AAEZ,QAAM,gBAAgB,QACnB,IAAI,CAAC,WAAW;AACf,eAAW,OAAO,SAAS;AACzB,UAAI,QAAQ,GAA2B,MAAM,QAAQ;AACnD,eAAO,OAAO,GAAG,OAAO,MAAM;AAAA,MAChC;AAAA,IACF;AAAA,EACF,CAAC,EACA,KAAK,KAAK;AAwBb,EAAAA,YAAW;AAAA;AACX,EAAAA,YAAW;AAAA,EAAmD,sBAAsB;AAAA;AAAA;AAAA;AACpF,EAAAA,YAAW;AAAA,EAA2B,aAAa;AAAA;AAAA;AAAA;AACnD,EAAAA,YAAW;AAAA;AACX,EAAAA,YAAW;AAEX,SAAOA;AACT;AAKO,MAAM,2BAA2B,MAAM;AAE5C,MAAI,CAAC,WAAW,qBAAqB,GAAG;AACtC,cAAU,uBAAuB,EAAE,WAAW,KAAK,CAAC;AAAA,EACtD;AAEA,QAAM,eAAyB,KAAK,GAAG,QAAQ,YAAY;AAG3D,QAAM,YAAY,yBAAyB,YAAY;AACvD,gBAAc,KAAK,uBAAuB,eAAe,GAAG,SAAS;AACvE;","names":["content"]}
1
+ {"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/createModuleAugmentation.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { basename, join, relative } from 'path';\nimport { Locales, getConfiguration } from '@intlayer/config';\nimport { sync } from 'glob';\nimport { getFileHash, transformToCamelCase } from '../../utils';\n\nconst { content, internationalization } = getConfiguration();\nconst { typesDir, moduleAugmentationDir } = content;\nconst { locales, strictMode } = internationalization;\n\nexport const getTypeName = (id: string): string =>\n transformToCamelCase(`${id}Content`);\n\n/**\n * This function generates the content of the module augmentation file\n */\nconst generateTypeIndexContent = (typeFiles: string[]): string => {\n let content = \"/* eslint-disable */\\nimport { Locales } from 'intlayer'\\n\";\n\n const dictionariesRef = typeFiles.map((dictionaryPath) => ({\n relativePath: relative(moduleAugmentationDir, dictionaryPath),\n id: basename(dictionaryPath, '.d.ts'), // Get the base name as the dictionary id\n hash: `_${getFileHash(dictionaryPath)}`, // Get the hash of the dictionary to avoid conflicts\n }));\n\n // Import all dictionaries\n dictionariesRef.forEach((dictionary) => {\n const typeName = getTypeName(dictionary.id);\n content += `import type { ${typeName} as ${dictionary.hash} } from '${dictionary.relativePath}';\\n`;\n });\n\n content += '\\n';\n\n // Format Dictionary Map\n const formattedDictionaryMap: string = dictionariesRef\n .map((dictionary) => ` \"${dictionary.id}\": ${dictionary.hash};`)\n .join('\\n');\n\n const formatLocales = locales\n .map((locale) => {\n for (const key in Locales) {\n if (Locales[key as keyof typeof Locales] === locale) {\n return `Locales.${key}`;\n }\n }\n })\n .join(' | ');\n\n const strictModeRecord =\n strictMode === 'strict'\n ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content> {}`\n : strictMode === 'required_only'\n ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}`\n : `interface IConfigLocales<Content> extends Partial<Record<Locales, Content>> {}`;\n\n /**\n * Write the module augmentation to extend the intlayer module with the dictionaries types\n * Will suggest the type resulting of the dictionaries\n *\n * declare module 'intlayer' {\n * interface IntlayerDictionaryTypesConnector = {\n * dictionaries: {\n * id: DictionaryType;\n * }\n * }\n *\n * type ConfigLocales = Locales.ENGLISH | Locales.FRENCH | Locales.SPANISH;\n * type ExtractedLocales = Extract<Locales, ConfigLocales>;\n * type ExcludedLocales = Exclude<Locales, ConfigLocales>;\n *\n * interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}\n *\n *\n * }\n * See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation\n */\n content += `declare module 'intlayer' {\\n`;\n content += ` interface IntlayerDictionaryTypesConnector {\\n${formattedDictionaryMap}\\n }\\n\\n`;\n content += ` type ConfigLocales = ${formatLocales};\\n`;\n content += ` type ExtractedLocales = Extract<Locales, ConfigLocales>;\\n`;\n content += ` type ExcludedLocales = Exclude<Locales, ConfigLocales>;\\n\\n`;\n content += ` ${strictModeRecord}\\n`;\n content += `}`;\n\n return content;\n};\n\n/**\n * This function generates a index file merging all the types\n */\nexport const createModuleAugmentation = () => {\n // Create main directory if it doesn't exist\n if (!existsSync(moduleAugmentationDir)) {\n mkdirSync(moduleAugmentationDir, { recursive: true });\n }\n\n const dictionaries: string[] = sync(`${typesDir}/**/*.d.ts`);\n // Create the dictionary list file\n\n const tsContent = generateTypeIndexContent(dictionaries);\n writeFileSync(join(moduleAugmentationDir, 'intlayer.d.ts'), tsContent);\n};\n"],"mappings":"AAAA,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,UAAU,MAAM,gBAAgB;AACzC,SAAS,SAAS,wBAAwB;AAC1C,SAAS,YAAY;AACrB,SAAS,aAAa,4BAA4B;AAElD,MAAM,EAAE,SAAS,qBAAqB,IAAI,iBAAiB;AAC3D,MAAM,EAAE,UAAU,sBAAsB,IAAI;AAC5C,MAAM,EAAE,SAAS,WAAW,IAAI;AAEzB,MAAM,cAAc,CAAC,OAC1B,qBAAqB,GAAG,EAAE,SAAS;AAKrC,MAAM,2BAA2B,CAAC,cAAgC;AAChE,MAAIA,WAAU;AAEd,QAAM,kBAAkB,UAAU,IAAI,CAAC,oBAAoB;AAAA,IACzD,cAAc,SAAS,uBAAuB,cAAc;AAAA,IAC5D,IAAI,SAAS,gBAAgB,OAAO;AAAA;AAAA,IACpC,MAAM,IAAI,YAAY,cAAc,CAAC;AAAA;AAAA,EACvC,EAAE;AAGF,kBAAgB,QAAQ,CAAC,eAAe;AACtC,UAAM,WAAW,YAAY,WAAW,EAAE;AAC1C,IAAAA,YAAW,iBAAiB,QAAQ,OAAO,WAAW,IAAI,YAAY,WAAW,YAAY;AAAA;AAAA,EAC/F,CAAC;AAED,EAAAA,YAAW;AAGX,QAAM,yBAAiC,gBACpC,IAAI,CAAC,eAAe,QAAQ,WAAW,EAAE,MAAM,WAAW,IAAI,GAAG,EACjE,KAAK,IAAI;AAEZ,QAAM,gBAAgB,QACnB,IAAI,CAAC,WAAW;AACf,eAAW,OAAO,SAAS;AACzB,UAAI,QAAQ,GAA2B,MAAM,QAAQ;AACnD,eAAO,WAAW,GAAG;AAAA,MACvB;AAAA,IACF;AAAA,EACF,CAAC,EACA,KAAK,KAAK;AAEb,QAAM,mBACJ,eAAe,WACX,mFACA,eAAe,kBACb,8HACA;AAuBR,EAAAA,YAAW;AAAA;AACX,EAAAA,YAAW;AAAA,EAAmD,sBAAsB;AAAA;AAAA;AAAA;AACpF,EAAAA,YAAW,0BAA0B,aAAa;AAAA;AAClD,EAAAA,YAAW;AAAA;AACX,EAAAA,YAAW;AAAA;AAAA;AACX,EAAAA,YAAW,KAAK,gBAAgB;AAAA;AAChC,EAAAA,YAAW;AAEX,SAAOA;AACT;AAKO,MAAM,2BAA2B,MAAM;AAE5C,MAAI,CAAC,WAAW,qBAAqB,GAAG;AACtC,cAAU,uBAAuB,EAAE,WAAW,KAAK,CAAC;AAAA,EACtD;AAEA,QAAM,eAAyB,KAAK,GAAG,QAAQ,YAAY;AAG3D,QAAM,YAAY,yBAAyB,YAAY;AACvD,gBAAc,KAAK,uBAAuB,eAAe,GAAG,SAAS;AACvE;","names":["content"]}
@@ -19,38 +19,50 @@ const generateTypeScriptType = (obj) => {
19
19
  typeDefinition += "};\n\n";
20
20
  return typeDefinition;
21
21
  };
22
+ const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined";
22
23
  const generateTypeScriptTypeContent = (obj) => {
24
+ if (typeof obj !== "object" || obj === null) {
25
+ return `${typeof obj}`;
26
+ }
27
+ const isReactNodeValue = isReactNode(obj);
28
+ if (isReactNodeValue) {
29
+ return `JSX.Element`;
30
+ }
23
31
  let typeDefinition = ``;
24
32
  for (const [key, value] of Object.entries(obj)) {
25
- const nodeType = value.nodeType;
33
+ const nodeType = value?.nodeType;
26
34
  if (
27
35
  // Check if the value is a typed node
28
36
  typeof value === "object" && nodeType === NodeType.Translation
29
37
  ) {
30
- const tsType = typeof value?.[internationalization.defaultLocale];
38
+ const tsType = generateTypeScriptTypeContent(
39
+ value?.[internationalization.defaultLocale]
40
+ );
31
41
  typeDefinition += ` ${key}: ${tsType},
32
42
  `;
33
43
  } else if (
34
44
  // Check if the value is a typed node
35
45
  typeof value === "object" && nodeType === NodeType.Enumeration
36
46
  ) {
37
- const tsType = typeof value?.[internationalization.defaultLocale];
47
+ const tsType = generateTypeScriptTypeContent(
48
+ value?.[internationalization.defaultLocale]
49
+ );
38
50
  typeDefinition += ` ${key}: (quantity: number) => ${tsType},
39
51
  `;
40
52
  } else if (
41
53
  // Check if the value is a nested object
42
- typeof value === "object" && !Array.isArray(value)
54
+ typeof value === "object"
43
55
  ) {
44
- const nestedType = generateTypeScriptTypeContent(value);
45
- typeDefinition += ` ${key}: {${nestedType}},
56
+ const isArray = Array.isArray(value);
57
+ if (isArray) {
58
+ const arrayType = generateTypeScriptTypeContent(value);
59
+ typeDefinition += ` ${key}: ${arrayType}[],
46
60
  `;
47
- } else if (
48
- // Check if the value is an array
49
- Array.isArray(value)
50
- ) {
51
- const arrayType = typeof value[0];
52
- typeDefinition += ` ${key}: ${arrayType}[],
61
+ } else {
62
+ const nestedType = generateTypeScriptTypeContent(value);
63
+ typeDefinition += ` ${key}: {${nestedType}},
53
64
  `;
65
+ }
54
66
  } else if (
55
67
  // Check if the value is an 'id'
56
68
  typeof value === "string" && key === "id"
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/createType.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { createRequire } from 'module';\nimport { resolve } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport {\n NodeType,\n type Content,\n type ContentModule,\n type TypedNode,\n} from '@intlayer/core';\nimport { getTypeName } from './createModuleAugmentation';\n\nconst { content, internationalization } = getConfiguration();\nconst { typesDir } = content;\n\nconst isESModule = typeof import.meta.url === 'string';\nconst requireFunction = isESModule ? createRequire(import.meta.url) : require;\n\n/**\n *\n * This function generates a TypeScript type definition from a JSON object\n *\n * Example:\n *\n * const input = {\n * id: '1',\n * name: 'John Doe',\n * address: {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * }\n * };\n *\n * const result = generateTypeScriptType(input, 'RootObject');\n * console.log(result);\n *\n * Output:\n *\n * type RootObject = {\n * id: '1',\n * name: string,\n * address: {\n * id: '2',\n * street: string,\n * city: string,\n * },\n * };\n *\n */\nexport const generateTypeScriptType = (obj: ContentModule): string => {\n let typeDefinition = ``;\n\n const typeName = getTypeName(obj.id);\n\n typeDefinition += `export type ${typeName} = {\\n`;\n typeDefinition += generateTypeScriptTypeContent(obj);\n typeDefinition += '};\\n\\n';\n\n return typeDefinition;\n};\n\nexport const generateTypeScriptTypeContent = (obj: Content): string => {\n let typeDefinition = ``;\n\n for (const [key, value] of Object.entries(obj)) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const nodeType: NodeType | undefined = (value as TypedNode).nodeType;\n type ValueKey = keyof typeof value;\n\n if (\n // Check if the value is a typed node\n typeof value === 'object' &&\n nodeType === NodeType.Translation\n ) {\n const tsType =\n typeof value?.[internationalization.defaultLocale as ValueKey];\n typeDefinition += ` ${key}: ${tsType},\\n`;\n } else if (\n // Check if the value is a typed node\n typeof value === 'object' &&\n nodeType === NodeType.Enumeration\n ) {\n const tsType =\n typeof value?.[internationalization.defaultLocale as ValueKey];\n\n typeDefinition += ` ${key}: (quantity: number) => ${tsType},\\n`;\n } else if (\n // Check if the value is a nested object\n typeof value === 'object' &&\n !Array.isArray(value)\n ) {\n // Nested object, recurse\n const nestedType = generateTypeScriptTypeContent(value as Content);\n typeDefinition += ` ${key}: {${nestedType}},\\n`;\n } else if (\n // Check if the value is an array\n Array.isArray(value)\n ) {\n // Array handling (simplified, assumes non-empty arrays with uniform type)\n const arrayType = typeof value[0];\n typeDefinition += ` ${key}: ${arrayType}[],\\n`;\n } else if (\n // Check if the value is an 'id'\n typeof value === 'string' &&\n key === 'id'\n ) {\n // Special handling for 'id' field\n const tsType = `\"${value}\"`;\n typeDefinition += ` ${key}: ${tsType},\\n`;\n } else {\n // Primitive type\n const tsType = typeof value;\n typeDefinition += ` ${key}: ${tsType},\\n`;\n }\n }\n\n return typeDefinition;\n};\n\n/**\n * This function generates a TypeScript type definition from a JSON object\n */\nexport const createTypes = (dictionariesPaths: string[]): string[] => {\n const resultTypesPaths: string[] = [];\n\n // Create type folders if they don't exist\n if (!existsSync(typesDir)) {\n mkdirSync(typesDir, { recursive: true });\n }\n\n for (const dictionaryPath of dictionariesPaths) {\n const contentModule: ContentModule = requireFunction(dictionaryPath);\n const dictionaryName: string = contentModule.id;\n const typeDefinition: string = generateTypeScriptType(contentModule);\n\n const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);\n\n writeFileSync(outputPath, typeDefinition);\n\n resultTypesPaths.push(outputPath);\n }\n\n return resultTypesPaths;\n};\n"],"mappings":"AAAA,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,qBAAqB;AAC9B,SAAS,eAAe;AACxB,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,OAIK;AACP,SAAS,mBAAmB;AAE5B,MAAM,EAAE,SAAS,qBAAqB,IAAI,iBAAiB;AAC3D,MAAM,EAAE,SAAS,IAAI;AAErB,MAAM,aAAa,OAAO,YAAY,QAAQ;AAC9C,MAAM,kBAAkB,aAAa,cAAc,YAAY,GAAG,IAAI;AAkC/D,MAAM,yBAAyB,CAAC,QAA+B;AACpE,MAAI,iBAAiB;AAErB,QAAM,WAAW,YAAY,IAAI,EAAE;AAEnC,oBAAkB,eAAe,QAAQ;AAAA;AACzC,oBAAkB,8BAA8B,GAAG;AACnD,oBAAkB;AAElB,SAAO;AACT;AAEO,MAAM,gCAAgC,CAAC,QAAyB;AACrE,MAAI,iBAAiB;AAErB,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAE9C,UAAM,WAAkC,MAAoB;AAG5D;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,aAAa,SAAS;AAAA,MACtB;AACA,YAAM,SACJ,OAAO,QAAQ,qBAAqB,aAAyB;AAC/D,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,aAAa,SAAS;AAAA,MACtB;AACA,YAAM,SACJ,OAAO,QAAQ,qBAAqB,aAAyB;AAE/D,wBAAkB,KAAK,GAAG,2BAA2B,MAAM;AAAA;AAAA,IAC7D;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,CAAC,MAAM,QAAQ,KAAK;AAAA,MACpB;AAEA,YAAM,aAAa,8BAA8B,KAAgB;AACjE,wBAAkB,KAAK,GAAG,MAAM,UAAU;AAAA;AAAA,IAC5C;AAAA;AAAA,MAEE,MAAM,QAAQ,KAAK;AAAA,MACnB;AAEA,YAAM,YAAY,OAAO,MAAM,CAAC;AAChC,wBAAkB,KAAK,GAAG,KAAK,SAAS;AAAA;AAAA,IAC1C;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,QAAQ;AAAA,MACR;AAEA,YAAM,SAAS,IAAI,KAAK;AACxB,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC,OAAO;AAEL,YAAM,SAAS,OAAO;AACtB,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC;AAAA,EACF;AAEA,SAAO;AACT;AAKO,MAAM,cAAc,CAAC,sBAA0C;AACpE,QAAM,mBAA6B,CAAC;AAGpC,MAAI,CAAC,WAAW,QAAQ,GAAG;AACzB,cAAU,UAAU,EAAE,WAAW,KAAK,CAAC;AAAA,EACzC;AAEA,aAAW,kBAAkB,mBAAmB;AAC9C,UAAM,gBAA+B,gBAAgB,cAAc;AACnE,UAAM,iBAAyB,cAAc;AAC7C,UAAM,iBAAyB,uBAAuB,aAAa;AAEnE,UAAM,aAAa,QAAQ,UAAU,GAAG,cAAc,OAAO;AAE7D,kBAAc,YAAY,cAAc;AAExC,qBAAiB,KAAK,UAAU;AAAA,EAClC;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/createType.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { createRequire } from 'module';\nimport { resolve } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport {\n NodeType,\n type Content,\n type ContentModule,\n type TypedNode,\n} from '@intlayer/core';\nimport { getTypeName } from './createModuleAugmentation';\n\nconst { content, internationalization } = getConfiguration();\nconst { typesDir } = content;\n\nconst isESModule = typeof import.meta.url === 'string';\nconst requireFunction = isESModule ? createRequire(import.meta.url) : require;\n\n/**\n *\n * This function generates a TypeScript type definition from a JSON object\n *\n * Example:\n *\n * const input = {\n * id: '1',\n * name: 'John Doe',\n * address: {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * }\n * };\n *\n * const result = generateTypeScriptType(input, 'RootObject');\n * console.log(result);\n *\n * Output:\n *\n * type RootObject = {\n * id: '1',\n * name: string,\n * address: {\n * id: '2',\n * street: string,\n * city: string,\n * },\n * };\n *\n */\nexport const generateTypeScriptType = (obj: ContentModule): string => {\n let typeDefinition = ``;\n\n const typeName = getTypeName(obj.id);\n\n typeDefinition += `export type ${typeName} = {\\n`;\n typeDefinition += generateTypeScriptTypeContent(obj);\n typeDefinition += '};\\n\\n';\n\n return typeDefinition;\n};\n\nconst isReactNode = (node: Record<string, unknown>): boolean =>\n typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nexport const generateTypeScriptTypeContent = (obj: Content): string => {\n if (typeof obj !== 'object' || obj === null) {\n return `${typeof obj}`;\n }\n\n const isReactNodeValue = isReactNode(obj as Record<string, unknown>);\n\n if (isReactNodeValue) {\n // ReactNode handling\n return `JSX.Element`;\n }\n\n let typeDefinition = ``;\n for (const [key, value] of Object.entries(obj)) {\n const nodeType: NodeType | undefined = (value as TypedNode)?.nodeType;\n type ValueKey = keyof typeof value;\n\n if (\n // Check if the value is a typed node\n typeof value === 'object' &&\n nodeType === NodeType.Translation\n ) {\n const tsType = generateTypeScriptTypeContent(\n value?.[internationalization.defaultLocale as ValueKey]\n );\n typeDefinition += ` ${key}: ${tsType},\\n`;\n } else if (\n // Check if the value is a typed node\n typeof value === 'object' &&\n nodeType === NodeType.Enumeration\n ) {\n const tsType = generateTypeScriptTypeContent(\n value?.[internationalization.defaultLocale as ValueKey] as Content\n );\n\n typeDefinition += ` ${key}: (quantity: number) => ${tsType},\\n`;\n } else if (\n // Check if the value is a nested object\n typeof value === 'object'\n ) {\n const isArray = Array.isArray(value);\n\n if (isArray) {\n // Array handling (simplified, assumes non-empty arrays with uniform type)\n const arrayType = generateTypeScriptTypeContent(value as Content);\n\n typeDefinition += ` ${key}: ${arrayType}[],\\n`;\n } else {\n // Nested object, recurse\n const nestedType = generateTypeScriptTypeContent(value as Content);\n\n typeDefinition += ` ${key}: {${nestedType}},\\n`;\n }\n } else if (\n // Check if the value is an 'id'\n typeof value === 'string' &&\n key === 'id'\n ) {\n // Special handling for 'id' field\n const tsType = `\"${value}\"`;\n typeDefinition += ` ${key}: ${tsType},\\n`;\n } else {\n // Primitive type\n const tsType = typeof value;\n typeDefinition += ` ${key}: ${tsType},\\n`;\n }\n }\n\n return typeDefinition;\n};\n\n/**\n * This function generates a TypeScript type definition from a JSON object\n */\nexport const createTypes = (dictionariesPaths: string[]): string[] => {\n const resultTypesPaths: string[] = [];\n\n // Create type folders if they don't exist\n if (!existsSync(typesDir)) {\n mkdirSync(typesDir, { recursive: true });\n }\n\n for (const dictionaryPath of dictionariesPaths) {\n const contentModule: ContentModule = requireFunction(dictionaryPath);\n const dictionaryName: string = contentModule.id;\n const typeDefinition: string = generateTypeScriptType(contentModule);\n\n const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);\n\n writeFileSync(outputPath, typeDefinition);\n\n resultTypesPaths.push(outputPath);\n }\n\n return resultTypesPaths;\n};\n"],"mappings":"AAAA,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,qBAAqB;AAC9B,SAAS,eAAe;AACxB,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,OAIK;AACP,SAAS,mBAAmB;AAE5B,MAAM,EAAE,SAAS,qBAAqB,IAAI,iBAAiB;AAC3D,MAAM,EAAE,SAAS,IAAI;AAErB,MAAM,aAAa,OAAO,YAAY,QAAQ;AAC9C,MAAM,kBAAkB,aAAa,cAAc,YAAY,GAAG,IAAI;AAkC/D,MAAM,yBAAyB,CAAC,QAA+B;AACpE,MAAI,iBAAiB;AAErB,QAAM,WAAW,YAAY,IAAI,EAAE;AAEnC,oBAAkB,eAAe,QAAQ;AAAA;AACzC,oBAAkB,8BAA8B,GAAG;AACnD,oBAAkB;AAElB,SAAO;AACT;AAEA,MAAM,cAAc,CAAC,SACnB,OAAO,MAAM,QAAQ,eAAe,OAAO,MAAM,UAAU;AAGtD,MAAM,gCAAgC,CAAC,QAAyB;AACrE,MAAI,OAAO,QAAQ,YAAY,QAAQ,MAAM;AAC3C,WAAO,GAAG,OAAO,GAAG;AAAA,EACtB;AAEA,QAAM,mBAAmB,YAAY,GAA8B;AAEnE,MAAI,kBAAkB;AAEpB,WAAO;AAAA,EACT;AAEA,MAAI,iBAAiB;AACrB,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC9C,UAAM,WAAkC,OAAqB;AAG7D;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,aAAa,SAAS;AAAA,MACtB;AACA,YAAM,SAAS;AAAA,QACb,QAAQ,qBAAqB,aAAyB;AAAA,MACxD;AACA,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,aAAa,SAAS;AAAA,MACtB;AACA,YAAM,SAAS;AAAA,QACb,QAAQ,qBAAqB,aAAyB;AAAA,MACxD;AAEA,wBAAkB,KAAK,GAAG,2BAA2B,MAAM;AAAA;AAAA,IAC7D;AAAA;AAAA,MAEE,OAAO,UAAU;AAAA,MACjB;AACA,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAI,SAAS;AAEX,cAAM,YAAY,8BAA8B,KAAgB;AAEhE,0BAAkB,KAAK,GAAG,KAAK,SAAS;AAAA;AAAA,MAC1C,OAAO;AAEL,cAAM,aAAa,8BAA8B,KAAgB;AAEjE,0BAAkB,KAAK,GAAG,MAAM,UAAU;AAAA;AAAA,MAC5C;AAAA,IACF;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,QAAQ;AAAA,MACR;AAEA,YAAM,SAAS,IAAI,KAAK;AACxB,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC,OAAO;AAEL,YAAM,SAAS,OAAO;AACtB,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC;AAAA,EACF;AAEA,SAAO;AACT;AAKO,MAAM,cAAc,CAAC,sBAA0C;AACpE,QAAM,mBAA6B,CAAC;AAGpC,MAAI,CAAC,WAAW,QAAQ,GAAG;AACzB,cAAU,UAAU,EAAE,WAAW,KAAK,CAAC;AAAA,EACzC;AAEA,aAAW,kBAAkB,mBAAmB;AAC9C,UAAM,gBAA+B,gBAAgB,cAAc;AACnE,UAAM,iBAAyB,cAAc;AAC7C,UAAM,iBAAyB,uBAAuB,aAAa;AAEnE,UAAM,aAAa,QAAQ,UAAU,GAAG,cAAc,OAAO;AAE7D,kBAAc,YAAY,cAAc;AAExC,qBAAiB,KAAK,UAAU;AAAA,EAClC;AAEA,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlayer/chokidar",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "private": false,
5
5
  "description": "Chokidar application for IntLayer - Transpile IntLayer declaration files into dictionaries.",
6
6
  "keywords": [
@@ -53,9 +53,9 @@
53
53
  "glob": "^10.3.12",
54
54
  "node-loader": "^2.0.0",
55
55
  "rimraf": "5.0.5",
56
- "@intlayer/config": "^1.2.0",
57
- "@intlayer/core": "^1.2.0",
58
- "intlayer": "^1.2.0"
56
+ "@intlayer/config": "^2.0.0",
57
+ "@intlayer/core": "^2.0.0",
58
+ "intlayer": "^2.0.0"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@changesets/cli": "2.27.1",
@@ -2,18 +2,19 @@ import { relative } from 'path';
2
2
  import { getConfiguration } from '@intlayer/config';
3
3
  import chokidar, { type WatchOptions } from 'chokidar';
4
4
  import { sync } from 'glob';
5
+ import { buildDictionary } from '../transpiler/declaration_file_to_dictionary/index';
5
6
  import { createDictionaryList } from '../transpiler/dictionary_to_main/createDictionaryList';
6
7
  import {
7
8
  createTypes,
8
9
  createModuleAugmentation,
9
10
  } from '../transpiler/dictionary_to_type/index';
10
- import { transpileContentDeclaration } from '../transpiler/intlater_module_to_dictionary/transpileContentDeclaration';
11
11
 
12
12
  // Initialize chokidar watcher (non-persistent)
13
13
  export const watch = (options?: WatchOptions) => {
14
14
  const { content } = getConfiguration({
15
15
  verbose: true,
16
16
  });
17
+
17
18
  const { watchedFilesPatternWithPath, baseDir } = content;
18
19
 
19
20
  const files: string[] = sync(watchedFilesPatternWithPath);
@@ -25,7 +26,7 @@ export const watch = (options?: WatchOptions) => {
25
26
  ...options,
26
27
  })
27
28
  .on('ready', async () => {
28
- const dictionariesPaths = await transpileContentDeclaration(files);
29
+ const dictionariesPaths = await buildDictionary(files);
29
30
 
30
31
  console.info('Building Intlayer types...');
31
32
  createTypes(dictionariesPaths);
@@ -45,21 +46,11 @@ export const watch = (options?: WatchOptions) => {
45
46
  .on('unlink', (filePath) => {
46
47
  // Process the file with the functionToRun
47
48
  console.info('Removed file detected: ', relative(baseDir, filePath));
48
- // await transpileContentDeclaration(filePath);
49
-
50
- // console.info('Building TypeScript types...');
51
- // createTypes([filePath]);
52
-
53
- // console.info('Building type index...');
54
- // createModuleAugmentation();
55
-
56
- // console.info('Building main...');
57
- // createDictionaryList();
58
49
  })
59
50
  .on('add', async (filePath) => {
60
51
  // Process the file with the functionToRun
61
52
  console.info('Additional file detected: ', relative(baseDir, filePath));
62
- const dictionaries = await transpileContentDeclaration(filePath);
53
+ const dictionaries = await buildDictionary(filePath);
63
54
 
64
55
  console.info('Building TypeScript types...');
65
56
  createTypes(dictionaries);
@@ -73,7 +64,7 @@ export const watch = (options?: WatchOptions) => {
73
64
  .on('change', async (filePath) => {
74
65
  // Process the file with the functionToRun
75
66
  console.info('Change detected: ', relative(baseDir, filePath));
76
- const dictionaries = await transpileContentDeclaration(filePath);
67
+ const dictionaries = await buildDictionary(filePath);
77
68
 
78
69
  console.info('Building TypeScript types...');
79
70
  createTypes(dictionaries);
@@ -0,0 +1,95 @@
1
+ import { mkdir, writeFile } from 'fs/promises';
2
+ import { resolve } from 'path';
3
+ import { getConfiguration } from '@intlayer/config';
4
+ import type { ContentModule } from '@intlayer/core';
5
+ import {
6
+ processContentDeclaration,
7
+ extractObjectsWithId,
8
+ } from '../intlayer_dictionary/index';
9
+ import {
10
+ type I18nDictionariesOutput,
11
+ createI18nDictionaries,
12
+ } from './convertContentDeclarationInto18nDictionaries';
13
+
14
+ const { content } = getConfiguration();
15
+ const { i18nDictionariesDir } = content;
16
+
17
+ type DictionariesDeclaration = Record<string, I18nDictionariesOutput>;
18
+
19
+ /**
20
+ * This function writes the dictionaries to the file system
21
+ */
22
+ const writeDictionary = async (
23
+ dictionariesDeclaration: DictionariesDeclaration
24
+ ) => {
25
+ const resultDictionariesPaths: string[] = [];
26
+
27
+ for (const [nameSpace, localContent] of Object.entries(
28
+ dictionariesDeclaration
29
+ )) {
30
+ for await (const [locale, content] of Object.entries(localContent)) {
31
+ const contentString = JSON.stringify(content);
32
+
33
+ const outputFileName = `${nameSpace}.json`;
34
+ const resultDirPath = resolve(i18nDictionariesDir, locale);
35
+ const resultFilePath = resolve(resultDirPath, outputFileName);
36
+
37
+ // Create the dictionaries folder if it doesn't exist
38
+ await mkdir(resultDirPath, { recursive: true });
39
+
40
+ // Create the json file
41
+ await writeFile(resultFilePath, contentString, 'utf8').catch((err) => {
42
+ console.error(`Error creating ${outputFileName}:`, err);
43
+ });
44
+
45
+ resultDictionariesPaths.push(resultFilePath);
46
+ }
47
+ }
48
+
49
+ return resultDictionariesPaths;
50
+ };
51
+
52
+ /**
53
+ * This function transpile content declaration to i18n dictionaries
54
+ */
55
+ export const buildI18nDictionary = async (
56
+ contentDeclarationsPaths: string[] | string
57
+ ) => {
58
+ const resultDictionariesPaths: string[] = [];
59
+
60
+ if (typeof contentDeclarationsPaths === 'string') {
61
+ contentDeclarationsPaths = [contentDeclarationsPaths];
62
+ }
63
+
64
+ for await (const contentDeclarationPath of contentDeclarationsPaths) {
65
+ const result = await processContentDeclaration(contentDeclarationPath);
66
+
67
+ if (!result) {
68
+ continue;
69
+ }
70
+
71
+ const nestedContent: ContentModule[] = extractObjectsWithId(result);
72
+
73
+ // Create dictionaries for each nested content and format them
74
+ const dictionariesDeclaration: DictionariesDeclaration =
75
+ nestedContent.reduce((acc, content) => {
76
+ const id = content.id;
77
+ const i18Content = createI18nDictionaries(content);
78
+
79
+ return {
80
+ ...acc,
81
+ [id]: i18Content,
82
+ };
83
+ }, {});
84
+
85
+ // Write the dictionaries to the file system
86
+ const dictionariesPaths: string[] = await writeDictionary(
87
+ dictionariesDeclaration
88
+ );
89
+
90
+ // Add the paths to the result
91
+ resultDictionariesPaths.push(...dictionariesPaths);
92
+ }
93
+
94
+ return resultDictionariesPaths;
95
+ };
@@ -0,0 +1,91 @@
1
+ import { getConfiguration, type Locales } from '@intlayer/config';
2
+ import {
3
+ NodeType,
4
+ type TranslationContent,
5
+ type Content,
6
+ type TypedNode,
7
+ type EnumerationContent,
8
+ } from '@intlayer/core';
9
+ import { convertPluralsValues } from './convertPluralsValues';
10
+
11
+ type Dictionary = Record<string, unknown>;
12
+ export type I18nDictionariesOutput = Partial<Record<Locales, Dictionary>>;
13
+
14
+ const {
15
+ internationalization: { locales },
16
+ } = getConfiguration();
17
+
18
+ const isReactNode = (node: Record<string, unknown>): boolean =>
19
+ typeof node?.key !== 'undefined' &&
20
+ typeof node?.props !== 'undefined' &&
21
+ typeof node?.type !== 'undefined';
22
+
23
+ // Build dictionary for a specific locale
24
+ const buildDictionary = (content: Dictionary, locale: Locales): unknown => {
25
+ if (
26
+ // Translation node
27
+ content &&
28
+ (content as TypedNode).nodeType === NodeType.Translation
29
+ ) {
30
+ const result = (content as TranslationContent<unknown>)[locale];
31
+
32
+ return buildDictionary(result as Dictionary, locale);
33
+ } else if (
34
+ // Translation node
35
+ content &&
36
+ (content as TypedNode).nodeType === NodeType.Enumeration
37
+ ) {
38
+ const plurals: Record<string, unknown> = {};
39
+
40
+ Object.keys(content).forEach((quantity) => {
41
+ const letterNumber = convertPluralsValues(quantity);
42
+
43
+ const value = (content as EnumerationContent<unknown>)[
44
+ quantity as keyof EnumerationContent<unknown>
45
+ ];
46
+
47
+ plurals[`${letterNumber}_${letterNumber}`] = buildDictionary(
48
+ value as Dictionary,
49
+ locale
50
+ );
51
+ });
52
+
53
+ return plurals;
54
+ } else if (
55
+ // React element node
56
+ isReactNode(content as Record<string, unknown>)
57
+ ) {
58
+ return JSON.stringify(content);
59
+ } else if (
60
+ // Nested object
61
+ typeof content === 'object'
62
+ ) {
63
+ const result: Record<string, unknown> = {};
64
+
65
+ Object.keys(content).forEach((dictionaryValue) => {
66
+ result[dictionaryValue] = buildDictionary(
67
+ content[dictionaryValue] as Dictionary,
68
+ locale
69
+ );
70
+ });
71
+
72
+ return result;
73
+ }
74
+
75
+ return content;
76
+ };
77
+
78
+ export const createI18nDictionaries = (
79
+ content: Content
80
+ ): I18nDictionariesOutput => {
81
+ // Map dictionaries for each locale
82
+ const result: I18nDictionariesOutput = locales.reduce(
83
+ (acc, locale) => ({
84
+ ...acc,
85
+ [locale]: buildDictionary(content, locale),
86
+ }),
87
+ {}
88
+ );
89
+
90
+ return result;
91
+ };
@@ -0,0 +1,22 @@
1
+ export const convertPluralsValues = (number: string): string => {
2
+ switch (number) {
3
+ case '1':
4
+ return 'one';
5
+ case '2':
6
+ return 'two';
7
+ case '3':
8
+ return 'three';
9
+ case '4':
10
+ return 'four';
11
+ case '5':
12
+ return 'five';
13
+ case '6':
14
+ return 'six';
15
+ case '7':
16
+ return 'seven';
17
+ case '8':
18
+ return 'eight';
19
+ default:
20
+ return number.toString();
21
+ }
22
+ };
@@ -0,0 +1 @@
1
+ export * from './buildI18nDictionary';
@@ -0,0 +1,21 @@
1
+ import { getConfiguration } from '@intlayer/config';
2
+ import { buildI18nDictionary } from './i18n_dictionary/index';
3
+ import { buildIntlayerDictionary } from './intlayer_dictionary/index';
4
+
5
+ const {
6
+ content: { dictionaryOutput },
7
+ } = getConfiguration();
8
+
9
+ export const buildDictionary = async (
10
+ contentDeclarationsPaths: string | string[]
11
+ ): Promise<string[]> => {
12
+ if (dictionaryOutput.includes('i18next')) {
13
+ return await buildI18nDictionary(contentDeclarationsPaths);
14
+ }
15
+
16
+ if (dictionaryOutput.includes('intlayer')) {
17
+ return await buildIntlayerDictionary(contentDeclarationsPaths);
18
+ }
19
+
20
+ return [];
21
+ };
@@ -3,12 +3,12 @@ import { resolve } from 'path';
3
3
  import { getConfiguration } from '@intlayer/config';
4
4
  import type { ContentModule } from '@intlayer/core';
5
5
  import { extractObjectsWithId } from './extractNestedJSON';
6
- import { processContentDeclaration } from './processModule';
6
+ import { processContentDeclaration } from './processContentDeclaration';
7
7
 
8
8
  const { content } = getConfiguration();
9
9
  const { dictionariesDir } = content;
10
10
 
11
- const buildDictionary = async (dictionaries: ContentModule[]) => {
11
+ const writeDictionary = async (dictionaries: ContentModule[]) => {
12
12
  const resultDictionariesPaths: string[] = [];
13
13
 
14
14
  for await (const content of dictionaries) {
@@ -32,7 +32,7 @@ const buildDictionary = async (dictionaries: ContentModule[]) => {
32
32
  /**
33
33
  * This function transpile the bundled code to to make dictionaries as JSON files
34
34
  */
35
- export const transpileContentDeclaration = async (
35
+ export const buildIntlayerDictionary = async (
36
36
  contentDeclarationsPaths: string[] | string
37
37
  ) => {
38
38
  const resultDictionariesPaths: string[] = [];
@@ -53,7 +53,15 @@ export const transpileContentDeclaration = async (
53
53
 
54
54
  const nestedContent: ContentModule[] = extractObjectsWithId(result);
55
55
 
56
- const dictionariesPaths: string[] = await buildDictionary(nestedContent);
56
+ const contentWithFilePath: ContentModule[] = nestedContent.map(
57
+ (content) => ({
58
+ ...content,
59
+ filePath: contentDeclarationPath,
60
+ })
61
+ );
62
+
63
+ const dictionariesPaths: string[] =
64
+ await writeDictionary(contentWithFilePath);
57
65
 
58
66
  resultDictionariesPaths.push(...dictionariesPaths);
59
67
  }
@@ -43,7 +43,7 @@ export const extractObjectsWithId = (input: ContentModule): ContentModule[] => {
43
43
  // Function to recursively search and extract nested objects with an 'id'
44
44
  const search = (obj: Content, results: ContentModule[]): void => {
45
45
  if (obj && typeof obj === 'object') {
46
- if (Object.prototype.hasOwnProperty.call(obj, 'id')) {
46
+ if (Object.hasOwn(obj, 'id')) {
47
47
  results.push(obj as ContentModule);
48
48
  }
49
49
  for (const key of Object.keys(obj)) {
@@ -0,0 +1,3 @@
1
+ export * from './extractNestedJSON';
2
+ export * from './processContentDeclaration';
3
+ export * from './buildIntlayerDictionary';
@@ -22,13 +22,12 @@ const processFunctionResults = async (entry: Content): Promise<FlatContent> => {
22
22
  result[key] = (await processFunctionResults(
23
23
  field as Content
24
24
  )) as FlatContentValue;
25
- }
26
-
27
- if (typeof field === 'function') {
25
+ } else if (typeof field === 'function') {
28
26
  // Wait for the function to resolve if it's an async function
29
27
  const promise = (async () => {
30
28
  // Execute the function and await the result if it's a Promise
31
29
  const value = await field();
30
+
32
31
  result[key] = value as FlatContentValue;
33
32
  })();
34
33
  promises.push(promise);
@@ -6,7 +6,7 @@ import { getFileHash, transformToCamelCase } from '../../utils';
6
6
 
7
7
  const { content, internationalization } = getConfiguration();
8
8
  const { typesDir, moduleAugmentationDir } = content;
9
- const { locales } = internationalization;
9
+ const { locales, strictMode } = internationalization;
10
10
 
11
11
  export const getTypeName = (id: string): string =>
12
12
  transformToCamelCase(`${id}Content`);
@@ -15,8 +15,7 @@ export const getTypeName = (id: string): string =>
15
15
  * This function generates the content of the module augmentation file
16
16
  */
17
17
  const generateTypeIndexContent = (typeFiles: string[]): string => {
18
- let content =
19
- "/* eslint-disable */\nimport 'intlayer';\nimport { Locales } from '@intlayer/config'\n";
18
+ let content = "/* eslint-disable */\nimport { Locales } from 'intlayer'\n";
20
19
 
21
20
  const dictionariesRef = typeFiles.map((dictionaryPath) => ({
22
21
  relativePath: relative(moduleAugmentationDir, dictionaryPath),
@@ -41,11 +40,18 @@ const generateTypeIndexContent = (typeFiles: string[]): string => {
41
40
  .map((locale) => {
42
41
  for (const key in Locales) {
43
42
  if (Locales[key as keyof typeof Locales] === locale) {
44
- return ` ${key} = '${locale}'`;
43
+ return `Locales.${key}`;
45
44
  }
46
45
  }
47
46
  })
48
- .join(',\n');
47
+ .join(' | ');
48
+
49
+ const strictModeRecord =
50
+ strictMode === 'strict'
51
+ ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content> {}`
52
+ : strictMode === 'required_only'
53
+ ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}`
54
+ : `interface IConfigLocales<Content> extends Partial<Record<Locales, Content>> {}`;
49
55
 
50
56
  /**
51
57
  * Write the module augmentation to extend the intlayer module with the dictionaries types
@@ -58,22 +64,23 @@ const generateTypeIndexContent = (typeFiles: string[]): string => {
58
64
  * }
59
65
  * }
60
66
  *
61
- * enum ConfigLocales {
62
- * ENGLISH = 'en',
63
- * FRENCH = 'fr',
64
- * SPANISH = 'es',
65
- * }
67
+ * type ConfigLocales = Locales.ENGLISH | Locales.FRENCH | Locales.SPANISH;
68
+ * type ExtractedLocales = Extract<Locales, ConfigLocales>;
69
+ * type ExcludedLocales = Exclude<Locales, ConfigLocales>;
70
+ *
71
+ * interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}
66
72
  *
67
- * interface IConfigLocales<Content> extends Record<ConfigLocales, Content> {}
68
73
  *
69
74
  * }
70
75
  * See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
71
76
  */
72
77
  content += `declare module 'intlayer' {\n`;
73
78
  content += ` interface IntlayerDictionaryTypesConnector {\n${formattedDictionaryMap}\n }\n\n`;
74
- content += ` enum ConfigLocales {\n${formatLocales}\n };\n\n`;
75
- content += ` interface IConfigLocales<Content> extends Record<ConfigLocales, Content> {}\n`;
76
- content += `};`;
79
+ content += ` type ConfigLocales = ${formatLocales};\n`;
80
+ content += ` type ExtractedLocales = Extract<Locales, ConfigLocales>;\n`;
81
+ content += ` type ExcludedLocales = Exclude<Locales, ConfigLocales>;\n\n`;
82
+ content += ` ${strictModeRecord}\n`;
83
+ content += `}`;
77
84
 
78
85
  return content;
79
86
  };