@lang-tag/cli 0.16.0 → 0.18.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/README.md +23 -14
- package/algorithms/collector/dictionary-collector.d.ts +2 -2
- package/algorithms/collector/index.d.ts +3 -3
- package/algorithms/collector/namespace-collector.d.ts +2 -2
- package/algorithms/collector/type.d.ts +2 -2
- package/algorithms/config-generation/config-keeper.d.ts +1 -1
- package/algorithms/config-generation/index.d.ts +3 -3
- package/algorithms/config-generation/path-based-config-generator.d.ts +1 -1
- package/algorithms/config-generation/prepend-namespace-to-path.d.ts +1 -1
- package/algorithms/import/flexible-import-algorithm.d.ts +1 -1
- package/algorithms/import/index.d.ts +2 -2
- package/algorithms/import/simple-mapping-import-algorithm.d.ts +1 -1
- package/algorithms/index.cjs +380 -27
- package/algorithms/index.d.ts +3 -3
- package/algorithms/index.js +361 -25
- package/chunks/namespace-collector.cjs +75 -0
- package/chunks/namespace-collector.js +76 -0
- package/index.cjs +1463 -862
- package/index.js +1425 -824
- package/logger.d.ts +1 -1
- package/package.json +5 -1
- package/templates/config/config.mustache +180 -0
- package/templates/config/generation-algorithm.mustache +73 -0
- package/{config.d.ts → type.d.ts} +3 -3
- package/flexible-import-algorithm-C-S1c742.js +0 -311
- package/flexible-import-algorithm-Fa-l4jWj.cjs +0 -327
- package/templates/config/init-config.mustache +0 -1
package/logger.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lang-tag/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -33,6 +33,10 @@
|
|
|
33
33
|
"lang-tag": "*"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"@inquirer/checkbox": "^4.3.0",
|
|
37
|
+
"@inquirer/confirm": "^5.1.19",
|
|
38
|
+
"@inquirer/prompts": "^7.9.0",
|
|
39
|
+
"@inquirer/select": "^4.4.0",
|
|
36
40
|
"acorn": "^8.15.0",
|
|
37
41
|
"case": "^1.6.3",
|
|
38
42
|
"chokidar": "^4.0.3",
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lang Tag CLI Configuration
|
|
3
|
+
*
|
|
4
|
+
* This file was generated. You can modify it to customize your translation workflow.
|
|
5
|
+
* Documentation: https://github.com/TheTonsOfCode/lang-tag-cli
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
{{#needsAlgorithms}}
|
|
9
|
+
{{#isCJS}}const{{/isCJS}}{{^isCJS}}import{{/isCJS}} {
|
|
10
|
+
{{#needsPathBasedImport}}
|
|
11
|
+
pathBasedConfigGenerator,
|
|
12
|
+
{{/needsPathBasedImport}}
|
|
13
|
+
{{#useKeeper}}
|
|
14
|
+
configKeeper,
|
|
15
|
+
{{/useKeeper}}
|
|
16
|
+
{{#isDictionaryCollector}}
|
|
17
|
+
DictionaryCollector,
|
|
18
|
+
{{/isDictionaryCollector}}
|
|
19
|
+
{{#isModifiedNamespaceCollector}}
|
|
20
|
+
NamespaceCollector,
|
|
21
|
+
{{/isModifiedNamespaceCollector}}
|
|
22
|
+
{{#importLibraries}}
|
|
23
|
+
flexibleImportAlgorithm,
|
|
24
|
+
{{/importLibraries}}
|
|
25
|
+
} {{#isCJS}}= require('@lang-tag/cli/algorithms');{{/isCJS}}{{^isCJS}}from '@lang-tag/cli/algorithms';{{/isCJS}}
|
|
26
|
+
{{/needsAlgorithms}}
|
|
27
|
+
|
|
28
|
+
{{#needsPathBasedImport}}
|
|
29
|
+
{{>generation-algorithm}}
|
|
30
|
+
{{/needsPathBasedImport}}
|
|
31
|
+
{{#useKeeper}}
|
|
32
|
+
{{#addComments}}
|
|
33
|
+
// Preserves tags marked with keep property. Example: {{tagName}}({ text: 'text'}, { namespace: 'custom', keep: 'namespace' })
|
|
34
|
+
{{/addComments}}
|
|
35
|
+
const keeper = configKeeper({ propertyName: 'keep' });
|
|
36
|
+
|
|
37
|
+
{{/useKeeper}}
|
|
38
|
+
/** @type {import('@lang-tag/cli/type').LangTagCLIConfig} */
|
|
39
|
+
const config = {
|
|
40
|
+
{{#needsTagName}}
|
|
41
|
+
tagName: '{{tagName}}',
|
|
42
|
+
{{/needsTagName}}
|
|
43
|
+
{{#isLibrary}}
|
|
44
|
+
isLibrary: true,
|
|
45
|
+
{{/isLibrary}}
|
|
46
|
+
{{#addComments}}
|
|
47
|
+
// Directories where we’re going to look for language tags
|
|
48
|
+
{{/addComments}}
|
|
49
|
+
includes: [{{includes}}],
|
|
50
|
+
excludes: [{{excludes}}],
|
|
51
|
+
{{^isLibrary}}
|
|
52
|
+
localesDirectory: '{{localesDirectory}}',
|
|
53
|
+
{{/isLibrary}}
|
|
54
|
+
{{#addComments}}
|
|
55
|
+
// The source translations language used in your code (e.g., 'en', 'pl')
|
|
56
|
+
{{/addComments}}
|
|
57
|
+
baseLanguageCode: '{{baseLanguageCode}}',
|
|
58
|
+
{{#hasConfigGeneration}}
|
|
59
|
+
{{#usePathBased}}
|
|
60
|
+
{{#addComments}}
|
|
61
|
+
/**
|
|
62
|
+
* Automatically generates namespace/path from file location.
|
|
63
|
+
* Example: src/features/auth/LoginForm.tsx → { namespace: 'auth', path: 'loginForm' }
|
|
64
|
+
*
|
|
65
|
+
* To customize, modify generationAlgorithm options above or add custom logic:
|
|
66
|
+
* - Change case formatting (namespaceCase, pathCase)
|
|
67
|
+
* - Ignore specific directories (ignoreDirectories)
|
|
68
|
+
* - Add path transformation rules (pathRules)
|
|
69
|
+
*/
|
|
70
|
+
{{/addComments}}
|
|
71
|
+
onConfigGeneration: async event => {
|
|
72
|
+
// We do not modify imported configurations
|
|
73
|
+
if (event.isImportedLibrary) return;
|
|
74
|
+
{{#useKeeper}}
|
|
75
|
+
if (event.config?.keep === 'both') return;
|
|
76
|
+
{{/useKeeper}}
|
|
77
|
+
|
|
78
|
+
await generationAlgorithm(event);
|
|
79
|
+
{{#useKeeper}}
|
|
80
|
+
await keeper(event);
|
|
81
|
+
{{/useKeeper}}
|
|
82
|
+
},
|
|
83
|
+
{{/usePathBased}}
|
|
84
|
+
{{#useCustom}}
|
|
85
|
+
{{#addComments}}
|
|
86
|
+
/**
|
|
87
|
+
* Custom hook to generate namespace/path for each tag based on file location.
|
|
88
|
+
*
|
|
89
|
+
* Sample properties:
|
|
90
|
+
* - event.relativePath: File path relative to project root (e.g., 'src/auth/Login.tsx')
|
|
91
|
+
* - event.config: Current tag config (may be undefined or contain user-provided values)
|
|
92
|
+
* - event.save(newConfig): Save new config. Example: event.save({ namespace: 'auth', path: 'login' })
|
|
93
|
+
*
|
|
94
|
+
* Example - generate from directory structure:
|
|
95
|
+
* const segments = event.relativePath.split('/').slice(1, -1);
|
|
96
|
+
* event.save({ namespace: segments[0], path: segments.slice(1).join('.') });
|
|
97
|
+
*/
|
|
98
|
+
{{/addComments}}
|
|
99
|
+
onConfigGeneration: async event => {
|
|
100
|
+
// We do not modify imported configurations
|
|
101
|
+
if (event.isImportedLibrary) return;
|
|
102
|
+
|
|
103
|
+
{{#useKeeper}}
|
|
104
|
+
if (event.config?.keep === 'both') return;
|
|
105
|
+
{{/useKeeper}}
|
|
106
|
+
// TODO: Implement your custom config generation logic here
|
|
107
|
+
// event.save({
|
|
108
|
+
// namespace: 'your-namespace',
|
|
109
|
+
// path: 'your.path'
|
|
110
|
+
// });
|
|
111
|
+
{{#useKeeper}}
|
|
112
|
+
await keeper(event);
|
|
113
|
+
{{/useKeeper}}
|
|
114
|
+
},
|
|
115
|
+
{{/useCustom}}
|
|
116
|
+
{{/hasConfigGeneration}}
|
|
117
|
+
{{#hasCollectContent}}
|
|
118
|
+
collect: {
|
|
119
|
+
{{#isDictionaryCollector}}
|
|
120
|
+
{{#addComments}}
|
|
121
|
+
// All translations in one file per language. Change to NamespaceCollector() for separate files per namespace.
|
|
122
|
+
{{/addComments}}
|
|
123
|
+
collector: new DictionaryCollector(),
|
|
124
|
+
{{/isDictionaryCollector}}
|
|
125
|
+
{{#isModifiedNamespaceCollector}}
|
|
126
|
+
{{#addComments}}
|
|
127
|
+
// Separate file per namespace (e.g., locales/en/auth.json, locales/en/dashboard.json)
|
|
128
|
+
{{/addComments}}
|
|
129
|
+
collector: new NamespaceCollector(),
|
|
130
|
+
{{/isModifiedNamespaceCollector}}
|
|
131
|
+
{{^isDictionary}}
|
|
132
|
+
{{^isDefaultNamespace}}
|
|
133
|
+
{{#addComments}}
|
|
134
|
+
// Tags without config or namespace in config will use this
|
|
135
|
+
{{/addComments}}
|
|
136
|
+
defaultNamespace: '{{defaultNamespace}}',
|
|
137
|
+
{{/isDefaultNamespace}}
|
|
138
|
+
{{/isDictionary}}
|
|
139
|
+
{{#interfereWithCollection}}
|
|
140
|
+
{{#addComments}}
|
|
141
|
+
/**
|
|
142
|
+
* Called for each duplicate key conflict (same namespace + path, different values).
|
|
143
|
+
* To stop on first conflict, call event.exit() here instead of in onCollectFinish.
|
|
144
|
+
*/
|
|
145
|
+
{{/addComments}}
|
|
146
|
+
onConflictResolution: async event => {
|
|
147
|
+
await event.logger.conflict(event.conflict, true);
|
|
148
|
+
// By default, continue processing even if conflicts occur
|
|
149
|
+
// Call event.exit(); to terminate the process upon the first conflict
|
|
150
|
+
},
|
|
151
|
+
{{#addComments}}
|
|
152
|
+
/**
|
|
153
|
+
* Called after collection completes. Check event.conflicts array to handle all conflicts.
|
|
154
|
+
* Remove this hook to allow merging despite conflicts.
|
|
155
|
+
*/
|
|
156
|
+
{{/addComments}}
|
|
157
|
+
onCollectFinish: event => {
|
|
158
|
+
if (event.conflicts.length) event.exit(); // Stop the process to avoid merging on conflict
|
|
159
|
+
}
|
|
160
|
+
{{/interfereWithCollection}}
|
|
161
|
+
},
|
|
162
|
+
{{/hasCollectContent}}
|
|
163
|
+
{{#importLibraries}}
|
|
164
|
+
{{#addComments}}
|
|
165
|
+
/**
|
|
166
|
+
* Imports translations from external libraries (node modules packages containing exported 'lang-tags.json').
|
|
167
|
+
* - dir: Where to generate import files
|
|
168
|
+
* - tagImportPath: Update path to your tag function
|
|
169
|
+
* - onImport: Controls file naming/structure (see flexibleImportAlgorithm options)
|
|
170
|
+
*/
|
|
171
|
+
{{/addComments}}
|
|
172
|
+
import: {
|
|
173
|
+
dir: 'src/lang-libraries',
|
|
174
|
+
tagImportPath: 'import { {{tagName}} } from "@/my-lang-tag-path"',
|
|
175
|
+
onImport: flexibleImportAlgorithm({ filePath: { includePackageInPath: true } })
|
|
176
|
+
},
|
|
177
|
+
{{/importLibraries}}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
{{#isCJS}}module.exports = config;{{/isCJS}}{{^isCJS}}export default config;{{/isCJS}}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{{#addComments}}
|
|
2
|
+
/**
|
|
3
|
+
* Generates namespace/path from directory structure (filename is excluded).
|
|
4
|
+
*
|
|
5
|
+
* Example with current settings (assuming includes: ['src/**']):
|
|
6
|
+
* File: src/(admin)/features/auth/utils/LoginForm.tsx
|
|
7
|
+
* 1. Remove filename → src/(admin)/features/auth/utils
|
|
8
|
+
* 2. Remove root 'src' → (admin)/features/auth/utils
|
|
9
|
+
* 3. Remove bracketed '(admin)' → features/auth/utils
|
|
10
|
+
* 4. Remove 'utils' (in ignoreDirectories) → features/auth
|
|
11
|
+
* 5. First segment = namespace, rest = path → namespace: 'features', path: 'auth'
|
|
12
|
+
*/
|
|
13
|
+
{{/addComments}}
|
|
14
|
+
const generationAlgorithm = pathBasedConfigGenerator({
|
|
15
|
+
{{#addComments}}
|
|
16
|
+
// Removes root directory from includes patterns (e.g., 'src' if includes: ['src/**'])
|
|
17
|
+
{{/addComments}}
|
|
18
|
+
ignoreIncludesRootDirectories: true,
|
|
19
|
+
{{#addComments}}
|
|
20
|
+
// Directories with () or [] are NOT used for namespace/path generation
|
|
21
|
+
// true: app/(admin)/[id]/page.tsx → app/page (completely removed)
|
|
22
|
+
// false: app/(admin)/[id]/page.tsx → app/admin/id/page (brackets removed, names kept)
|
|
23
|
+
{{/addComments}}
|
|
24
|
+
removeBracketedDirectories: true,
|
|
25
|
+
{{#addComments}}
|
|
26
|
+
// Case for namespace. Options: 'kebab', 'camel', 'pascal', 'snake'
|
|
27
|
+
{{/addComments}}
|
|
28
|
+
namespaceCase: 'kebab',
|
|
29
|
+
{{#addComments}}
|
|
30
|
+
// Case for path. Options: 'kebab', 'camel', 'pascal', 'snake'
|
|
31
|
+
{{/addComments}}
|
|
32
|
+
pathCase: 'camel',
|
|
33
|
+
{{#addComments}}
|
|
34
|
+
// If namespace equals defaultNamespace, omit it from config (namespace becomes undefined)
|
|
35
|
+
{{/addComments}}
|
|
36
|
+
clearOnDefaultNamespace: true,
|
|
37
|
+
{{#addComments}}
|
|
38
|
+
// Skip these directory names globally when building namespace/path
|
|
39
|
+
{{/addComments}}
|
|
40
|
+
ignoreDirectories: ['core', 'utils', 'helpers'],{{#addComments}}
|
|
41
|
+
/**
|
|
42
|
+
* Advanced: pathRules for hierarchical transformations
|
|
43
|
+
*
|
|
44
|
+
* Special operators:
|
|
45
|
+
* - _: false → ignore current directory, continue with nested rules
|
|
46
|
+
* - '>': 'newName' → rename directory in namespace
|
|
47
|
+
* - '>>': redirect → jump to different namespace
|
|
48
|
+
* - '>>': 'namespace' → use specified namespace, remaining segments become path
|
|
49
|
+
* - '>>': { namespace: 'ui', pathPrefix: 'admin' } → jump to 'ui' namespace with 'admin.' prefix
|
|
50
|
+
* - '>>': '' or null → use current directory as namespace
|
|
51
|
+
*
|
|
52
|
+
* Example usage:
|
|
53
|
+
*/
|
|
54
|
+
// pathRules: {
|
|
55
|
+
// app: {
|
|
56
|
+
// dashboard: {
|
|
57
|
+
// _: false, // ignore "dashboard" but continue with nested rules
|
|
58
|
+
// modules: false // also ignore "modules"
|
|
59
|
+
// },
|
|
60
|
+
// admin: {
|
|
61
|
+
// '>': 'management', // rename "admin" to "management"
|
|
62
|
+
// users: false // ignore "users"
|
|
63
|
+
// },
|
|
64
|
+
// components: {
|
|
65
|
+
// '>>': { // jump to 'ui' namespace, prefix all paths with 'components'
|
|
66
|
+
// namespace: 'ui',
|
|
67
|
+
// pathPrefix: 'components'
|
|
68
|
+
// }
|
|
69
|
+
// }
|
|
70
|
+
// }
|
|
71
|
+
// }{{/addComments}}
|
|
72
|
+
});
|
|
73
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { LangTagTranslationsConfig } from 'lang-tag';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { TranslationsCollector } from './algorithms/collector/type';
|
|
3
|
+
import { LangTagCLILogger } from './logger';
|
|
4
4
|
export interface LangTagCLIConfig {
|
|
5
5
|
/**
|
|
6
6
|
* Tag name used to mark translations in code.
|
|
@@ -214,6 +214,7 @@ export interface LangTagCLIConfigGenerationEvent {
|
|
|
214
214
|
* To update the configuration, use the `save()` method with a new configuration object.
|
|
215
215
|
*/
|
|
216
216
|
readonly config: Readonly<LangTagTranslationsConfig> | undefined;
|
|
217
|
+
readonly logger: LangTagCLILogger;
|
|
217
218
|
readonly langTagConfig: LangTagCLIConfig;
|
|
218
219
|
/**
|
|
219
220
|
* Indicates whether the `save()` method has been called during this event.
|
|
@@ -250,5 +251,4 @@ export interface LangTagCLICollectFinishEvent {
|
|
|
250
251
|
/** Breaks translation collection process */
|
|
251
252
|
exit(): void;
|
|
252
253
|
}
|
|
253
|
-
export declare const LANG_TAG_DEFAULT_CONFIG: LangTagCLIConfig;
|
|
254
254
|
export {};
|
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
import { mkdir, writeFile, readFile, rm } from "fs/promises";
|
|
2
|
-
import { dirname, resolve } from "path";
|
|
3
|
-
import path, { resolve as resolve$1, join } from "pathe";
|
|
4
|
-
import process__default from "node:process";
|
|
5
|
-
import * as caseLib from "case";
|
|
6
|
-
import micromatch from "micromatch";
|
|
7
|
-
function applyCaseTransform(str, caseType) {
|
|
8
|
-
if (caseType === "no") {
|
|
9
|
-
return str;
|
|
10
|
-
}
|
|
11
|
-
const caseFunction = caseLib[caseType];
|
|
12
|
-
if (typeof caseFunction === "function") {
|
|
13
|
-
return caseFunction(str);
|
|
14
|
-
}
|
|
15
|
-
return str;
|
|
16
|
-
}
|
|
17
|
-
class TranslationsCollector {
|
|
18
|
-
config;
|
|
19
|
-
logger;
|
|
20
|
-
}
|
|
21
|
-
async function $LT_EnsureDirectoryExists(filePath) {
|
|
22
|
-
await mkdir(filePath, { recursive: true });
|
|
23
|
-
}
|
|
24
|
-
async function $LT_RemoveDirectory(dirPath) {
|
|
25
|
-
try {
|
|
26
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
27
|
-
} catch (error) {
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
async function $LT_RemoveFile(filePath) {
|
|
31
|
-
try {
|
|
32
|
-
await rm(filePath, { force: true });
|
|
33
|
-
} catch (error) {
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
async function $LT_WriteJSON(filePath, data) {
|
|
37
|
-
await writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
|
|
38
|
-
}
|
|
39
|
-
async function $LT_ReadJSON(filePath) {
|
|
40
|
-
const content = await readFile(filePath, "utf-8");
|
|
41
|
-
return JSON.parse(content);
|
|
42
|
-
}
|
|
43
|
-
async function $LT_WriteFileWithDirs(filePath, content) {
|
|
44
|
-
const dir = dirname(filePath);
|
|
45
|
-
try {
|
|
46
|
-
await mkdir(dir, { recursive: true });
|
|
47
|
-
} catch (error) {
|
|
48
|
-
}
|
|
49
|
-
await writeFile(filePath, content, "utf-8");
|
|
50
|
-
}
|
|
51
|
-
async function $LT_ReadFileContent(relativeFilePath) {
|
|
52
|
-
const cwd = process.cwd();
|
|
53
|
-
const absolutePath = resolve(cwd, relativeFilePath);
|
|
54
|
-
return await readFile(absolutePath, "utf-8");
|
|
55
|
-
}
|
|
56
|
-
class NamespaceCollector extends TranslationsCollector {
|
|
57
|
-
clean;
|
|
58
|
-
languageDirectory;
|
|
59
|
-
aggregateCollection(namespace) {
|
|
60
|
-
return namespace;
|
|
61
|
-
}
|
|
62
|
-
transformTag(tag) {
|
|
63
|
-
return tag;
|
|
64
|
-
}
|
|
65
|
-
async preWrite(clean) {
|
|
66
|
-
this.clean = clean;
|
|
67
|
-
this.languageDirectory = path.join(this.config.localesDirectory, this.config.baseLanguageCode);
|
|
68
|
-
if (clean) {
|
|
69
|
-
this.logger.info("Cleaning output directory...");
|
|
70
|
-
await $LT_RemoveDirectory(this.languageDirectory);
|
|
71
|
-
}
|
|
72
|
-
await $LT_EnsureDirectoryExists(this.languageDirectory);
|
|
73
|
-
}
|
|
74
|
-
async resolveCollectionFilePath(collectionName) {
|
|
75
|
-
return resolve$1(
|
|
76
|
-
process__default.cwd(),
|
|
77
|
-
this.languageDirectory,
|
|
78
|
-
collectionName + ".json"
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
async onMissingCollection(collectionName) {
|
|
82
|
-
if (!this.clean) {
|
|
83
|
-
this.logger.warn(`Original namespace file "{namespace}.json" not found. A new one will be created.`, { namespace: collectionName });
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
async postWrite(changedCollections) {
|
|
87
|
-
if (!changedCollections?.length) {
|
|
88
|
-
this.logger.info("No changes were made based on the current configuration and files");
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
const n = changedCollections.map((n2) => `"${n2}.json"`).join(", ");
|
|
92
|
-
this.logger.success("Updated namespaces {outputDir} ({namespaces})", {
|
|
93
|
-
outputDir: this.config.localesDirectory,
|
|
94
|
-
namespaces: n
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
function flexibleImportAlgorithm(options = {}) {
|
|
99
|
-
const {
|
|
100
|
-
variableName = {},
|
|
101
|
-
filePath = {},
|
|
102
|
-
include,
|
|
103
|
-
exclude = {},
|
|
104
|
-
configRemap
|
|
105
|
-
} = options;
|
|
106
|
-
const { packages: includePackages, namespaces: includeNamespaces } = include || {};
|
|
107
|
-
const { packages: excludePackages = [], namespaces: excludeNamespaces = [] } = exclude;
|
|
108
|
-
return (event) => {
|
|
109
|
-
const { exports, importManager, logger } = event;
|
|
110
|
-
for (const { packageJSON, exportData } of exports) {
|
|
111
|
-
const packageName = packageJSON.name || "unknown-package";
|
|
112
|
-
if (includePackages && !matchesAnyPattern(packageName, includePackages)) {
|
|
113
|
-
logger.debug(`Skipping package not in include list: ${packageName}`);
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
if (matchesAnyPattern(packageName, excludePackages)) {
|
|
117
|
-
logger.debug(`Skipping excluded package: ${packageName}`);
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
logger.debug(`Processing library: ${packageName}`);
|
|
121
|
-
for (const file of exportData.files) {
|
|
122
|
-
const originalFileName = file.relativeFilePath;
|
|
123
|
-
const targetFilePath = generateFilePath(packageName, originalFileName, filePath);
|
|
124
|
-
for (let i = 0; i < file.tags.length; i++) {
|
|
125
|
-
const tag = file.tags[i];
|
|
126
|
-
const tagNamespace = tag.config?.namespace;
|
|
127
|
-
if (includeNamespaces && tagNamespace && !matchesAnyPattern(tagNamespace, includeNamespaces)) {
|
|
128
|
-
logger.debug(`Skipping namespace not in include list: ${tagNamespace}`);
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
if (tagNamespace && matchesAnyPattern(tagNamespace, excludeNamespaces)) {
|
|
132
|
-
logger.debug(`Skipping excluded namespace: ${tagNamespace}`);
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
const finalVariableName = generateVariableName(tag.variableName, packageName, originalFileName, i, variableName, tag);
|
|
136
|
-
if (finalVariableName === null) {
|
|
137
|
-
logger.debug(`Skipping tag without variableName in ${join(packageName, originalFileName)}`);
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
let finalConfig = tag.config;
|
|
141
|
-
if (configRemap) {
|
|
142
|
-
const remappedConfig = configRemap(tag.config, {
|
|
143
|
-
packageName,
|
|
144
|
-
fileName: originalFileName,
|
|
145
|
-
variableName: finalVariableName,
|
|
146
|
-
tagIndex: i
|
|
147
|
-
});
|
|
148
|
-
if (remappedConfig === null) {
|
|
149
|
-
logger.debug(`Removing config due to configRemap returning null in ${join(packageName, originalFileName)}`);
|
|
150
|
-
finalConfig = null;
|
|
151
|
-
} else {
|
|
152
|
-
finalConfig = remappedConfig;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
importManager.importTag(targetFilePath, {
|
|
156
|
-
variableName: finalVariableName,
|
|
157
|
-
translations: tag.translations,
|
|
158
|
-
config: finalConfig
|
|
159
|
-
});
|
|
160
|
-
logger.debug(`Imported: ${finalVariableName} -> ${targetFilePath}`);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
function sanitizeVariableName(name) {
|
|
167
|
-
let sanitized = name.replace(/[^a-zA-Z0-9_$]/g, "$");
|
|
168
|
-
if (/^[0-9]/.test(sanitized)) {
|
|
169
|
-
sanitized = "$" + sanitized;
|
|
170
|
-
}
|
|
171
|
-
if (sanitized === "") {
|
|
172
|
-
sanitized = "$";
|
|
173
|
-
}
|
|
174
|
-
return sanitized;
|
|
175
|
-
}
|
|
176
|
-
function applyCaseTransformToPath(filePath, caseType) {
|
|
177
|
-
if (typeof caseType === "string") {
|
|
178
|
-
const segments = filePath.split("/");
|
|
179
|
-
const fileName = segments[segments.length - 1];
|
|
180
|
-
const directorySegments = segments.slice(0, -1);
|
|
181
|
-
const transformedDirectories = directorySegments.map((dir) => applyCaseTransform(dir, caseType));
|
|
182
|
-
const transformedFileName = applyCaseTransformToFileName(fileName, caseType);
|
|
183
|
-
if (transformedDirectories.length === 0) {
|
|
184
|
-
return transformedFileName;
|
|
185
|
-
}
|
|
186
|
-
return [...transformedDirectories, transformedFileName].join("/");
|
|
187
|
-
}
|
|
188
|
-
if (typeof caseType === "object") {
|
|
189
|
-
const { directories = "no", files = "no" } = caseType;
|
|
190
|
-
const segments = filePath.split("/");
|
|
191
|
-
const fileName = segments[segments.length - 1];
|
|
192
|
-
const directorySegments = segments.slice(0, -1);
|
|
193
|
-
const transformedDirectories = directorySegments.map((dir) => applyCaseTransform(dir, directories));
|
|
194
|
-
const transformedFileName = applyCaseTransformToFileName(fileName, files);
|
|
195
|
-
if (transformedDirectories.length === 0) {
|
|
196
|
-
return transformedFileName;
|
|
197
|
-
}
|
|
198
|
-
return [...transformedDirectories, transformedFileName].join("/");
|
|
199
|
-
}
|
|
200
|
-
return filePath;
|
|
201
|
-
}
|
|
202
|
-
function normalizePackageName(packageName, scopedPackageHandling = "replace", context = "variableName") {
|
|
203
|
-
switch (scopedPackageHandling) {
|
|
204
|
-
case "remove-scope":
|
|
205
|
-
let result = packageName.replace(/^@[^/]+\//, "");
|
|
206
|
-
if (context === "variableName") {
|
|
207
|
-
result = result.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
208
|
-
}
|
|
209
|
-
return result;
|
|
210
|
-
case "replace":
|
|
211
|
-
default:
|
|
212
|
-
let normalized = packageName.replace(/@/g, "").replace(/\//g, context === "variableName" ? "_" : "-");
|
|
213
|
-
if (context === "variableName") {
|
|
214
|
-
normalized = normalized.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
215
|
-
}
|
|
216
|
-
return normalized;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
function generateVariableName(originalVariableName, packageName, fileName, index, options, tag) {
|
|
220
|
-
const {
|
|
221
|
-
prefixWithPackageName = false,
|
|
222
|
-
scopedPackageHandling = "replace",
|
|
223
|
-
case: caseType = "no",
|
|
224
|
-
sanitizeVariableName: shouldSanitize = true,
|
|
225
|
-
handleMissingVariableName = "auto-generate",
|
|
226
|
-
customVariableName
|
|
227
|
-
} = options;
|
|
228
|
-
if (customVariableName) {
|
|
229
|
-
const customName = customVariableName({
|
|
230
|
-
packageName,
|
|
231
|
-
fileName,
|
|
232
|
-
originalVariableName,
|
|
233
|
-
tagIndex: index,
|
|
234
|
-
tag
|
|
235
|
-
});
|
|
236
|
-
if (customName !== null) {
|
|
237
|
-
originalVariableName = customName;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
if (!originalVariableName) {
|
|
241
|
-
switch (handleMissingVariableName) {
|
|
242
|
-
case "skip":
|
|
243
|
-
return null;
|
|
244
|
-
case "auto-generate":
|
|
245
|
-
originalVariableName = `translations${index + 1}`;
|
|
246
|
-
break;
|
|
247
|
-
default:
|
|
248
|
-
if (typeof handleMissingVariableName === "function") {
|
|
249
|
-
originalVariableName = handleMissingVariableName({}, packageName, fileName, index);
|
|
250
|
-
} else {
|
|
251
|
-
return null;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
let finalName = originalVariableName;
|
|
256
|
-
if (prefixWithPackageName) {
|
|
257
|
-
const normalizedPackageName = normalizePackageName(packageName, scopedPackageHandling, "variableName");
|
|
258
|
-
finalName = `${normalizedPackageName}_${originalVariableName}`;
|
|
259
|
-
}
|
|
260
|
-
const transformedName = applyCaseTransform(finalName, caseType);
|
|
261
|
-
return shouldSanitize ? sanitizeVariableName(transformedName) : transformedName;
|
|
262
|
-
}
|
|
263
|
-
function generateFilePath(packageName, originalFileName, options) {
|
|
264
|
-
const { groupByPackage = false, includePackageInPath = false, scopedPackageHandling = "replace", case: caseType = "no" } = options;
|
|
265
|
-
if (groupByPackage) {
|
|
266
|
-
const normalizedPackageName = normalizePackageName(packageName, scopedPackageHandling, "filePath");
|
|
267
|
-
const fileName = `${normalizedPackageName}.ts`;
|
|
268
|
-
return applyCaseTransformToFileName(fileName, typeof caseType === "string" ? caseType : caseType.files || "no");
|
|
269
|
-
} else if (includePackageInPath) {
|
|
270
|
-
const normalizedPackageName = normalizePackageName(packageName, scopedPackageHandling, "filePath");
|
|
271
|
-
if (typeof caseType === "string") {
|
|
272
|
-
const transformedPackageName = applyCaseTransform(normalizedPackageName, caseType);
|
|
273
|
-
const transformedFilePath = applyCaseTransformToPath(originalFileName, caseType);
|
|
274
|
-
return join(transformedPackageName, transformedFilePath);
|
|
275
|
-
} else {
|
|
276
|
-
const transformedPackageName = applyCaseTransform(normalizedPackageName, caseType.directories || "no");
|
|
277
|
-
const transformedFilePath = applyCaseTransformToPath(originalFileName, caseType);
|
|
278
|
-
return join(transformedPackageName, transformedFilePath);
|
|
279
|
-
}
|
|
280
|
-
} else {
|
|
281
|
-
return applyCaseTransformToPath(originalFileName, caseType);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
function applyCaseTransformToFileName(fileName, caseType) {
|
|
285
|
-
if (caseType === "no") {
|
|
286
|
-
return fileName;
|
|
287
|
-
}
|
|
288
|
-
const lastDotIndex = fileName.lastIndexOf(".");
|
|
289
|
-
if (lastDotIndex === -1) {
|
|
290
|
-
return applyCaseTransform(fileName, caseType);
|
|
291
|
-
}
|
|
292
|
-
const nameWithoutExt = fileName.substring(0, lastDotIndex);
|
|
293
|
-
const extension = fileName.substring(lastDotIndex);
|
|
294
|
-
const transformedName = applyCaseTransform(nameWithoutExt, caseType);
|
|
295
|
-
return transformedName + extension;
|
|
296
|
-
}
|
|
297
|
-
function matchesAnyPattern(str, patterns) {
|
|
298
|
-
return micromatch.isMatch(str, patterns);
|
|
299
|
-
}
|
|
300
|
-
export {
|
|
301
|
-
$LT_ReadFileContent as $,
|
|
302
|
-
NamespaceCollector as N,
|
|
303
|
-
TranslationsCollector as T,
|
|
304
|
-
$LT_ReadJSON as a,
|
|
305
|
-
$LT_WriteJSON as b,
|
|
306
|
-
$LT_EnsureDirectoryExists as c,
|
|
307
|
-
$LT_WriteFileWithDirs as d,
|
|
308
|
-
$LT_RemoveFile as e,
|
|
309
|
-
flexibleImportAlgorithm as f,
|
|
310
|
-
applyCaseTransform as g
|
|
311
|
-
};
|