@intlayer/webpack 1.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 (98) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/index.cjs +46 -0
  3. package/dist/cjs/index.cjs.map +1 -0
  4. package/dist/cjs/scripts/bundle.cjs +68 -0
  5. package/dist/cjs/scripts/bundle.cjs.map +1 -0
  6. package/dist/cjs/scripts/index.cjs +33 -0
  7. package/dist/cjs/scripts/index.cjs.map +1 -0
  8. package/dist/cjs/scripts/startServer.cjs +44 -0
  9. package/dist/cjs/scripts/startServer.cjs.map +1 -0
  10. package/dist/cjs/transpiler/dictionary_to_main/createDictionaryList.cjs +92 -0
  11. package/dist/cjs/transpiler/dictionary_to_main/createDictionaryList.cjs.map +1 -0
  12. package/dist/cjs/transpiler/dictionary_to_main/index.cjs +35 -0
  13. package/dist/cjs/transpiler/dictionary_to_main/index.cjs.map +1 -0
  14. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs +83 -0
  15. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs.map +1 -0
  16. package/dist/cjs/transpiler/dictionary_to_type/createType.cjs +122 -0
  17. package/dist/cjs/transpiler/dictionary_to_type/createType.cjs.map +1 -0
  18. package/dist/cjs/transpiler/dictionary_to_type/index.cjs +41 -0
  19. package/dist/cjs/transpiler/dictionary_to_type/index.cjs.map +1 -0
  20. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.cjs +50 -0
  21. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.cjs.map +1 -0
  22. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.cjs +47 -0
  23. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.cjs.map +1 -0
  24. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.cjs +70 -0
  25. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.cjs.map +1 -0
  26. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileBundledCode.cjs +112 -0
  27. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileBundledCode.cjs.map +1 -0
  28. package/dist/cjs/utils.cjs +53 -0
  29. package/dist/cjs/utils.cjs.map +1 -0
  30. package/dist/cjs/webpack-plugin.cjs +184 -0
  31. package/dist/cjs/webpack-plugin.cjs.map +1 -0
  32. package/dist/cjs/webpack.config.cjs +180 -0
  33. package/dist/cjs/webpack.config.cjs.map +1 -0
  34. package/dist/esm/index.d.mts +9 -0
  35. package/dist/esm/index.mjs +12 -0
  36. package/dist/esm/index.mjs.map +1 -0
  37. package/dist/esm/scripts/bundle.d.mts +7 -0
  38. package/dist/esm/scripts/bundle.mjs +35 -0
  39. package/dist/esm/scripts/bundle.mjs.map +1 -0
  40. package/dist/esm/scripts/index.d.mts +3 -0
  41. package/dist/esm/scripts/index.mjs +3 -0
  42. package/dist/esm/scripts/index.mjs.map +1 -0
  43. package/dist/esm/scripts/startServer.d.mts +5 -0
  44. package/dist/esm/scripts/startServer.mjs +10 -0
  45. package/dist/esm/scripts/startServer.mjs.map +1 -0
  46. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.d.mts +6 -0
  47. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.mjs +52 -0
  48. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.mjs.map +1 -0
  49. package/dist/esm/transpiler/dictionary_to_main/index.d.mts +1 -0
  50. package/dist/esm/transpiler/dictionary_to_main/index.mjs +2 -0
  51. package/dist/esm/transpiler/dictionary_to_main/index.mjs.map +1 -0
  52. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.d.mts +7 -0
  53. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs +43 -0
  54. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs.map +1 -0
  55. package/dist/esm/transpiler/dictionary_to_type/createType.d.mts +42 -0
  56. package/dist/esm/transpiler/dictionary_to_type/createType.mjs +84 -0
  57. package/dist/esm/transpiler/dictionary_to_type/createType.mjs.map +1 -0
  58. package/dist/esm/transpiler/dictionary_to_type/index.d.mts +10 -0
  59. package/dist/esm/transpiler/dictionary_to_type/index.mjs +3 -0
  60. package/dist/esm/transpiler/dictionary_to_type/index.mjs.map +1 -0
  61. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.d.mts +44 -0
  62. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.mjs +19 -0
  63. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.mjs.map +1 -0
  64. package/dist/esm/transpiler/intlater_module_to_dictionary/index.d.mts +4 -0
  65. package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs +4 -0
  66. package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs.map +1 -0
  67. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.d.mts +10 -0
  68. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.mjs +39 -0
  69. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.mjs.map +1 -0
  70. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileBundledCode.d.mts +8 -0
  71. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileBundledCode.mjs +70 -0
  72. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileBundledCode.mjs.map +1 -0
  73. package/dist/esm/utils.d.mts +4 -0
  74. package/dist/esm/utils.mjs +20 -0
  75. package/dist/esm/utils.mjs.map +1 -0
  76. package/dist/esm/webpack-plugin.d.mts +15 -0
  77. package/dist/esm/webpack-plugin.mjs +145 -0
  78. package/dist/esm/webpack-plugin.mjs.map +1 -0
  79. package/dist/esm/webpack.config.d.mts +7 -0
  80. package/dist/esm/webpack.config.mjs +136 -0
  81. package/dist/esm/webpack.config.mjs.map +1 -0
  82. package/package.json +97 -0
  83. package/src/index.ts +12 -0
  84. package/src/scripts/bundle.ts +44 -0
  85. package/src/scripts/index.ts +2 -0
  86. package/src/scripts/startServer.ts +14 -0
  87. package/src/transpiler/dictionary_to_main/createDictionaryList.ts +65 -0
  88. package/src/transpiler/dictionary_to_main/index.ts +1 -0
  89. package/src/transpiler/dictionary_to_type/createModuleAugmentation.ts +70 -0
  90. package/src/transpiler/dictionary_to_type/createType.ts +140 -0
  91. package/src/transpiler/dictionary_to_type/index.ts +2 -0
  92. package/src/transpiler/intlater_module_to_dictionary/extractNestedJSON.ts +60 -0
  93. package/src/transpiler/intlater_module_to_dictionary/index.ts +3 -0
  94. package/src/transpiler/intlater_module_to_dictionary/processModule.ts +69 -0
  95. package/src/transpiler/intlater_module_to_dictionary/transpileBundledCode.ts +107 -0
  96. package/src/utils.ts +26 -0
  97. package/src/webpack-plugin.ts +185 -0
  98. package/src/webpack.config.ts +149 -0
