@intlayer/chokidar 1.2.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 (100) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/chokidar/index.cjs +23 -0
  3. package/dist/cjs/chokidar/index.cjs.map +1 -0
  4. package/dist/cjs/chokidar/index.d.ts +2 -0
  5. package/dist/cjs/chokidar/watcher.cjs +89 -0
  6. package/dist/cjs/chokidar/watcher.cjs.map +1 -0
  7. package/dist/cjs/chokidar/watcher.d.ts +5 -0
  8. package/dist/cjs/index.cjs +35 -0
  9. package/dist/cjs/index.cjs.map +1 -0
  10. package/dist/cjs/index.d.ts +4 -0
  11. package/dist/cjs/transpiler/dictionary_to_main/createDictionaryList.cjs +76 -0
  12. package/dist/cjs/transpiler/dictionary_to_main/createDictionaryList.cjs.map +1 -0
  13. package/dist/cjs/transpiler/dictionary_to_main/createDictionaryList.d.ts +6 -0
  14. package/dist/cjs/transpiler/dictionary_to_main/index.cjs +23 -0
  15. package/dist/cjs/transpiler/dictionary_to_main/index.cjs.map +1 -0
  16. package/dist/cjs/transpiler/dictionary_to_main/index.d.ts +1 -0
  17. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs +87 -0
  18. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs.map +1 -0
  19. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.d.ts +7 -0
  20. package/dist/cjs/transpiler/dictionary_to_type/createType.cjs +114 -0
  21. package/dist/cjs/transpiler/dictionary_to_type/createType.cjs.map +1 -0
  22. package/dist/cjs/transpiler/dictionary_to_type/createType.d.ts +42 -0
  23. package/dist/cjs/transpiler/dictionary_to_type/index.cjs +25 -0
  24. package/dist/cjs/transpiler/dictionary_to_type/index.cjs.map +1 -0
  25. package/dist/cjs/transpiler/dictionary_to_type/index.d.ts +3 -0
  26. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.cjs +45 -0
  27. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.cjs.map +1 -0
  28. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.d.ts +44 -0
  29. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.cjs +27 -0
  30. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.cjs.map +1 -0
  31. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.d.ts +4 -0
  32. package/dist/cjs/transpiler/intlater_module_to_dictionary/loadContentDeclaration.cjs +98 -0
  33. package/dist/cjs/transpiler/intlater_module_to_dictionary/loadContentDeclaration.cjs.map +1 -0
  34. package/dist/cjs/transpiler/intlater_module_to_dictionary/loadContentDeclaration.d.ts +10 -0
  35. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.cjs +69 -0
  36. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.cjs.map +1 -0
  37. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.d.ts +8 -0
  38. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.cjs +66 -0
  39. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.cjs.map +1 -0
  40. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.d.ts +6 -0
  41. package/dist/cjs/utils.cjs +55 -0
  42. package/dist/cjs/utils.cjs.map +1 -0
  43. package/dist/cjs/utils.d.ts +4 -0
  44. package/dist/esm/chokidar/index.d.mts +2 -0
  45. package/dist/esm/chokidar/index.mjs +2 -0
  46. package/dist/esm/chokidar/index.mjs.map +1 -0
  47. package/dist/esm/chokidar/watcher.d.mts +5 -0
  48. package/dist/esm/chokidar/watcher.mjs +58 -0
  49. package/dist/esm/chokidar/watcher.mjs.map +1 -0
  50. package/dist/esm/index.d.mts +4 -0
  51. package/dist/esm/index.mjs +9 -0
  52. package/dist/esm/index.mjs.map +1 -0
  53. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.d.mts +6 -0
  54. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.mjs +52 -0
  55. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.mjs.map +1 -0
  56. package/dist/esm/transpiler/dictionary_to_main/index.d.mts +1 -0
  57. package/dist/esm/transpiler/dictionary_to_main/index.mjs +2 -0
  58. package/dist/esm/transpiler/dictionary_to_main/index.mjs.map +1 -0
  59. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.d.mts +7 -0
  60. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs +62 -0
  61. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs.map +1 -0
  62. package/dist/esm/transpiler/dictionary_to_type/createType.d.mts +42 -0
  63. package/dist/esm/transpiler/dictionary_to_type/createType.mjs +89 -0
  64. package/dist/esm/transpiler/dictionary_to_type/createType.mjs.map +1 -0
  65. package/dist/esm/transpiler/dictionary_to_type/index.d.mts +3 -0
  66. package/dist/esm/transpiler/dictionary_to_type/index.mjs +3 -0
  67. package/dist/esm/transpiler/dictionary_to_type/index.mjs.map +1 -0
  68. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.d.mts +44 -0
  69. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.mjs +21 -0
  70. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.mjs.map +1 -0
  71. package/dist/esm/transpiler/intlater_module_to_dictionary/index.d.mts +4 -0
  72. package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs +4 -0
  73. package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs.map +1 -0
  74. package/dist/esm/transpiler/intlater_module_to_dictionary/loadContentDeclaration.d.mts +10 -0
  75. package/dist/esm/transpiler/intlater_module_to_dictionary/loadContentDeclaration.mjs +73 -0
  76. package/dist/esm/transpiler/intlater_module_to_dictionary/loadContentDeclaration.mjs.map +1 -0
  77. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.d.mts +8 -0
  78. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.mjs +45 -0
  79. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.mjs.map +1 -0
  80. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.d.mts +6 -0
  81. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.mjs +42 -0
  82. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.mjs.map +1 -0
  83. package/dist/esm/utils.d.mts +4 -0
  84. package/dist/esm/utils.mjs +20 -0
  85. package/dist/esm/utils.mjs.map +1 -0
  86. package/package.json +85 -0
  87. package/src/chokidar/index.ts +1 -0
  88. package/src/chokidar/watcher.ts +84 -0
  89. package/src/index.ts +3 -0
  90. package/src/transpiler/dictionary_to_main/createDictionaryList.ts +65 -0
  91. package/src/transpiler/dictionary_to_main/index.ts +1 -0
  92. package/src/transpiler/dictionary_to_type/createModuleAugmentation.ts +95 -0
  93. package/src/transpiler/dictionary_to_type/createType.ts +145 -0
  94. package/src/transpiler/dictionary_to_type/index.ts +2 -0
  95. package/src/transpiler/intlater_module_to_dictionary/extractNestedJSON.ts +60 -0
  96. package/src/transpiler/intlater_module_to_dictionary/index.ts +3 -0
  97. package/src/transpiler/intlater_module_to_dictionary/loadContentDeclaration.ts +114 -0
  98. package/src/transpiler/intlater_module_to_dictionary/processModule.ts +66 -0
  99. package/src/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.ts +62 -0
  100. package/src/utils.ts +26 -0
