@lang-tag/cli 0.14.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.
@@ -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;
@@ -0,0 +1,17 @@
1
+ import { TranslationsCollector } from './type.ts';
2
+ import { LangTagCLIProcessedTag } from '../../config.ts';
3
+ interface Options {
4
+ appendNamespaceToPath: boolean;
5
+ }
6
+ export declare class DictionaryCollector extends TranslationsCollector {
7
+ private readonly options;
8
+ private clean;
9
+ constructor(options?: Options);
10
+ aggregateCollection(namespace: string): string;
11
+ transformTag(tag: LangTagCLIProcessedTag): LangTagCLIProcessedTag;
12
+ preWrite(clean: boolean): Promise<void>;
13
+ resolveCollectionFilePath(baseLanguageCode: string): Promise<any>;
14
+ onMissingCollection(baseLanguageCode: string): Promise<void>;
15
+ postWrite(changedCollections: string[]): Promise<void>;
16
+ }
17
+ export {};
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Translation collectors for organizing translation tags into output files.
3
+ *
4
+ * These collectors define how translation tags are grouped and written to files
5
+ * during the collection process. Each collector implements a different strategy
6
+ * for organizing translations (e.g., single dictionary vs. namespace-based files).
7
+ */
8
+ export { DictionaryCollector } from './dictionary-collector.ts';
9
+ export { NamespaceCollector } from './namespace-collector.ts';
10
+ export { type TranslationsCollector } from './type.ts';
@@ -0,0 +1,12 @@
1
+ import { TranslationsCollector } from './type.ts';
2
+ import { LangTagCLIProcessedTag } from '../../config.ts';
3
+ export declare class NamespaceCollector extends TranslationsCollector {
4
+ private clean;
5
+ private languageDirectory;
6
+ aggregateCollection(namespace: string): string;
7
+ transformTag(tag: LangTagCLIProcessedTag): LangTagCLIProcessedTag;
8
+ preWrite(clean: boolean): Promise<void>;
9
+ resolveCollectionFilePath(collectionName: string): Promise<any>;
10
+ onMissingCollection(collectionName: string): Promise<void>;
11
+ postWrite(changedCollections: string[]): Promise<void>;
12
+ }
@@ -0,0 +1,12 @@
1
+ import { LangTagCLIConfig, LangTagCLIProcessedTag } from '../../config.ts';
2
+ import { LangTagCLILogger } from '../../logger.ts';
3
+ export declare abstract class TranslationsCollector {
4
+ config: LangTagCLIConfig;
5
+ logger: LangTagCLILogger;
6
+ abstract aggregateCollection(namespace: string): string;
7
+ abstract transformTag(tag: LangTagCLIProcessedTag): LangTagCLIProcessedTag;
8
+ abstract preWrite(clean?: boolean): Promise<void>;
9
+ abstract resolveCollectionFilePath(collectionName: string): Promise<string>;
10
+ abstract onMissingCollection(collectionName: string): Promise<void>;
11
+ abstract postWrite(changedCollections: string[]): Promise<void>;
12
+ }
@@ -6,3 +6,4 @@
6
6
  */
7
7
  export { pathBasedConfigGenerator, type PathBasedConfigGeneratorOptions } from './path-based-config-generator.ts';
8
8
  export { configKeeper, type ConfigKeeperOptions, type ConfigKeeperMode } from './config-keeper.ts';
9
+ export { prependNamespaceToPath, type PrependNamespaceToPathOptions } from './prepend-namespace-to-path.ts';
@@ -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?: 'camel' | 'capital' | 'constant' | 'dot' | 'header' | 'kebab' | 'lower' | 'no' | 'param' | 'pascal' | 'path' | 'sentence' | 'snake' | 'swap' | 'title' | 'upper';
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?: 'camel' | 'capital' | 'constant' | 'dot' | 'header' | 'kebab' | 'lower' | 'no' | 'param' | 'pascal' | 'path' | 'sentence' | 'snake' | 'swap' | 'title' | 'upper';
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,28 @@
1
+ import { LangTagCLIConfigGenerationEvent } from '../../config.ts';
2
+ /**
3
+ * Options for the prependNamespaceToPath algorithm.
4
+ */
5
+ export interface PrependNamespaceToPathOptions {
6
+ }
7
+ /**
8
+ * Algorithm that prepends the namespace to the path in translation tag configurations.
9
+ *
10
+ * This is useful when you want to flatten the namespace structure by moving the namespace
11
+ * into the path, effectively removing the namespace separation.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // Before: { namespace: 'common', path: 'hello.world' }
16
+ * // After: { path: 'common.hello.world' }
17
+ *
18
+ * // Before: { namespace: 'common' }
19
+ * // After: { path: 'common' }
20
+ *
21
+ * // Before: {}
22
+ * // After: { path: 'common' }
23
+ * ```
24
+ *
25
+ * @param options Configuration options for the algorithm
26
+ * @returns A function compatible with LangTagCLIConfigGenerationEvent
27
+ */
28
+ export declare function prependNamespaceToPath(options?: PrependNamespaceToPathOptions): (event: LangTagCLIConfigGenerationEvent) => Promise<void>;
@@ -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;