@lang-tag/cli 0.15.0 → 0.16.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/algorithms/case-utils.d.ts +12 -0
- package/algorithms/config-generation/path-based-config-generator.d.ts +3 -2
- package/algorithms/import/flexible-import-algorithm.d.ts +232 -0
- package/algorithms/import/index.d.ts +2 -1
- package/algorithms/import/simple-mapping-import-algorithm.d.ts +120 -0
- package/algorithms/index.cjs +71 -32
- package/algorithms/index.d.ts +4 -1
- package/algorithms/index.js +68 -12
- package/config.d.ts +39 -30
- package/flexible-import-algorithm-C-S1c742.js +311 -0
- package/flexible-import-algorithm-Fa-l4jWj.cjs +327 -0
- package/index.cjs +192 -121
- package/index.js +186 -115
- package/package.json +1 -1
- package/templates/config/init-config.mustache +1 -0
- package/templates/import/imported-tag.mustache +14 -0
- package/namespace-collector-DCruv_PK.js +0 -95
- package/namespace-collector-DRnZvkDR.cjs +0 -94
- /package/{template → templates/tag}/base-app.mustache +0 -0
- /package/{template → templates/tag}/base-library.mustache +0 -0
- /package/{template → templates/tag}/placeholder.mustache +0 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Available case transformation types supported by the case library.
|
|
3
|
+
*/
|
|
4
|
+
export type CaseType = 'no' | 'camel' | 'capital' | 'constant' | 'dot' | 'header' | 'kebab' | 'lower' | 'param' | 'pascal' | 'path' | 'sentence' | 'snake' | 'swap' | 'title' | 'upper';
|
|
5
|
+
/**
|
|
6
|
+
* Applies case transformation to a string using the case library.
|
|
7
|
+
*
|
|
8
|
+
* @param str - The string to transform
|
|
9
|
+
* @param caseType - The case transformation type
|
|
10
|
+
* @returns The transformed string
|
|
11
|
+
*/
|
|
12
|
+
export declare function applyCaseTransform(str: string, caseType: CaseType): string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LangTagCLIConfigGenerationEvent } from '../../config.ts';
|
|
2
|
+
import { CaseType } from '../case-utils';
|
|
2
3
|
export interface PathBasedConfigGeneratorOptions {
|
|
3
4
|
/**
|
|
4
5
|
* Whether to include the filename (without extension) as part of the path segments.
|
|
@@ -120,14 +121,14 @@ export interface PathBasedConfigGeneratorOptions {
|
|
|
120
121
|
* 'lower', 'no', 'param', 'pascal', 'path', 'sentence', 'snake', 'swap', 'title', 'upper'
|
|
121
122
|
* @default undefined (no transformation)
|
|
122
123
|
*/
|
|
123
|
-
namespaceCase?:
|
|
124
|
+
namespaceCase?: CaseType;
|
|
124
125
|
/**
|
|
125
126
|
* Case transformation to apply to the path segments.
|
|
126
127
|
* Available options: 'camel', 'capital', 'constant', 'dot', 'header', 'kebab',
|
|
127
128
|
* 'lower', 'no', 'param', 'pascal', 'path', 'sentence', 'snake', 'swap', 'title', 'upper'
|
|
128
129
|
* @default undefined (no transformation)
|
|
129
130
|
*/
|
|
130
|
-
pathCase?:
|
|
131
|
+
pathCase?: CaseType;
|
|
131
132
|
/**
|
|
132
133
|
* Fallback namespace to use when no segments remain after filtering.
|
|
133
134
|
* Defaults to the defaultNamespace from langTagConfig.collect.defaultNamespace if not provided.
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { LangTagCLIImportEvent } from '../../config.ts';
|
|
2
|
+
import { CaseType } from '../case-utils';
|
|
3
|
+
/**
|
|
4
|
+
* Available case transformation options for variable names.
|
|
5
|
+
* Only includes transformations that produce valid JavaScript identifiers.
|
|
6
|
+
*/
|
|
7
|
+
export type VariableNameCaseType = 'no' | 'camel' | 'capital' | 'constant' | 'lower' | 'pascal' | 'snake' | 'swap' | 'upper';
|
|
8
|
+
/**
|
|
9
|
+
* Case transformation configuration for file paths.
|
|
10
|
+
* Can be a string for uniform case transformation, or an object with separate
|
|
11
|
+
* case transformations for directories and files.
|
|
12
|
+
*/
|
|
13
|
+
export type FilePathCaseType = CaseType | {
|
|
14
|
+
directories?: CaseType;
|
|
15
|
+
files?: CaseType;
|
|
16
|
+
};
|
|
17
|
+
export interface VariableNameOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Whether to prefix variable names with package name to avoid conflicts.
|
|
20
|
+
* @default false
|
|
21
|
+
*/
|
|
22
|
+
prefixWithPackageName?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* How to handle scoped package names (e.g., '@scope/package').
|
|
25
|
+
* - 'remove-scope': Remove @scope/ part, keep only package name
|
|
26
|
+
* - 'replace': Remove @ and replace / with underscores
|
|
27
|
+
* @default 'replace'
|
|
28
|
+
*/
|
|
29
|
+
scopedPackageHandling?: 'remove-scope' | 'replace';
|
|
30
|
+
/**
|
|
31
|
+
* Case transformation to apply to variable names.
|
|
32
|
+
* Available options: 'camel', 'capital', 'constant', 'lower', 'no', 'pascal', 'snake', 'swap', 'upper'
|
|
33
|
+
* @default 'no'
|
|
34
|
+
*/
|
|
35
|
+
case?: VariableNameCaseType;
|
|
36
|
+
/**
|
|
37
|
+
* Whether to sanitize variable names by replacing invalid characters with $.
|
|
38
|
+
* This ensures the final variable name is a valid JavaScript identifier.
|
|
39
|
+
* @default true
|
|
40
|
+
*/
|
|
41
|
+
sanitizeVariableName?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* How to handle tags without variableName.
|
|
44
|
+
* - 'skip': Skip tags without variableName
|
|
45
|
+
* - 'auto-generate': Generate names like 'translations1', 'translations2', etc. (default)
|
|
46
|
+
* - Function: Custom function to generate variable names
|
|
47
|
+
* @default 'auto-generate'
|
|
48
|
+
*/
|
|
49
|
+
handleMissingVariableName?: 'skip' | 'auto-generate' | ((tag: any, packageName: string, fileName: string, index: number) => string);
|
|
50
|
+
/**
|
|
51
|
+
* Custom function to generate variable names, completely overriding original names from export.
|
|
52
|
+
* When provided, this function will be used instead of the original variableName from the export.
|
|
53
|
+
*
|
|
54
|
+
* Note: The returned custom name will still be processed by all other transformations
|
|
55
|
+
* (case transformation, sanitization, prefixWithPackageName, etc.).
|
|
56
|
+
*
|
|
57
|
+
* @param context - Context information about the import
|
|
58
|
+
* @returns The custom variable name, or null to fall back to original naming logic
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* customVariableName: (context) => {
|
|
63
|
+
* // Generate names based on package and file structure
|
|
64
|
+
* const packagePrefix = context.packageName.replace('@company/', '').replace('-', '');
|
|
65
|
+
* const fileBase = context.fileName.split('/').pop()?.replace('.ts', '') || 'unknown';
|
|
66
|
+
* return `${packagePrefix}_${fileBase}_${context.tagIndex + 1}`;
|
|
67
|
+
* // This will still be processed by case transformation, sanitization, etc.
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
customVariableName?: (context: {
|
|
72
|
+
packageName: string;
|
|
73
|
+
fileName: string;
|
|
74
|
+
originalVariableName: string | undefined;
|
|
75
|
+
tagIndex: number;
|
|
76
|
+
tag: any;
|
|
77
|
+
}) => string | null;
|
|
78
|
+
}
|
|
79
|
+
export interface FilePathOptions {
|
|
80
|
+
/**
|
|
81
|
+
* Whether to group all translations from each package into a single file.
|
|
82
|
+
* If false, preserves the original file structure from the library.
|
|
83
|
+
* @default false
|
|
84
|
+
*/
|
|
85
|
+
groupByPackage?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Whether to include package name in the file path.
|
|
88
|
+
* @default false
|
|
89
|
+
*/
|
|
90
|
+
includePackageInPath?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* How to handle scoped package names in file paths (e.g., '@scope/package').
|
|
93
|
+
* - 'remove-scope': Remove @scope/ part, keep only package name
|
|
94
|
+
* - 'replace': Remove @ and replace / with dashes
|
|
95
|
+
* @default 'replace'
|
|
96
|
+
*/
|
|
97
|
+
scopedPackageHandling?: 'remove-scope' | 'replace';
|
|
98
|
+
/**
|
|
99
|
+
* Case transformation to apply to file names and path segments.
|
|
100
|
+
* Can be a string for uniform case transformation, or an object with separate
|
|
101
|
+
* case transformations for directories and files.
|
|
102
|
+
* Available options: 'camel', 'capital', 'constant', 'dot', 'header', 'kebab',
|
|
103
|
+
* 'lower', 'no', 'param', 'pascal', 'path', 'sentence', 'snake', 'swap', 'title', 'upper'
|
|
104
|
+
* @default 'no'
|
|
105
|
+
*/
|
|
106
|
+
case?: FilePathCaseType;
|
|
107
|
+
}
|
|
108
|
+
export interface FlexibleImportAlgorithmOptions {
|
|
109
|
+
/**
|
|
110
|
+
* Options for controlling variable name generation.
|
|
111
|
+
*/
|
|
112
|
+
variableName?: VariableNameOptions;
|
|
113
|
+
/**
|
|
114
|
+
* Options for controlling file path generation.
|
|
115
|
+
*/
|
|
116
|
+
filePath?: FilePathOptions;
|
|
117
|
+
/**
|
|
118
|
+
* Inclusion rules for filtering imports.
|
|
119
|
+
* If undefined, all packages and namespaces are processed.
|
|
120
|
+
* If defined, only matching packages and namespaces are processed.
|
|
121
|
+
*/
|
|
122
|
+
include?: {
|
|
123
|
+
/**
|
|
124
|
+
* List of package name patterns to include from import.
|
|
125
|
+
* Supports wildcards with * (e.g., '@company/*', 'ui-*')
|
|
126
|
+
* @default undefined (include all)
|
|
127
|
+
*/
|
|
128
|
+
packages?: string[];
|
|
129
|
+
/**
|
|
130
|
+
* List of namespace patterns to include from import.
|
|
131
|
+
* Supports wildcards with * (e.g., 'ui.*', '*.common')
|
|
132
|
+
* @default undefined (include all)
|
|
133
|
+
*/
|
|
134
|
+
namespaces?: string[];
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Exclusion rules for filtering imports.
|
|
138
|
+
* Applied after inclusion rules.
|
|
139
|
+
*/
|
|
140
|
+
exclude?: {
|
|
141
|
+
/**
|
|
142
|
+
* List of package name patterns to exclude from import.
|
|
143
|
+
* Supports wildcards with * (e.g., '@company/*', 'ui-*')
|
|
144
|
+
* @default []
|
|
145
|
+
*/
|
|
146
|
+
packages?: string[];
|
|
147
|
+
/**
|
|
148
|
+
* List of namespace patterns to exclude from import.
|
|
149
|
+
* Supports wildcards with * (e.g., 'admin.*', '*.internal')
|
|
150
|
+
* @default []
|
|
151
|
+
*/
|
|
152
|
+
namespaces?: string[];
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Function to remap/override configs before saving imported tags.
|
|
156
|
+
* Allows modification of namespace, path, and other config properties
|
|
157
|
+
* based on package name, file path, or other context.
|
|
158
|
+
*
|
|
159
|
+
* @param originalConfig - The original config from the imported tag
|
|
160
|
+
* @param context - Context information about the import
|
|
161
|
+
* @returns The modified config object, or null to remove the config from the tag
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* configRemap: (config, context) => {
|
|
166
|
+
* // Override namespace based on package name
|
|
167
|
+
* if (context.packageName === 'ui-components') {
|
|
168
|
+
* return { ...config, namespace: 'ui' };
|
|
169
|
+
* }
|
|
170
|
+
*
|
|
171
|
+
* // Remove config for certain packages (tag will be imported without config)
|
|
172
|
+
* if (context.packageName === 'no-config-package') {
|
|
173
|
+
* return null;
|
|
174
|
+
* }
|
|
175
|
+
*
|
|
176
|
+
* // Add prefix to all paths
|
|
177
|
+
* if (config.path) {
|
|
178
|
+
* return { ...config, path: `lib.${config.path}` };
|
|
179
|
+
* }
|
|
180
|
+
*
|
|
181
|
+
* return config;
|
|
182
|
+
* }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
configRemap?: (originalConfig: any, context: {
|
|
186
|
+
packageName: string;
|
|
187
|
+
fileName: string;
|
|
188
|
+
variableName: string;
|
|
189
|
+
tagIndex: number;
|
|
190
|
+
}) => any | null;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Default import algorithm that imports translations from libraries.
|
|
194
|
+
*
|
|
195
|
+
* This algorithm provides flexible options for organizing imported translations
|
|
196
|
+
* while preserving the ability to customize the import process.
|
|
197
|
+
*
|
|
198
|
+
* @param options - Configuration options for the import algorithm
|
|
199
|
+
* @returns A function compatible with LangTagCLIConfig.import.onImport
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* import { flexibleImportAlgorithm } from '@lang-tag/cli/algorithms';
|
|
204
|
+
*
|
|
205
|
+
* export default {
|
|
206
|
+
* import: {
|
|
207
|
+
* onImport: flexibleImportAlgorithm({
|
|
208
|
+
* variableName: {
|
|
209
|
+
* prefixWithPackageName: true,
|
|
210
|
+
* scopedPackageHandling: 'replace', // '@scope/package' -> 'scope_package'
|
|
211
|
+
* case: 'camel', // 'scope_package_myVar' -> 'scopePackageMyVar'
|
|
212
|
+
* handleMissingVariableName: 'auto-generate' // Generate 'translations1', 'translations2', etc.
|
|
213
|
+
* },
|
|
214
|
+
* filePath: {
|
|
215
|
+
* groupByPackage: true,
|
|
216
|
+
* scopedPackageHandling: 'remove-scope', // '@scope/package' -> 'package'
|
|
217
|
+
* case: 'kebab' // 'package.ts' -> 'package.ts' (no change), 'my-file.ts' -> 'my-file.ts'
|
|
218
|
+
* },
|
|
219
|
+
* include: {
|
|
220
|
+
* packages: ['@company/*', 'ui-*'],
|
|
221
|
+
* namespaces: ['ui.*', '*.common']
|
|
222
|
+
* },
|
|
223
|
+
* exclude: {
|
|
224
|
+
* packages: ['@company/internal-*'],
|
|
225
|
+
* namespaces: ['admin.*', '*.internal']
|
|
226
|
+
* }
|
|
227
|
+
* })
|
|
228
|
+
* }
|
|
229
|
+
* };
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export declare function flexibleImportAlgorithm(options?: FlexibleImportAlgorithmOptions): (event: LangTagCLIImportEvent) => void;
|
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
* These algorithms customize how library translations are imported
|
|
5
5
|
* and organized in your project.
|
|
6
6
|
*/
|
|
7
|
-
export {};
|
|
7
|
+
export { flexibleImportAlgorithm, type FlexibleImportAlgorithmOptions, type FilePathCaseType, type VariableNameCaseType } from './flexible-import-algorithm.ts';
|
|
8
|
+
export { simpleMappingImportAlgorithm, type SimpleMappingImportAlgorithmOptions, type PackageMapping, type FileMapping } from './simple-mapping-import-algorithm.ts';
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { LangTagCLIImportEvent } from '../../config.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Mapping for a specific file within a package.
|
|
4
|
+
* Defines which variables to import and optionally rename them.
|
|
5
|
+
*/
|
|
6
|
+
export interface FileMapping {
|
|
7
|
+
/**
|
|
8
|
+
* Source file path within the package
|
|
9
|
+
* @example 'components/button.ts'
|
|
10
|
+
*/
|
|
11
|
+
sourceFile: string;
|
|
12
|
+
/**
|
|
13
|
+
* Target file path where variables should be imported
|
|
14
|
+
* @example 'ui/buttons.ts'
|
|
15
|
+
*/
|
|
16
|
+
targetFile: string;
|
|
17
|
+
/**
|
|
18
|
+
* Map of variable names to import and their optional new names.
|
|
19
|
+
* If no new name is provided, the original name is used.
|
|
20
|
+
* @example { 'primaryButton': 'button', 'secondaryButton': 'secondary' }
|
|
21
|
+
*/
|
|
22
|
+
variables: Record<string, string | undefined>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Mapping for a specific package.
|
|
26
|
+
* Defines which files to import and how to map their variables.
|
|
27
|
+
*/
|
|
28
|
+
export interface PackageMapping {
|
|
29
|
+
/**
|
|
30
|
+
* Package name to match
|
|
31
|
+
* @example '@company/ui-components'
|
|
32
|
+
*/
|
|
33
|
+
packageName: string;
|
|
34
|
+
/**
|
|
35
|
+
* Array of file mappings for this package
|
|
36
|
+
*/
|
|
37
|
+
files: FileMapping[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Options for the simple mapping import algorithm.
|
|
41
|
+
*/
|
|
42
|
+
export interface SimpleMappingImportAlgorithmOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Array of package mappings defining how to import from each package
|
|
45
|
+
*/
|
|
46
|
+
mappings: PackageMapping[];
|
|
47
|
+
/**
|
|
48
|
+
* Global config remapping function applied to all imported tags
|
|
49
|
+
*/
|
|
50
|
+
configRemap?: (originalConfig: any, context: {
|
|
51
|
+
packageName: string;
|
|
52
|
+
sourceFile: string;
|
|
53
|
+
targetFile: string;
|
|
54
|
+
variableName: string;
|
|
55
|
+
originalVariableName: string;
|
|
56
|
+
}) => any | null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Simple mapping import algorithm that provides straightforward control over
|
|
60
|
+
* how library translations are imported based on explicit package->file->variable mappings.
|
|
61
|
+
*
|
|
62
|
+
* This algorithm allows you to:
|
|
63
|
+
* - Specify exactly which packages to import from
|
|
64
|
+
* - Map specific files from packages to target files
|
|
65
|
+
* - Select which variables to import and optionally rename them
|
|
66
|
+
* - Skip packages/files/variables not explicitly mapped
|
|
67
|
+
*
|
|
68
|
+
* @param options - Configuration options for the simple mapping algorithm
|
|
69
|
+
* @returns A function compatible with LangTagCLIConfig.import.onImport
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* import { simpleMappingImportAlgorithm } from '@lang-tag/cli/algorithms';
|
|
74
|
+
*
|
|
75
|
+
* export default {
|
|
76
|
+
* import: {
|
|
77
|
+
* onImport: simpleMappingImportAlgorithm({
|
|
78
|
+
* mappings: [
|
|
79
|
+
* {
|
|
80
|
+
* packageName: '@company/ui-components',
|
|
81
|
+
* files: [
|
|
82
|
+
* {
|
|
83
|
+
* sourceFile: 'components/button.ts',
|
|
84
|
+
* targetFile: 'ui/buttons.ts',
|
|
85
|
+
* variables: {
|
|
86
|
+
* 'primaryButton': 'button',
|
|
87
|
+
* 'secondaryButton': 'secondary',
|
|
88
|
+
* 'tertiaryButton': undefined // keep original name
|
|
89
|
+
* }
|
|
90
|
+
* },
|
|
91
|
+
* {
|
|
92
|
+
* sourceFile: 'components/input.ts',
|
|
93
|
+
* targetFile: 'ui/inputs.ts',
|
|
94
|
+
* variables: {
|
|
95
|
+
* 'textInput': 'input',
|
|
96
|
+
* 'passwordInput': 'password'
|
|
97
|
+
* }
|
|
98
|
+
* }
|
|
99
|
+
* ]
|
|
100
|
+
* },
|
|
101
|
+
* {
|
|
102
|
+
* packageName: '@company/utils',
|
|
103
|
+
* files: [
|
|
104
|
+
* {
|
|
105
|
+
* sourceFile: 'helpers.ts',
|
|
106
|
+
* targetFile: 'utils/helpers.ts',
|
|
107
|
+
* variables: {
|
|
108
|
+
* 'formatDate': undefined,
|
|
109
|
+
* 'formatCurrency': 'currency'
|
|
110
|
+
* }
|
|
111
|
+
* }
|
|
112
|
+
* ]
|
|
113
|
+
* }
|
|
114
|
+
* ]
|
|
115
|
+
* })
|
|
116
|
+
* }
|
|
117
|
+
* };
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare function simpleMappingImportAlgorithm(options: SimpleMappingImportAlgorithmOptions): (event: LangTagCLIImportEvent) => void;
|
package/algorithms/index.cjs
CHANGED
|
@@ -1,27 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
3
|
+
const flexibleImportAlgorithm = require("../flexible-import-algorithm-Fa-l4jWj.cjs");
|
|
4
4
|
const path = require("pathe");
|
|
5
5
|
const process = require("node:process");
|
|
6
|
-
|
|
7
|
-
function _interopNamespaceDefault(e) {
|
|
8
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
9
|
-
if (e) {
|
|
10
|
-
for (const k in e) {
|
|
11
|
-
if (k !== "default") {
|
|
12
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
get: () => e[k]
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
n.default = e;
|
|
21
|
-
return Object.freeze(n);
|
|
22
|
-
}
|
|
23
|
-
const caseLib__namespace = /* @__PURE__ */ _interopNamespaceDefault(caseLib);
|
|
24
|
-
class DictionaryCollector extends namespaceCollector.TranslationsCollector {
|
|
6
|
+
class DictionaryCollector extends flexibleImportAlgorithm.TranslationsCollector {
|
|
25
7
|
constructor(options = {
|
|
26
8
|
appendNamespaceToPath: false
|
|
27
9
|
}) {
|
|
@@ -57,9 +39,9 @@ class DictionaryCollector extends namespaceCollector.TranslationsCollector {
|
|
|
57
39
|
const baseDictionaryFile = path.join(this.config.localesDirectory, `${this.config.baseLanguageCode}.json`);
|
|
58
40
|
if (clean) {
|
|
59
41
|
this.logger.info("Removing {file}", { file: baseDictionaryFile });
|
|
60
|
-
await
|
|
42
|
+
await flexibleImportAlgorithm.$LT_RemoveFile(baseDictionaryFile);
|
|
61
43
|
}
|
|
62
|
-
await
|
|
44
|
+
await flexibleImportAlgorithm.$LT_EnsureDirectoryExists(this.config.localesDirectory);
|
|
63
45
|
}
|
|
64
46
|
async resolveCollectionFilePath(baseLanguageCode) {
|
|
65
47
|
return path.resolve(
|
|
@@ -160,12 +142,12 @@ function pathBasedConfigGenerator(options = {}) {
|
|
|
160
142
|
namespace = namespace.toLowerCase();
|
|
161
143
|
}
|
|
162
144
|
if (namespaceCase) {
|
|
163
|
-
namespace = applyCaseTransform(namespace, namespaceCase);
|
|
145
|
+
namespace = flexibleImportAlgorithm.applyCaseTransform(namespace, namespaceCase);
|
|
164
146
|
}
|
|
165
147
|
}
|
|
166
148
|
if (path$1 && pathCase) {
|
|
167
149
|
const pathParts = path$1.split(".");
|
|
168
|
-
const transformedParts = pathParts.map((part) => applyCaseTransform(part, pathCase));
|
|
150
|
+
const transformedParts = pathParts.map((part) => flexibleImportAlgorithm.applyCaseTransform(part, pathCase));
|
|
169
151
|
path$1 = transformedParts.join(".");
|
|
170
152
|
}
|
|
171
153
|
const newConfig = event.config ? { ...event.config } : {};
|
|
@@ -358,13 +340,6 @@ function applyPathRules(segments, structure) {
|
|
|
358
340
|
}
|
|
359
341
|
return result;
|
|
360
342
|
}
|
|
361
|
-
function applyCaseTransform(str, caseType) {
|
|
362
|
-
const caseFunction = caseLib__namespace[caseType];
|
|
363
|
-
if (typeof caseFunction === "function") {
|
|
364
|
-
return caseFunction(str);
|
|
365
|
-
}
|
|
366
|
-
return str;
|
|
367
|
-
}
|
|
368
343
|
function extractRootDirectoriesFromIncludes(includes) {
|
|
369
344
|
const directories = /* @__PURE__ */ new Set();
|
|
370
345
|
for (const pattern of includes) {
|
|
@@ -464,8 +439,72 @@ function prependNamespaceToPath(options = {}) {
|
|
|
464
439
|
}, TRIGGER_NAME);
|
|
465
440
|
};
|
|
466
441
|
}
|
|
467
|
-
|
|
442
|
+
function simpleMappingImportAlgorithm(options) {
|
|
443
|
+
const { mappings, configRemap } = options;
|
|
444
|
+
const packageMap = /* @__PURE__ */ new Map();
|
|
445
|
+
const fileMap = /* @__PURE__ */ new Map();
|
|
446
|
+
for (const mapping of mappings) {
|
|
447
|
+
packageMap.set(mapping.packageName, mapping);
|
|
448
|
+
const files = /* @__PURE__ */ new Map();
|
|
449
|
+
for (const file of mapping.files) {
|
|
450
|
+
files.set(file.sourceFile, file);
|
|
451
|
+
}
|
|
452
|
+
fileMap.set(mapping.packageName, files);
|
|
453
|
+
}
|
|
454
|
+
return (event) => {
|
|
455
|
+
const { exports: exports2, importManager, logger } = event;
|
|
456
|
+
for (const { packageJSON, exportData } of exports2) {
|
|
457
|
+
const packageName = packageJSON.name || "unknown-package";
|
|
458
|
+
const packageMapping = packageMap.get(packageName);
|
|
459
|
+
if (!packageMapping) {
|
|
460
|
+
logger.debug(`Skipping unmapped package: ${packageName}`);
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
logger.debug(`Processing mapped package: ${packageName}`);
|
|
464
|
+
for (const file of exportData.files) {
|
|
465
|
+
const sourceFile = file.relativeFilePath;
|
|
466
|
+
const packageFiles = fileMap.get(packageName);
|
|
467
|
+
if (!packageFiles) continue;
|
|
468
|
+
const fileMapping = packageFiles.get(sourceFile);
|
|
469
|
+
if (!fileMapping) {
|
|
470
|
+
logger.debug(`Skipping unmapped file: ${packageName}/${sourceFile}`);
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
logger.debug(`Processing mapped file: ${packageName}/${sourceFile} -> ${fileMapping.targetFile}`);
|
|
474
|
+
for (const tag of file.tags) {
|
|
475
|
+
const originalVariableName = tag.variableName;
|
|
476
|
+
if (!originalVariableName || !(originalVariableName in fileMapping.variables)) {
|
|
477
|
+
logger.debug(`Skipping unmapped variable: ${originalVariableName} in ${packageName}/${sourceFile}`);
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
const newVariableName = fileMapping.variables[originalVariableName] || originalVariableName;
|
|
481
|
+
const targetFilePath = fileMapping.targetFile;
|
|
482
|
+
let finalConfig = tag.config;
|
|
483
|
+
if (configRemap) {
|
|
484
|
+
finalConfig = configRemap(tag.config, {
|
|
485
|
+
packageName,
|
|
486
|
+
sourceFile,
|
|
487
|
+
targetFile: targetFilePath,
|
|
488
|
+
variableName: newVariableName,
|
|
489
|
+
originalVariableName
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
importManager.importTag(targetFilePath, {
|
|
493
|
+
variableName: newVariableName,
|
|
494
|
+
translations: tag.translations,
|
|
495
|
+
config: finalConfig
|
|
496
|
+
});
|
|
497
|
+
logger.debug(`Imported: ${originalVariableName} -> ${newVariableName} in ${targetFilePath}`);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
exports.NamespaceCollector = flexibleImportAlgorithm.NamespaceCollector;
|
|
504
|
+
exports.applyCaseTransform = flexibleImportAlgorithm.applyCaseTransform;
|
|
505
|
+
exports.flexibleImportAlgorithm = flexibleImportAlgorithm.flexibleImportAlgorithm;
|
|
468
506
|
exports.DictionaryCollector = DictionaryCollector;
|
|
469
507
|
exports.configKeeper = configKeeper;
|
|
470
508
|
exports.pathBasedConfigGenerator = pathBasedConfigGenerator;
|
|
471
509
|
exports.prependNamespaceToPath = prependNamespaceToPath;
|
|
510
|
+
exports.simpleMappingImportAlgorithm = simpleMappingImportAlgorithm;
|
package/algorithms/index.d.ts
CHANGED
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
* This module provides access to all available algorithms organized by category:
|
|
5
5
|
* - Collectors: Define how translation tags are organized into output files
|
|
6
6
|
* - Config Generation: Customize tag configuration generation
|
|
7
|
-
* - Import: Handle importing translation libraries
|
|
7
|
+
* - Import: Handle importing translation libraries
|
|
8
|
+
* - Case Utils: Common case transformation utilities
|
|
8
9
|
*/
|
|
10
|
+
export * from './case-utils';
|
|
9
11
|
export * from './collector/index.ts';
|
|
10
12
|
export * from './config-generation/index.ts';
|
|
13
|
+
export * from './import/index.ts';
|
package/algorithms/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { T as TranslationsCollector, e as $LT_RemoveFile, c as $LT_EnsureDirectoryExists } from "../
|
|
2
|
-
import { N } from "../
|
|
1
|
+
import { T as TranslationsCollector, e as $LT_RemoveFile, c as $LT_EnsureDirectoryExists, g as applyCaseTransform } from "../flexible-import-algorithm-C-S1c742.js";
|
|
2
|
+
import { N, f } from "../flexible-import-algorithm-C-S1c742.js";
|
|
3
3
|
import path, { resolve, sep } from "pathe";
|
|
4
4
|
import process__default from "node:process";
|
|
5
|
-
import * as caseLib from "case";
|
|
6
5
|
class DictionaryCollector extends TranslationsCollector {
|
|
7
6
|
constructor(options = {
|
|
8
7
|
appendNamespaceToPath: false
|
|
@@ -340,13 +339,6 @@ function applyPathRules(segments, structure) {
|
|
|
340
339
|
}
|
|
341
340
|
return result;
|
|
342
341
|
}
|
|
343
|
-
function applyCaseTransform(str, caseType) {
|
|
344
|
-
const caseFunction = caseLib[caseType];
|
|
345
|
-
if (typeof caseFunction === "function") {
|
|
346
|
-
return caseFunction(str);
|
|
347
|
-
}
|
|
348
|
-
return str;
|
|
349
|
-
}
|
|
350
342
|
function extractRootDirectoriesFromIncludes(includes) {
|
|
351
343
|
const directories = /* @__PURE__ */ new Set();
|
|
352
344
|
for (const pattern of includes) {
|
|
@@ -356,7 +348,7 @@ function extractRootDirectoriesFromIncludes(includes) {
|
|
|
356
348
|
const firstSegment = match[1];
|
|
357
349
|
const groupMatch = firstSegment.match(/^[\(\[]([^\)\]]+)[\)\]]$/);
|
|
358
350
|
if (groupMatch) {
|
|
359
|
-
const groupDirectories = groupMatch[1].split("|").map((
|
|
351
|
+
const groupDirectories = groupMatch[1].split("|").map((f2) => f2.trim());
|
|
360
352
|
groupDirectories.forEach((directory) => directories.add(directory));
|
|
361
353
|
} else {
|
|
362
354
|
directories.add(firstSegment);
|
|
@@ -446,10 +438,74 @@ function prependNamespaceToPath(options = {}) {
|
|
|
446
438
|
}, TRIGGER_NAME);
|
|
447
439
|
};
|
|
448
440
|
}
|
|
441
|
+
function simpleMappingImportAlgorithm(options) {
|
|
442
|
+
const { mappings, configRemap } = options;
|
|
443
|
+
const packageMap = /* @__PURE__ */ new Map();
|
|
444
|
+
const fileMap = /* @__PURE__ */ new Map();
|
|
445
|
+
for (const mapping of mappings) {
|
|
446
|
+
packageMap.set(mapping.packageName, mapping);
|
|
447
|
+
const files = /* @__PURE__ */ new Map();
|
|
448
|
+
for (const file of mapping.files) {
|
|
449
|
+
files.set(file.sourceFile, file);
|
|
450
|
+
}
|
|
451
|
+
fileMap.set(mapping.packageName, files);
|
|
452
|
+
}
|
|
453
|
+
return (event) => {
|
|
454
|
+
const { exports, importManager, logger } = event;
|
|
455
|
+
for (const { packageJSON, exportData } of exports) {
|
|
456
|
+
const packageName = packageJSON.name || "unknown-package";
|
|
457
|
+
const packageMapping = packageMap.get(packageName);
|
|
458
|
+
if (!packageMapping) {
|
|
459
|
+
logger.debug(`Skipping unmapped package: ${packageName}`);
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
logger.debug(`Processing mapped package: ${packageName}`);
|
|
463
|
+
for (const file of exportData.files) {
|
|
464
|
+
const sourceFile = file.relativeFilePath;
|
|
465
|
+
const packageFiles = fileMap.get(packageName);
|
|
466
|
+
if (!packageFiles) continue;
|
|
467
|
+
const fileMapping = packageFiles.get(sourceFile);
|
|
468
|
+
if (!fileMapping) {
|
|
469
|
+
logger.debug(`Skipping unmapped file: ${packageName}/${sourceFile}`);
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
logger.debug(`Processing mapped file: ${packageName}/${sourceFile} -> ${fileMapping.targetFile}`);
|
|
473
|
+
for (const tag of file.tags) {
|
|
474
|
+
const originalVariableName = tag.variableName;
|
|
475
|
+
if (!originalVariableName || !(originalVariableName in fileMapping.variables)) {
|
|
476
|
+
logger.debug(`Skipping unmapped variable: ${originalVariableName} in ${packageName}/${sourceFile}`);
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
const newVariableName = fileMapping.variables[originalVariableName] || originalVariableName;
|
|
480
|
+
const targetFilePath = fileMapping.targetFile;
|
|
481
|
+
let finalConfig = tag.config;
|
|
482
|
+
if (configRemap) {
|
|
483
|
+
finalConfig = configRemap(tag.config, {
|
|
484
|
+
packageName,
|
|
485
|
+
sourceFile,
|
|
486
|
+
targetFile: targetFilePath,
|
|
487
|
+
variableName: newVariableName,
|
|
488
|
+
originalVariableName
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
importManager.importTag(targetFilePath, {
|
|
492
|
+
variableName: newVariableName,
|
|
493
|
+
translations: tag.translations,
|
|
494
|
+
config: finalConfig
|
|
495
|
+
});
|
|
496
|
+
logger.debug(`Imported: ${originalVariableName} -> ${newVariableName} in ${targetFilePath}`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
}
|
|
449
502
|
export {
|
|
450
503
|
DictionaryCollector,
|
|
451
504
|
N as NamespaceCollector,
|
|
505
|
+
applyCaseTransform,
|
|
452
506
|
configKeeper,
|
|
507
|
+
f as flexibleImportAlgorithm,
|
|
453
508
|
pathBasedConfigGenerator,
|
|
454
|
-
prependNamespaceToPath
|
|
509
|
+
prependNamespaceToPath,
|
|
510
|
+
simpleMappingImportAlgorithm
|
|
455
511
|
};
|