@intlayer/chokidar 2.0.0 → 2.0.1
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/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.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 +38 -52
- package/dist/cjs/transpiler/dictionary_to_type/createType.cjs.map +1 -1
- package/dist/cjs/transpiler/dictionary_to_type/createType.d.ts +3 -3
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.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 -3
- package/dist/esm/transpiler/dictionary_to_type/createType.mjs +38 -52
- package/dist/esm/transpiler/dictionary_to_type/createType.mjs.map +1 -1
- package/package.json +4 -6
- 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
|
@@ -6,17 +6,17 @@ import {
|
|
|
6
6
|
NodeType
|
|
7
7
|
} from "@intlayer/core";
|
|
8
8
|
import { getTypeName } from './createModuleAugmentation.mjs';
|
|
9
|
-
const { content
|
|
9
|
+
const { content } = getConfiguration();
|
|
10
10
|
const { typesDir } = content;
|
|
11
11
|
const isESModule = typeof import.meta.url === "string";
|
|
12
12
|
const requireFunction = isESModule ? createRequire(import.meta.url) : require;
|
|
13
|
+
const getFirstValue = (obj) => Object.values(obj)[0];
|
|
13
14
|
const generateTypeScriptType = (obj) => {
|
|
14
15
|
let typeDefinition = ``;
|
|
15
16
|
const typeName = getTypeName(obj.id);
|
|
16
|
-
typeDefinition += `export type ${typeName} =
|
|
17
|
-
`;
|
|
17
|
+
typeDefinition += `export type ${typeName} = `;
|
|
18
18
|
typeDefinition += generateTypeScriptTypeContent(obj);
|
|
19
|
-
typeDefinition += "
|
|
19
|
+
typeDefinition += ";\n\n";
|
|
20
20
|
return typeDefinition;
|
|
21
21
|
};
|
|
22
22
|
const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined";
|
|
@@ -28,54 +28,40 @@ const generateTypeScriptTypeContent = (obj) => {
|
|
|
28
28
|
if (isReactNodeValue) {
|
|
29
29
|
return `JSX.Element`;
|
|
30
30
|
}
|
|
31
|
-
|
|
31
|
+
if (
|
|
32
|
+
// Check if the value is a typed node
|
|
33
|
+
obj.nodeType === NodeType.Translation
|
|
34
|
+
) {
|
|
35
|
+
const { nodeType, ...content2 } = obj;
|
|
36
|
+
const languageValue = getFirstValue(
|
|
37
|
+
content2
|
|
38
|
+
);
|
|
39
|
+
const tsType = generateTypeScriptTypeContent(languageValue);
|
|
40
|
+
return `${tsType}`;
|
|
41
|
+
} else if (
|
|
42
|
+
// Check if the value is a typed node
|
|
43
|
+
obj.nodeType === NodeType.Enumeration
|
|
44
|
+
) {
|
|
45
|
+
const { nodeType, ...content2 } = obj;
|
|
46
|
+
const quantifiedValue = getFirstValue(
|
|
47
|
+
content2
|
|
48
|
+
);
|
|
49
|
+
const tsType = generateTypeScriptTypeContent(quantifiedValue);
|
|
50
|
+
return `(quantity: number) => ${tsType}`;
|
|
51
|
+
} else if (Array.isArray(obj)) {
|
|
52
|
+
const arrayType = generateTypeScriptTypeContent(obj[0]);
|
|
53
|
+
return `${arrayType}[]`;
|
|
54
|
+
}
|
|
55
|
+
let typeDefinition = "{";
|
|
32
56
|
for (const [key, value] of Object.entries(obj)) {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const tsType = generateTypeScriptTypeContent(
|
|
39
|
-
value?.[internationalization.defaultLocale]
|
|
40
|
-
);
|
|
41
|
-
typeDefinition += ` ${key}: ${tsType},
|
|
42
|
-
`;
|
|
43
|
-
} else if (
|
|
44
|
-
// Check if the value is a typed node
|
|
45
|
-
typeof value === "object" && nodeType === NodeType.Enumeration
|
|
46
|
-
) {
|
|
47
|
-
const tsType = generateTypeScriptTypeContent(
|
|
48
|
-
value?.[internationalization.defaultLocale]
|
|
49
|
-
);
|
|
50
|
-
typeDefinition += ` ${key}: (quantity: number) => ${tsType},
|
|
51
|
-
`;
|
|
52
|
-
} else if (
|
|
53
|
-
// Check if the value is a nested object
|
|
54
|
-
typeof value === "object"
|
|
55
|
-
) {
|
|
56
|
-
const isArray = Array.isArray(value);
|
|
57
|
-
if (isArray) {
|
|
58
|
-
const arrayType = generateTypeScriptTypeContent(value);
|
|
59
|
-
typeDefinition += ` ${key}: ${arrayType}[],
|
|
60
|
-
`;
|
|
61
|
-
} else {
|
|
62
|
-
const nestedType = generateTypeScriptTypeContent(value);
|
|
63
|
-
typeDefinition += ` ${key}: {${nestedType}},
|
|
64
|
-
`;
|
|
65
|
-
}
|
|
66
|
-
} else if (
|
|
67
|
-
// Check if the value is an 'id'
|
|
68
|
-
typeof value === "string" && key === "id"
|
|
69
|
-
) {
|
|
70
|
-
const tsType = `"${value}"`;
|
|
71
|
-
typeDefinition += ` ${key}: ${tsType},
|
|
72
|
-
`;
|
|
73
|
-
} else {
|
|
74
|
-
const tsType = typeof value;
|
|
75
|
-
typeDefinition += ` ${key}: ${tsType},
|
|
76
|
-
`;
|
|
57
|
+
const isLast = Object.keys(obj).indexOf(key) === Object.keys(obj).length - 1;
|
|
58
|
+
const nestedType = generateTypeScriptTypeContent(value);
|
|
59
|
+
typeDefinition += `'${key}': ${nestedType}`;
|
|
60
|
+
if (!isLast) {
|
|
61
|
+
typeDefinition += ",";
|
|
77
62
|
}
|
|
78
63
|
}
|
|
64
|
+
typeDefinition += "}";
|
|
79
65
|
return typeDefinition;
|
|
80
66
|
};
|
|
81
67
|
const createTypes = (dictionariesPaths) => {
|
|
@@ -84,9 +70,9 @@ const createTypes = (dictionariesPaths) => {
|
|
|
84
70
|
mkdirSync(typesDir, { recursive: true });
|
|
85
71
|
}
|
|
86
72
|
for (const dictionaryPath of dictionariesPaths) {
|
|
87
|
-
const
|
|
88
|
-
const dictionaryName =
|
|
89
|
-
const typeDefinition = generateTypeScriptType(
|
|
73
|
+
const dictionary = requireFunction(dictionaryPath);
|
|
74
|
+
const dictionaryName = dictionary.id;
|
|
75
|
+
const typeDefinition = generateTypeScriptType(dictionary);
|
|
90
76
|
const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);
|
|
91
77
|
writeFileSync(outputPath, typeDefinition);
|
|
92
78
|
resultTypesPaths.push(outputPath);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/createType.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { createRequire } from 'module';\nimport { resolve } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport {\n NodeType,\n type
|
|
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 Dictionary,\n type DictionaryValue,\n type TypedNode,\n} from '@intlayer/core';\nimport { getTypeName } from './createModuleAugmentation';\n\nconst { content } = getConfiguration();\nconst { typesDir } = content;\n\nconst isESModule = typeof import.meta.url === 'string';\nconst requireFunction = isESModule ? createRequire(import.meta.url) : require;\n\nconst getFirstValue = (obj: Record<string, DictionaryValue>): DictionaryValue =>\n Object.values(obj)[0];\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: Dictionary): string => {\n let typeDefinition = ``;\n\n const typeName = getTypeName(obj.id);\n\n typeDefinition += `export type ${typeName} = `;\n typeDefinition += generateTypeScriptTypeContent(obj as DictionaryValue);\n typeDefinition += ';\\n\\n';\n\n return typeDefinition;\n};\n\nconst isReactNode = (node: Record<string, unknown>): boolean =>\n typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';\n\n// eslint-disable-next-line sonarjs/cognitive-complexity\nexport const generateTypeScriptTypeContent = (obj: DictionaryValue): string => {\n if (typeof obj !== 'object' || obj === null) {\n return `${typeof obj}`;\n }\n\n const isReactNodeValue = isReactNode(obj as Record<string, unknown>);\n\n if (isReactNodeValue) {\n // ReactNode handling\n return `JSX.Element`;\n }\n\n if (\n // Check if the value is a typed node\n (obj as TypedNode).nodeType === NodeType.Translation\n ) {\n const { nodeType, ...content } = obj as TypedNode;\n\n const languageValue: DictionaryValue = getFirstValue(\n content as Record<string, DictionaryValue>\n );\n\n const tsType = generateTypeScriptTypeContent(languageValue);\n return `${tsType}`;\n } else if (\n // Check if the value is a typed node\n (obj as TypedNode).nodeType === NodeType.Enumeration\n ) {\n const { nodeType, ...content } = obj as TypedNode;\n\n const quantifiedValue: DictionaryValue = getFirstValue(\n content as Record<string, DictionaryValue>\n );\n\n const tsType = generateTypeScriptTypeContent(quantifiedValue);\n\n return `(quantity: number) => ${tsType}`;\n } else if (Array.isArray(obj)) {\n // Array handling (simplified, assumes non-empty arrays with uniform type)\n const arrayType = generateTypeScriptTypeContent(obj[0] as DictionaryValue);\n\n return `${arrayType}[]`;\n }\n\n let typeDefinition = '{';\n // Nested object, recurse\n for (const [key, value] of Object.entries(obj)) {\n const isLast =\n Object.keys(obj).indexOf(key) === Object.keys(obj).length - 1;\n\n const nestedType = generateTypeScriptTypeContent(value as DictionaryValue);\n\n typeDefinition += `'${key}': ${nestedType}`;\n\n if (!isLast) {\n typeDefinition += ',';\n }\n }\n typeDefinition += '}';\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 dictionary: Dictionary = requireFunction(dictionaryPath);\n const dictionaryName: string = dictionary.id;\n const typeDefinition: string = generateTypeScriptType(dictionary);\n\n const outputPath: string = resolve(typesDir, `${dictionaryName}.d.ts`);\n\n writeFileSync(outputPath, typeDefinition);\n\n resultTypesPaths.push(outputPath);\n }\n\n return resultTypesPaths;\n};\n"],"mappings":"AAAA,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,qBAAqB;AAC9B,SAAS,eAAe;AACxB,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,OAIK;AACP,SAAS,mBAAmB;AAE5B,MAAM,EAAE,QAAQ,IAAI,iBAAiB;AACrC,MAAM,EAAE,SAAS,IAAI;AAErB,MAAM,aAAa,OAAO,YAAY,QAAQ;AAC9C,MAAM,kBAAkB,aAAa,cAAc,YAAY,GAAG,IAAI;AAEtE,MAAM,gBAAgB,CAAC,QACrB,OAAO,OAAO,GAAG,EAAE,CAAC;AAkCf,MAAM,yBAAyB,CAAC,QAA4B;AACjE,MAAI,iBAAiB;AAErB,QAAM,WAAW,YAAY,IAAI,EAAE;AAEnC,oBAAkB,eAAe,QAAQ;AACzC,oBAAkB,8BAA8B,GAAsB;AACtE,oBAAkB;AAElB,SAAO;AACT;AAEA,MAAM,cAAc,CAAC,SACnB,OAAO,MAAM,QAAQ,eAAe,OAAO,MAAM,UAAU;AAGtD,MAAM,gCAAgC,CAAC,QAAiC;AAC7E,MAAI,OAAO,QAAQ,YAAY,QAAQ,MAAM;AAC3C,WAAO,GAAG,OAAO,GAAG;AAAA,EACtB;AAEA,QAAM,mBAAmB,YAAY,GAA8B;AAEnE,MAAI,kBAAkB;AAEpB,WAAO;AAAA,EACT;AAEA;AAAA;AAAA,IAEG,IAAkB,aAAa,SAAS;AAAA,IACzC;AACA,UAAM,EAAE,UAAU,GAAGA,SAAQ,IAAI;AAEjC,UAAM,gBAAiC;AAAA,MACrCA;AAAA,IACF;AAEA,UAAM,SAAS,8BAA8B,aAAa;AAC1D,WAAO,GAAG,MAAM;AAAA,EAClB;AAAA;AAAA,IAEG,IAAkB,aAAa,SAAS;AAAA,IACzC;AACA,UAAM,EAAE,UAAU,GAAGA,SAAQ,IAAI;AAEjC,UAAM,kBAAmC;AAAA,MACvCA;AAAA,IACF;AAEA,UAAM,SAAS,8BAA8B,eAAe;AAE5D,WAAO,yBAAyB,MAAM;AAAA,EACxC,WAAW,MAAM,QAAQ,GAAG,GAAG;AAE7B,UAAM,YAAY,8BAA8B,IAAI,CAAC,CAAoB;AAEzE,WAAO,GAAG,SAAS;AAAA,EACrB;AAEA,MAAI,iBAAiB;AAErB,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC9C,UAAM,SACJ,OAAO,KAAK,GAAG,EAAE,QAAQ,GAAG,MAAM,OAAO,KAAK,GAAG,EAAE,SAAS;AAE9D,UAAM,aAAa,8BAA8B,KAAwB;AAEzE,sBAAkB,IAAI,GAAG,MAAM,UAAU;AAEzC,QAAI,CAAC,QAAQ;AACX,wBAAkB;AAAA,IACpB;AAAA,EACF;AACA,oBAAkB;AAClB,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,aAAyB,gBAAgB,cAAc;AAC7D,UAAM,iBAAyB,WAAW;AAC1C,UAAM,iBAAyB,uBAAuB,UAAU;AAEhE,UAAM,aAAqB,QAAQ,UAAU,GAAG,cAAc,OAAO;AAErE,kBAAc,YAAY,cAAc;AAExC,qBAAiB,KAAK,UAAU;AAAA,EAClC;AAEA,SAAO;AACT;","names":["content"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/chokidar",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Chokidar application for IntLayer - Transpile IntLayer declaration files into dictionaries.",
|
|
6
6
|
"keywords": [
|
|
@@ -41,8 +41,6 @@
|
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"./dist",
|
|
44
|
-
"./src",
|
|
45
|
-
"./bin",
|
|
46
44
|
"./package.json"
|
|
47
45
|
],
|
|
48
46
|
"dependencies": {
|
|
@@ -53,9 +51,9 @@
|
|
|
53
51
|
"glob": "^10.3.12",
|
|
54
52
|
"node-loader": "^2.0.0",
|
|
55
53
|
"rimraf": "5.0.5",
|
|
56
|
-
"@intlayer/config": "^2.0.
|
|
57
|
-
"@intlayer/core": "^2.0.
|
|
58
|
-
"intlayer": "^2.0.
|
|
54
|
+
"@intlayer/config": "^2.0.1",
|
|
55
|
+
"@intlayer/core": "^2.0.1",
|
|
56
|
+
"intlayer": "^2.0.1"
|
|
59
57
|
},
|
|
60
58
|
"devDependencies": {
|
|
61
59
|
"@changesets/cli": "2.27.1",
|
package/src/chokidar/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './watcher';
|
package/src/chokidar/watcher.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { relative } from 'path';
|
|
2
|
-
import { getConfiguration } from '@intlayer/config';
|
|
3
|
-
import chokidar, { type WatchOptions } from 'chokidar';
|
|
4
|
-
import { sync } from 'glob';
|
|
5
|
-
import { buildDictionary } from '../transpiler/declaration_file_to_dictionary/index';
|
|
6
|
-
import { createDictionaryList } from '../transpiler/dictionary_to_main/createDictionaryList';
|
|
7
|
-
import {
|
|
8
|
-
createTypes,
|
|
9
|
-
createModuleAugmentation,
|
|
10
|
-
} from '../transpiler/dictionary_to_type/index';
|
|
11
|
-
|
|
12
|
-
// Initialize chokidar watcher (non-persistent)
|
|
13
|
-
export const watch = (options?: WatchOptions) => {
|
|
14
|
-
const { content } = getConfiguration({
|
|
15
|
-
verbose: true,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const { watchedFilesPatternWithPath, baseDir } = content;
|
|
19
|
-
|
|
20
|
-
const files: string[] = sync(watchedFilesPatternWithPath);
|
|
21
|
-
|
|
22
|
-
return chokidar
|
|
23
|
-
.watch(watchedFilesPatternWithPath, {
|
|
24
|
-
persistent: true, // Make the watcher persistent
|
|
25
|
-
ignoreInitial: true, // Process existing files
|
|
26
|
-
...options,
|
|
27
|
-
})
|
|
28
|
-
.on('ready', async () => {
|
|
29
|
-
const dictionariesPaths = await buildDictionary(files);
|
|
30
|
-
|
|
31
|
-
console.info('Building Intlayer types...');
|
|
32
|
-
createTypes(dictionariesPaths);
|
|
33
|
-
|
|
34
|
-
console.info('Building Intlayer module augmentation...');
|
|
35
|
-
createModuleAugmentation();
|
|
36
|
-
|
|
37
|
-
console.info('Building Intlayer dictionary list...');
|
|
38
|
-
createDictionaryList();
|
|
39
|
-
|
|
40
|
-
const relativeDictionariesPath = dictionariesPaths.map((dictionary) =>
|
|
41
|
-
relative(baseDir, dictionary)
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
console.info('Dictionaries:', relativeDictionariesPath);
|
|
45
|
-
})
|
|
46
|
-
.on('unlink', (filePath) => {
|
|
47
|
-
// Process the file with the functionToRun
|
|
48
|
-
console.info('Removed file detected: ', relative(baseDir, filePath));
|
|
49
|
-
})
|
|
50
|
-
.on('add', async (filePath) => {
|
|
51
|
-
// Process the file with the functionToRun
|
|
52
|
-
console.info('Additional file detected: ', relative(baseDir, filePath));
|
|
53
|
-
const dictionaries = await buildDictionary(filePath);
|
|
54
|
-
|
|
55
|
-
console.info('Building TypeScript types...');
|
|
56
|
-
createTypes(dictionaries);
|
|
57
|
-
|
|
58
|
-
console.info('Building type index...');
|
|
59
|
-
createModuleAugmentation();
|
|
60
|
-
|
|
61
|
-
console.info('Building main...');
|
|
62
|
-
createDictionaryList();
|
|
63
|
-
})
|
|
64
|
-
.on('change', async (filePath) => {
|
|
65
|
-
// Process the file with the functionToRun
|
|
66
|
-
console.info('Change detected: ', relative(baseDir, filePath));
|
|
67
|
-
const dictionaries = await buildDictionary(filePath);
|
|
68
|
-
|
|
69
|
-
console.info('Building TypeScript types...');
|
|
70
|
-
createTypes(dictionaries);
|
|
71
|
-
})
|
|
72
|
-
.on('error', (error) => {
|
|
73
|
-
console.error('Watcher error:', error);
|
|
74
|
-
});
|
|
75
|
-
};
|
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
|
-
};
|