@intlayer/chokidar 2.0.0 → 2.0.2
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.
- package/dist/cjs/chokidar/watcher.cjs +11 -5
- package/dist/cjs/chokidar/watcher.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.cjs +4 -2
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.cjs +3 -3
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.d.ts +2 -2
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.d.ts +2 -2
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.cjs +8 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.d.ts +3 -3
- package/dist/cjs/transpiler/dictionary_to_type/createType.cjs +42 -76
- package/dist/cjs/transpiler/dictionary_to_type/createType.cjs.map +1 -1
- package/dist/cjs/transpiler/dictionary_to_type/createType.d.ts +3 -38
- package/dist/cjs/transpiler/dictionary_to_type/index.d.ts +1 -2
- package/dist/esm/chokidar/watcher.mjs +11 -5
- package/dist/esm/chokidar/watcher.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.mjs +4 -2
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.mjs +3 -3
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.d.mts +2 -2
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.d.mts +2 -2
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.d.mts +3 -3
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.mjs +8 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.mjs.map +1 -1
- package/dist/esm/transpiler/dictionary_to_type/createType.d.mts +3 -38
- package/dist/esm/transpiler/dictionary_to_type/createType.mjs +44 -75
- package/dist/esm/transpiler/dictionary_to_type/createType.mjs.map +1 -1
- package/dist/esm/transpiler/dictionary_to_type/index.d.mts +1 -2
- package/package.json +12 -13
- package/src/chokidar/index.ts +0 -1
- package/src/chokidar/watcher.ts +0 -75
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.ts +0 -95
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.ts +0 -91
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.ts +0 -22
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.ts +0 -1
- package/src/transpiler/declaration_file_to_dictionary/index.ts +0 -21
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.ts +0 -70
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.ts +0 -60
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.ts +0 -3
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.ts +0 -114
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.ts +0 -65
- package/src/transpiler/dictionary_to_main/createDictionaryList.ts +0 -65
- package/src/transpiler/dictionary_to_main/index.ts +0 -1
- package/src/transpiler/dictionary_to_type/createModuleAugmentation.ts +0 -102
- package/src/transpiler/dictionary_to_type/createType.ts +0 -162
- package/src/transpiler/dictionary_to_type/index.ts +0 -2
- package/src/utils.ts +0 -26
package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,91 +0,0 @@
|
|
|
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
|
-
};
|
package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './buildI18nDictionary';
|
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
};
|
package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
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 './processContentDeclaration';
|
|
7
|
-
|
|
8
|
-
const { content } = getConfiguration();
|
|
9
|
-
const { dictionariesDir } = content;
|
|
10
|
-
|
|
11
|
-
const writeDictionary = 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 buildIntlayerDictionary = 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 contentWithFilePath: ContentModule[] = nestedContent.map(
|
|
57
|
-
(content) => ({
|
|
58
|
-
...content,
|
|
59
|
-
filePath: contentDeclarationPath,
|
|
60
|
-
})
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
const dictionariesPaths: string[] =
|
|
64
|
-
await writeDictionary(contentWithFilePath);
|
|
65
|
-
|
|
66
|
-
resultDictionariesPaths.push(...dictionariesPaths);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return resultDictionariesPaths;
|
|
70
|
-
};
|
package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
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.hasOwn(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
|
-
};
|
package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,65 +0,0 @@
|
|
|
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
|
-
} else if (typeof field === 'function') {
|
|
26
|
-
// Wait for the function to resolve if it's an async function
|
|
27
|
-
const promise = (async () => {
|
|
28
|
-
// Execute the function and await the result if it's a Promise
|
|
29
|
-
const value = await field();
|
|
30
|
-
|
|
31
|
-
result[key] = value as FlatContentValue;
|
|
32
|
-
})();
|
|
33
|
-
promises.push(promise);
|
|
34
|
-
} else {
|
|
35
|
-
result[key] = field as FlatContentValue;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Wait for all async operations to complete
|
|
40
|
-
await Promise.all(promises);
|
|
41
|
-
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return entry;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Function to load, process the module and return the Intlayer ContentModule from the module file
|
|
50
|
-
*/
|
|
51
|
-
export const processContentDeclaration = async (file: string) => {
|
|
52
|
-
try {
|
|
53
|
-
const functionPath = resolve(file);
|
|
54
|
-
const entry = loadContentDeclaration(functionPath);
|
|
55
|
-
|
|
56
|
-
if (!entry) {
|
|
57
|
-
console.error('No entry found in module:', functionPath);
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return (await processFunctionResults(entry)) as ContentModule;
|
|
62
|
-
} catch (error) {
|
|
63
|
-
console.error('Error processing module:', error);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
@@ -1,65 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './createDictionaryList';
|