@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,102 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
2
|
-
import { basename, join, relative } from 'path';
|
|
3
|
-
import { Locales, getConfiguration } from '@intlayer/config';
|
|
4
|
-
import { sync } from 'glob';
|
|
5
|
-
import { getFileHash, transformToCamelCase } from '../../utils';
|
|
6
|
-
|
|
7
|
-
const { content, internationalization } = getConfiguration();
|
|
8
|
-
const { typesDir, moduleAugmentationDir } = content;
|
|
9
|
-
const { locales, strictMode } = internationalization;
|
|
10
|
-
|
|
11
|
-
export const getTypeName = (id: string): string =>
|
|
12
|
-
transformToCamelCase(`${id}Content`);
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* This function generates the content of the module augmentation file
|
|
16
|
-
*/
|
|
17
|
-
const generateTypeIndexContent = (typeFiles: string[]): string => {
|
|
18
|
-
let content = "/* eslint-disable */\nimport { Locales } from 'intlayer'\n";
|
|
19
|
-
|
|
20
|
-
const dictionariesRef = typeFiles.map((dictionaryPath) => ({
|
|
21
|
-
relativePath: relative(moduleAugmentationDir, dictionaryPath),
|
|
22
|
-
id: basename(dictionaryPath, '.d.ts'), // Get the base name as the dictionary id
|
|
23
|
-
hash: `_${getFileHash(dictionaryPath)}`, // Get the hash of the dictionary to avoid conflicts
|
|
24
|
-
}));
|
|
25
|
-
|
|
26
|
-
// Import all dictionaries
|
|
27
|
-
dictionariesRef.forEach((dictionary) => {
|
|
28
|
-
const typeName = getTypeName(dictionary.id);
|
|
29
|
-
content += `import type { ${typeName} as ${dictionary.hash} } from '${dictionary.relativePath}';\n`;
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
content += '\n';
|
|
33
|
-
|
|
34
|
-
// Format Dictionary Map
|
|
35
|
-
const formattedDictionaryMap: string = dictionariesRef
|
|
36
|
-
.map((dictionary) => ` "${dictionary.id}": ${dictionary.hash};`)
|
|
37
|
-
.join('\n');
|
|
38
|
-
|
|
39
|
-
const formatLocales = locales
|
|
40
|
-
.map((locale) => {
|
|
41
|
-
for (const key in Locales) {
|
|
42
|
-
if (Locales[key as keyof typeof Locales] === locale) {
|
|
43
|
-
return `Locales.${key}`;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
.join(' | ');
|
|
48
|
-
|
|
49
|
-
const strictModeRecord =
|
|
50
|
-
strictMode === 'strict'
|
|
51
|
-
? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content> {}`
|
|
52
|
-
: strictMode === 'required_only'
|
|
53
|
-
? `interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}`
|
|
54
|
-
: `interface IConfigLocales<Content> extends Partial<Record<Locales, Content>> {}`;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Write the module augmentation to extend the intlayer module with the dictionaries types
|
|
58
|
-
* Will suggest the type resulting of the dictionaries
|
|
59
|
-
*
|
|
60
|
-
* declare module 'intlayer' {
|
|
61
|
-
* interface IntlayerDictionaryTypesConnector = {
|
|
62
|
-
* dictionaries: {
|
|
63
|
-
* id: DictionaryType;
|
|
64
|
-
* }
|
|
65
|
-
* }
|
|
66
|
-
*
|
|
67
|
-
* type ConfigLocales = Locales.ENGLISH | Locales.FRENCH | Locales.SPANISH;
|
|
68
|
-
* type ExtractedLocales = Extract<Locales, ConfigLocales>;
|
|
69
|
-
* type ExcludedLocales = Exclude<Locales, ConfigLocales>;
|
|
70
|
-
*
|
|
71
|
-
* interface IConfigLocales<Content> extends Record<ExtractedLocales, Content>, Partial<Record<ExcludedLocales, Content>> {}
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* }
|
|
75
|
-
* See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
|
|
76
|
-
*/
|
|
77
|
-
content += `declare module 'intlayer' {\n`;
|
|
78
|
-
content += ` interface IntlayerDictionaryTypesConnector {\n${formattedDictionaryMap}\n }\n\n`;
|
|
79
|
-
content += ` type ConfigLocales = ${formatLocales};\n`;
|
|
80
|
-
content += ` type ExtractedLocales = Extract<Locales, ConfigLocales>;\n`;
|
|
81
|
-
content += ` type ExcludedLocales = Exclude<Locales, ConfigLocales>;\n\n`;
|
|
82
|
-
content += ` ${strictModeRecord}\n`;
|
|
83
|
-
content += `}`;
|
|
84
|
-
|
|
85
|
-
return content;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* This function generates a index file merging all the types
|
|
90
|
-
*/
|
|
91
|
-
export const createModuleAugmentation = () => {
|
|
92
|
-
// Create main directory if it doesn't exist
|
|
93
|
-
if (!existsSync(moduleAugmentationDir)) {
|
|
94
|
-
mkdirSync(moduleAugmentationDir, { recursive: true });
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const dictionaries: string[] = sync(`${typesDir}/**/*.d.ts`);
|
|
98
|
-
// Create the dictionary list file
|
|
99
|
-
|
|
100
|
-
const tsContent = generateTypeIndexContent(dictionaries);
|
|
101
|
-
writeFileSync(join(moduleAugmentationDir, 'intlayer.d.ts'), tsContent);
|
|
102
|
-
};
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
2
|
-
import { createRequire } from 'module';
|
|
3
|
-
import { resolve } from 'path';
|
|
4
|
-
import { getConfiguration } from '@intlayer/config';
|
|
5
|
-
import {
|
|
6
|
-
NodeType,
|
|
7
|
-
type Content,
|
|
8
|
-
type ContentModule,
|
|
9
|
-
type TypedNode,
|
|
10
|
-
} from '@intlayer/core';
|
|
11
|
-
import { getTypeName } from './createModuleAugmentation';
|
|
12
|
-
|
|
13
|
-
const { content, internationalization } = getConfiguration();
|
|
14
|
-
const { typesDir } = content;
|
|
15
|
-
|
|
16
|
-
const isESModule = typeof import.meta.url === 'string';
|
|
17
|
-
const requireFunction = isESModule ? createRequire(import.meta.url) : require;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
*
|
|
21
|
-
* This function generates a TypeScript type definition from a JSON object
|
|
22
|
-
*
|
|
23
|
-
* Example:
|
|
24
|
-
*
|
|
25
|
-
* const input = {
|
|
26
|
-
* id: '1',
|
|
27
|
-
* name: 'John Doe',
|
|
28
|
-
* address: {
|
|
29
|
-
* id: '2',
|
|
30
|
-
* street: '123 Main St',
|
|
31
|
-
* city: 'Springfield',
|
|
32
|
-
* }
|
|
33
|
-
* };
|
|
34
|
-
*
|
|
35
|
-
* const result = generateTypeScriptType(input, 'RootObject');
|
|
36
|
-
* console.log(result);
|
|
37
|
-
*
|
|
38
|
-
* Output:
|
|
39
|
-
*
|
|
40
|
-
* type RootObject = {
|
|
41
|
-
* id: '1',
|
|
42
|
-
* name: string,
|
|
43
|
-
* address: {
|
|
44
|
-
* id: '2',
|
|
45
|
-
* street: string,
|
|
46
|
-
* city: string,
|
|
47
|
-
* },
|
|
48
|
-
* };
|
|
49
|
-
*
|
|
50
|
-
*/
|
|
51
|
-
export const generateTypeScriptType = (obj: ContentModule): string => {
|
|
52
|
-
let typeDefinition = ``;
|
|
53
|
-
|
|
54
|
-
const typeName = getTypeName(obj.id);
|
|
55
|
-
|
|
56
|
-
typeDefinition += `export type ${typeName} = {\n`;
|
|
57
|
-
typeDefinition += generateTypeScriptTypeContent(obj);
|
|
58
|
-
typeDefinition += '};\n\n';
|
|
59
|
-
|
|
60
|
-
return typeDefinition;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const isReactNode = (node: Record<string, unknown>): boolean =>
|
|
64
|
-
typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';
|
|
65
|
-
|
|
66
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
67
|
-
export const generateTypeScriptTypeContent = (obj: Content): string => {
|
|
68
|
-
if (typeof obj !== 'object' || obj === null) {
|
|
69
|
-
return `${typeof obj}`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const isReactNodeValue = isReactNode(obj as Record<string, unknown>);
|
|
73
|
-
|
|
74
|
-
if (isReactNodeValue) {
|
|
75
|
-
// ReactNode handling
|
|
76
|
-
return `JSX.Element`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
let typeDefinition = ``;
|
|
80
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
81
|
-
const nodeType: NodeType | undefined = (value as TypedNode)?.nodeType;
|
|
82
|
-
type ValueKey = keyof typeof value;
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
// Check if the value is a typed node
|
|
86
|
-
typeof value === 'object' &&
|
|
87
|
-
nodeType === NodeType.Translation
|
|
88
|
-
) {
|
|
89
|
-
const tsType = generateTypeScriptTypeContent(
|
|
90
|
-
value?.[internationalization.defaultLocale as ValueKey]
|
|
91
|
-
);
|
|
92
|
-
typeDefinition += ` ${key}: ${tsType},\n`;
|
|
93
|
-
} else if (
|
|
94
|
-
// Check if the value is a typed node
|
|
95
|
-
typeof value === 'object' &&
|
|
96
|
-
nodeType === NodeType.Enumeration
|
|
97
|
-
) {
|
|
98
|
-
const tsType = generateTypeScriptTypeContent(
|
|
99
|
-
value?.[internationalization.defaultLocale as ValueKey] as Content
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
typeDefinition += ` ${key}: (quantity: number) => ${tsType},\n`;
|
|
103
|
-
} else if (
|
|
104
|
-
// Check if the value is a nested object
|
|
105
|
-
typeof value === 'object'
|
|
106
|
-
) {
|
|
107
|
-
const isArray = Array.isArray(value);
|
|
108
|
-
|
|
109
|
-
if (isArray) {
|
|
110
|
-
// Array handling (simplified, assumes non-empty arrays with uniform type)
|
|
111
|
-
const arrayType = generateTypeScriptTypeContent(value as Content);
|
|
112
|
-
|
|
113
|
-
typeDefinition += ` ${key}: ${arrayType}[],\n`;
|
|
114
|
-
} else {
|
|
115
|
-
// Nested object, recurse
|
|
116
|
-
const nestedType = generateTypeScriptTypeContent(value as Content);
|
|
117
|
-
|
|
118
|
-
typeDefinition += ` ${key}: {${nestedType}},\n`;
|
|
119
|
-
}
|
|
120
|
-
} else if (
|
|
121
|
-
// Check if the value is an 'id'
|
|
122
|
-
typeof value === 'string' &&
|
|
123
|
-
key === 'id'
|
|
124
|
-
) {
|
|
125
|
-
// Special handling for 'id' field
|
|
126
|
-
const tsType = `"${value}"`;
|
|
127
|
-
typeDefinition += ` ${key}: ${tsType},\n`;
|
|
128
|
-
} else {
|
|
129
|
-
// Primitive type
|
|
130
|
-
const tsType = typeof value;
|
|
131
|
-
typeDefinition += ` ${key}: ${tsType},\n`;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return typeDefinition;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* This function generates a TypeScript type definition from a JSON object
|
|
140
|
-
*/
|
|
141
|
-
export const createTypes = (dictionariesPaths: string[]): string[] => {
|
|
142
|
-
const resultTypesPaths: string[] = [];
|
|
143
|
-
|
|
144
|
-
// Create type folders if they don't exist
|
|
145
|
-
if (!existsSync(typesDir)) {
|
|
146
|
-
mkdirSync(typesDir, { recursive: true });
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
for (const dictionaryPath of dictionariesPaths) {
|
|
150
|
-
const contentModule: ContentModule = requireFunction(dictionaryPath);
|
|
151
|
-
const dictionaryName: string = contentModule.id;
|
|
152
|
-
const typeDefinition: string = generateTypeScriptType(contentModule);
|
|
153
|
-
|
|
154
|
-
const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);
|
|
155
|
-
|
|
156
|
-
writeFileSync(outputPath, typeDefinition);
|
|
157
|
-
|
|
158
|
-
resultTypesPaths.push(outputPath);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return resultTypesPaths;
|
|
162
|
-
};
|
package/src/utils.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import crypto from 'crypto-js';
|
|
2
|
-
|
|
3
|
-
export const getFileHash = (filePath: string) => {
|
|
4
|
-
const hash = crypto.SHA3(filePath);
|
|
5
|
-
|
|
6
|
-
return hash
|
|
7
|
-
.toString(crypto.enc.Base64)
|
|
8
|
-
.replace(/[^A-Z\d]/gi, '')
|
|
9
|
-
.substring(0, 20);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const transformToCamelCase = (string: string): string => {
|
|
13
|
-
// Split the string into words using a regex that finds spaces, hyphens, and underscores
|
|
14
|
-
const words = string.split(/[\s\-_]+/);
|
|
15
|
-
|
|
16
|
-
// Transform each word except the first to have its first letter uppercase
|
|
17
|
-
const camelCasedWords = words.map((word, index) => {
|
|
18
|
-
if (index === 0) {
|
|
19
|
-
return word[0].toUpperCase() + word.slice(1);
|
|
20
|
-
}
|
|
21
|
-
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
// Join the words back together
|
|
25
|
-
return camelCasedWords.join('');
|
|
26
|
-
};
|