@@ -0,0 +1,52 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from "fs";
2
+ import { basename, extname, relative, resolve } from "path";
3
+ import { getConfiguration } from "@intlayer/config";
4
+ import { sync } from "glob";
5
+ import { getFileHash } from '../../utils.mjs';
6
+ const { content } = getConfiguration();
7
+ const { dictionariesDir, mainDir } = content;
8
+ const generateDictionaryListContent = (dictionaries, format = "esm") => {
9
+ let content2 = "";
10
+ const dictionariesRef = dictionaries.map((dictionaryPath) => ({
11
+ relativePath: relative(mainDir, dictionaryPath),
12
+ id: basename(dictionaryPath, extname(dictionaryPath)),
13
+ // Get the base name as the dictionary id
14
+ hash: `_${getFileHash(dictionaryPath)}`
15
+ // Get the hash of the dictionary to avoid conflicts
16
+ }));
17
+ dictionariesRef.forEach((dictionary) => {
18
+ if (format === "esm")
19
+ content2 += `import ${dictionary.hash} from '${dictionary.relativePath}';
20
+ `;
21
+ if (format === "cjs")
22
+ content2 += `const ${dictionary.hash} = require('${dictionary.relativePath}');
23
+ `;
24
+ });
25
+ content2 += "\n";
26
+ const formattedDictionaryMap = dictionariesRef.map((dictionary) => `"${dictionary.id}": ${dictionary.hash}`).join(",\n");
27
+ if (format === "esm")
28
+ content2 += `export default {
29
+ ${formattedDictionaryMap}
30
+ };
31
+ `;
32
+ if (format === "cjs")
33
+ content2 += `module.exports = {
34
+ ${formattedDictionaryMap}
35
+ };
36
+ `;
37
+ return content2;
38
+ };
39
+ const createDictionaryList = () => {
40
+ if (!existsSync(mainDir)) {
41
+ mkdirSync(mainDir, { recursive: true });
42
+ }
43
+ const dictionaries = sync(`${dictionariesDir}/**/*.json`);
44
+ const cjsContent = generateDictionaryListContent(dictionaries, "cjs");
45
+ writeFileSync(resolve(mainDir, "dictionaries.cjs"), cjsContent);
46
+ const esmContent = generateDictionaryListContent(dictionaries, "esm");
47
+ writeFileSync(resolve(mainDir, "dictionaries.mjs"), esmContent);
48
+ };
49
+ export {
50
+ createDictionaryList
51
+ };
52
+ //# sourceMappingURL=createDictionaryList.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/transpiler/dictionary_to_main/createDictionaryList.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { basename, extname, relative, resolve } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport { sync } from 'glob';\nimport { getFileHash } from '../../utils';\n\nconst { content } = getConfiguration();\nconst { dictionariesDir, mainDir } = content;\n\n/**\n * This function generates the content of the dictionary list file\n */\nconst generateDictionaryListContent = (\n dictionaries: string[],\n format: 'cjs' | 'esm' = 'esm'\n): string => {\n let content = '';\n\n const dictionariesRef = dictionaries.map((dictionaryPath) => ({\n relativePath: relative(mainDir, dictionaryPath),\n id: basename(dictionaryPath, extname(dictionaryPath)), // 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 if (format === 'esm')\n content += `import ${dictionary.hash} from '${dictionary.relativePath}';\\n`;\n if (format === 'cjs')\n content += `const ${dictionary.hash} = require('${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 if (format === 'esm')\n content += `export default {\\n${formattedDictionaryMap}\\n};\\n`;\n if (format === 'cjs')\n content += `module.exports = {\\n${formattedDictionaryMap}\\n};\\n`;\n\n return content;\n};\n\n/**\n * This function generates a list of dictionaries in the main directory\n */\nexport const createDictionaryList = () => {\n // Create main directory if it doesn't exist\n if (!existsSync(mainDir)) {\n mkdirSync(mainDir, { recursive: true });\n }\n\n const dictionaries: string[] = sync(`${dictionariesDir}/**/*.json`);\n\n // Create the dictionary list file\n const cjsContent = generateDictionaryListContent(dictionaries, 'cjs');\n writeFileSync(resolve(mainDir, 'dictionaries.cjs'), cjsContent);\n\n const esmContent = generateDictionaryListContent(dictionaries, 'esm');\n writeFileSync(resolve(mainDir, 'dictionaries.mjs'), esmContent);\n};\n"],"mappings":"AAAA,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,UAAU,SAAS,UAAU,eAAe;AACrD,SAAS,wBAAwB;AACjC,SAAS,YAAY;AACrB,SAAS,mBAAmB;AAE5B,MAAM,EAAE,QAAQ,IAAI,iBAAiB;AACrC,MAAM,EAAE,iBAAiB,QAAQ,IAAI;AAKrC,MAAM,gCAAgC,CACpC,cACA,SAAwB,UACb;AACX,MAAIA,WAAU;AAEd,QAAM,kBAAkB,aAAa,IAAI,CAAC,oBAAoB;AAAA,IAC5D,cAAc,SAAS,SAAS,cAAc;AAAA,IAC9C,IAAI,SAAS,gBAAgB,QAAQ,cAAc,CAAC;AAAA;AAAA,IACpD,MAAM,IAAI,YAAY,cAAc,CAAC;AAAA;AAAA,EACvC,EAAE;AAGF,kBAAgB,QAAQ,CAAC,eAAe;AACtC,QAAI,WAAW;AACb,MAAAA,YAAW,UAAU,WAAW,IAAI,UAAU,WAAW,YAAY;AAAA;AACvE,QAAI,WAAW;AACb,MAAAA,YAAW,SAAS,WAAW,IAAI,eAAe,WAAW,YAAY;AAAA;AAAA,EAC7E,CAAC;AAED,EAAAA,YAAW;AAGX,QAAM,yBAAiC,gBACpC,IAAI,CAAC,eAAe,IAAI,WAAW,EAAE,MAAM,WAAW,IAAI,EAAE,EAC5D,KAAK,KAAK;AAEb,MAAI,WAAW;AACb,IAAAA,YAAW;AAAA,EAAqB,sBAAsB;AAAA;AAAA;AACxD,MAAI,WAAW;AACb,IAAAA,YAAW;AAAA,EAAuB,sBAAsB;AAAA;AAAA;AAE1D,SAAOA;AACT;AAKO,MAAM,uBAAuB,MAAM;AAExC,MAAI,CAAC,WAAW,OAAO,GAAG;AACxB,cAAU,SAAS,EAAE,WAAW,KAAK,CAAC;AAAA,EACxC;AAEA,QAAM,eAAyB,KAAK,GAAG,eAAe,YAAY;AAGlE,QAAM,aAAa,8BAA8B,cAAc,KAAK;AACpE,gBAAc,QAAQ,SAAS,kBAAkB,GAAG,UAAU;AAE9D,QAAM,aAAa,8BAA8B,cAAc,KAAK;AACpE,gBAAc,QAAQ,SAAS,kBAAkB,GAAG,UAAU;AAChE;","names":["content"]}
@@ -0,0 +1 @@
1
+ export { createDictionaryList } from './createDictionaryList.mjs';
@@ -0,0 +1,2 @@
1
+ export * from './createDictionaryList.mjs';
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/transpiler/dictionary_to_main/index.ts"],"sourcesContent":["export * from './createDictionaryList';\n"],"mappings":"AAAA,cAAc;","names":[]}
@@ -0,0 +1,7 @@
1
+ declare const getTypeName: (id: string) => string;
2
+ /**
3
+ * This function generates a index file merging all the types
4
+ */
5
+ declare const createModuleAugmentation: () => void;
6
+
7
+ export { createModuleAugmentation, getTypeName };
@@ -0,0 +1,62 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from "fs";
2
+ import { basename, join, relative } from "path";
3
+ import { Locales, getConfiguration } from "@intlayer/config";
4
+ import { sync } from "glob";
5
+ import { getFileHash, transformToCamelCase } from '../../utils.mjs';
6
+ const { content, internationalization } = getConfiguration();
7
+ const { typesDir, moduleAugmentationDir } = content;
8
+ const { locales } = internationalization;
9
+ const getTypeName = (id) => transformToCamelCase(`${id}Content`);
10
+ const generateTypeIndexContent = (typeFiles) => {
11
+ let content2 = "/* eslint-disable */\nimport 'intlayer';\nimport { Locales } from '@intlayer/config'\n";
12
+ const dictionariesRef = typeFiles.map((dictionaryPath) => ({
13
+ relativePath: relative(moduleAugmentationDir, dictionaryPath),
14
+ id: basename(dictionaryPath, ".d.ts"),
15
+ // Get the base name as the dictionary id
16
+ hash: `_${getFileHash(dictionaryPath)}`
17
+ // Get the hash of the dictionary to avoid conflicts
18
+ }));
19
+ dictionariesRef.forEach((dictionary) => {
20
+ const typeName = getTypeName(dictionary.id);
21
+ content2 += `import type { ${typeName} as ${dictionary.hash} } from '${dictionary.relativePath}';
22
+ `;
23
+ });
24
+ content2 += "\n";
25
+ const formattedDictionaryMap = dictionariesRef.map((dictionary) => ` "${dictionary.id}": ${dictionary.hash};`).join("\n");
26
+ const formatLocales = locales.map((locale) => {
27
+ for (const key in Locales) {
28
+ if (Locales[key] === locale) {
29
+ return ` ${key} = '${locale}'`;
30
+ }
31
+ }
32
+ }).join(",\n");
33
+ content2 += `declare module 'intlayer' {
34
+ `;
35
+ content2 += ` interface IntlayerDictionaryTypesConnector {
36
+ ${formattedDictionaryMap}
37
+ }
38
+
39
+ `;
40
+ content2 += ` enum ConfigLocales {
41
+ ${formatLocales}
42
+ };
43
+
44
+ `;
45
+ content2 += ` interface IConfigLocales<Content> extends Record<ConfigLocales, Content> {}
46
+ `;
47
+ content2 += `};`;
48
+ return content2;
49
+ };
50
+ const createModuleAugmentation = () => {
51
+ if (!existsSync(moduleAugmentationDir)) {
52
+ mkdirSync(moduleAugmentationDir, { recursive: true });
53
+ }
54
+ const dictionaries = sync(`${typesDir}/**/*.d.ts`);
55
+ const tsContent = generateTypeIndexContent(dictionaries);
56
+ writeFileSync(join(moduleAugmentationDir, "intlayer.d.ts"), tsContent);
57
+ };
58
+ export {
59
+ createModuleAugmentation,
60
+ getTypeName
61
+ };
62
+ //# sourceMappingURL=createModuleAugmentation.mjs.map
@@ -0,0 +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,4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K,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"]}
@@ -0,0 +1,42 @@
1
+ import { ContentModule, Content } from '@intlayer/core';
2
+
3
+ /**
4
+ *
5
+ * This function generates a TypeScript type definition from a JSON object
6
+ *
7
+ * Example:
8
+ *
9
+ * const input = {
10
+ * id: '1',
11
+ * name: 'John Doe',
12
+ * address: {
13
+ * id: '2',
14
+ * street: '123 Main St',
15
+ * city: 'Springfield',
16
+ * }
17
+ * };
18
+ *
19
+ * const result = generateTypeScriptType(input, 'RootObject');
20
+ * console.log(result);
21
+ *
22
+ * Output:
23
+ *
24
+ * type RootObject = {
25
+ * id: '1',
26
+ * name: string,
27
+ * address: {
28
+ * id: '2',
29
+ * street: string,
30
+ * city: string,
31
+ * },
32
+ * };
33
+ *
34
+ */
35
+ declare const generateTypeScriptType: (obj: ContentModule) => string;
36
+ declare const generateTypeScriptTypeContent: (obj: Content) => string;
37
+ /**
38
+ * This function generates a TypeScript type definition from a JSON object
39
+ */
40
+ declare const createTypes: (dictionariesPaths: string[]) => string[];
41
+
42
+ export { createTypes, generateTypeScriptType, generateTypeScriptTypeContent };
@@ -0,0 +1,89 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from "fs";
2
+ import { createRequire } from "module";
3
+ import { resolve } from "path";
4
+ import { getConfiguration } from "@intlayer/config";
5
+ import {
6
+ NodeType
7
+ } from "@intlayer/core";
8
+ import { getTypeName } from './createModuleAugmentation.mjs';
9
+ const { content, internationalization } = getConfiguration();
10
+ const { typesDir } = content;
11
+ const isESModule = typeof import.meta.url === "string";
12
+ const requireFunction = isESModule ? createRequire(import.meta.url) : require;
13
+ const generateTypeScriptType = (obj) => {
14
+ let typeDefinition = ``;
15
+ const typeName = getTypeName(obj.id);
16
+ typeDefinition += `export type ${typeName} = {
17
+ `;
18
+ typeDefinition += generateTypeScriptTypeContent(obj);
19
+ typeDefinition += "};\n\n";
20
+ return typeDefinition;
21
+ };
22
+ const generateTypeScriptTypeContent = (obj) => {
23
+ let typeDefinition = ``;
24
+ for (const [key, value] of Object.entries(obj)) {
25
+ const nodeType = value.nodeType;
26
+ if (
27
+ // Check if the value is a typed node
28
+ typeof value === "object" && nodeType === NodeType.Translation
29
+ ) {
30
+ const tsType = typeof value?.[internationalization.defaultLocale];
31
+ typeDefinition += ` ${key}: ${tsType},
32
+ `;
33
+ } else if (
34
+ // Check if the value is a typed node
35
+ typeof value === "object" && nodeType === NodeType.Enumeration
36
+ ) {
37
+ const tsType = typeof value?.[internationalization.defaultLocale];
38
+ typeDefinition += ` ${key}: (quantity: number) => ${tsType},
39
+ `;
40
+ } else if (
41
+ // Check if the value is a nested object
42
+ typeof value === "object" && !Array.isArray(value)
43
+ ) {
44
+ const nestedType = generateTypeScriptTypeContent(value);
45
+ typeDefinition += ` ${key}: {${nestedType}},
46
+ `;
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}[],
53
+ `;
54
+ } else if (
55
+ // Check if the value is an 'id'
56
+ typeof value === "string" && key === "id"
57
+ ) {
58
+ const tsType = `"${value}"`;
59
+ typeDefinition += ` ${key}: ${tsType},
60
+ `;
61
+ } else {
62
+ const tsType = typeof value;
63
+ typeDefinition += ` ${key}: ${tsType},
64
+ `;
65
+ }
66
+ }
67
+ return typeDefinition;
68
+ };
69
+ const createTypes = (dictionariesPaths) => {
70
+ const resultTypesPaths = [];
71
+ if (!existsSync(typesDir)) {
72
+ mkdirSync(typesDir, { recursive: true });
73
+ }
74
+ for (const dictionaryPath of dictionariesPaths) {
75
+ const contentModule = requireFunction(dictionaryPath);
76
+ const dictionaryName = contentModule.id;
77
+ const typeDefinition = generateTypeScriptType(contentModule);
78
+ const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);
79
+ writeFileSync(outputPath, typeDefinition);
80
+ resultTypesPaths.push(outputPath);
81
+ }
82
+ return resultTypesPaths;
83
+ };
84
+ export {
85
+ createTypes,
86
+ generateTypeScriptType,
87
+ generateTypeScriptTypeContent
88
+ };
89
+ //# sourceMappingURL=createType.mjs.map
@@ -0,0 +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;AACj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;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":[]}
@@ -0,0 +1,3 @@
1
+ export { createModuleAugmentation, getTypeName } from './createModuleAugmentation.mjs';
2
+ export { createTypes, generateTypeScriptType, generateTypeScriptTypeContent } from './createType.mjs';
3
+ import '@intlayer/core';
@@ -0,0 +1,3 @@
1
+ export * from './createModuleAugmentation.mjs';
2
+ export * from './createType.mjs';
3
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/index.ts"],"sourcesContent":["export * from './createModuleAugmentation';\nexport * from './createType';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
@@ -0,0 +1,44 @@
1
+ import { ContentModule } from '@intlayer/core';
2
+
3
+ /**
4
+ *
5
+ * This function extracts all nested objects with an 'id' field from the input object and returns them as an array
6
+ *
7
+ * Example:
8
+ *
9
+ * const input = {
10
+ * id: '1',
11
+ * name: 'John Doe',
12
+ * address: {
13
+ * id: '2',
14
+ * street: '123 Main St',
15
+ * city: 'Springfield',
16
+ * state: 'IL'
17
+ * },
18
+ * };
19
+ * const result = extractObjectsWithId(input);
20
+ * console.log(result);
21
+ *
22
+ * Output:
23
+ *
24
+ * [{
25
+ * id: '1',
26
+ * name: 'John Doe',
27
+ * address: {
28
+ * id: '2',
29
+ * street: '123 Main St',
30
+ * city: 'Springfield',
31
+ * state: 'IL'
32
+ * }
33
+ * },
34
+ * {
35
+ * id: '2',
36
+ * street: '123 Main St',
37
+ * city: 'Springfield',
38
+ * state: 'IL'
39
+ * }]
40
+ *
41
+ */
42
+ declare const extractObjectsWithId: (input: ContentModule) => ContentModule[];
43
+
44
+ export { extractObjectsWithId };
@@ -0,0 +1,21 @@
1
+ const extractObjectsWithId = (input) => {
2
+ const search = (obj, results2) => {
3
+ if (obj && typeof obj === "object") {
4
+ if (Object.prototype.hasOwnProperty.call(obj, "id")) {
5
+ results2.push(obj);
6
+ }
7
+ for (const key of Object.keys(obj)) {
8
+ if (typeof obj[key] === "object") {
9
+ search(obj[key], results2);
10
+ }
11
+ }
12
+ }
13
+ };
14
+ const results = [];
15
+ search(input, results);
16
+ return results;
17
+ };
18
+ export {
19
+ extractObjectsWithId
20
+ };
21
+ //# sourceMappingURL=extractNestedJSON.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/transpiler/intlater_module_to_dictionary/extractNestedJSON.ts"],"sourcesContent":["import type { Content, ContentModule } from '@intlayer/core';\n\n/**\n *\n * This function extracts all nested objects with an 'id' field from the input object and returns them as an array\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 * state: 'IL'\n * },\n * };\n * const result = extractObjectsWithId(input);\n * console.log(result);\n *\n * Output:\n *\n * [{\n * id: '1',\n * name: 'John Doe',\n * address: {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * state: 'IL'\n * }\n * },\n * {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * state: 'IL'\n * }]\n *\n */\nexport const extractObjectsWithId = (input: ContentModule): ContentModule[] => {\n // Function to recursively search and extract nested objects with an 'id'\n const search = (obj: Content, results: ContentModule[]): void => {\n if (obj && typeof obj === 'object') {\n if (Object.prototype.hasOwnProperty.call(obj, 'id')) {\n results.push(obj as ContentModule);\n }\n for (const key of Object.keys(obj)) {\n if (typeof obj[key] === 'object') {\n search(obj[key] as Content, results);\n }\n }\n }\n };\n\n const results: ContentModule[] = [];\n search(input, results);\n return results;\n};\n"],"mappings":"AAyCO,MAAM,uBAAuB,CAAC,UAA0C;AAE7E,QAAM,SAAS,CAAC,KAAcA,aAAmC;AAC/D,QAAI,OAAO,OAAO,QAAQ,UAAU;AAClC,UAAI,OAAO,UAAU,eAAe,KAAK,KAAK,IAAI,GAAG;AACnD,QAAAA,SAAQ,KAAK,GAAoB;AAAA,MACnC;AACA,iBAAW,OAAO,OAAO,KAAK,GAAG,GAAG;AAClC,YAAI,OAAO,IAAI,GAAG,MAAM,UAAU;AAChC,iBAAO,IAAI,GAAG,GAAcA,QAAO;AAAA,QACrC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,QAAM,UAA2B,CAAC;AAClC,SAAO,OAAO,OAAO;AACrB,SAAO;AACT;","names":["results"]}
@@ -0,0 +1,4 @@
1
+ export { extractObjectsWithId } from './extractNestedJSON.mjs';
2
+ export { processContentDeclaration } from './processModule.mjs';
3
+ export { transpileContentDeclaration } from './transpileContentDeclaration.mjs';
4
+ import '@intlayer/core';
@@ -0,0 +1,4 @@
1
+ export * from './extractNestedJSON.mjs';
2
+ export * from './processModule.mjs';
3
+ export * from './transpileContentDeclaration.mjs';
4
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/transpiler/intlater_module_to_dictionary/index.ts"],"sourcesContent":["export * from './extractNestedJSON';\nexport * from './processModule';\nexport * from './transpileContentDeclaration';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
@@ -0,0 +1,10 @@
1
+ import { ContentModule } from '@intlayer/core';
2
+
3
+ /**
4
+ * Load the content declaration from the given path
5
+ *
6
+ * Accepts JSON, JS, MJS and TS files as configuration
7
+ */
8
+ declare const loadContentDeclaration: (contentDeclarationFilePath: string) => ContentModule | undefined;
9
+
10
+ export { loadContentDeclaration };
@@ -0,0 +1,73 @@
1
+ import { createRequire } from "module";
2
+ import { runInNewContext } from "vm";
3
+ import { buildSync } from "esbuild";
4
+ const isESModule = typeof import.meta.url === "string";
5
+ const sandboxContext = {
6
+ exports: {
7
+ default: {}
8
+ },
9
+ module: {
10
+ exports: {}
11
+ },
12
+ console,
13
+ require: isESModule ? createRequire(import.meta.url) : require
14
+ };
15
+ const transformationOption = {
16
+ loader: {
17
+ ".js": "js",
18
+ ".jsx": "jsx",
19
+ ".mjs": "js",
20
+ ".ts": "ts",
21
+ ".tsx": "tsx",
22
+ ".cjs": "js",
23
+ ".json": "json"
24
+ },
25
+ format: "cjs",
26
+ // Output format as commonjs
27
+ target: "es2017",
28
+ packages: "external",
29
+ write: false,
30
+ bundle: true
31
+ };
32
+ const filterValidContentDeclaration = (contentDeclaration) => {
33
+ return contentDeclaration;
34
+ };
35
+ const loadContentDeclaration = (contentDeclarationFilePath) => {
36
+ let contentDeclaration = void 0;
37
+ const fileExtension = contentDeclarationFilePath.split(".").pop() ?? "";
38
+ try {
39
+ if (fileExtension === "json") {
40
+ return require(contentDeclarationFilePath);
41
+ }
42
+ const moduleResult = buildSync({
43
+ entryPoints: [contentDeclarationFilePath],
44
+ ...transformationOption
45
+ });
46
+ const moduleResultString = moduleResult.outputFiles?.[0].text;
47
+ if (!moduleResultString) {
48
+ console.error("Configuration file could not be loaded.");
49
+ return void 0;
50
+ }
51
+ runInNewContext(moduleResultString, sandboxContext);
52
+ if (sandboxContext.exports.default && Object.keys(sandboxContext.exports.default).length > 0) {
53
+ contentDeclaration = sandboxContext.exports.default;
54
+ } else if (sandboxContext.module.exports.defaults && Object.keys(sandboxContext.module.exports.defaults).length > 0) {
55
+ contentDeclaration = sandboxContext.module.exports.default;
56
+ } else if (sandboxContext.module.exports.default && Object.keys(sandboxContext.module.exports.default).length > 0) {
57
+ contentDeclaration = sandboxContext.module.exports.default;
58
+ } else if (sandboxContext.module.exports && Object.keys(sandboxContext.module.exports).length > 0) {
59
+ contentDeclaration = sandboxContext.module.exports;
60
+ }
61
+ if (typeof contentDeclaration === "undefined") {
62
+ console.error("Configuration file could not be loaded.");
63
+ return void 0;
64
+ }
65
+ return filterValidContentDeclaration(contentDeclaration);
66
+ } catch (error) {
67
+ console.error("Error:", error);
68
+ }
69
+ };
70
+ export {
71
+ loadContentDeclaration
72
+ };
73
+ //# sourceMappingURL=loadContentDeclaration.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/transpiler/intlater_module_to_dictionary/loadContentDeclaration.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-var-requires */\nimport { createRequire } from 'module';\nimport { type Context, runInNewContext } from 'vm';\nimport type { ContentModule } from '@intlayer/core';\nimport { type BuildOptions, buildSync, type BuildResult } from 'esbuild';\n\nconst isESModule = typeof import.meta.url === 'string';\n\nconst sandboxContext: Context = {\n exports: {\n default: {},\n },\n module: {\n exports: {},\n },\n console,\n require: isESModule ? createRequire(import.meta.url) : require,\n};\n\nconst transformationOption: BuildOptions = {\n loader: {\n '.js': 'js',\n '.jsx': 'jsx',\n '.mjs': 'js',\n '.ts': 'ts',\n '.tsx': 'tsx',\n '.cjs': 'js',\n '.json': 'json',\n },\n format: 'cjs', // Output format as commonjs\n target: 'es2017',\n packages: 'external',\n write: false,\n bundle: true,\n};\n\nconst filterValidContentDeclaration = (\n contentDeclaration: ContentModule\n): ContentModule => {\n // @TODO Implement filtering of valid content declaration\n return contentDeclaration;\n};\n\n/**\n * Load the content declaration from the given path\n *\n * Accepts JSON, JS, MJS and TS files as configuration\n */\nexport const loadContentDeclaration = (\n contentDeclarationFilePath: string\n): ContentModule | undefined => {\n let contentDeclaration: ContentModule | undefined = undefined;\n\n const fileExtension = contentDeclarationFilePath.split('.').pop() ?? '';\n\n try {\n if (fileExtension === 'json') {\n // Assume JSON\n return require(contentDeclarationFilePath);\n }\n\n // Rest is JS, MJS or TS\n\n const moduleResult: BuildResult = buildSync({\n entryPoints: [contentDeclarationFilePath],\n\n ...transformationOption,\n });\n\n const moduleResultString = moduleResult.outputFiles?.[0].text;\n\n if (!moduleResultString) {\n console.error('Configuration file could not be loaded.');\n return undefined;\n }\n\n runInNewContext(moduleResultString, sandboxContext);\n\n if (\n sandboxContext.exports.default &&\n Object.keys(sandboxContext.exports.default).length > 0\n ) {\n // ES Module\n contentDeclaration = sandboxContext.exports.default;\n } else if (\n sandboxContext.module.exports.defaults &&\n Object.keys(sandboxContext.module.exports.defaults).length > 0\n ) {\n // CommonJS\n contentDeclaration = sandboxContext.module.exports.default;\n } else if (\n sandboxContext.module.exports.default &&\n Object.keys(sandboxContext.module.exports.default).length > 0\n ) {\n // ES Module\n contentDeclaration = sandboxContext.module.exports.default;\n } else if (\n sandboxContext.module.exports &&\n Object.keys(sandboxContext.module.exports).length > 0\n ) {\n // Other\n contentDeclaration = sandboxContext.module.exports;\n }\n\n if (typeof contentDeclaration === 'undefined') {\n console.error('Configuration file could not be loaded.');\n return undefined;\n }\n\n return filterValidContentDeclaration(contentDeclaration);\n } catch (error) {\n console.error('Error:', error);\n }\n};\n"],"mappings":"AACA,SAAS,qBAAqB;AAC9B,SAAuB,uBAAuB;AAE9C,SAA4B,iBAAmC;AAE/D,MAAM,aAAa,OAAO,YAAY,QAAQ;AAE9C,MAAM,iBAA0B;AAAA,EAC9B,SAAS;AAAA,IACP,SAAS,CAAC;AAAA,EACZ;AAAA,EACA,QAAQ;AAAA,IACN,SAAS,CAAC;AAAA,EACZ;AAAA,EACA;AAAA,EACA,SAAS,aAAa,cAAc,YAAY,GAAG,IAAI;AACzD;AAEA,MAAM,uBAAqC;AAAA,EACzC,Q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EA,QAAI,OAAO,uBAAuB,aAAa;AAC7C,cAAQ,MAAM,yCAAyC;AACvD,aAAO;AAAA,IACT;AAEA,WAAO,8BAA8B,kBAAkB;AAAA,EACzD,SAAS,OAAO;AACd,YAAQ,MAAM,UAAU,KAAK;AAAA,EAC/B;AACF;","names":[]}
@@ -0,0 +1,8 @@
1
+ import { ContentModule } from '@intlayer/core';
2
+
3
+ /**
4
+ * Function to load, process the module and return the Intlayer ContentModule from the module file
5
+ */
6
+ declare const processContentDeclaration: (file: string) => Promise<ContentModule | undefined>;
7
+
8
+ export { processContentDeclaration };
@@ -0,0 +1,45 @@
1
+ import { resolve } from "path";
2
+ import { loadContentDeclaration } from './loadContentDeclaration.mjs';
3
+ const processFunctionResults = async (entry) => {
4
+ if (entry && typeof entry === "object") {
5
+ const promises = [];
6
+ const result = {};
7
+ for (const key of Object.keys(entry)) {
8
+ const field = entry?.[key];
9
+ if (typeof field === "object") {
10
+ result[key] = await processFunctionResults(
11
+ field
12
+ );
13
+ }
14
+ if (typeof field === "function") {
15
+ const promise = (async () => {
16
+ const value = await field();
17
+ result[key] = value;
18
+ })();
19
+ promises.push(promise);
20
+ } else {
21
+ result[key] = field;
22
+ }
23
+ }
24
+ await Promise.all(promises);
25
+ return result;
26
+ }
27
+ return entry;
28
+ };
29
+ const processContentDeclaration = async (file) => {
30
+ try {
31
+ const functionPath = resolve(file);
32
+ const entry = loadContentDeclaration(functionPath);
33
+ if (!entry) {
34
+ console.error("No entry found in module:", functionPath);
35
+ return;
36
+ }
37
+ return await processFunctionResults(entry);
38
+ } catch (error) {
39
+ console.error("Error processing module:", error);
40
+ }
41
+ };
42
+ export {
43
+ processContentDeclaration
44
+ };
45
+ //# sourceMappingURL=processModule.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/transpiler/intlater_module_to_dictionary/processModule.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 }\n\n 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 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;AAEA,UAAI,OAAO,UAAU,YAAY;AAE/B,cAAM,WAAW,YAAY;AAE3B,gBAAM,QAAQ,MAAM,MAAM;AAC1B,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":[]}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * This function transpile the bundled code to to make dictionaries as JSON files
3
+ */
4
+ declare const transpileContentDeclaration: (contentDeclarationsPaths: string[] | string) => Promise<string[]>;
5
+
6
+ export { transpileContentDeclaration };
@@ -0,0 +1,42 @@
1
+ import { mkdir, writeFile } from "fs/promises";
2
+ import { resolve } from "path";
3
+ import { getConfiguration } from "@intlayer/config";
4
+ import { extractObjectsWithId } from './extractNestedJSON.mjs';
5
+ import { processContentDeclaration } from './processModule.mjs';
6
+ const { content } = getConfiguration();
7
+ const { dictionariesDir } = content;
8
+ const buildDictionary = async (dictionaries) => {
9
+ const resultDictionariesPaths = [];
10
+ for await (const content2 of dictionaries) {
11
+ const contentString = JSON.stringify(content2);
12
+ const id = content2.id;
13
+ const outputFileName = `${id}.json`;
14
+ const resultFilePath = resolve(dictionariesDir, outputFileName);
15
+ await writeFile(resultFilePath, contentString, "utf8").catch((err) => {
16
+ console.error(`Error creating ${outputFileName}:`, err);
17
+ });
18
+ resultDictionariesPaths.push(resultFilePath);
19
+ }
20
+ return resultDictionariesPaths;
21
+ };
22
+ const transpileContentDeclaration = async (contentDeclarationsPaths) => {
23
+ const resultDictionariesPaths = [];
24
+ if (typeof contentDeclarationsPaths === "string") {
25
+ contentDeclarationsPaths = [contentDeclarationsPaths];
26
+ }
27
+ await mkdir(resolve(dictionariesDir), { recursive: true });
28
+ for await (const contentDeclarationPath of contentDeclarationsPaths) {
29
+ const result = await processContentDeclaration(contentDeclarationPath);
30
+ if (!result) {
31
+ continue;
32
+ }
33
+ const nestedContent = extractObjectsWithId(result);
34
+ const dictionariesPaths = await buildDictionary(nestedContent);
35
+ resultDictionariesPaths.push(...dictionariesPaths);
36
+ }
37
+ return resultDictionariesPaths;
38
+ };
39
+ export {
40
+ transpileContentDeclaration
41
+ };
42
+ //# sourceMappingURL=transpileContentDeclaration.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.ts"],"sourcesContent":["import { mkdir, writeFile } from 'fs/promises';\nimport { resolve } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport type { ContentModule } from '@intlayer/core';\nimport { extractObjectsWithId } from './extractNestedJSON';\nimport { processContentDeclaration } from './processModule';\n\nconst { content } = getConfiguration();\nconst { dictionariesDir } = content;\n\nconst buildDictionary = async (dictionaries: ContentModule[]) => {\n const resultDictionariesPaths: string[] = [];\n\n for await (const content of dictionaries) {\n const contentString = JSON.stringify(content);\n\n const id = content.id;\n const outputFileName = `${id}.json`;\n const resultFilePath = resolve(dictionariesDir, outputFileName);\n\n // Create the json file\n await writeFile(resultFilePath, contentString, 'utf8').catch((err) => {\n console.error(`Error creating ${outputFileName}:`, err);\n });\n\n resultDictionariesPaths.push(resultFilePath);\n }\n\n return resultDictionariesPaths;\n};\n\n/**\n * This function transpile the bundled code to to make dictionaries as JSON files\n */\nexport const transpileContentDeclaration = async (\n contentDeclarationsPaths: string[] | string\n) => {\n const resultDictionariesPaths: string[] = [];\n\n if (typeof contentDeclarationsPaths === 'string') {\n contentDeclarationsPaths = [contentDeclarationsPaths];\n }\n\n // Create the dictionaries folder if it doesn't exist\n await mkdir(resolve(dictionariesDir), { recursive: true });\n\n for await (const contentDeclarationPath of contentDeclarationsPaths) {\n const result = await processContentDeclaration(contentDeclarationPath);\n\n if (!result) {\n continue;\n }\n\n const nestedContent: ContentModule[] = extractObjectsWithId(result);\n\n const dictionariesPaths: string[] = await buildDictionary(nestedContent);\n\n resultDictionariesPaths.push(...dictionariesPaths);\n }\n\n return resultDictionariesPaths;\n};\n"],"mappings":"AAAA,SAAS,OAAO,iBAAiB;AACjC,SAAS,eAAe;AACxB,SAAS,wBAAwB;AAEjC,SAAS,4BAA4B;AACrC,SAAS,iCAAiC;AAE1C,MAAM,EAAE,QAAQ,IAAI,iBAAiB;AACrC,MAAM,EAAE,gBAAgB,IAAI;AAE5B,MAAM,kBAAkB,OAAO,iBAAkC;AAC/D,QAAM,0BAAoC,CAAC;AAE3C,mBAAiBA,YAAW,cAAc;AACxC,UAAM,gBAAgB,KAAK,UAAUA,QAAO;AAE5C,UAAM,KAAKA,SAAQ;AACnB,UAAM,iBAAiB,GAAG,EAAE;AAC5B,UAAM,iBAAiB,QAAQ,iBAAiB,cAAc;AAG9D,UAAM,UAAU,gBAAgB,eAAe,MAAM,EAAE,MAAM,CAAC,QAAQ;AACpE,cAAQ,MAAM,kBAAkB,cAAc,KAAK,GAAG;AAAA,IACxD,CAAC;AAED,4BAAwB,KAAK,cAAc;AAAA,EAC7C;AAEA,SAAO;AACT;AAKO,MAAM,8BAA8B,OACzC,6BACG;AACH,QAAM,0BAAoC,CAAC;AAE3C,MAAI,OAAO,6BAA6B,UAAU;AAChD,+BAA2B,CAAC,wBAAwB;AAAA,EACtD;AAGA,QAAM,MAAM,QAAQ,eAAe,GAAG,EAAE,WAAW,KAAK,CAAC;AAEzD,mBAAiB,0BAA0B,0BAA0B;AACnE,UAAM,SAAS,MAAM,0BAA0B,sBAAsB;AAErE,QAAI,CAAC,QAAQ;AACX;AAAA,IACF;AAEA,UAAM,gBAAiC,qBAAqB,MAAM;AAElE,UAAM,oBAA8B,MAAM,gBAAgB,aAAa;AAEvE,4BAAwB,KAAK,GAAG,iBAAiB;AAAA,EACnD;AAEA,SAAO;AACT;","names":["content"]}
@@ -0,0 +1,4 @@
1
+ declare const getFileHash: (filePath: string) => string;
2
+ declare const transformToCamelCase: (string: string) => string;
3
+
4
+ export { getFileHash, transformToCamelCase };