package/package.json ADDED
@@ -0,0 +1,97 @@
1
+ {
2
+ "name": "@intlayer/webpack",
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "description": "Webpack application for IntLayer - Webpack process to transpile TypeScript files to JavaScript files into IntLayer dictionaries.",
6
+ "keywords": [
7
+ "intlayer",
8
+ "webpack",
9
+ "application",
10
+ "transpile",
11
+ "typescript",
12
+ "javascript",
13
+ "json",
14
+ "file"
15
+ ],
16
+ "homepage": "https://github.com/aypineau/intlayer",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/aypineau/intlayer"
20
+ },
21
+ "license": "MIT",
22
+ "author": {
23
+ "name": "Aymeric PINEAU",
24
+ "url": "https://github.com/aypineau"
25
+ },
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/esm/index.d.mts",
29
+ "require": "./dist/cjs/index.cjs",
30
+ "import": "./dist/esm/index.mjs"
31
+ },
32
+ "./package.json": "./package.json"
33
+ },
34
+ "main": "src/index.ts",
35
+ "typesVersions": {
36
+ "*": {
37
+ "package.json": [
38
+ "./package.json"
39
+ ]
40
+ }
41
+ },
42
+ "files": [
43
+ "./dist",
44
+ "./src",
45
+ "./bin",
46
+ "./package.json"
47
+ ],
48
+ "dependencies": {
49
+ "crypto-js": "^4.2.0",
50
+ "esbuild": "^0.20.2",
51
+ "esbuild-loader": "^4.1.0",
52
+ "glob": "^10.3.10",
53
+ "node-loader": "^2.0.0",
54
+ "rimraf": "5.0.5",
55
+ "util": "^0.12.5",
56
+ "webpack": "^5.91.0",
57
+ "webpack-cli": "^5.1.4",
58
+ "webpack-dev-server": "^5.0.4",
59
+ "yargs": "^17.7.2",
60
+ "@intlayer/cli": "^1.0.0",
61
+ "@intlayer/config": "^1.0.0",
62
+ "@intlayer/core": "^1.0.0"
63
+ },
64
+ "devDependencies": {
65
+ "@changesets/changelog-github": "0.5.0",
66
+ "@changesets/cli": "2.27.1",
67
+ "@types/crypto-js": "^4.2.2",
68
+ "@types/node": "^20.11.30",
69
+ "@types/uuid": "^9.0.8",
70
+ "@types/webpack-watch-files-plugin": "^1.1.3",
71
+ "@types/yargs": "^17.0.32",
72
+ "node-polyfill-webpack-plugin": "^3.0.0",
73
+ "tsup": "^8.0.2",
74
+ "typescript": "^5.3.3",
75
+ "webpack-watch-files-plugin": "^1.2.1",
76
+ "@utils/eslint-config": "^1.0.0",
77
+ "@utils/ts-config": "^1.0.0"
78
+ },
79
+ "engines": {
80
+ "node": ">=14.18"
81
+ },
82
+ "bug": {
83
+ "url": "https://github.com/aypineau/intlayer/issues"
84
+ },
85
+ "scripts": {
86
+ "build": "tsup",
87
+ "clean": "rimraf ./dist",
88
+ "dev": "tsup --watch",
89
+ "lint": "eslint . --ext .ts,.tsx,.js,.jsx,.cjs,.mjs",
90
+ "lint:fix": "eslint . --ext .ts,.tsx,.js,.jsx,.cjs,.mjs --fix",
91
+ "prettier:fix": "prettier --write src/**/*",
92
+ "serve": "webpack serve --config ./webpack.config.ts",
93
+ "transpile": "webpack --config ./webpack.config.ts",
94
+ "typecheck": "tsup--project ./tsconfig.json --noEmit",
95
+ "watch": "webpack --config ./webpack.config.ts --watch"
96
+ }
97
+ }
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { bundle, startServer, watch } from './scripts/index';
2
+ import { IntLayerPlugin } from './webpack-plugin';
3
+ import { devServerConfig, webpackConfig } from './webpack.config';
4
+
5
+ export {
6
+ bundle,
7
+ watch,
8
+ startServer,
9
+ devServerConfig,
10
+ webpackConfig,
11
+ IntLayerPlugin,
12
+ };
@@ -0,0 +1,44 @@
1
+ import type { Compiler } from 'webpack';
2
+
3
+ export const bundle = (compiler: Compiler) =>
4
+ // Run the compiler
5
+ compiler.run((err, stats) => {
6
+ // Handle errors and webpack compilation stats
7
+ if (err) {
8
+ console.error('Webpack compilation error:', err);
9
+ return;
10
+ }
11
+
12
+ console.info('Webpack compilation successful.');
13
+
14
+ if (stats)
15
+ console.info(
16
+ stats.toString({
17
+ // Add options here to customize the stats output
18
+ colors: true,
19
+ })
20
+ );
21
+ });
22
+
23
+ export const watch = (compiler: Compiler) =>
24
+ // Run the compiler
25
+ compiler.watch({}, (err, stats) => {
26
+ // Handle errors and webpack compilation stats
27
+ if (err) {
28
+ console.error('Webpack compilation error:', err);
29
+ return;
30
+ }
31
+
32
+ console.info('Webpack compilation successful.');
33
+
34
+ if (stats)
35
+ console.info(
36
+ stats.toString({
37
+ // Add options here to customize the stats output
38
+ colors: true,
39
+ })
40
+ );
41
+
42
+ // After successful compilation, start the server
43
+ console.info('Watch IntLayer content...');
44
+ });
@@ -0,0 +1,2 @@
1
+ export * from './startServer';
2
+ export * from './bundle';
@@ -0,0 +1,14 @@
1
+ import type { Compiler } from 'webpack';
2
+ import { devServerConfig } from '../webpack.config';
3
+
4
+ const WebpackDevServer = require('webpack-dev-server');
5
+
6
+ export const startServer = (compiler: Compiler) => {
7
+ // Create a new instance of webpack-dev-server
8
+ const devServer = new WebpackDevServer(devServerConfig, compiler);
9
+
10
+ // Start the server
11
+ devServer.startCallback(() => {
12
+ console.info('Webpack Dev Server is running...');
13
+ });
14
+ };
@@ -0,0 +1,65 @@
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';
6
+
7
+ const { content } = getConfiguration();
8
+ const { dictionariesDir, mainDir } = content;
9
+
10
+ /**
11
+ * This function generates the content of the dictionary list file
12
+ */
13
+ const generateDictionaryListContent = (
14
+ dictionaries: string[],
15
+ format: 'cjs' | 'esm' = 'esm'
16
+ ): string => {
17
+ let content = '';
18
+
19
+ const dictionariesRef = dictionaries.map((dictionaryPath) => ({
20
+ relativePath: relative(mainDir, dictionaryPath),
21
+ id: basename(dictionaryPath, extname(dictionaryPath)), // Get the base name as the dictionary id
22
+ hash: `_${getFileHash(dictionaryPath)}`, // Get the hash of the dictionary to avoid conflicts
23
+ }));
24
+
25
+ // Import all dictionaries
26
+ dictionariesRef.forEach((dictionary) => {
27
+ if (format === 'esm')
28
+ content += `import ${dictionary.hash} from '${dictionary.relativePath}';\n`;
29
+ if (format === 'cjs')
30
+ content += `const ${dictionary.hash} = require('${dictionary.relativePath}');\n`;
31
+ });
32
+
33
+ content += '\n';
34
+
35
+ // Format Dictionary Map
36
+ const formattedDictionaryMap: string = dictionariesRef
37
+ .map((dictionary) => `"${dictionary.id}": ${dictionary.hash}`)
38
+ .join(',\n');
39
+
40
+ if (format === 'esm')
41
+ content += `export default {\n${formattedDictionaryMap}\n};\n`;
42
+ if (format === 'cjs')
43
+ content += `module.exports = {\n${formattedDictionaryMap}\n};\n`;
44
+
45
+ return content;
46
+ };
47
+
48
+ /**
49
+ * This function generates a list of dictionaries in the main directory
50
+ */
51
+ export const createDictionaryList = () => {
52
+ // Create main directory if it doesn't exist
53
+ if (!existsSync(mainDir)) {
54
+ mkdirSync(mainDir, { recursive: true });
55
+ }
56
+
57
+ const dictionaries: string[] = sync(`${dictionariesDir}/**/*.json`);
58
+
59
+ // Create the dictionary list file
60
+ const cjsContent = generateDictionaryListContent(dictionaries, 'cjs');
61
+ writeFileSync(resolve(mainDir, 'dictionaries.cjs'), cjsContent);
62
+
63
+ const esmContent = generateDictionaryListContent(dictionaries, 'esm');
64
+ writeFileSync(resolve(mainDir, 'dictionaries.mjs'), esmContent);
65
+ };
@@ -0,0 +1 @@
1
+ export * from './createDictionaryList';
@@ -0,0 +1,70 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from 'fs';
2
+ import { basename, join, relative } from 'path';
3
+ import { getConfiguration } from '@intlayer/config';
4
+ import { sync } from 'glob';
5
+ import { getFileHash, transformToCamelCase } from '../../utils';
6
+
7
+ const { content } = getConfiguration();
8
+ const { typesDir, moduleAugmentationDir } = content;
9
+
10
+ export const getTypeName = (id: string): string =>
11
+ transformToCamelCase(`${id}Content`);
12
+
13
+ /**
14
+ * This function generates the content of the module augmentation file
15
+ */
16
+ const generateTypeIndexContent = (typeFiles: string[]): string => {
17
+ let content = "import 'intlayer';\n\n";
18
+
19
+ const dictionariesRef = typeFiles.map((dictionaryPath) => ({
20
+ relativePath: relative(moduleAugmentationDir, dictionaryPath),
21
+ id: basename(dictionaryPath, '.d.ts'), // Get the base name as the dictionary id
22
+ hash: `_${getFileHash(dictionaryPath)}`, // Get the hash of the dictionary to avoid conflicts
23
+ }));
24
+
25
+ // Import all dictionaries
26
+ dictionariesRef.forEach((dictionary) => {
27
+ const typeName = getTypeName(dictionary.id);
28
+ content += `import type { ${typeName} as ${dictionary.hash} } from '${dictionary.relativePath}';\n`;
29
+ });
30
+
31
+ content += '\n';
32
+
33
+ // Format Dictionary Map
34
+ const formattedDictionaryMap: string = dictionariesRef
35
+ .map((dictionary) => ` "${dictionary.id}": ${dictionary.hash};`)
36
+ .join('\n');
37
+
38
+ /**
39
+ * Write the module augmentation to extend the intlayer module with the dictionaries types
40
+ * Will suggest the type resulting of the dictionaries
41
+ *
42
+ * declare module 'intlayer' {
43
+ * interface IntlayerDictionaryTypesConnector = {
44
+ * dictionaries: {
45
+ * id: DictionaryType;
46
+ * }
47
+ * }
48
+ *
49
+ * See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
50
+ */
51
+ content += `declare module 'intlayer' {\n interface IntlayerDictionaryTypesConnector {\n${formattedDictionaryMap}\n };\n};`;
52
+
53
+ return content;
54
+ };
55
+
56
+ /**
57
+ * This function generates a index file merging all the types
58
+ */
59
+ export const createModuleAugmentation = () => {
60
+ // Create main directory if it doesn't exist
61
+ if (!existsSync(moduleAugmentationDir)) {
62
+ mkdirSync(moduleAugmentationDir, { recursive: true });
63
+ }
64
+
65
+ const dictionaries: string[] = sync(`${typesDir}/**/*.d.ts`);
66
+ // Create the dictionary list file
67
+
68
+ const tsContent = generateTypeIndexContent(dictionaries);
69
+ writeFileSync(join(moduleAugmentationDir, 'intlayer.d.ts'), tsContent);
70
+ };
@@ -0,0 +1,140 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from 'fs';
2
+ import { resolve } from 'path';
3
+ import { getConfiguration } from '@intlayer/config';
4
+ import {
5
+ NodeType,
6
+ type Content,
7
+ type ContentModule,
8
+ type TypedNode,
9
+ } from '@intlayer/core';
10
+ import { getTypeName } from './createModuleAugmentation';
11
+
12
+ const { content, internationalization } = getConfiguration();
13
+ const { typesDir } = content;
14
+
15
+ /**
16
+ *
17
+ * This function generates a TypeScript type definition from a JSON object
18
+ *
19
+ * Example:
20
+ *
21
+ * const input = {
22
+ * id: '1',
23
+ * name: 'John Doe',
24
+ * address: {
25
+ * id: '2',
26
+ * street: '123 Main St',
27
+ * city: 'Springfield',
28
+ * }
29
+ * };
30
+ *
31
+ * const result = generateTypeScriptType(input, 'RootObject');
32
+ * console.log(result);
33
+ *
34
+ * Output:
35
+ *
36
+ * type RootObject = {
37
+ * id: '1',
38
+ * name: string,
39
+ * address: {
40
+ * id: '2',
41
+ * street: string,
42
+ * city: string,
43
+ * },
44
+ * };
45
+ *
46
+ */
47
+ export const generateTypeScriptType = (obj: ContentModule): string => {
48
+ let typeDefinition = ``;
49
+
50
+ const typeName = getTypeName(obj.id);
51
+
52
+ typeDefinition += `export type ${typeName} = {\n`;
53
+ typeDefinition += generateTypeScriptTypeContent(obj);
54
+ typeDefinition += '};\n\n';
55
+
56
+ return typeDefinition;
57
+ };
58
+
59
+ export const generateTypeScriptTypeContent = (obj: Content): string => {
60
+ let typeDefinition = ``;
61
+
62
+ for (const [key, value] of Object.entries(obj)) {
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ const nodeType: NodeType | undefined = (value as TypedNode).nodeType;
65
+
66
+ if (
67
+ // Check if the value is a typed node
68
+ typeof value === 'object' &&
69
+ nodeType === NodeType.Translation
70
+ ) {
71
+ const tsType =
72
+ typeof value[internationalization.defaultLocale as keyof typeof value];
73
+ typeDefinition += ` ${key}: ${tsType},\n`;
74
+ } else if (
75
+ // Check if the value is a typed node
76
+ typeof value === 'object' &&
77
+ nodeType === NodeType.Enumeration
78
+ ) {
79
+ const tsType =
80
+ typeof value[internationalization.defaultLocale as keyof typeof value];
81
+
82
+ typeDefinition += ` ${key}: (quantity: number) => ${tsType},\n`;
83
+ } else if (
84
+ // Check if the value is a nested object
85
+ typeof value === 'object' &&
86
+ !Array.isArray(value)
87
+ ) {
88
+ // Nested object, recurse
89
+ const nestedType = generateTypeScriptTypeContent(value as Content);
90
+ typeDefinition += ` ${key}: {${nestedType}},\n`;
91
+ } else if (
92
+ // Check if the value is an array
93
+ Array.isArray(value)
94
+ ) {
95
+ // Array handling (simplified, assumes non-empty arrays with uniform type)
96
+ const arrayType = typeof value[0];
97
+ typeDefinition += ` ${key}: ${arrayType}[],\n`;
98
+ } else if (
99
+ // Check if the value is an 'id'
100
+ typeof value === 'string' &&
101
+ key === 'id'
102
+ ) {
103
+ // Special handling for 'id' field
104
+ const tsType = `"${value}"`;
105
+ typeDefinition += ` ${key}: ${tsType},\n`;
106
+ } else {
107
+ // Primitive type
108
+ const tsType = typeof value;
109
+ typeDefinition += ` ${key}: ${tsType},\n`;
110
+ }
111
+ }
112
+
113
+ return typeDefinition;
114
+ };
115
+
116
+ /**
117
+ * This function generates a TypeScript type definition from a JSON object
118
+ */
119
+ export const createTypes = (dictionariesPaths: string[]): string[] => {
120
+ const resultTypesPaths: string[] = [];
121
+
122
+ // Create type folders if they don't exist
123
+ if (!existsSync(typesDir)) {
124
+ mkdirSync(typesDir, { recursive: true });
125
+ }
126
+
127
+ for (const dictionaryPath of dictionariesPaths) {
128
+ const contentModule: ContentModule = require(dictionaryPath);
129
+ const dictionaryName: string = contentModule.id;
130
+ const typeDefinition: string = generateTypeScriptType(contentModule);
131
+
132
+ const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);
133
+
134
+ writeFileSync(outputPath, typeDefinition);
135
+
136
+ resultTypesPaths.push(outputPath);
137
+ }
138
+
139
+ return resultTypesPaths;
140
+ };
@@ -0,0 +1,2 @@
1
+ export * from './createModuleAugmentation';
2
+ export * from './createType';
@@ -0,0 +1,60 @@
1
+ import type { Content, 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
+ export const extractObjectsWithId = (input: ContentModule): ContentModule[] => {
43
+ // Function to recursively search and extract nested objects with an 'id'
44
+ const search = (obj: Content, results: ContentModule[]): void => {
45
+ if (obj && typeof obj === 'object') {
46
+ if (Object.prototype.hasOwnProperty.call(obj, 'id')) {
47
+ results.push(obj as ContentModule);
48
+ }
49
+ for (const key of Object.keys(obj)) {
50
+ if (typeof obj[key] === 'object') {
51
+ search(obj[key] as Content, results);
52
+ }
53
+ }
54
+ }
55
+ };
56
+
57
+ const results: ContentModule[] = [];
58
+ search(input, results);
59
+ return results;
60
+ };
@@ -0,0 +1,3 @@
1
+ export * from './extractNestedJSON';
2
+ export * from './processModule';
3
+ export * from './transpileBundledCode';
@@ -0,0 +1,69 @@
1
+ import { resolve } from 'path';
2
+ import type {
3
+ Content,
4
+ ContentModule,
5
+ FlatContent,
6
+ FlatContentValue,
7
+ } from '@intlayer/core';
8
+
9
+ /**
10
+ * Function to load the module file in a sandboxed environment
11
+ */
12
+ const loadModule = async (modulePath: string): Promise<ContentModule> => {
13
+ // @TODO: Sandbox the module to prevent malicious code execution
14
+
15
+ return (await import(modulePath)).default;
16
+ };
17
+
18
+ /**
19
+ * Function to replace function and async function fields with their results in the object
20
+ */
21
+ const processFunctionResults = async (entry: Content): Promise<FlatContent> => {
22
+ if (entry && typeof entry === 'object') {
23
+ const promises: Promise<void>[] = [];
24
+ const result: FlatContent = {};
25
+
26
+ for (const key of Object.keys(entry)) {
27
+ const field = entry?.[key];
28
+
29
+ if (typeof field === 'object') {
30
+ result[key] = (await processFunctionResults(
31
+ field as Content
32
+ )) as FlatContentValue;
33
+ }
34
+
35
+ if (typeof field === 'function') {
36
+ // Wait for the function to resolve if it's an async function
37
+ const promise = (async () => {
38
+ // Execute the function and await the result if it's a Promise
39
+ const value = await field();
40
+ result[key] = value as FlatContentValue;
41
+ })();
42
+ promises.push(promise);
43
+ } else {
44
+ result[key] = field as FlatContentValue;
45
+ }
46
+ }
47
+
48
+ // Wait for all async operations to complete
49
+ await Promise.all(promises);
50
+
51
+ return result;
52
+ }
53
+
54
+ return entry;
55
+ };
56
+
57
+ /**
58
+ * Function to load, process the module and return the Intlayer ContentModule from the module file
59
+ */
60
+ export const processModule = async (file: string) => {
61
+ try {
62
+ const functionPath = resolve(file);
63
+ const entry = await loadModule(functionPath);
64
+
65
+ return (await processFunctionResults(entry)) as ContentModule;
66
+ } catch (error) {
67
+ console.error('Error processing module:', error);
68
+ }
69
+ };