@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,114 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
+ import { createRequire } from 'module';
3
+ import { type Context, runInNewContext } from 'vm';
4
+ import type { ContentModule } from '@intlayer/core';
5
+ import { type BuildOptions, buildSync, type BuildResult } from 'esbuild';
6
+
7
+ const isESModule = typeof import.meta.url === 'string';
8
+
9
+ const sandboxContext: Context = {
10
+ exports: {
11
+ default: {},
12
+ },
13
+ module: {
14
+ exports: {},
15
+ },
16
+ console,
17
+ require: isESModule ? createRequire(import.meta.url) : require,
18
+ };
19
+
20
+ const transformationOption: BuildOptions = {
21
+ loader: {
22
+ '.js': 'js',
23
+ '.jsx': 'jsx',
24
+ '.mjs': 'js',
25
+ '.ts': 'ts',
26
+ '.tsx': 'tsx',
27
+ '.cjs': 'js',
28
+ '.json': 'json',
29
+ },
30
+ format: 'cjs', // Output format as commonjs
31
+ target: 'es2017',
32
+ packages: 'external',
33
+ write: false,
34
+ bundle: true,
35
+ };
36
+
37
+ const filterValidContentDeclaration = (
38
+ contentDeclaration: ContentModule
39
+ ): ContentModule => {
40
+ // @TODO Implement filtering of valid content declaration
41
+ return contentDeclaration;
42
+ };
43
+
44
+ /**
45
+ * Load the content declaration from the given path
46
+ *
47
+ * Accepts JSON, JS, MJS and TS files as configuration
48
+ */
49
+ export const loadContentDeclaration = (
50
+ contentDeclarationFilePath: string
51
+ ): ContentModule | undefined => {
52
+ let contentDeclaration: ContentModule | undefined = undefined;
53
+
54
+ const fileExtension = contentDeclarationFilePath.split('.').pop() ?? '';
55
+
56
+ try {
57
+ if (fileExtension === 'json') {
58
+ // Assume JSON
59
+ return require(contentDeclarationFilePath);
60
+ }
61
+
62
+ // Rest is JS, MJS or TS
63
+
64
+ const moduleResult: BuildResult = buildSync({
65
+ entryPoints: [contentDeclarationFilePath],
66
+
67
+ ...transformationOption,
68
+ });
69
+
70
+ const moduleResultString = moduleResult.outputFiles?.[0].text;
71
+
72
+ if (!moduleResultString) {
73
+ console.error('Configuration file could not be loaded.');
74
+ return undefined;
75
+ }
76
+
77
+ runInNewContext(moduleResultString, sandboxContext);
78
+
79
+ if (
80
+ sandboxContext.exports.default &&
81
+ Object.keys(sandboxContext.exports.default).length > 0
82
+ ) {
83
+ // ES Module
84
+ contentDeclaration = sandboxContext.exports.default;
85
+ } else if (
86
+ sandboxContext.module.exports.defaults &&
87
+ Object.keys(sandboxContext.module.exports.defaults).length > 0
88
+ ) {
89
+ // CommonJS
90
+ contentDeclaration = sandboxContext.module.exports.default;
91
+ } else if (
92
+ sandboxContext.module.exports.default &&
93
+ Object.keys(sandboxContext.module.exports.default).length > 0
94
+ ) {
95
+ // ES Module
96
+ contentDeclaration = sandboxContext.module.exports.default;
97
+ } else if (
98
+ sandboxContext.module.exports &&
99
+ Object.keys(sandboxContext.module.exports).length > 0
100
+ ) {
101
+ // Other
102
+ contentDeclaration = sandboxContext.module.exports;
103
+ }
104
+
105
+ if (typeof contentDeclaration === 'undefined') {
106
+ console.error('Configuration file could not be loaded.');
107
+ return undefined;
108
+ }
109
+
110
+ return filterValidContentDeclaration(contentDeclaration);
111
+ } catch (error) {
112
+ console.error('Error:', error);
113
+ }
114
+ };
@@ -0,0 +1,66 @@
1
+ import { resolve } from 'path';
2
+ import type {
3
+ Content,
4
+ ContentModule,
5
+ FlatContent,
6
+ FlatContentValue,
7
+ } from '@intlayer/core';
8
+ import { loadContentDeclaration } from './loadContentDeclaration';
9
+
10
+ /**
11
+ * Function to replace function and async function fields with their results in the object
12
+ */
13
+ const processFunctionResults = async (entry: Content): Promise<FlatContent> => {
14
+ if (entry && typeof entry === 'object') {
15
+ const promises: Promise<void>[] = [];
16
+ const result: FlatContent = {};
17
+
18
+ for (const key of Object.keys(entry)) {
19
+ const field = entry?.[key];
20
+
21
+ if (typeof field === 'object') {
22
+ result[key] = (await processFunctionResults(
23
+ field as Content
24
+ )) as FlatContentValue;
25
+ }
26
+
27
+ if (typeof field === 'function') {
28
+ // Wait for the function to resolve if it's an async function
29
+ const promise = (async () => {
30
+ // Execute the function and await the result if it's a Promise
31
+ const value = await field();
32
+ result[key] = value as FlatContentValue;
33
+ })();
34
+ promises.push(promise);
35
+ } else {
36
+ result[key] = field as FlatContentValue;
37
+ }
38
+ }
39
+
40
+ // Wait for all async operations to complete
41
+ await Promise.all(promises);
42
+
43
+ return result;
44
+ }
45
+
46
+ return entry;
47
+ };
48
+
49
+ /**
50
+ * Function to load, process the module and return the Intlayer ContentModule from the module file
51
+ */
52
+ export const processContentDeclaration = async (file: string) => {
53
+ try {
54
+ const functionPath = resolve(file);
55
+ const entry = loadContentDeclaration(functionPath);
56
+
57
+ if (!entry) {
58
+ console.error('No entry found in module:', functionPath);
59
+ return;
60
+ }
61
+
62
+ return (await processFunctionResults(entry)) as ContentModule;
63
+ } catch (error) {
64
+ console.error('Error processing module:', error);
65
+ }
66
+ };
@@ -0,0 +1,62 @@
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 { extractObjectsWithId } from './extractNestedJSON';
6
+ import { processContentDeclaration } from './processModule';
7
+
8
+ const { content } = getConfiguration();
9
+ const { dictionariesDir } = content;
10
+
11
+ const buildDictionary = async (dictionaries: ContentModule[]) => {
12
+ const resultDictionariesPaths: string[] = [];
13
+
14
+ for await (const content of dictionaries) {
15
+ const contentString = JSON.stringify(content);
16
+
17
+ const id = content.id;
18
+ const outputFileName = `${id}.json`;
19
+ const resultFilePath = resolve(dictionariesDir, outputFileName);
20
+
21
+ // Create the json file
22
+ await writeFile(resultFilePath, contentString, 'utf8').catch((err) => {
23
+ console.error(`Error creating ${outputFileName}:`, err);
24
+ });
25
+
26
+ resultDictionariesPaths.push(resultFilePath);
27
+ }
28
+
29
+ return resultDictionariesPaths;
30
+ };
31
+
32
+ /**
33
+ * This function transpile the bundled code to to make dictionaries as JSON files
34
+ */
35
+ export const transpileContentDeclaration = async (
36
+ contentDeclarationsPaths: string[] | string
37
+ ) => {
38
+ const resultDictionariesPaths: string[] = [];
39
+
40
+ if (typeof contentDeclarationsPaths === 'string') {
41
+ contentDeclarationsPaths = [contentDeclarationsPaths];
42
+ }
43
+
44
+ // Create the dictionaries folder if it doesn't exist
45
+ await mkdir(resolve(dictionariesDir), { recursive: true });
46
+
47
+ for await (const contentDeclarationPath of contentDeclarationsPaths) {
48
+ const result = await processContentDeclaration(contentDeclarationPath);
49
+
50
+ if (!result) {
51
+ continue;
52
+ }
53
+
54
+ const nestedContent: ContentModule[] = extractObjectsWithId(result);
55
+
56
+ const dictionariesPaths: string[] = await buildDictionary(nestedContent);
57
+
58
+ resultDictionariesPaths.push(...dictionariesPaths);
59
+ }
60
+
61
+ return resultDictionariesPaths;
62
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,26 @@
1
+ import crypto from 'crypto-js';
2
+
3
+ export const getFileHash = (filePath: string) => {
4
+ const hash = crypto.SHA3(filePath);
5
+
6
+ return hash
7
+ .toString(crypto.enc.Base64)
8
+ .replace(/[^A-Z\d]/gi, '')
9
+ .substring(0, 20);
10
+ };
11
+
12
+ export const transformToCamelCase = (string: string): string => {
13
+ // Split the string into words using a regex that finds spaces, hyphens, and underscores
14
+ const words = string.split(/[\s\-_]+/);
15
+
16
+ // Transform each word except the first to have its first letter uppercase
17
+ const camelCasedWords = words.map((word, index) => {
18
+ if (index === 0) {
19
+ return word[0].toUpperCase() + word.slice(1);
20
+ }
21
+ return word.charAt(0).toUpperCase() + word.slice(1);
22
+ });
23
+
24
+ // Join the words back together
25
+ return camelCasedWords.join('');
26
+ };