@intlayer/chokidar 1.2.0 → 2.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.
- package/dist/cjs/chokidar/watcher.cjs +4 -4
- package/dist/cjs/chokidar/watcher.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.cjs +80 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.cjs.map +1 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.d.ts +6 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.cjs +86 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.cjs.map +1 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.d.ts +8 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.cjs +50 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.cjs.map +1 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.d.ts +3 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.cjs +23 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.cjs.map +1 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.d.ts +1 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/index.cjs +43 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/index.cjs.map +1 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/index.d.ts +3 -0
- package/dist/cjs/transpiler/{intlater_module_to_dictionary/transpileContentDeclaration.cjs → declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.cjs} +17 -11
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.cjs.map +1 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.d.ts +6 -0
- package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.cjs +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.cjs.map +1 -0
- package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/index.cjs +7 -7
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.cjs.map +1 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.d.ts +4 -0
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.cjs.map +1 -0
- package/dist/cjs/transpiler/{intlater_module_to_dictionary/processModule.cjs → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.cjs} +5 -6
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.cjs.map +1 -0
- package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs +12 -9
- package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs.map +1 -1
- package/dist/cjs/transpiler/dictionary_to_type/createType.cjs +24 -12
- package/dist/cjs/transpiler/dictionary_to_type/createType.cjs.map +1 -1
- package/dist/esm/chokidar/watcher.mjs +4 -4
- package/dist/esm/chokidar/watcher.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.d.mts +6 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.mjs +61 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.mjs.map +1 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.d.mts +8 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.mjs +64 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.mjs.map +1 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.d.mts +3 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.mjs +26 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.mjs.map +1 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.d.mts +1 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.mjs +2 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.mjs.map +1 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/index.d.mts +3 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/index.mjs +19 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/index.mjs.map +1 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.d.mts +6 -0
- package/dist/esm/transpiler/{intlater_module_to_dictionary/transpileContentDeclaration.mjs → declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.mjs} +12 -6
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.mjs.map +1 -0
- package/dist/esm/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.mjs +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.mjs.map +1 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.d.mts +4 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.mjs +4 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.mjs.map +1 -0
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.mjs.map +1 -0
- package/dist/esm/transpiler/{intlater_module_to_dictionary/processModule.mjs → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.mjs} +2 -3
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.mjs.map +1 -0
- package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs +12 -9
- package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs.map +1 -1
- package/dist/esm/transpiler/dictionary_to_type/createType.mjs +24 -12
- package/dist/esm/transpiler/dictionary_to_type/createType.mjs.map +1 -1
- package/package.json +4 -4
- package/src/chokidar/watcher.ts +5 -14
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.ts +95 -0
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.ts +91 -0
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.ts +22 -0
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.ts +1 -0
- package/src/transpiler/declaration_file_to_dictionary/index.ts +21 -0
- package/src/transpiler/{intlater_module_to_dictionary/transpileContentDeclaration.ts → declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.ts} +12 -4
- package/src/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.ts +1 -1
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.ts +3 -0
- package/src/transpiler/{intlater_module_to_dictionary/processModule.ts → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.ts} +2 -3
- package/src/transpiler/dictionary_to_type/createModuleAugmentation.ts +21 -14
- package/src/transpiler/dictionary_to_type/createType.ts +36 -19
- package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.cjs.map +0 -1
- package/dist/cjs/transpiler/intlater_module_to_dictionary/index.cjs.map +0 -1
- package/dist/cjs/transpiler/intlater_module_to_dictionary/index.d.ts +0 -4
- package/dist/cjs/transpiler/intlater_module_to_dictionary/loadContentDeclaration.cjs.map +0 -1
- package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.cjs.map +0 -1
- package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.cjs.map +0 -1
- package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.d.ts +0 -6
- package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.mjs.map +0 -1
- package/dist/esm/transpiler/intlater_module_to_dictionary/index.d.mts +0 -4
- package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs +0 -4
- package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs.map +0 -1
- package/dist/esm/transpiler/intlater_module_to_dictionary/loadContentDeclaration.mjs.map +0 -1
- package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.mjs.map +0 -1
- package/dist/esm/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.d.mts +0 -6
- package/dist/esm/transpiler/intlater_module_to_dictionary/transpileContentDeclaration.mjs.map +0 -1
- package/src/transpiler/intlater_module_to_dictionary/index.ts +0 -3
- /package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.d.ts +0 -0
- /package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.cjs +0 -0
- /package/dist/cjs/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.d.ts +0 -0
- /package/dist/cjs/transpiler/{intlater_module_to_dictionary/processModule.d.ts → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.d.ts} +0 -0
- /package/dist/esm/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/extractNestedJSON.d.mts +0 -0
- /package/dist/esm/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.d.mts +0 -0
- /package/dist/esm/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.mjs +0 -0
- /package/dist/esm/transpiler/{intlater_module_to_dictionary/processModule.d.mts → declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.d.mts} +0 -0
- /package/src/transpiler/{intlater_module_to_dictionary → declaration_file_to_dictionary/intlayer_dictionary}/loadContentDeclaration.ts +0 -0
|
@@ -29,10 +29,10 @@ var import_glob = require("glob");
|
|
|
29
29
|
var import_utils = require('../../utils.cjs');
|
|
30
30
|
const { content, internationalization } = (0, import_config.getConfiguration)();
|
|
31
31
|
const { typesDir, moduleAugmentationDir } = content;
|
|
32
|
-
const { locales } = internationalization;
|
|
32
|
+
const { locales, strictMode } = internationalization;
|
|
33
33
|
const getTypeName = (id) => (0, import_utils.transformToCamelCase)(`${id}Content`);
|
|
34
34
|
const generateTypeIndexContent = (typeFiles) => {
|
|
35
|
-
let content2 = "/* eslint-disable */\nimport
|
|
35
|
+
let content2 = "/* eslint-disable */\nimport { Locales } from 'intlayer'\n";
|
|
36
36
|
const dictionariesRef = typeFiles.map((dictionaryPath) => ({
|
|
37
37
|
relativePath: (0, import_path.relative)(moduleAugmentationDir, dictionaryPath),
|
|
38
38
|
id: (0, import_path.basename)(dictionaryPath, ".d.ts"),
|
|
@@ -50,10 +50,11 @@ const generateTypeIndexContent = (typeFiles) => {
|
|
|
50
50
|
const formatLocales = locales.map((locale) => {
|
|
51
51
|
for (const key in import_config.Locales) {
|
|
52
52
|
if (import_config.Locales[key] === locale) {
|
|
53
|
-
return `
|
|
53
|
+
return `Locales.${key}`;
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
-
}).join("
|
|
56
|
+
}).join(" | ");
|
|
57
|
+
const strictModeRecord = strictMode === "strict" ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content> {}` : strictMode === "required_only" ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}` : `interface IConfigLocales<Content> extends Partial<Record<Locales, Content>> {}`;
|
|
57
58
|
content2 += `declare module 'intlayer' {
|
|
58
59
|
`;
|
|
59
60
|
content2 += ` interface IntlayerDictionaryTypesConnector {
|
|
@@ -61,14 +62,16 @@ ${formattedDictionaryMap}
|
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
`;
|
|
64
|
-
content2 += `
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
content2 += ` type ConfigLocales = ${formatLocales};
|
|
66
|
+
`;
|
|
67
|
+
content2 += ` type ExtractedLocales = Extract<Locales, ConfigLocales>;
|
|
68
|
+
`;
|
|
69
|
+
content2 += ` type ExcludedLocales = Exclude<Locales, ConfigLocales>;
|
|
67
70
|
|
|
68
71
|
`;
|
|
69
|
-
content2 += `
|
|
72
|
+
content2 += ` ${strictModeRecord}
|
|
70
73
|
`;
|
|
71
|
-
content2 += `}
|
|
74
|
+
content2 += `}`;
|
|
72
75
|
return content2;
|
|
73
76
|
};
|
|
74
77
|
const createModuleAugmentation = () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/createModuleAugmentation.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { basename, join, relative } from 'path';\nimport { Locales, getConfiguration } from '@intlayer/config';\nimport { sync } from 'glob';\nimport { getFileHash, transformToCamelCase } from '../../utils';\n\nconst { content, internationalization } = getConfiguration();\nconst { typesDir, moduleAugmentationDir } = content;\nconst { locales } = internationalization;\n\nexport const getTypeName = (id: string): string =>\n transformToCamelCase(`${id}Content`);\n\n/**\n * This function generates the content of the module augmentation file\n */\nconst generateTypeIndexContent = (typeFiles: string[]): string => {\n let content
|
|
1
|
+
{"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/createModuleAugmentation.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { basename, join, relative } from 'path';\nimport { Locales, getConfiguration } from '@intlayer/config';\nimport { sync } from 'glob';\nimport { getFileHash, transformToCamelCase } from '../../utils';\n\nconst { content, internationalization } = getConfiguration();\nconst { typesDir, moduleAugmentationDir } = content;\nconst { locales, strictMode } = internationalization;\n\nexport const getTypeName = (id: string): string =>\n transformToCamelCase(`${id}Content`);\n\n/**\n * This function generates the content of the module augmentation file\n */\nconst generateTypeIndexContent = (typeFiles: string[]): string => {\n let content = \"/* eslint-disable */\\nimport { Locales } from 'intlayer'\\n\";\n\n const dictionariesRef = typeFiles.map((dictionaryPath) => ({\n relativePath: relative(moduleAugmentationDir, dictionaryPath),\n id: basename(dictionaryPath, '.d.ts'), // Get the base name as the dictionary id\n hash: `_${getFileHash(dictionaryPath)}`, // Get the hash of the dictionary to avoid conflicts\n }));\n\n // Import all dictionaries\n dictionariesRef.forEach((dictionary) => {\n const typeName = getTypeName(dictionary.id);\n content += `import type { ${typeName} as ${dictionary.hash} } from '${dictionary.relativePath}';\\n`;\n });\n\n content += '\\n';\n\n // Format Dictionary Map\n const formattedDictionaryMap: string = dictionariesRef\n .map((dictionary) => ` \"${dictionary.id}\": ${dictionary.hash};`)\n .join('\\n');\n\n const formatLocales = locales\n .map((locale) => {\n for (const key in Locales) {\n if (Locales[key as keyof typeof Locales] === locale) {\n return `Locales.${key}`;\n }\n }\n })\n .join(' | ');\n\n const strictModeRecord =\n strictMode === 'strict'\n ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content> {}`\n : strictMode === 'required_only'\n ? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}`\n : `interface IConfigLocales<Content> extends Partial<Record<Locales, Content>> {}`;\n\n /**\n * Write the module augmentation to extend the intlayer module with the dictionaries types\n * Will suggest the type resulting of the dictionaries\n *\n * declare module 'intlayer' {\n * interface IntlayerDictionaryTypesConnector = {\n * dictionaries: {\n * id: DictionaryType;\n * }\n * }\n *\n * type ConfigLocales = Locales.ENGLISH | Locales.FRENCH | Locales.SPANISH;\n * type ExtractedLocales = Extract<Locales, ConfigLocales>;\n * type ExcludedLocales = Exclude<Locales, ConfigLocales>;\n *\n * interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}\n *\n *\n * }\n * See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation\n */\n content += `declare module 'intlayer' {\\n`;\n content += ` interface IntlayerDictionaryTypesConnector {\\n${formattedDictionaryMap}\\n }\\n\\n`;\n content += ` type ConfigLocales = ${formatLocales};\\n`;\n content += ` type ExtractedLocales = Extract<Locales, ConfigLocales>;\\n`;\n content += ` type ExcludedLocales = Exclude<Locales, ConfigLocales>;\\n\\n`;\n content += ` ${strictModeRecord}\\n`;\n content += `}`;\n\n return content;\n};\n\n/**\n * This function generates a index file merging all the types\n */\nexport const createModuleAugmentation = () => {\n // Create main directory if it doesn't exist\n if (!existsSync(moduleAugmentationDir)) {\n mkdirSync(moduleAugmentationDir, { recursive: true });\n }\n\n const dictionaries: string[] = sync(`${typesDir}/**/*.d.ts`);\n // Create the dictionary list file\n\n const tsContent = generateTypeIndexContent(dictionaries);\n writeFileSync(join(moduleAugmentationDir, 'intlayer.d.ts'), tsContent);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAqD;AACrD,kBAAyC;AACzC,oBAA0C;AAC1C,kBAAqB;AACrB,mBAAkD;AAElD,MAAM,EAAE,SAAS,qBAAqB,QAAI,gCAAiB;AAC3D,MAAM,EAAE,UAAU,sBAAsB,IAAI;AAC5C,MAAM,EAAE,SAAS,WAAW,IAAI;AAEzB,MAAM,cAAc,CAAC,WAC1B,mCAAqB,GAAG,EAAE,SAAS;AAKrC,MAAM,2BAA2B,CAAC,cAAgC;AAChE,MAAIA,WAAU;AAEd,QAAM,kBAAkB,UAAU,IAAI,CAAC,oBAAoB;AAAA,IACzD,kBAAc,sBAAS,uBAAuB,cAAc;AAAA,IAC5D,QAAI,sBAAS,gBAAgB,OAAO;AAAA;AAAA,IACpC,MAAM,QAAI,0BAAY,cAAc,CAAC;AAAA;AAAA,EACvC,EAAE;AAGF,kBAAgB,QAAQ,CAAC,eAAe;AACtC,UAAM,WAAW,YAAY,WAAW,EAAE;AAC1C,IAAAA,YAAW,iBAAiB,QAAQ,OAAO,WAAW,IAAI,YAAY,WAAW,YAAY;AAAA;AAAA,EAC/F,CAAC;AAED,EAAAA,YAAW;AAGX,QAAM,yBAAiC,gBACpC,IAAI,CAAC,eAAe,QAAQ,WAAW,EAAE,MAAM,WAAW,IAAI,GAAG,EACjE,KAAK,IAAI;AAEZ,QAAM,gBAAgB,QACnB,IAAI,CAAC,WAAW;AACf,eAAW,OAAO,uBAAS;AACzB,UAAI,sBAAQ,GAA2B,MAAM,QAAQ;AACnD,eAAO,WAAW,GAAG;AAAA,MACvB;AAAA,IACF;AAAA,EACF,CAAC,EACA,KAAK,KAAK;AAEb,QAAM,mBACJ,eAAe,WACX,mFACA,eAAe,kBACb,8HACA;AAuBR,EAAAA,YAAW;AAAA;AACX,EAAAA,YAAW;AAAA,EAAmD,sBAAsB;AAAA;AAAA;AAAA;AACpF,EAAAA,YAAW,0BAA0B,aAAa;AAAA;AAClD,EAAAA,YAAW;AAAA;AACX,EAAAA,YAAW;AAAA;AAAA;AACX,EAAAA,YAAW,KAAK,gBAAgB;AAAA;AAChC,EAAAA,YAAW;AAEX,SAAOA;AACT;AAKO,MAAM,2BAA2B,MAAM;AAE5C,MAAI,KAAC,sBAAW,qBAAqB,GAAG;AACtC,6BAAU,uBAAuB,EAAE,WAAW,KAAK,CAAC;AAAA,EACtD;AAEA,QAAM,mBAAyB,kBAAK,GAAG,QAAQ,YAAY;AAG3D,QAAM,YAAY,yBAAyB,YAAY;AACvD,mCAAc,kBAAK,uBAAuB,eAAe,GAAG,SAAS;AACvE;","names":["content"]}
|
|
@@ -43,38 +43,50 @@ const generateTypeScriptType = (obj) => {
|
|
|
43
43
|
typeDefinition += "};\n\n";
|
|
44
44
|
return typeDefinition;
|
|
45
45
|
};
|
|
46
|
+
const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined";
|
|
46
47
|
const generateTypeScriptTypeContent = (obj) => {
|
|
48
|
+
if (typeof obj !== "object" || obj === null) {
|
|
49
|
+
return `${typeof obj}`;
|
|
50
|
+
}
|
|
51
|
+
const isReactNodeValue = isReactNode(obj);
|
|
52
|
+
if (isReactNodeValue) {
|
|
53
|
+
return `JSX.Element`;
|
|
54
|
+
}
|
|
47
55
|
let typeDefinition = ``;
|
|
48
56
|
for (const [key, value] of Object.entries(obj)) {
|
|
49
|
-
const nodeType = value
|
|
57
|
+
const nodeType = value?.nodeType;
|
|
50
58
|
if (
|
|
51
59
|
// Check if the value is a typed node
|
|
52
60
|
typeof value === "object" && nodeType === import_core.NodeType.Translation
|
|
53
61
|
) {
|
|
54
|
-
const tsType =
|
|
62
|
+
const tsType = generateTypeScriptTypeContent(
|
|
63
|
+
value?.[internationalization.defaultLocale]
|
|
64
|
+
);
|
|
55
65
|
typeDefinition += ` ${key}: ${tsType},
|
|
56
66
|
`;
|
|
57
67
|
} else if (
|
|
58
68
|
// Check if the value is a typed node
|
|
59
69
|
typeof value === "object" && nodeType === import_core.NodeType.Enumeration
|
|
60
70
|
) {
|
|
61
|
-
const tsType =
|
|
71
|
+
const tsType = generateTypeScriptTypeContent(
|
|
72
|
+
value?.[internationalization.defaultLocale]
|
|
73
|
+
);
|
|
62
74
|
typeDefinition += ` ${key}: (quantity: number) => ${tsType},
|
|
63
75
|
`;
|
|
64
76
|
} else if (
|
|
65
77
|
// Check if the value is a nested object
|
|
66
|
-
typeof value === "object"
|
|
78
|
+
typeof value === "object"
|
|
67
79
|
) {
|
|
68
|
-
const
|
|
69
|
-
|
|
80
|
+
const isArray = Array.isArray(value);
|
|
81
|
+
if (isArray) {
|
|
82
|
+
const arrayType = generateTypeScriptTypeContent(value);
|
|
83
|
+
typeDefinition += ` ${key}: ${arrayType}[],
|
|
70
84
|
`;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
) {
|
|
75
|
-
const arrayType = typeof value[0];
|
|
76
|
-
typeDefinition += ` ${key}: ${arrayType}[],
|
|
85
|
+
} else {
|
|
86
|
+
const nestedType = generateTypeScriptTypeContent(value);
|
|
87
|
+
typeDefinition += ` ${key}: {${nestedType}},
|
|
77
88
|
`;
|
|
89
|
+
}
|
|
78
90
|
} else if (
|
|
79
91
|
// Check if the value is an 'id'
|
|
80
92
|
typeof value === "string" && key === "id"
|
|
@@ -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 Content,\n type ContentModule,\n type TypedNode,\n} from '@intlayer/core';\nimport { getTypeName } from './createModuleAugmentation';\n\nconst { content, internationalization } = getConfiguration();\nconst { typesDir } = content;\n\nconst isESModule = typeof import.meta.url === 'string';\nconst requireFunction = isESModule ? createRequire(import.meta.url) : require;\n\n/**\n *\n * This function generates a TypeScript type definition from a JSON object\n *\n * Example:\n *\n * const input = {\n * id: '1',\n * name: 'John Doe',\n * address: {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * }\n * };\n *\n * const result = generateTypeScriptType(input, 'RootObject');\n * console.log(result);\n *\n * Output:\n *\n * type RootObject = {\n * id: '1',\n * name: string,\n * address: {\n * id: '2',\n * street: string,\n * city: string,\n * },\n * };\n *\n */\nexport const generateTypeScriptType = (obj: ContentModule): string => {\n let typeDefinition = ``;\n\n const typeName = getTypeName(obj.id);\n\n typeDefinition += `export type ${typeName} = {\\n`;\n typeDefinition += generateTypeScriptTypeContent(obj);\n typeDefinition += '};\\n\\n';\n\n return typeDefinition;\n};\n\nexport const generateTypeScriptTypeContent = (obj: Content): string => {\n let typeDefinition = ``;\n
|
|
1
|
+
{"version":3,"sources":["../../../../src/transpiler/dictionary_to_type/createType.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { createRequire } from 'module';\nimport { resolve } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport {\n NodeType,\n type Content,\n type ContentModule,\n type TypedNode,\n} from '@intlayer/core';\nimport { getTypeName } from './createModuleAugmentation';\n\nconst { content, internationalization } = getConfiguration();\nconst { typesDir } = content;\n\nconst isESModule = typeof import.meta.url === 'string';\nconst requireFunction = isESModule ? createRequire(import.meta.url) : require;\n\n/**\n *\n * This function generates a TypeScript type definition from a JSON object\n *\n * Example:\n *\n * const input = {\n * id: '1',\n * name: 'John Doe',\n * address: {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * }\n * };\n *\n * const result = generateTypeScriptType(input, 'RootObject');\n * console.log(result);\n *\n * Output:\n *\n * type RootObject = {\n * id: '1',\n * name: string,\n * address: {\n * id: '2',\n * street: string,\n * city: string,\n * },\n * };\n *\n */\nexport const generateTypeScriptType = (obj: ContentModule): string => {\n let typeDefinition = ``;\n\n const typeName = getTypeName(obj.id);\n\n typeDefinition += `export type ${typeName} = {\\n`;\n typeDefinition += generateTypeScriptTypeContent(obj);\n typeDefinition += '};\\n\\n';\n\n return typeDefinition;\n};\n\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: Content): 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 let typeDefinition = ``;\n for (const [key, value] of Object.entries(obj)) {\n const nodeType: NodeType | undefined = (value as TypedNode)?.nodeType;\n type ValueKey = keyof typeof value;\n\n if (\n // Check if the value is a typed node\n typeof value === 'object' &&\n nodeType === NodeType.Translation\n ) {\n const tsType = generateTypeScriptTypeContent(\n value?.[internationalization.defaultLocale as ValueKey]\n );\n typeDefinition += ` ${key}: ${tsType},\\n`;\n } else if (\n // Check if the value is a typed node\n typeof value === 'object' &&\n nodeType === NodeType.Enumeration\n ) {\n const tsType = generateTypeScriptTypeContent(\n value?.[internationalization.defaultLocale as ValueKey] as Content\n );\n\n typeDefinition += ` ${key}: (quantity: number) => ${tsType},\\n`;\n } else if (\n // Check if the value is a nested object\n typeof value === 'object'\n ) {\n const isArray = Array.isArray(value);\n\n if (isArray) {\n // Array handling (simplified, assumes non-empty arrays with uniform type)\n const arrayType = generateTypeScriptTypeContent(value as Content);\n\n typeDefinition += ` ${key}: ${arrayType}[],\\n`;\n } else {\n // Nested object, recurse\n const nestedType = generateTypeScriptTypeContent(value as Content);\n\n typeDefinition += ` ${key}: {${nestedType}},\\n`;\n }\n } else if (\n // Check if the value is an 'id'\n typeof value === 'string' &&\n key === 'id'\n ) {\n // Special handling for 'id' field\n const tsType = `\"${value}\"`;\n typeDefinition += ` ${key}: ${tsType},\\n`;\n } else {\n // Primitive type\n const tsType = typeof value;\n typeDefinition += ` ${key}: ${tsType},\\n`;\n }\n }\n\n return typeDefinition;\n};\n\n/**\n * This function generates a TypeScript type definition from a JSON object\n */\nexport const createTypes = (dictionariesPaths: string[]): string[] => {\n const resultTypesPaths: string[] = [];\n\n // Create type folders if they don't exist\n if (!existsSync(typesDir)) {\n mkdirSync(typesDir, { recursive: true });\n }\n\n for (const dictionaryPath of dictionariesPaths) {\n const contentModule: ContentModule = requireFunction(dictionaryPath);\n const dictionaryName: string = contentModule.id;\n const typeDefinition: string = generateTypeScriptType(contentModule);\n\n const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);\n\n writeFileSync(outputPath, typeDefinition);\n\n resultTypesPaths.push(outputPath);\n }\n\n return resultTypesPaths;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAqD;AACrD,oBAA8B;AAC9B,kBAAwB;AACxB,oBAAiC;AACjC,kBAKO;AACP,sCAA4B;AAV5B;AAYA,MAAM,EAAE,SAAS,qBAAqB,QAAI,gCAAiB;AAC3D,MAAM,EAAE,SAAS,IAAI;AAErB,MAAM,aAAa,OAAO,YAAY,QAAQ;AAC9C,MAAM,kBAAkB,iBAAa,6BAAc,YAAY,GAAG,IAAI;AAkC/D,MAAM,yBAAyB,CAAC,QAA+B;AACpE,MAAI,iBAAiB;AAErB,QAAM,eAAW,6CAAY,IAAI,EAAE;AAEnC,oBAAkB,eAAe,QAAQ;AAAA;AACzC,oBAAkB,8BAA8B,GAAG;AACnD,oBAAkB;AAElB,SAAO;AACT;AAEA,MAAM,cAAc,CAAC,SACnB,OAAO,MAAM,QAAQ,eAAe,OAAO,MAAM,UAAU;AAGtD,MAAM,gCAAgC,CAAC,QAAyB;AACrE,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,MAAI,iBAAiB;AACrB,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC9C,UAAM,WAAkC,OAAqB;AAG7D;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,aAAa,qBAAS;AAAA,MACtB;AACA,YAAM,SAAS;AAAA,QACb,QAAQ,qBAAqB,aAAyB;AAAA,MACxD;AACA,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,aAAa,qBAAS;AAAA,MACtB;AACA,YAAM,SAAS;AAAA,QACb,QAAQ,qBAAqB,aAAyB;AAAA,MACxD;AAEA,wBAAkB,KAAK,GAAG,2BAA2B,MAAM;AAAA;AAAA,IAC7D;AAAA;AAAA,MAEE,OAAO,UAAU;AAAA,MACjB;AACA,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAI,SAAS;AAEX,cAAM,YAAY,8BAA8B,KAAgB;AAEhE,0BAAkB,KAAK,GAAG,KAAK,SAAS;AAAA;AAAA,MAC1C,OAAO;AAEL,cAAM,aAAa,8BAA8B,KAAgB;AAEjE,0BAAkB,KAAK,GAAG,MAAM,UAAU;AAAA;AAAA,MAC5C;AAAA,IACF;AAAA;AAAA,MAEE,OAAO,UAAU,YACjB,QAAQ;AAAA,MACR;AAEA,YAAM,SAAS,IAAI,KAAK;AACxB,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC,OAAO;AAEL,YAAM,SAAS,OAAO;AACtB,wBAAkB,KAAK,GAAG,KAAK,MAAM;AAAA;AAAA,IACvC;AAAA,EACF;AAEA,SAAO;AACT;AAKO,MAAM,cAAc,CAAC,sBAA0C;AACpE,QAAM,mBAA6B,CAAC;AAGpC,MAAI,KAAC,sBAAW,QAAQ,GAAG;AACzB,6BAAU,UAAU,EAAE,WAAW,KAAK,CAAC;AAAA,EACzC;AAEA,aAAW,kBAAkB,mBAAmB;AAC9C,UAAM,gBAA+B,gBAAgB,cAAc;AACnE,UAAM,iBAAyB,cAAc;AAC7C,UAAM,iBAAyB,uBAAuB,aAAa;AAEnE,UAAM,iBAAa,qBAAQ,UAAU,GAAG,cAAc,OAAO;AAE7D,iCAAc,YAAY,cAAc;AAExC,qBAAiB,KAAK,UAAU;AAAA,EAClC;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -2,12 +2,12 @@ import { relative } from "path";
|
|
|
2
2
|
import { getConfiguration } from "@intlayer/config";
|
|
3
3
|
import chokidar from "chokidar";
|
|
4
4
|
import { sync } from "glob";
|
|
5
|
+
import { buildDictionary } from '../transpiler/declaration_file_to_dictionary/index.mjs';
|
|
5
6
|
import { createDictionaryList } from '../transpiler/dictionary_to_main/createDictionaryList.mjs';
|
|
6
7
|
import {
|
|
7
8
|
createTypes,
|
|
8
9
|
createModuleAugmentation
|
|
9
10
|
} from '../transpiler/dictionary_to_type/index.mjs';
|
|
10
|
-
import { transpileContentDeclaration } from '../transpiler/intlater_module_to_dictionary/transpileContentDeclaration.mjs';
|
|
11
11
|
const watch = (options) => {
|
|
12
12
|
const { content } = getConfiguration({
|
|
13
13
|
verbose: true
|
|
@@ -21,7 +21,7 @@ const watch = (options) => {
|
|
|
21
21
|
// Process existing files
|
|
22
22
|
...options
|
|
23
23
|
}).on("ready", async () => {
|
|
24
|
-
const dictionariesPaths = await
|
|
24
|
+
const dictionariesPaths = await buildDictionary(files);
|
|
25
25
|
console.info("Building Intlayer types...");
|
|
26
26
|
createTypes(dictionariesPaths);
|
|
27
27
|
console.info("Building Intlayer module augmentation...");
|
|
@@ -36,7 +36,7 @@ const watch = (options) => {
|
|
|
36
36
|
console.info("Removed file detected: ", relative(baseDir, filePath));
|
|
37
37
|
}).on("add", async (filePath) => {
|
|
38
38
|
console.info("Additional file detected: ", relative(baseDir, filePath));
|
|
39
|
-
const dictionaries = await
|
|
39
|
+
const dictionaries = await buildDictionary(filePath);
|
|
40
40
|
console.info("Building TypeScript types...");
|
|
41
41
|
createTypes(dictionaries);
|
|
42
42
|
console.info("Building type index...");
|
|
@@ -45,7 +45,7 @@ const watch = (options) => {
|
|
|
45
45
|
createDictionaryList();
|
|
46
46
|
}).on("change", async (filePath) => {
|
|
47
47
|
console.info("Change detected: ", relative(baseDir, filePath));
|
|
48
|
-
const dictionaries = await
|
|
48
|
+
const dictionaries = await buildDictionary(filePath);
|
|
49
49
|
console.info("Building TypeScript types...");
|
|
50
50
|
createTypes(dictionaries);
|
|
51
51
|
}).on("error", (error) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/chokidar/watcher.ts"],"sourcesContent":["import { relative } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport chokidar, { type WatchOptions } from 'chokidar';\nimport { sync } from 'glob';\nimport { createDictionaryList } from '../transpiler/dictionary_to_main/createDictionaryList';\nimport {\n createTypes,\n createModuleAugmentation,\n} from '../transpiler/dictionary_to_type/index';\
|
|
1
|
+
{"version":3,"sources":["../../../src/chokidar/watcher.ts"],"sourcesContent":["import { relative } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport chokidar, { type WatchOptions } from 'chokidar';\nimport { sync } from 'glob';\nimport { buildDictionary } from '../transpiler/declaration_file_to_dictionary/index';\nimport { createDictionaryList } from '../transpiler/dictionary_to_main/createDictionaryList';\nimport {\n createTypes,\n createModuleAugmentation,\n} from '../transpiler/dictionary_to_type/index';\n\n// Initialize chokidar watcher (non-persistent)\nexport const watch = (options?: WatchOptions) => {\n const { content } = getConfiguration({\n verbose: true,\n });\n\n const { watchedFilesPatternWithPath, baseDir } = content;\n\n const files: string[] = sync(watchedFilesPatternWithPath);\n\n return chokidar\n .watch(watchedFilesPatternWithPath, {\n persistent: true, // Make the watcher persistent\n ignoreInitial: true, // Process existing files\n ...options,\n })\n .on('ready', async () => {\n const dictionariesPaths = await buildDictionary(files);\n\n console.info('Building Intlayer types...');\n createTypes(dictionariesPaths);\n\n console.info('Building Intlayer module augmentation...');\n createModuleAugmentation();\n\n console.info('Building Intlayer dictionary list...');\n createDictionaryList();\n\n const relativeDictionariesPath = dictionariesPaths.map((dictionary) =>\n relative(baseDir, dictionary)\n );\n\n console.info('Dictionaries:', relativeDictionariesPath);\n })\n .on('unlink', (filePath) => {\n // Process the file with the functionToRun\n console.info('Removed file detected: ', relative(baseDir, filePath));\n })\n .on('add', async (filePath) => {\n // Process the file with the functionToRun\n console.info('Additional file detected: ', relative(baseDir, filePath));\n const dictionaries = await buildDictionary(filePath);\n\n console.info('Building TypeScript types...');\n createTypes(dictionaries);\n\n console.info('Building type index...');\n createModuleAugmentation();\n\n console.info('Building main...');\n createDictionaryList();\n })\n .on('change', async (filePath) => {\n // Process the file with the functionToRun\n console.info('Change detected: ', relative(baseDir, filePath));\n const dictionaries = await buildDictionary(filePath);\n\n console.info('Building TypeScript types...');\n createTypes(dictionaries);\n })\n .on('error', (error) => {\n console.error('Watcher error:', error);\n });\n};\n"],"mappings":"AAAA,SAAS,gBAAgB;AACzB,SAAS,wBAAwB;AACjC,OAAO,cAAqC;AAC5C,SAAS,YAAY;AACrB,SAAS,uBAAuB;AAChC,SAAS,4BAA4B;AACrC;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAGA,MAAM,QAAQ,CAAC,YAA2B;AAC/C,QAAM,EAAE,QAAQ,IAAI,iBAAiB;AAAA,IACnC,SAAS;AAAA,EACX,CAAC;AAED,QAAM,EAAE,6BAA6B,QAAQ,IAAI;AAEjD,QAAM,QAAkB,KAAK,2BAA2B;AAExD,SAAO,SACJ,MAAM,6BAA6B;AAAA,IAClC,YAAY;AAAA;AAAA,IACZ,eAAe;AAAA;AAAA,IACf,GAAG;AAAA,EACL,CAAC,EACA,GAAG,SAAS,YAAY;AACvB,UAAM,oBAAoB,MAAM,gBAAgB,KAAK;AAErD,YAAQ,KAAK,4BAA4B;AACzC,gBAAY,iBAAiB;AAE7B,YAAQ,KAAK,0CAA0C;AACvD,6BAAyB;AAEzB,YAAQ,KAAK,sCAAsC;AACnD,yBAAqB;AAErB,UAAM,2BAA2B,kBAAkB;AAAA,MAAI,CAAC,eACtD,SAAS,SAAS,UAAU;AAAA,IAC9B;AAEA,YAAQ,KAAK,iBAAiB,wBAAwB;AAAA,EACxD,CAAC,EACA,GAAG,UAAU,CAAC,aAAa;AAE1B,YAAQ,KAAK,2BAA2B,SAAS,SAAS,QAAQ,CAAC;AAAA,EACrE,CAAC,EACA,GAAG,OAAO,OAAO,aAAa;AAE7B,YAAQ,KAAK,8BAA8B,SAAS,SAAS,QAAQ,CAAC;AACtE,UAAM,eAAe,MAAM,gBAAgB,QAAQ;AAEnD,YAAQ,KAAK,8BAA8B;AAC3C,gBAAY,YAAY;AAExB,YAAQ,KAAK,wBAAwB;AACrC,6BAAyB;AAEzB,YAAQ,KAAK,kBAAkB;AAC/B,yBAAqB;AAAA,EACvB,CAAC,EACA,GAAG,UAAU,OAAO,aAAa;AAEhC,YAAQ,KAAK,qBAAqB,SAAS,SAAS,QAAQ,CAAC;AAC7D,UAAM,eAAe,MAAM,gBAAgB,QAAQ;AAEnD,YAAQ,KAAK,8BAA8B;AAC3C,gBAAY,YAAY;AAAA,EAC1B,CAAC,EACA,GAAG,SAAS,CAAC,UAAU;AACtB,YAAQ,MAAM,kBAAkB,KAAK;AAAA,EACvC,CAAC;AACL;","names":[]}
|
package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.mjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { getConfiguration } from "@intlayer/config";
|
|
4
|
+
import {
|
|
5
|
+
processContentDeclaration,
|
|
6
|
+
extractObjectsWithId
|
|
7
|
+
} from '../intlayer_dictionary/index.mjs';
|
|
8
|
+
import {
|
|
9
|
+
createI18nDictionaries
|
|
10
|
+
} from './convertContentDeclarationInto18nDictionaries.mjs';
|
|
11
|
+
const { content } = getConfiguration();
|
|
12
|
+
const { i18nDictionariesDir } = content;
|
|
13
|
+
const writeDictionary = async (dictionariesDeclaration) => {
|
|
14
|
+
const resultDictionariesPaths = [];
|
|
15
|
+
for (const [nameSpace, localContent] of Object.entries(
|
|
16
|
+
dictionariesDeclaration
|
|
17
|
+
)) {
|
|
18
|
+
for await (const [locale, content2] of Object.entries(localContent)) {
|
|
19
|
+
const contentString = JSON.stringify(content2);
|
|
20
|
+
const outputFileName = `${nameSpace}.json`;
|
|
21
|
+
const resultDirPath = resolve(i18nDictionariesDir, locale);
|
|
22
|
+
const resultFilePath = resolve(resultDirPath, outputFileName);
|
|
23
|
+
await mkdir(resultDirPath, { recursive: true });
|
|
24
|
+
await writeFile(resultFilePath, contentString, "utf8").catch((err) => {
|
|
25
|
+
console.error(`Error creating ${outputFileName}:`, err);
|
|
26
|
+
});
|
|
27
|
+
resultDictionariesPaths.push(resultFilePath);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return resultDictionariesPaths;
|
|
31
|
+
};
|
|
32
|
+
const buildI18nDictionary = async (contentDeclarationsPaths) => {
|
|
33
|
+
const resultDictionariesPaths = [];
|
|
34
|
+
if (typeof contentDeclarationsPaths === "string") {
|
|
35
|
+
contentDeclarationsPaths = [contentDeclarationsPaths];
|
|
36
|
+
}
|
|
37
|
+
for await (const contentDeclarationPath of contentDeclarationsPaths) {
|
|
38
|
+
const result = await processContentDeclaration(contentDeclarationPath);
|
|
39
|
+
if (!result) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const nestedContent = extractObjectsWithId(result);
|
|
43
|
+
const dictionariesDeclaration = nestedContent.reduce((acc, content2) => {
|
|
44
|
+
const id = content2.id;
|
|
45
|
+
const i18Content = createI18nDictionaries(content2);
|
|
46
|
+
return {
|
|
47
|
+
...acc,
|
|
48
|
+
[id]: i18Content
|
|
49
|
+
};
|
|
50
|
+
}, {});
|
|
51
|
+
const dictionariesPaths = await writeDictionary(
|
|
52
|
+
dictionariesDeclaration
|
|
53
|
+
);
|
|
54
|
+
resultDictionariesPaths.push(...dictionariesPaths);
|
|
55
|
+
}
|
|
56
|
+
return resultDictionariesPaths;
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
buildI18nDictionary
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=buildI18nDictionary.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.ts"],"sourcesContent":["import { mkdir, writeFile } from 'fs/promises';\nimport { resolve } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport type { ContentModule } from '@intlayer/core';\nimport {\n processContentDeclaration,\n extractObjectsWithId,\n} from '../intlayer_dictionary/index';\nimport {\n type I18nDictionariesOutput,\n createI18nDictionaries,\n} from './convertContentDeclarationInto18nDictionaries';\n\nconst { content } = getConfiguration();\nconst { i18nDictionariesDir } = content;\n\ntype DictionariesDeclaration = Record<string, I18nDictionariesOutput>;\n\n/**\n * This function writes the dictionaries to the file system\n */\nconst writeDictionary = async (\n dictionariesDeclaration: DictionariesDeclaration\n) => {\n const resultDictionariesPaths: string[] = [];\n\n for (const [nameSpace, localContent] of Object.entries(\n dictionariesDeclaration\n )) {\n for await (const [locale, content] of Object.entries(localContent)) {\n const contentString = JSON.stringify(content);\n\n const outputFileName = `${nameSpace}.json`;\n const resultDirPath = resolve(i18nDictionariesDir, locale);\n const resultFilePath = resolve(resultDirPath, outputFileName);\n\n // Create the dictionaries folder if it doesn't exist\n await mkdir(resultDirPath, { recursive: true });\n\n // Create the json file\n await writeFile(resultFilePath, contentString, 'utf8').catch((err) => {\n console.error(`Error creating ${outputFileName}:`, err);\n });\n\n resultDictionariesPaths.push(resultFilePath);\n }\n }\n\n return resultDictionariesPaths;\n};\n\n/**\n * This function transpile content declaration to i18n dictionaries\n */\nexport const buildI18nDictionary = async (\n contentDeclarationsPaths: string[] | string\n) => {\n const resultDictionariesPaths: string[] = [];\n\n if (typeof contentDeclarationsPaths === 'string') {\n contentDeclarationsPaths = [contentDeclarationsPaths];\n }\n\n for await (const contentDeclarationPath of contentDeclarationsPaths) {\n const result = await processContentDeclaration(contentDeclarationPath);\n\n if (!result) {\n continue;\n }\n\n const nestedContent: ContentModule[] = extractObjectsWithId(result);\n\n // Create dictionaries for each nested content and format them\n const dictionariesDeclaration: DictionariesDeclaration =\n nestedContent.reduce((acc, content) => {\n const id = content.id;\n const i18Content = createI18nDictionaries(content);\n\n return {\n ...acc,\n [id]: i18Content,\n };\n }, {});\n\n // Write the dictionaries to the file system\n const dictionariesPaths: string[] = await writeDictionary(\n dictionariesDeclaration\n );\n\n // Add the paths to the result\n resultDictionariesPaths.push(...dictionariesPaths);\n }\n\n return resultDictionariesPaths;\n};\n"],"mappings":"AAAA,SAAS,OAAO,iBAAiB;AACjC,SAAS,eAAe;AACxB,SAAS,wBAAwB;AAEjC;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EAEE;AAAA,OACK;AAEP,MAAM,EAAE,QAAQ,IAAI,iBAAiB;AACrC,MAAM,EAAE,oBAAoB,IAAI;AAOhC,MAAM,kBAAkB,OACtB,4BACG;AACH,QAAM,0BAAoC,CAAC;AAE3C,aAAW,CAAC,WAAW,YAAY,KAAK,OAAO;AAAA,IAC7C;AAAA,EACF,GAAG;AACD,qBAAiB,CAAC,QAAQA,QAAO,KAAK,OAAO,QAAQ,YAAY,GAAG;AAClE,YAAM,gBAAgB,KAAK,UAAUA,QAAO;AAE5C,YAAM,iBAAiB,GAAG,SAAS;AACnC,YAAM,gBAAgB,QAAQ,qBAAqB,MAAM;AACzD,YAAM,iBAAiB,QAAQ,eAAe,cAAc;AAG5D,YAAM,MAAM,eAAe,EAAE,WAAW,KAAK,CAAC;AAG9C,YAAM,UAAU,gBAAgB,eAAe,MAAM,EAAE,MAAM,CAAC,QAAQ;AACpE,gBAAQ,MAAM,kBAAkB,cAAc,KAAK,GAAG;AAAA,MACxD,CAAC;AAED,8BAAwB,KAAK,cAAc;AAAA,IAC7C;AAAA,EACF;AAEA,SAAO;AACT;AAKO,MAAM,sBAAsB,OACjC,6BACG;AACH,QAAM,0BAAoC,CAAC;AAE3C,MAAI,OAAO,6BAA6B,UAAU;AAChD,+BAA2B,CAAC,wBAAwB;AAAA,EACtD;AAEA,mBAAiB,0BAA0B,0BAA0B;AACnE,UAAM,SAAS,MAAM,0BAA0B,sBAAsB;AAErE,QAAI,CAAC,QAAQ;AACX;AAAA,IACF;AAEA,UAAM,gBAAiC,qBAAqB,MAAM;AAGlE,UAAM,0BACJ,cAAc,OAAO,CAAC,KAAKA,aAAY;AACrC,YAAM,KAAKA,SAAQ;AACnB,YAAM,aAAa,uBAAuBA,QAAO;AAEjD,aAAO;AAAA,QACL,GAAG;AAAA,QACH,CAAC,EAAE,GAAG;AAAA,MACR;AAAA,IACF,GAAG,CAAC,CAAC;AAGP,UAAM,oBAA8B,MAAM;AAAA,MACxC;AAAA,IACF;AAGA,4BAAwB,KAAK,GAAG,iBAAiB;AAAA,EACnD;AAEA,SAAO;AACT;","names":["content"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Locales } from '@intlayer/config';
|
|
2
|
+
import { Content } from '@intlayer/core';
|
|
3
|
+
|
|
4
|
+
type Dictionary = Record<string, unknown>;
|
|
5
|
+
type I18nDictionariesOutput = Partial<Record<Locales, Dictionary>>;
|
|
6
|
+
declare const createI18nDictionaries: (content: Content) => I18nDictionariesOutput;
|
|
7
|
+
|
|
8
|
+
export { type I18nDictionariesOutput, createI18nDictionaries };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { getConfiguration } from "@intlayer/config";
|
|
2
|
+
import {
|
|
3
|
+
NodeType
|
|
4
|
+
} from "@intlayer/core";
|
|
5
|
+
import { convertPluralsValues } from './convertPluralsValues.mjs';
|
|
6
|
+
const {
|
|
7
|
+
internationalization: { locales }
|
|
8
|
+
} = getConfiguration();
|
|
9
|
+
const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined" && typeof node?.type !== "undefined";
|
|
10
|
+
const buildDictionary = (content, locale) => {
|
|
11
|
+
if (
|
|
12
|
+
// Translation node
|
|
13
|
+
content && content.nodeType === NodeType.Translation
|
|
14
|
+
) {
|
|
15
|
+
const result = content[locale];
|
|
16
|
+
return buildDictionary(result, locale);
|
|
17
|
+
} else if (
|
|
18
|
+
// Translation node
|
|
19
|
+
content && content.nodeType === NodeType.Enumeration
|
|
20
|
+
) {
|
|
21
|
+
const plurals = {};
|
|
22
|
+
Object.keys(content).forEach((quantity) => {
|
|
23
|
+
const letterNumber = convertPluralsValues(quantity);
|
|
24
|
+
const value = content[quantity];
|
|
25
|
+
plurals[`${letterNumber}_${letterNumber}`] = buildDictionary(
|
|
26
|
+
value,
|
|
27
|
+
locale
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
return plurals;
|
|
31
|
+
} else if (
|
|
32
|
+
// React element node
|
|
33
|
+
isReactNode(content)
|
|
34
|
+
) {
|
|
35
|
+
return JSON.stringify(content);
|
|
36
|
+
} else if (
|
|
37
|
+
// Nested object
|
|
38
|
+
typeof content === "object"
|
|
39
|
+
) {
|
|
40
|
+
const result = {};
|
|
41
|
+
Object.keys(content).forEach((dictionaryValue) => {
|
|
42
|
+
result[dictionaryValue] = buildDictionary(
|
|
43
|
+
content[dictionaryValue],
|
|
44
|
+
locale
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
return content;
|
|
50
|
+
};
|
|
51
|
+
const createI18nDictionaries = (content) => {
|
|
52
|
+
const result = locales.reduce(
|
|
53
|
+
(acc, locale) => ({
|
|
54
|
+
...acc,
|
|
55
|
+
[locale]: buildDictionary(content, locale)
|
|
56
|
+
}),
|
|
57
|
+
{}
|
|
58
|
+
);
|
|
59
|
+
return result;
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
createI18nDictionaries
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=convertContentDeclarationInto18nDictionaries.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n NodeType,\n type TranslationContent,\n type Content,\n type TypedNode,\n type EnumerationContent,\n} from '@intlayer/core';\nimport { convertPluralsValues } from './convertPluralsValues';\n\ntype Dictionary = Record<string, unknown>;\nexport type I18nDictionariesOutput = Partial<Record<Locales, Dictionary>>;\n\nconst {\n internationalization: { locales },\n} = getConfiguration();\n\nconst isReactNode = (node: Record<string, unknown>): boolean =>\n typeof node?.key !== 'undefined' &&\n typeof node?.props !== 'undefined' &&\n typeof node?.type !== 'undefined';\n\n// Build dictionary for a specific locale\nconst buildDictionary = (content: Dictionary, locale: Locales): unknown => {\n if (\n // Translation node\n content &&\n (content as TypedNode).nodeType === NodeType.Translation\n ) {\n const result = (content as TranslationContent<unknown>)[locale];\n\n return buildDictionary(result as Dictionary, locale);\n } else if (\n // Translation node\n content &&\n (content as TypedNode).nodeType === NodeType.Enumeration\n ) {\n const plurals: Record<string, unknown> = {};\n\n Object.keys(content).forEach((quantity) => {\n const letterNumber = convertPluralsValues(quantity);\n\n const value = (content as EnumerationContent<unknown>)[\n quantity as keyof EnumerationContent<unknown>\n ];\n\n plurals[`${letterNumber}_${letterNumber}`] = buildDictionary(\n value as Dictionary,\n locale\n );\n });\n\n return plurals;\n } else if (\n // React element node\n isReactNode(content as Record<string, unknown>)\n ) {\n return JSON.stringify(content);\n } else if (\n // Nested object\n typeof content === 'object'\n ) {\n const result: Record<string, unknown> = {};\n\n Object.keys(content).forEach((dictionaryValue) => {\n result[dictionaryValue] = buildDictionary(\n content[dictionaryValue] as Dictionary,\n locale\n );\n });\n\n return result;\n }\n\n return content;\n};\n\nexport const createI18nDictionaries = (\n content: Content\n): I18nDictionariesOutput => {\n // Map dictionaries for each locale\n const result: I18nDictionariesOutput = locales.reduce(\n (acc, locale) => ({\n ...acc,\n [locale]: buildDictionary(content, locale),\n }),\n {}\n );\n\n return result;\n};\n"],"mappings":"AAAA,SAAS,wBAAsC;AAC/C;AAAA,EACE;AAAA,OAKK;AACP,SAAS,4BAA4B;AAKrC,MAAM;AAAA,EACJ,sBAAsB,EAAE,QAAQ;AAClC,IAAI,iBAAiB;AAErB,MAAM,cAAc,CAAC,SACnB,OAAO,MAAM,QAAQ,eACrB,OAAO,MAAM,UAAU,eACvB,OAAO,MAAM,SAAS;AAGxB,MAAM,kBAAkB,CAAC,SAAqB,WAA6B;AACzE;AAAA;AAAA,IAEE,WACC,QAAsB,aAAa,SAAS;AAAA,IAC7C;AACA,UAAM,SAAU,QAAwC,MAAM;AAE9D,WAAO,gBAAgB,QAAsB,MAAM;AAAA,EACrD;AAAA;AAAA,IAEE,WACC,QAAsB,aAAa,SAAS;AAAA,IAC7C;AACA,UAAM,UAAmC,CAAC;AAE1C,WAAO,KAAK,OAAO,EAAE,QAAQ,CAAC,aAAa;AACzC,YAAM,eAAe,qBAAqB,QAAQ;AAElD,YAAM,QAAS,QACb,QACF;AAEA,cAAQ,GAAG,YAAY,IAAI,YAAY,EAAE,IAAI;AAAA,QAC3C;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AAAA;AAAA,IAEE,YAAY,OAAkC;AAAA,IAC9C;AACA,WAAO,KAAK,UAAU,OAAO;AAAA,EAC/B;AAAA;AAAA,IAEE,OAAO,YAAY;AAAA,IACnB;AACA,UAAM,SAAkC,CAAC;AAEzC,WAAO,KAAK,OAAO,EAAE,QAAQ,CAAC,oBAAoB;AAChD,aAAO,eAAe,IAAI;AAAA,QACxB,QAAQ,eAAe;AAAA,QACvB;AAAA,MACF;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,MAAM,yBAAyB,CACpC,YAC2B;AAE3B,QAAM,SAAiC,QAAQ;AAAA,IAC7C,CAAC,KAAK,YAAY;AAAA,MAChB,GAAG;AAAA,MACH,CAAC,MAAM,GAAG,gBAAgB,SAAS,MAAM;AAAA,IAC3C;AAAA,IACA,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const convertPluralsValues = (number) => {
|
|
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
|
+
};
|
|
23
|
+
export {
|
|
24
|
+
convertPluralsValues
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=convertPluralsValues.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.ts"],"sourcesContent":["export const convertPluralsValues = (number: string): string => {\n switch (number) {\n case '1':\n return 'one';\n case '2':\n return 'two';\n case '3':\n return 'three';\n case '4':\n return 'four';\n case '5':\n return 'five';\n case '6':\n return 'six';\n case '7':\n return 'seven';\n case '8':\n return 'eight';\n default:\n return number.toString();\n }\n};\n"],"mappings":"AAAO,MAAM,uBAAuB,CAAC,WAA2B;AAC9D,UAAQ,QAAQ;AAAA,IACd,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT;AACE,aAAO,OAAO,SAAS;AAAA,EAC3B;AACF;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { buildI18nDictionary } from './buildI18nDictionary.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.ts"],"sourcesContent":["export * from './buildI18nDictionary';\n"],"mappings":"AAAA,cAAc;","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getConfiguration } from "@intlayer/config";
|
|
2
|
+
import { buildI18nDictionary } from './i18n_dictionary/index.mjs';
|
|
3
|
+
import { buildIntlayerDictionary } from './intlayer_dictionary/index.mjs';
|
|
4
|
+
const {
|
|
5
|
+
content: { dictionaryOutput }
|
|
6
|
+
} = getConfiguration();
|
|
7
|
+
const buildDictionary = async (contentDeclarationsPaths) => {
|
|
8
|
+
if (dictionaryOutput.includes("i18next")) {
|
|
9
|
+
return await buildI18nDictionary(contentDeclarationsPaths);
|
|
10
|
+
}
|
|
11
|
+
if (dictionaryOutput.includes("intlayer")) {
|
|
12
|
+
return await buildIntlayerDictionary(contentDeclarationsPaths);
|
|
13
|
+
}
|
|
14
|
+
return [];
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
buildDictionary
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/transpiler/declaration_file_to_dictionary/index.ts"],"sourcesContent":["import { getConfiguration } from '@intlayer/config';\nimport { buildI18nDictionary } from './i18n_dictionary/index';\nimport { buildIntlayerDictionary } from './intlayer_dictionary/index';\n\nconst {\n content: { dictionaryOutput },\n} = getConfiguration();\n\nexport const buildDictionary = async (\n contentDeclarationsPaths: string | string[]\n): Promise<string[]> => {\n if (dictionaryOutput.includes('i18next')) {\n return await buildI18nDictionary(contentDeclarationsPaths);\n }\n\n if (dictionaryOutput.includes('intlayer')) {\n return await buildIntlayerDictionary(contentDeclarationsPaths);\n }\n\n return [];\n};\n"],"mappings":"AAAA,SAAS,wBAAwB;AACjC,SAAS,2BAA2B;AACpC,SAAS,+BAA+B;AAExC,MAAM;AAAA,EACJ,SAAS,EAAE,iBAAiB;AAC9B,IAAI,iBAAiB;AAEd,MAAM,kBAAkB,OAC7B,6BACsB;AACtB,MAAI,iBAAiB,SAAS,SAAS,GAAG;AACxC,WAAO,MAAM,oBAAoB,wBAAwB;AAAA,EAC3D;AAEA,MAAI,iBAAiB,SAAS,UAAU,GAAG;AACzC,WAAO,MAAM,wBAAwB,wBAAwB;AAAA,EAC/D;AAEA,SAAO,CAAC;AACV;","names":[]}
|
|
@@ -2,10 +2,10 @@ import { mkdir, writeFile } from "fs/promises";
|
|
|
2
2
|
import { resolve } from "path";
|
|
3
3
|
import { getConfiguration } from "@intlayer/config";
|
|
4
4
|
import { extractObjectsWithId } from './extractNestedJSON.mjs';
|
|
5
|
-
import { processContentDeclaration } from './
|
|
5
|
+
import { processContentDeclaration } from './processContentDeclaration.mjs';
|
|
6
6
|
const { content } = getConfiguration();
|
|
7
7
|
const { dictionariesDir } = content;
|
|
8
|
-
const
|
|
8
|
+
const writeDictionary = async (dictionaries) => {
|
|
9
9
|
const resultDictionariesPaths = [];
|
|
10
10
|
for await (const content2 of dictionaries) {
|
|
11
11
|
const contentString = JSON.stringify(content2);
|
|
@@ -19,7 +19,7 @@ const buildDictionary = async (dictionaries) => {
|
|
|
19
19
|
}
|
|
20
20
|
return resultDictionariesPaths;
|
|
21
21
|
};
|
|
22
|
-
const
|
|
22
|
+
const buildIntlayerDictionary = async (contentDeclarationsPaths) => {
|
|
23
23
|
const resultDictionariesPaths = [];
|
|
24
24
|
if (typeof contentDeclarationsPaths === "string") {
|
|
25
25
|
contentDeclarationsPaths = [contentDeclarationsPaths];
|
|
@@ -31,12 +31,18 @@ const transpileContentDeclaration = async (contentDeclarationsPaths) => {
|
|
|
31
31
|
continue;
|
|
32
32
|
}
|
|
33
33
|
const nestedContent = extractObjectsWithId(result);
|
|
34
|
-
const
|
|
34
|
+
const contentWithFilePath = nestedContent.map(
|
|
35
|
+
(content2) => ({
|
|
36
|
+
...content2,
|
|
37
|
+
filePath: contentDeclarationPath
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
const dictionariesPaths = await writeDictionary(contentWithFilePath);
|
|
35
41
|
resultDictionariesPaths.push(...dictionariesPaths);
|
|
36
42
|
}
|
|
37
43
|
return resultDictionariesPaths;
|
|
38
44
|
};
|
|
39
45
|
export {
|
|
40
|
-
|
|
46
|
+
buildIntlayerDictionary
|
|
41
47
|
};
|
|
42
|
-
//# sourceMappingURL=
|
|
48
|
+
//# sourceMappingURL=buildIntlayerDictionary.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.ts"],"sourcesContent":["import { mkdir, writeFile } from 'fs/promises';\nimport { resolve } from 'path';\nimport { getConfiguration } from '@intlayer/config';\nimport type { ContentModule } from '@intlayer/core';\nimport { extractObjectsWithId } from './extractNestedJSON';\nimport { processContentDeclaration } from './processContentDeclaration';\n\nconst { content } = getConfiguration();\nconst { dictionariesDir } = content;\n\nconst writeDictionary = async (dictionaries: ContentModule[]) => {\n const resultDictionariesPaths: string[] = [];\n\n for await (const content of dictionaries) {\n const contentString = JSON.stringify(content);\n\n const id = content.id;\n const outputFileName = `${id}.json`;\n const resultFilePath = resolve(dictionariesDir, outputFileName);\n\n // Create the json file\n await writeFile(resultFilePath, contentString, 'utf8').catch((err) => {\n console.error(`Error creating ${outputFileName}:`, err);\n });\n\n resultDictionariesPaths.push(resultFilePath);\n }\n\n return resultDictionariesPaths;\n};\n\n/**\n * This function transpile the bundled code to to make dictionaries as JSON files\n */\nexport const buildIntlayerDictionary = async (\n contentDeclarationsPaths: string[] | string\n) => {\n const resultDictionariesPaths: string[] = [];\n\n if (typeof contentDeclarationsPaths === 'string') {\n contentDeclarationsPaths = [contentDeclarationsPaths];\n }\n\n // Create the dictionaries folder if it doesn't exist\n await mkdir(resolve(dictionariesDir), { recursive: true });\n\n for await (const contentDeclarationPath of contentDeclarationsPaths) {\n const result = await processContentDeclaration(contentDeclarationPath);\n\n if (!result) {\n continue;\n }\n\n const nestedContent: ContentModule[] = extractObjectsWithId(result);\n\n const contentWithFilePath: ContentModule[] = nestedContent.map(\n (content) => ({\n ...content,\n filePath: contentDeclarationPath,\n })\n );\n\n const dictionariesPaths: string[] =\n await writeDictionary(contentWithFilePath);\n\n resultDictionariesPaths.push(...dictionariesPaths);\n }\n\n return resultDictionariesPaths;\n};\n"],"mappings":"AAAA,SAAS,OAAO,iBAAiB;AACjC,SAAS,eAAe;AACxB,SAAS,wBAAwB;AAEjC,SAAS,4BAA4B;AACrC,SAAS,iCAAiC;AAE1C,MAAM,EAAE,QAAQ,IAAI,iBAAiB;AACrC,MAAM,EAAE,gBAAgB,IAAI;AAE5B,MAAM,kBAAkB,OAAO,iBAAkC;AAC/D,QAAM,0BAAoC,CAAC;AAE3C,mBAAiBA,YAAW,cAAc;AACxC,UAAM,gBAAgB,KAAK,UAAUA,QAAO;AAE5C,UAAM,KAAKA,SAAQ;AACnB,UAAM,iBAAiB,GAAG,EAAE;AAC5B,UAAM,iBAAiB,QAAQ,iBAAiB,cAAc;AAG9D,UAAM,UAAU,gBAAgB,eAAe,MAAM,EAAE,MAAM,CAAC,QAAQ;AACpE,cAAQ,MAAM,kBAAkB,cAAc,KAAK,GAAG;AAAA,IACxD,CAAC;AAED,4BAAwB,KAAK,cAAc;AAAA,EAC7C;AAEA,SAAO;AACT;AAKO,MAAM,0BAA0B,OACrC,6BACG;AACH,QAAM,0BAAoC,CAAC;AAE3C,MAAI,OAAO,6BAA6B,UAAU;AAChD,+BAA2B,CAAC,wBAAwB;AAAA,EACtD;AAGA,QAAM,MAAM,QAAQ,eAAe,GAAG,EAAE,WAAW,KAAK,CAAC;AAEzD,mBAAiB,0BAA0B,0BAA0B;AACnE,UAAM,SAAS,MAAM,0BAA0B,sBAAsB;AAErE,QAAI,CAAC,QAAQ;AACX;AAAA,IACF;AAEA,UAAM,gBAAiC,qBAAqB,MAAM;AAElE,UAAM,sBAAuC,cAAc;AAAA,MACzD,CAACA,cAAa;AAAA,QACZ,GAAGA;AAAA,QACH,UAAU;AAAA,MACZ;AAAA,IACF;AAEA,UAAM,oBACJ,MAAM,gBAAgB,mBAAmB;AAE3C,4BAAwB,KAAK,GAAG,iBAAiB;AAAA,EACnD;AAEA,SAAO;AACT;","names":["content"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const extractObjectsWithId = (input) => {
|
|
2
2
|
const search = (obj, results2) => {
|
|
3
3
|
if (obj && typeof obj === "object") {
|
|
4
|
-
if (Object.
|
|
4
|
+
if (Object.hasOwn(obj, "id")) {
|
|
5
5
|
results2.push(obj);
|
|
6
6
|
}
|
|
7
7
|
for (const key of Object.keys(obj)) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.ts"],"sourcesContent":["import type { Content, ContentModule } from '@intlayer/core';\n\n/**\n *\n * This function extracts all nested objects with an 'id' field from the input object and returns them as an array\n *\n * Example:\n *\n * const input = {\n * id: '1',\n * name: 'John Doe',\n * address: {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * state: 'IL'\n * },\n * };\n * const result = extractObjectsWithId(input);\n * console.log(result);\n *\n * Output:\n *\n * [{\n * id: '1',\n * name: 'John Doe',\n * address: {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * state: 'IL'\n * }\n * },\n * {\n * id: '2',\n * street: '123 Main St',\n * city: 'Springfield',\n * state: 'IL'\n * }]\n *\n */\nexport const extractObjectsWithId = (input: ContentModule): ContentModule[] => {\n // Function to recursively search and extract nested objects with an 'id'\n const search = (obj: Content, results: ContentModule[]): void => {\n if (obj && typeof obj === 'object') {\n if (Object.hasOwn(obj, 'id')) {\n results.push(obj as ContentModule);\n }\n for (const key of Object.keys(obj)) {\n if (typeof obj[key] === 'object') {\n search(obj[key] as Content, results);\n }\n }\n }\n };\n\n const results: ContentModule[] = [];\n search(input, results);\n return results;\n};\n"],"mappings":"AAyCO,MAAM,uBAAuB,CAAC,UAA0C;AAE7E,QAAM,SAAS,CAAC,KAAcA,aAAmC;AAC/D,QAAI,OAAO,OAAO,QAAQ,UAAU;AAClC,UAAI,OAAO,OAAO,KAAK,IAAI,GAAG;AAC5B,QAAAA,SAAQ,KAAK,GAAoB;AAAA,MACnC;AACA,iBAAW,OAAO,OAAO,KAAK,GAAG,GAAG;AAClC,YAAI,OAAO,IAAI,GAAG,MAAM,UAAU;AAChC,iBAAO,IAAI,GAAG,GAAcA,QAAO;AAAA,QACrC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,QAAM,UAA2B,CAAC;AAClC,SAAO,OAAO,OAAO;AACrB,SAAO;AACT;","names":["results"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.ts"],"sourcesContent":["export * from './extractNestedJSON';\nexport * from './processContentDeclaration';\nexport * from './buildIntlayerDictionary';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-var-requires */\nimport { createRequire } from 'module';\nimport { type Context, runInNewContext } from 'vm';\nimport type { ContentModule } from '@intlayer/core';\nimport { type BuildOptions, buildSync, type BuildResult } from 'esbuild';\n\nconst isESModule = typeof import.meta.url === 'string';\n\nconst sandboxContext: Context = {\n exports: {\n default: {},\n },\n module: {\n exports: {},\n },\n console,\n require: isESModule ? createRequire(import.meta.url) : require,\n};\n\nconst transformationOption: BuildOptions = {\n loader: {\n '.js': 'js',\n '.jsx': 'jsx',\n '.mjs': 'js',\n '.ts': 'ts',\n '.tsx': 'tsx',\n '.cjs': 'js',\n '.json': 'json',\n },\n format: 'cjs', // Output format as commonjs\n target: 'es2017',\n packages: 'external',\n write: false,\n bundle: true,\n};\n\nconst filterValidContentDeclaration = (\n contentDeclaration: ContentModule\n): ContentModule => {\n // @TODO Implement filtering of valid content declaration\n return contentDeclaration;\n};\n\n/**\n * Load the content declaration from the given path\n *\n * Accepts JSON, JS, MJS and TS files as configuration\n */\nexport const loadContentDeclaration = (\n contentDeclarationFilePath: string\n): ContentModule | undefined => {\n let contentDeclaration: ContentModule | undefined = undefined;\n\n const fileExtension = contentDeclarationFilePath.split('.').pop() ?? '';\n\n try {\n if (fileExtension === 'json') {\n // Assume JSON\n return require(contentDeclarationFilePath);\n }\n\n // Rest is JS, MJS or TS\n\n const moduleResult: BuildResult = buildSync({\n entryPoints: [contentDeclarationFilePath],\n\n ...transformationOption,\n });\n\n const moduleResultString = moduleResult.outputFiles?.[0].text;\n\n if (!moduleResultString) {\n console.error('Configuration file could not be loaded.');\n return undefined;\n }\n\n runInNewContext(moduleResultString, sandboxContext);\n\n if (\n sandboxContext.exports.default &&\n Object.keys(sandboxContext.exports.default).length > 0\n ) {\n // ES Module\n contentDeclaration = sandboxContext.exports.default;\n } else if (\n sandboxContext.module.exports.defaults &&\n Object.keys(sandboxContext.module.exports.defaults).length > 0\n ) {\n // CommonJS\n contentDeclaration = sandboxContext.module.exports.default;\n } else if (\n sandboxContext.module.exports.default &&\n Object.keys(sandboxContext.module.exports.default).length > 0\n ) {\n // ES Module\n contentDeclaration = sandboxContext.module.exports.default;\n } else if (\n sandboxContext.module.exports &&\n Object.keys(sandboxContext.module.exports).length > 0\n ) {\n // Other\n contentDeclaration = sandboxContext.module.exports;\n }\n\n if (typeof contentDeclaration === 'undefined') {\n console.error('Configuration file could not be loaded.');\n return undefined;\n }\n\n return filterValidContentDeclaration(contentDeclaration);\n } catch (error) {\n console.error('Error:', error);\n }\n};\n"],"mappings":"AACA,SAAS,qBAAqB;AAC9B,SAAuB,uBAAuB;AAE9C,SAA4B,iBAAmC;AAE/D,MAAM,aAAa,OAAO,YAAY,QAAQ;AAE9C,MAAM,iBAA0B;AAAA,EAC9B,SAAS;AAAA,IACP,SAAS,CAAC;AAAA,EACZ;AAAA,EACA,QAAQ;AAAA,IACN,SAAS,CAAC;AAAA,EACZ;AAAA,EACA;AAAA,EACA,SAAS,aAAa,cAAc,YAAY,GAAG,IAAI;AACzD;AAEA,MAAM,uBAAqC;AAAA,EACzC,QAAQ;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,SAAS;AAAA,EACX;AAAA,EACA,QAAQ;AAAA;AAAA,EACR,QAAQ;AAAA,EACR,UAAU;AAAA,EACV,OAAO;AAAA,EACP,QAAQ;AACV;AAEA,MAAM,gCAAgC,CACpC,uBACkB;AAElB,SAAO;AACT;AAOO,MAAM,yBAAyB,CACpC,+BAC8B;AAC9B,MAAI,qBAAgD;AAEpD,QAAM,gBAAgB,2BAA2B,MAAM,GAAG,EAAE,IAAI,KAAK;AAErE,MAAI;AACF,QAAI,kBAAkB,QAAQ;AAE5B,aAAO,QAAQ,0BAA0B;AAAA,IAC3C;AAIA,UAAM,eAA4B,UAAU;AAAA,MAC1C,aAAa,CAAC,0BAA0B;AAAA,MAExC,GAAG;AAAA,IACL,CAAC;AAED,UAAM,qBAAqB,aAAa,cAAc,CAAC,EAAE;AAEzD,QAAI,CAAC,oBAAoB;AACvB,cAAQ,MAAM,yCAAyC;AACvD,aAAO;AAAA,IACT;AAEA,oBAAgB,oBAAoB,cAAc;AAElD,QACE,eAAe,QAAQ,WACvB,OAAO,KAAK,eAAe,QAAQ,OAAO,EAAE,SAAS,GACrD;AAEA,2BAAqB,eAAe,QAAQ;AAAA,IAC9C,WACE,eAAe,OAAO,QAAQ,YAC9B,OAAO,KAAK,eAAe,OAAO,QAAQ,QAAQ,EAAE,SAAS,GAC7D;AAEA,2BAAqB,eAAe,OAAO,QAAQ;AAAA,IACrD,WACE,eAAe,OAAO,QAAQ,WAC9B,OAAO,KAAK,eAAe,OAAO,QAAQ,OAAO,EAAE,SAAS,GAC5D;AAEA,2BAAqB,eAAe,OAAO,QAAQ;AAAA,IACrD,WACE,eAAe,OAAO,WACtB,OAAO,KAAK,eAAe,OAAO,OAAO,EAAE,SAAS,GACpD;AAEA,2BAAqB,eAAe,OAAO;AAAA,IAC7C;AAEA,QAAI,OAAO,uBAAuB,aAAa;AAC7C,cAAQ,MAAM,yCAAyC;AACvD,aAAO;AAAA,IACT;AAEA,WAAO,8BAA8B,kBAAkB;AAAA,EACzD,SAAS,OAAO;AACd,YAAQ,MAAM,UAAU,KAAK;AAAA,EAC/B;AACF;","names":[]}
|
|
@@ -10,8 +10,7 @@ const processFunctionResults = async (entry) => {
|
|
|
10
10
|
result[key] = await processFunctionResults(
|
|
11
11
|
field
|
|
12
12
|
);
|
|
13
|
-
}
|
|
14
|
-
if (typeof field === "function") {
|
|
13
|
+
} else if (typeof field === "function") {
|
|
15
14
|
const promise = (async () => {
|
|
16
15
|
const value = await field();
|
|
17
16
|
result[key] = value;
|
|
@@ -42,4 +41,4 @@ const processContentDeclaration = async (file) => {
|
|
|
42
41
|
export {
|
|
43
42
|
processContentDeclaration
|
|
44
43
|
};
|
|
45
|
-
//# sourceMappingURL=
|
|
44
|
+
//# sourceMappingURL=processContentDeclaration.mjs.map
|