@intlayer/chokidar 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/chokidar/watcher.cjs +11 -5
- package/dist/cjs/chokidar/watcher.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.cjs +4 -2
- package/dist/cjs/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.cjs +3 -3
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.d.ts +2 -2
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.d.ts +2 -2
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.cjs +8 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.cjs.map +1 -1
- package/dist/cjs/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.d.ts +3 -3
- package/dist/cjs/transpiler/dictionary_to_type/createType.cjs +42 -76
- package/dist/cjs/transpiler/dictionary_to_type/createType.cjs.map +1 -1
- package/dist/cjs/transpiler/dictionary_to_type/createType.d.ts +3 -38
- package/dist/cjs/transpiler/dictionary_to_type/index.d.ts +1 -2
- package/dist/esm/chokidar/watcher.mjs +11 -5
- package/dist/esm/chokidar/watcher.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.mjs +4 -2
- package/dist/esm/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.mjs +3 -3
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.d.mts +2 -2
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.d.mts +2 -2
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.mjs.map +1 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.d.mts +3 -3
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.mjs +8 -1
- package/dist/esm/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.mjs.map +1 -1
- package/dist/esm/transpiler/dictionary_to_type/createType.d.mts +3 -38
- package/dist/esm/transpiler/dictionary_to_type/createType.mjs +44 -75
- package/dist/esm/transpiler/dictionary_to_type/createType.mjs.map +1 -1
- package/dist/esm/transpiler/dictionary_to_type/index.d.mts +1 -2
- package/package.json +12 -13
- package/src/chokidar/index.ts +0 -1
- package/src/chokidar/watcher.ts +0 -75
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/buildI18nDictionary.ts +0 -95
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertContentDeclarationInto18nDictionaries.ts +0 -91
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/convertPluralsValues.ts +0 -22
- package/src/transpiler/declaration_file_to_dictionary/i18n_dictionary/index.ts +0 -1
- package/src/transpiler/declaration_file_to_dictionary/index.ts +0 -21
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/buildIntlayerDictionary.ts +0 -70
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.ts +0 -60
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/index.ts +0 -3
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/loadContentDeclaration.ts +0 -114
- package/src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.ts +0 -65
- package/src/transpiler/dictionary_to_main/createDictionaryList.ts +0 -65
- package/src/transpiler/dictionary_to_main/index.ts +0 -1
- package/src/transpiler/dictionary_to_type/createModuleAugmentation.ts +0 -102
- package/src/transpiler/dictionary_to_type/createType.ts +0 -162
- package/src/transpiler/dictionary_to_type/index.ts +0 -2
- package/src/utils.ts +0 -26
|
@@ -1 +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 {
|
|
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 { DeclarationContent } 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: DeclarationContent[] = 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: string = 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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A8B,MAAM;AAAA,MACxC;AAAA,IACF;AAGA,4BAAwB,KAAK,GAAG,iBAAiB;AAAA,EACnD;AAEA,SAAO;AACT;","names":["content"]}
|
|
@@ -12,14 +12,16 @@ const buildDictionary = (content, locale) => {
|
|
|
12
12
|
// Translation node
|
|
13
13
|
content && content.nodeType === NodeType.Translation
|
|
14
14
|
) {
|
|
15
|
-
const result = content[locale];
|
|
15
|
+
const result = content[NodeType.Translation][locale];
|
|
16
16
|
return buildDictionary(result, locale);
|
|
17
17
|
} else if (
|
|
18
18
|
// Translation node
|
|
19
19
|
content && content.nodeType === NodeType.Enumeration
|
|
20
20
|
) {
|
|
21
21
|
const plurals = {};
|
|
22
|
-
Object.keys(
|
|
22
|
+
Object.keys(
|
|
23
|
+
content[NodeType.Enumeration]
|
|
24
|
+
).forEach((quantity) => {
|
|
23
25
|
const letterNumber = convertPluralsValues(quantity);
|
|
24
26
|
const value = content[quantity];
|
|
25
27
|
plurals[`${letterNumber}_${letterNumber}`] = buildDictionary(
|
|
@@ -1 +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,
|
|
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>)[\n NodeType.Translation\n ][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(\n (content as EnumerationContent<unknown>)[NodeType.Enumeration]\n ).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,S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A,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -7,9 +7,9 @@ const { content } = getConfiguration();
|
|
|
7
7
|
const { dictionariesDir } = content;
|
|
8
8
|
const writeDictionary = async (dictionaries) => {
|
|
9
9
|
const resultDictionariesPaths = [];
|
|
10
|
-
for await (const
|
|
11
|
-
const contentString = JSON.stringify(
|
|
12
|
-
const id =
|
|
10
|
+
for await (const dictionaryContent of dictionaries) {
|
|
11
|
+
const contentString = JSON.stringify(dictionaryContent);
|
|
12
|
+
const id = dictionaryContent.id;
|
|
13
13
|
const outputFileName = `${id}.json`;
|
|
14
14
|
const resultFilePath = resolve(dictionariesDir, outputFileName);
|
|
15
15
|
await writeFile(resultFilePath, contentString, "utf8").catch((err) => {
|
|
@@ -1 +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 {
|
|
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 { DeclarationContent } from '@intlayer/core';\nimport { extractObjectsWithId } from './extractNestedJSON';\nimport { processContentDeclaration } from './processContentDeclaration';\n\nconst { content } = getConfiguration();\nconst { dictionariesDir } = content;\n\nconst writeDictionary = async (dictionaries: DeclarationContent[]) => {\n const resultDictionariesPaths: string[] = [];\n\n for await (const dictionaryContent of dictionaries) {\n const contentString = JSON.stringify(dictionaryContent);\n\n const id = dictionaryContent.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: DeclarationContent[] = extractObjectsWithId(result);\n\n const contentWithFilePath: DeclarationContent[] = 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,iBAAuC;AACpE,QAAM,0BAAoC,CAAC;AAE3C,mBAAiB,qBAAqB,cAAc;AAClD,UAAM,gBAAgB,KAAK,UAAU,iBAAiB;AAEtD,UAAM,KAAK,kBAAkB;AAC7B,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,gBAAsC,qBAAqB,MAAM;AAEvE,UAAM,sBAA4C,cAAc;AAAA,MAC9D,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,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DeclarationContent } from '@intlayer/core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
@@ -39,6 +39,6 @@ import { ContentModule } from '@intlayer/core';
|
|
|
39
39
|
* }]
|
|
40
40
|
*
|
|
41
41
|
*/
|
|
42
|
-
declare const extractObjectsWithId: (input:
|
|
42
|
+
declare const extractObjectsWithId: (input: DeclarationContent) => DeclarationContent[];
|
|
43
43
|
|
|
44
44
|
export { extractObjectsWithId };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.ts"],"sourcesContent":["import type { Content,
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/extractNestedJSON.ts"],"sourcesContent":["import type { Content, DeclarationContent } 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 = (\n input: DeclarationContent\n): DeclarationContent[] => {\n // Function to recursively search and extract nested objects with an 'id'\n const search = (obj: Content, results: DeclarationContent[]): void => {\n if (obj && typeof obj === 'object') {\n if (Object.hasOwn(obj, 'id')) {\n results.push(obj as DeclarationContent);\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: DeclarationContent[] = [];\n search(input, results);\n return results;\n};\n"],"mappings":"AAyCO,MAAM,uBAAuB,CAClC,UACyB;AAEzB,QAAM,SAAS,CAAC,KAAcA,aAAwC;AACpE,QAAI,OAAO,OAAO,QAAQ,UAAU;AAClC,UAAI,OAAO,OAAO,KAAK,IAAI,GAAG;AAC5B,QAAAA,SAAQ,KAAK,GAAyB;AAAA,MACxC;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,UAAgC,CAAC;AACvC,SAAO,OAAO,OAAO;AACrB,SAAO;AACT;","names":["results"]}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DeclarationContent } from '@intlayer/core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Load the content declaration from the given path
|
|
5
5
|
*
|
|
6
6
|
* Accepts JSON, JS, MJS and TS files as configuration
|
|
7
7
|
*/
|
|
8
|
-
declare const loadContentDeclaration: (contentDeclarationFilePath: string) =>
|
|
8
|
+
declare const loadContentDeclaration: (contentDeclarationFilePath: string) => DeclarationContent | undefined;
|
|
9
9
|
|
|
10
10
|
export { loadContentDeclaration };
|
|
@@ -1 +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 {
|
|
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 { DeclarationContent } 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: DeclarationContent\n): DeclarationContent => {\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): DeclarationContent | undefined => {\n let contentDeclaration: DeclarationContent | 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;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IACT;AAEA,WAAO,8BAA8B,kBAAkB;AAAA,EACzD,SAAS,OAAO;AACd,YAAQ,MAAM,UAAU,KAAK;AAAA,EAC/B;AACF;","names":[]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DeclarationContent } from '@intlayer/core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Function to load, process the module and return the Intlayer
|
|
4
|
+
* Function to load, process the module and return the Intlayer DeclarationContent from the module file
|
|
5
5
|
*/
|
|
6
|
-
declare const processContentDeclaration: (file: string) => Promise<
|
|
6
|
+
declare const processContentDeclaration: (file: string) => Promise<DeclarationContent | undefined>;
|
|
7
7
|
|
|
8
8
|
export { processContentDeclaration };
|
|
@@ -6,7 +6,14 @@ const processFunctionResults = async (entry) => {
|
|
|
6
6
|
const result = {};
|
|
7
7
|
for (const key of Object.keys(entry)) {
|
|
8
8
|
const field = entry?.[key];
|
|
9
|
-
|
|
9
|
+
const isArray = Array.isArray(field);
|
|
10
|
+
if (typeof field === "object" && isArray) {
|
|
11
|
+
result[key] = await Promise.all(
|
|
12
|
+
field.map(async (el) => {
|
|
13
|
+
return await processFunctionResults(el);
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
} else if (typeof field === "object") {
|
|
10
17
|
result[key] = await processFunctionResults(
|
|
11
18
|
field
|
|
12
19
|
);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.ts"],"sourcesContent":["import { resolve } from 'path';\nimport type {\n Content,\n
|
|
1
|
+
{"version":3,"sources":["../../../../../src/transpiler/declaration_file_to_dictionary/intlayer_dictionary/processContentDeclaration.ts"],"sourcesContent":["import { resolve } from 'path';\nimport type {\n Content,\n DeclarationContent,\n FlatContent,\n FlatContentValue,\n} from '@intlayer/core';\nimport { loadContentDeclaration } from './loadContentDeclaration';\n\n/**\n * Function to replace function and async function fields with their results in the object\n */\nconst processFunctionResults = async (entry: Content): Promise<FlatContent> => {\n if (entry && typeof entry === 'object') {\n const promises: Promise<void>[] = [];\n const result: FlatContent = {};\n\n for (const key of Object.keys(entry)) {\n const field = entry?.[key];\n const isArray = Array.isArray(field);\n\n if (typeof field === 'object' && isArray) {\n result[key] = (await Promise.all(\n field.map(async (el) => {\n return await processFunctionResults(el as Content);\n })\n )) as unknown as FlatContentValue;\n } else if (typeof field === 'object') {\n result[key] = (await processFunctionResults(\n field as Content\n )) as FlatContentValue;\n } else if (typeof field === 'function') {\n // Wait for the function to resolve if it's an async function\n const promise = (async () => {\n // Execute the function and await the result if it's a Promise\n const value = await field();\n\n result[key] = value as FlatContentValue;\n })();\n promises.push(promise);\n } else {\n result[key] = field as FlatContentValue;\n }\n }\n\n // Wait for all async operations to complete\n await Promise.all(promises);\n\n return result;\n }\n\n return entry;\n};\n\n/**\n * Function to load, process the module and return the Intlayer DeclarationContent from the module file\n */\nexport const processContentDeclaration = async (file: string) => {\n try {\n const functionPath = resolve(file);\n const entry = loadContentDeclaration(functionPath);\n\n if (!entry) {\n console.error('No entry found in module:', functionPath);\n return;\n }\n\n return (await processFunctionResults(entry)) as DeclarationContent;\n } catch (error) {\n console.error('Error processing module:', error);\n }\n};\n"],"mappings":"AAAA,SAAS,eAAe;AAOxB,SAAS,8BAA8B;AAKvC,MAAM,yBAAyB,OAAO,UAAyC;AAC7E,MAAI,SAAS,OAAO,UAAU,UAAU;AACtC,UAAM,WAA4B,CAAC;AACnC,UAAM,SAAsB,CAAC;AAE7B,eAAW,OAAO,OAAO,KAAK,KAAK,GAAG;AACpC,YAAM,QAAQ,QAAQ,GAAG;AACzB,YAAM,UAAU,MAAM,QAAQ,KAAK;AAEnC,UAAI,OAAO,UAAU,YAAY,SAAS;AACxC,eAAO,GAAG,IAAK,MAAM,QAAQ;AAAA,UAC3B,MAAM,IAAI,OAAO,OAAO;AACtB,mBAAO,MAAM,uBAAuB,EAAa;AAAA,UACnD,CAAC;AAAA,QACH;AAAA,MACF,WAAW,OAAO,UAAU,UAAU;AACpC,eAAO,GAAG,IAAK,MAAM;AAAA,UACnB;AAAA,QACF;AAAA,MACF,WAAW,OAAO,UAAU,YAAY;AAEtC,cAAM,WAAW,YAAY;AAE3B,gBAAM,QAAQ,MAAM,MAAM;AAE1B,iBAAO,GAAG,IAAI;AAAA,QAChB,GAAG;AACH,iBAAS,KAAK,OAAO;AAAA,MACvB,OAAO;AACL,eAAO,GAAG,IAAI;AAAA,MAChB;AAAA,IACF;AAGA,UAAM,QAAQ,IAAI,QAAQ;AAE1B,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAKO,MAAM,4BAA4B,OAAO,SAAiB;AAC/D,MAAI;AACF,UAAM,eAAe,QAAQ,IAAI;AACjC,UAAM,QAAQ,uBAAuB,YAAY;AAEjD,QAAI,CAAC,OAAO;AACV,cAAQ,MAAM,6BAA6B,YAAY;AACvD;AAAA,IACF;AAEA,WAAQ,MAAM,uBAAuB,KAAK;AAAA,EAC5C,SAAS,OAAO;AACd,YAAQ,MAAM,4BAA4B,KAAK;AAAA,EACjD;AACF;","names":[]}
|
|
@@ -1,42 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* This function generates a TypeScript type definition from a JSON object
|
|
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
|
-
* }
|
|
17
|
-
* };
|
|
18
|
-
*
|
|
19
|
-
* const result = generateTypeScriptType(input, 'RootObject');
|
|
20
|
-
* console.log(result);
|
|
21
|
-
*
|
|
22
|
-
* Output:
|
|
23
|
-
*
|
|
24
|
-
* type RootObject = {
|
|
25
|
-
* id: '1',
|
|
26
|
-
* name: string,
|
|
27
|
-
* address: {
|
|
28
|
-
* id: '2',
|
|
29
|
-
* street: string,
|
|
30
|
-
* city: string,
|
|
31
|
-
* },
|
|
32
|
-
* };
|
|
33
|
-
*
|
|
34
|
-
*/
|
|
35
|
-
declare const generateTypeScriptType: (obj: ContentModule) => string;
|
|
36
|
-
declare const generateTypeScriptTypeContent: (obj: Content) => string;
|
|
1
|
+
declare const generateTypeScriptType: (typeName: string, jsonString: string) => Promise<string>;
|
|
37
2
|
/**
|
|
38
3
|
* This function generates a TypeScript type definition from a JSON object
|
|
39
4
|
*/
|
|
40
|
-
declare const createTypes: (dictionariesPaths: string[]) => string[]
|
|
5
|
+
declare const createTypes: (dictionariesPaths: string[]) => Promise<string[]>;
|
|
41
6
|
|
|
42
|
-
export { createTypes, generateTypeScriptType
|
|
7
|
+
export { createTypes, generateTypeScriptType };
|
|
@@ -3,90 +3,60 @@ import { createRequire } from "module";
|
|
|
3
3
|
import { resolve } from "path";
|
|
4
4
|
import { getConfiguration } from "@intlayer/config";
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
quicktype,
|
|
7
|
+
InputData,
|
|
8
|
+
jsonInputForTargetLanguage
|
|
9
|
+
} from "quicktype-core";
|
|
10
|
+
const { content } = getConfiguration();
|
|
10
11
|
const { typesDir } = content;
|
|
11
12
|
const isESModule = typeof import.meta.url === "string";
|
|
12
13
|
const requireFunction = isESModule ? createRequire(import.meta.url) : require;
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
typeDefinition += `export type ${typeName} = {
|
|
17
|
-
`;
|
|
18
|
-
typeDefinition += generateTypeScriptTypeContent(obj);
|
|
19
|
-
typeDefinition += "};\n\n";
|
|
20
|
-
return typeDefinition;
|
|
21
|
-
};
|
|
22
|
-
const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined";
|
|
23
|
-
const generateTypeScriptTypeContent = (obj) => {
|
|
24
|
-
if (typeof obj !== "object" || obj === null) {
|
|
25
|
-
return `${typeof obj}`;
|
|
26
|
-
}
|
|
27
|
-
const isReactNodeValue = isReactNode(obj);
|
|
28
|
-
if (isReactNodeValue) {
|
|
29
|
-
return `JSX.Element`;
|
|
14
|
+
const kebabCaseToCammelCase = (name) => name.split(/[\s\-_]+/).map((word, index) => {
|
|
15
|
+
if (index === 0) {
|
|
16
|
+
return word;
|
|
30
17
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
`;
|
|
18
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
19
|
+
}).join("");
|
|
20
|
+
const generateTypeScriptType = async (typeName, jsonString) => {
|
|
21
|
+
const { lines } = await quicktypeJSON(typeName, jsonString);
|
|
22
|
+
const linesString = lines.join("\n");
|
|
23
|
+
return linesString;
|
|
24
|
+
};
|
|
25
|
+
const quicktypeJSON = async (typeName, jsonString) => {
|
|
26
|
+
const jsonInput = jsonInputForTargetLanguage("typescript");
|
|
27
|
+
await jsonInput.addSource({
|
|
28
|
+
name: typeName,
|
|
29
|
+
samples: [jsonString]
|
|
30
|
+
});
|
|
31
|
+
const inputData = new InputData();
|
|
32
|
+
inputData.addInput(jsonInput);
|
|
33
|
+
return await quicktype({
|
|
34
|
+
inputData,
|
|
35
|
+
lang: "typescript",
|
|
36
|
+
alphabetizeProperties: true,
|
|
37
|
+
rendererOptions: {
|
|
38
|
+
"just-types": "true",
|
|
39
|
+
"explicit-unions": "true",
|
|
40
|
+
"acronym-style": "camel",
|
|
41
|
+
"prefer-types": "true",
|
|
42
|
+
readonly: "false"
|
|
77
43
|
}
|
|
78
|
-
}
|
|
79
|
-
return typeDefinition;
|
|
44
|
+
});
|
|
80
45
|
};
|
|
81
|
-
const createTypes = (dictionariesPaths) => {
|
|
46
|
+
const createTypes = async (dictionariesPaths) => {
|
|
82
47
|
const resultTypesPaths = [];
|
|
83
48
|
if (!existsSync(typesDir)) {
|
|
84
49
|
mkdirSync(typesDir, { recursive: true });
|
|
85
50
|
}
|
|
86
51
|
for (const dictionaryPath of dictionariesPaths) {
|
|
87
|
-
const
|
|
88
|
-
const dictionaryName =
|
|
89
|
-
const
|
|
52
|
+
const dictionary = requireFunction(dictionaryPath);
|
|
53
|
+
const dictionaryName = dictionary.id;
|
|
54
|
+
const dictionaryNameCamelCase = kebabCaseToCammelCase(dictionaryName) + "Content";
|
|
55
|
+
const dictionaryContentString = JSON.stringify(dictionary);
|
|
56
|
+
const typeDefinition = await generateTypeScriptType(
|
|
57
|
+
dictionaryNameCamelCase,
|
|
58
|
+
dictionaryContentString
|
|
59
|
+
);
|
|
90
60
|
const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);
|
|
91
61
|
writeFileSync(outputPath, typeDefinition);
|
|
92
62
|
resultTypesPaths.push(outputPath);
|
|
@@ -95,7 +65,6 @@ const createTypes = (dictionariesPaths) => {
|
|
|
95
65
|
};
|
|
96
66
|
export {
|
|
97
67
|
createTypes,
|
|
98
|
-
generateTypeScriptType
|
|
99
|
-
generateTypeScriptTypeContent
|
|
68
|
+
generateTypeScriptType
|
|
100
69
|
};
|
|
101
70
|
//# sourceMappingURL=createType.mjs.map
|
|
@@ -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
|
|
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 type { Dictionary } from '@intlayer/core';\nimport {\n quicktype,\n InputData,\n jsonInputForTargetLanguage,\n} from 'quicktype-core';\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 kebabCaseToCammelCase = (name: string): string =>\n name\n .split(/[\\s\\-_]+/) // Regular expression to match space, hyphen, or underscore\n .map((word, index) => {\n if (index === 0) {\n return word; // Return the first word as is\n }\n return word.charAt(0).toUpperCase() + word.slice(1); // Capitalize the first letter of subsequent words\n })\n .join(''); // Join all the segments into one string\n\nexport const generateTypeScriptType = async (\n typeName: string,\n jsonString: string\n) => {\n const { lines } = await quicktypeJSON(typeName, jsonString);\n\n const linesString: string = lines.join('\\n');\n\n return linesString;\n};\n\nconst quicktypeJSON = async (typeName: string, jsonString: string) => {\n const jsonInput = jsonInputForTargetLanguage('typescript');\n\n // We could add multiple samples for the same desired\n // type, or many sources for other types. Here we're\n // just making one type from one piece of sample JSON.\n\n await jsonInput.addSource({\n name: typeName,\n samples: [jsonString],\n });\n\n const inputData = new InputData();\n inputData.addInput(jsonInput);\n\n return await quicktype({\n inputData,\n lang: 'typescript',\n alphabetizeProperties: true,\n rendererOptions: {\n 'just-types': 'true',\n 'explicit-unions': 'true',\n 'acronym-style': 'camel',\n 'prefer-types': 'true',\n readonly: 'false',\n },\n });\n};\n\n/**\n * This function generates a TypeScript type definition from a JSON object\n */\nexport const createTypes = async (\n dictionariesPaths: string[]\n): Promise<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 dictionaryNameCamelCase: string =\n kebabCaseToCammelCase(dictionaryName) + 'Content';\n\n const dictionaryContentString: string = JSON.stringify(dictionary);\n\n const typeDefinition: string = await generateTypeScriptType(\n dictionaryNameCamelCase,\n dictionaryContentString\n );\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;AAEjC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,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,wBAAwB,CAAC,SAC7B,KACG,MAAM,UAAU,EAChB,IAAI,CAAC,MAAM,UAAU;AACpB,MAAI,UAAU,GAAG;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-1,3 +1,2 @@
|
|
|
1
1
|
export { createModuleAugmentation, getTypeName } from './createModuleAugmentation.mjs';
|
|
2
|
-
export { createTypes, generateTypeScriptType
|
|
3
|
-
import '@intlayer/core';
|
|
2
|
+
export { createTypes, generateTypeScriptType } from './createType.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/chokidar",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Chokidar application for IntLayer - Transpile IntLayer declaration files into dictionaries.",
|
|
6
6
|
"keywords": [
|
|
@@ -41,32 +41,31 @@
|
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"./dist",
|
|
44
|
-
"./src",
|
|
45
|
-
"./bin",
|
|
46
44
|
"./package.json"
|
|
47
45
|
],
|
|
48
46
|
"dependencies": {
|
|
49
47
|
"chokidar": "^3.6.0",
|
|
50
48
|
"crypto-js": "^4.2.0",
|
|
51
49
|
"esbuild": "^0.20.2",
|
|
52
|
-
"esbuild-loader": "^4.
|
|
53
|
-
"glob": "^10.
|
|
50
|
+
"esbuild-loader": "^4.2.0",
|
|
51
|
+
"glob": "^10.4.2",
|
|
54
52
|
"node-loader": "^2.0.0",
|
|
53
|
+
"quicktype-core": "^23.0.170",
|
|
55
54
|
"rimraf": "5.0.5",
|
|
56
|
-
"@intlayer/config": "^2.0.
|
|
57
|
-
"@intlayer/core": "^2.0.
|
|
58
|
-
"intlayer": "^2.0.
|
|
55
|
+
"@intlayer/config": "^2.0.2",
|
|
56
|
+
"@intlayer/core": "^2.0.2",
|
|
57
|
+
"intlayer": "^2.0.2"
|
|
59
58
|
},
|
|
60
59
|
"devDependencies": {
|
|
61
60
|
"@changesets/cli": "2.27.1",
|
|
62
61
|
"@types/crypto-js": "^4.2.2",
|
|
63
|
-
"@types/node": "^20.
|
|
62
|
+
"@types/node": "^20.14.9",
|
|
64
63
|
"node-polyfill-webpack-plugin": "^3.0.0",
|
|
65
|
-
"tsup": "^8.0
|
|
66
|
-
"typescript": "^5.
|
|
64
|
+
"tsup": "^8.1.0",
|
|
65
|
+
"typescript": "^5.5.2",
|
|
67
66
|
"webpack-watch-files-plugin": "^1.2.1",
|
|
68
|
-
"@utils/eslint-config": "^1.0.
|
|
69
|
-
"@utils/ts-config": "^1.0.
|
|
67
|
+
"@utils/eslint-config": "^1.0.2",
|
|
68
|
+
"@utils/ts-config": "^1.0.2"
|
|
70
69
|
},
|
|
71
70
|
"engines": {
|
|
72
71
|
"node": ">=14.18"
|
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
|
-
};
|