@lang-tag/cli 0.16.0 → 0.17.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 +1172 -830
- package/index.js +1171 -829
- package/logger.d.ts +1 -1
- package/package.json +1 -1
- 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/logger.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -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
|
-
};
|
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const promises = require("fs/promises");
|
|
3
|
-
const path = require("path");
|
|
4
|
-
const path$1 = require("pathe");
|
|
5
|
-
const process$1 = require("node:process");
|
|
6
|
-
const caseLib = require("case");
|
|
7
|
-
const micromatch = require("micromatch");
|
|
8
|
-
function _interopNamespaceDefault(e) {
|
|
9
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
10
|
-
if (e) {
|
|
11
|
-
for (const k in e) {
|
|
12
|
-
if (k !== "default") {
|
|
13
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: () => e[k]
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
n.default = e;
|
|
22
|
-
return Object.freeze(n);
|
|
23
|
-
}
|
|
24
|
-
const caseLib__namespace = /* @__PURE__ */ _interopNamespaceDefault(caseLib);
|
|
25
|
-
function applyCaseTransform(str, caseType) {
|
|
26
|
-
if (caseType === "no") {
|
|
27
|
-
return str;
|
|
28
|
-
}
|
|
29
|
-
const caseFunction = caseLib__namespace[caseType];
|
|
30
|
-
if (typeof caseFunction === "function") {
|
|
31
|
-
return caseFunction(str);
|
|
32
|
-
}
|
|
33
|
-
return str;
|
|
34
|
-
}
|
|
35
|
-
class TranslationsCollector {
|
|
36
|
-
config;
|
|
37
|
-
logger;
|
|
38
|
-
}
|
|
39
|
-
async function $LT_EnsureDirectoryExists(filePath) {
|
|
40
|
-
await promises.mkdir(filePath, { recursive: true });
|
|
41
|
-
}
|
|
42
|
-
async function $LT_RemoveDirectory(dirPath) {
|
|
43
|
-
try {
|
|
44
|
-
await promises.rm(dirPath, { recursive: true, force: true });
|
|
45
|
-
} catch (error) {
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
async function $LT_RemoveFile(filePath) {
|
|
49
|
-
try {
|
|
50
|
-
await promises.rm(filePath, { force: true });
|
|
51
|
-
} catch (error) {
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async function $LT_WriteJSON(filePath, data) {
|
|
55
|
-
await promises.writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
|
|
56
|
-
}
|
|
57
|
-
async function $LT_ReadJSON(filePath) {
|
|
58
|
-
const content = await promises.readFile(filePath, "utf-8");
|
|
59
|
-
return JSON.parse(content);
|
|
60
|
-
}
|
|
61
|
-
async function $LT_WriteFileWithDirs(filePath, content) {
|
|
62
|
-
const dir = path.dirname(filePath);
|
|
63
|
-
try {
|
|
64
|
-
await promises.mkdir(dir, { recursive: true });
|
|
65
|
-
} catch (error) {
|
|
66
|
-
}
|
|
67
|
-
await promises.writeFile(filePath, content, "utf-8");
|
|
68
|
-
}
|
|
69
|
-
async function $LT_ReadFileContent(relativeFilePath) {
|
|
70
|
-
const cwd = process.cwd();
|
|
71
|
-
const absolutePath = path.resolve(cwd, relativeFilePath);
|
|
72
|
-
return await promises.readFile(absolutePath, "utf-8");
|
|
73
|
-
}
|
|
74
|
-
class NamespaceCollector extends TranslationsCollector {
|
|
75
|
-
clean;
|
|
76
|
-
languageDirectory;
|
|
77
|
-
aggregateCollection(namespace) {
|
|
78
|
-
return namespace;
|
|
79
|
-
}
|
|
80
|
-
transformTag(tag) {
|
|
81
|
-
return tag;
|
|
82
|
-
}
|
|
83
|
-
async preWrite(clean) {
|
|
84
|
-
this.clean = clean;
|
|
85
|
-
this.languageDirectory = path$1.join(this.config.localesDirectory, this.config.baseLanguageCode);
|
|
86
|
-
if (clean) {
|
|
87
|
-
this.logger.info("Cleaning output directory...");
|
|
88
|
-
await $LT_RemoveDirectory(this.languageDirectory);
|
|
89
|
-
}
|
|
90
|
-
await $LT_EnsureDirectoryExists(this.languageDirectory);
|
|
91
|
-
}
|
|
92
|
-
async resolveCollectionFilePath(collectionName) {
|
|
93
|
-
return path$1.resolve(
|
|
94
|
-
process$1.cwd(),
|
|
95
|
-
this.languageDirectory,
|
|
96
|
-
collectionName + ".json"
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
async onMissingCollection(collectionName) {
|
|
100
|
-
if (!this.clean) {
|
|
101
|
-
this.logger.warn(`Original namespace file "{namespace}.json" not found. A new one will be created.`, { namespace: collectionName });
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
async postWrite(changedCollections) {
|
|
105
|
-
if (!changedCollections?.length) {
|
|
106
|
-
this.logger.info("No changes were made based on the current configuration and files");
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
const n = changedCollections.map((n2) => `"${n2}.json"`).join(", ");
|
|
110
|
-
this.logger.success("Updated namespaces {outputDir} ({namespaces})", {
|
|
111
|
-
outputDir: this.config.localesDirectory,
|
|
112
|
-
namespaces: n
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
function flexibleImportAlgorithm(options = {}) {
|
|
117
|
-
const {
|
|
118
|
-
variableName = {},
|
|
119
|
-
filePath = {},
|
|
120
|
-
include,
|
|
121
|
-
exclude = {},
|
|
122
|
-
configRemap
|
|
123
|
-
} = options;
|
|
124
|
-
const { packages: includePackages, namespaces: includeNamespaces } = include || {};
|
|
125
|
-
const { packages: excludePackages = [], namespaces: excludeNamespaces = [] } = exclude;
|
|
126
|
-
return (event) => {
|
|
127
|
-
const { exports: exports2, importManager, logger } = event;
|
|
128
|
-
for (const { packageJSON, exportData } of exports2) {
|
|
129
|
-
const packageName = packageJSON.name || "unknown-package";
|
|
130
|
-
if (includePackages && !matchesAnyPattern(packageName, includePackages)) {
|
|
131
|
-
logger.debug(`Skipping package not in include list: ${packageName}`);
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
if (matchesAnyPattern(packageName, excludePackages)) {
|
|
135
|
-
logger.debug(`Skipping excluded package: ${packageName}`);
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
logger.debug(`Processing library: ${packageName}`);
|
|
139
|
-
for (const file of exportData.files) {
|
|
140
|
-
const originalFileName = file.relativeFilePath;
|
|
141
|
-
const targetFilePath = generateFilePath(packageName, originalFileName, filePath);
|
|
142
|
-
for (let i = 0; i < file.tags.length; i++) {
|
|
143
|
-
const tag = file.tags[i];
|
|
144
|
-
const tagNamespace = tag.config?.namespace;
|
|
145
|
-
if (includeNamespaces && tagNamespace && !matchesAnyPattern(tagNamespace, includeNamespaces)) {
|
|
146
|
-
logger.debug(`Skipping namespace not in include list: ${tagNamespace}`);
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
if (tagNamespace && matchesAnyPattern(tagNamespace, excludeNamespaces)) {
|
|
150
|
-
logger.debug(`Skipping excluded namespace: ${tagNamespace}`);
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const finalVariableName = generateVariableName(tag.variableName, packageName, originalFileName, i, variableName, tag);
|
|
154
|
-
if (finalVariableName === null) {
|
|
155
|
-
logger.debug(`Skipping tag without variableName in ${path$1.join(packageName, originalFileName)}`);
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
let finalConfig = tag.config;
|
|
159
|
-
if (configRemap) {
|
|
160
|
-
const remappedConfig = configRemap(tag.config, {
|
|
161
|
-
packageName,
|
|
162
|
-
fileName: originalFileName,
|
|
163
|
-
variableName: finalVariableName,
|
|
164
|
-
tagIndex: i
|
|
165
|
-
});
|
|
166
|
-
if (remappedConfig === null) {
|
|
167
|
-
logger.debug(`Removing config due to configRemap returning null in ${path$1.join(packageName, originalFileName)}`);
|
|
168
|
-
finalConfig = null;
|
|
169
|
-
} else {
|
|
170
|
-
finalConfig = remappedConfig;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
importManager.importTag(targetFilePath, {
|
|
174
|
-
variableName: finalVariableName,
|
|
175
|
-
translations: tag.translations,
|
|
176
|
-
config: finalConfig
|
|
177
|
-
});
|
|
178
|
-
logger.debug(`Imported: ${finalVariableName} -> ${targetFilePath}`);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
function sanitizeVariableName(name) {
|
|
185
|
-
let sanitized = name.replace(/[^a-zA-Z0-9_$]/g, "$");
|
|
186
|
-
if (/^[0-9]/.test(sanitized)) {
|
|
187
|
-
sanitized = "$" + sanitized;
|
|
188
|
-
}
|
|
189
|
-
if (sanitized === "") {
|
|
190
|
-
sanitized = "$";
|
|
191
|
-
}
|
|
192
|
-
return sanitized;
|
|
193
|
-
}
|
|
194
|
-
function applyCaseTransformToPath(filePath, caseType) {
|
|
195
|
-
if (typeof caseType === "string") {
|
|
196
|
-
const segments = filePath.split("/");
|
|
197
|
-
const fileName = segments[segments.length - 1];
|
|
198
|
-
const directorySegments = segments.slice(0, -1);
|
|
199
|
-
const transformedDirectories = directorySegments.map((dir) => applyCaseTransform(dir, caseType));
|
|
200
|
-
const transformedFileName = applyCaseTransformToFileName(fileName, caseType);
|
|
201
|
-
if (transformedDirectories.length === 0) {
|
|
202
|
-
return transformedFileName;
|
|
203
|
-
}
|
|
204
|
-
return [...transformedDirectories, transformedFileName].join("/");
|
|
205
|
-
}
|
|
206
|
-
if (typeof caseType === "object") {
|
|
207
|
-
const { directories = "no", files = "no" } = caseType;
|
|
208
|
-
const segments = filePath.split("/");
|
|
209
|
-
const fileName = segments[segments.length - 1];
|
|
210
|
-
const directorySegments = segments.slice(0, -1);
|
|
211
|
-
const transformedDirectories = directorySegments.map((dir) => applyCaseTransform(dir, directories));
|
|
212
|
-
const transformedFileName = applyCaseTransformToFileName(fileName, files);
|
|
213
|
-
if (transformedDirectories.length === 0) {
|
|
214
|
-
return transformedFileName;
|
|
215
|
-
}
|
|
216
|
-
return [...transformedDirectories, transformedFileName].join("/");
|
|
217
|
-
}
|
|
218
|
-
return filePath;
|
|
219
|
-
}
|
|
220
|
-
function normalizePackageName(packageName, scopedPackageHandling = "replace", context = "variableName") {
|
|
221
|
-
switch (scopedPackageHandling) {
|
|
222
|
-
case "remove-scope":
|
|
223
|
-
let result = packageName.replace(/^@[^/]+\//, "");
|
|
224
|
-
if (context === "variableName") {
|
|
225
|
-
result = result.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
226
|
-
}
|
|
227
|
-
return result;
|
|
228
|
-
case "replace":
|
|
229
|
-
default:
|
|
230
|
-
let normalized = packageName.replace(/@/g, "").replace(/\//g, context === "variableName" ? "_" : "-");
|
|
231
|
-
if (context === "variableName") {
|
|
232
|
-
normalized = normalized.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
233
|
-
}
|
|
234
|
-
return normalized;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
function generateVariableName(originalVariableName, packageName, fileName, index, options, tag) {
|
|
238
|
-
const {
|
|
239
|
-
prefixWithPackageName = false,
|
|
240
|
-
scopedPackageHandling = "replace",
|
|
241
|
-
case: caseType = "no",
|
|
242
|
-
sanitizeVariableName: shouldSanitize = true,
|
|
243
|
-
handleMissingVariableName = "auto-generate",
|
|
244
|
-
customVariableName
|
|
245
|
-
} = options;
|
|
246
|
-
if (customVariableName) {
|
|
247
|
-
const customName = customVariableName({
|
|
248
|
-
packageName,
|
|
249
|
-
fileName,
|
|
250
|
-
originalVariableName,
|
|
251
|
-
tagIndex: index,
|
|
252
|
-
tag
|
|
253
|
-
});
|
|
254
|
-
if (customName !== null) {
|
|
255
|
-
originalVariableName = customName;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
if (!originalVariableName) {
|
|
259
|
-
switch (handleMissingVariableName) {
|
|
260
|
-
case "skip":
|
|
261
|
-
return null;
|
|
262
|
-
case "auto-generate":
|
|
263
|
-
originalVariableName = `translations${index + 1}`;
|
|
264
|
-
break;
|
|
265
|
-
default:
|
|
266
|
-
if (typeof handleMissingVariableName === "function") {
|
|
267
|
-
originalVariableName = handleMissingVariableName({}, packageName, fileName, index);
|
|
268
|
-
} else {
|
|
269
|
-
return null;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
let finalName = originalVariableName;
|
|
274
|
-
if (prefixWithPackageName) {
|
|
275
|
-
const normalizedPackageName = normalizePackageName(packageName, scopedPackageHandling, "variableName");
|
|
276
|
-
finalName = `${normalizedPackageName}_${originalVariableName}`;
|
|
277
|
-
}
|
|
278
|
-
const transformedName = applyCaseTransform(finalName, caseType);
|
|
279
|
-
return shouldSanitize ? sanitizeVariableName(transformedName) : transformedName;
|
|
280
|
-
}
|
|
281
|
-
function generateFilePath(packageName, originalFileName, options) {
|
|
282
|
-
const { groupByPackage = false, includePackageInPath = false, scopedPackageHandling = "replace", case: caseType = "no" } = options;
|
|
283
|
-
if (groupByPackage) {
|
|
284
|
-
const normalizedPackageName = normalizePackageName(packageName, scopedPackageHandling, "filePath");
|
|
285
|
-
const fileName = `${normalizedPackageName}.ts`;
|
|
286
|
-
return applyCaseTransformToFileName(fileName, typeof caseType === "string" ? caseType : caseType.files || "no");
|
|
287
|
-
} else if (includePackageInPath) {
|
|
288
|
-
const normalizedPackageName = normalizePackageName(packageName, scopedPackageHandling, "filePath");
|
|
289
|
-
if (typeof caseType === "string") {
|
|
290
|
-
const transformedPackageName = applyCaseTransform(normalizedPackageName, caseType);
|
|
291
|
-
const transformedFilePath = applyCaseTransformToPath(originalFileName, caseType);
|
|
292
|
-
return path$1.join(transformedPackageName, transformedFilePath);
|
|
293
|
-
} else {
|
|
294
|
-
const transformedPackageName = applyCaseTransform(normalizedPackageName, caseType.directories || "no");
|
|
295
|
-
const transformedFilePath = applyCaseTransformToPath(originalFileName, caseType);
|
|
296
|
-
return path$1.join(transformedPackageName, transformedFilePath);
|
|
297
|
-
}
|
|
298
|
-
} else {
|
|
299
|
-
return applyCaseTransformToPath(originalFileName, caseType);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
function applyCaseTransformToFileName(fileName, caseType) {
|
|
303
|
-
if (caseType === "no") {
|
|
304
|
-
return fileName;
|
|
305
|
-
}
|
|
306
|
-
const lastDotIndex = fileName.lastIndexOf(".");
|
|
307
|
-
if (lastDotIndex === -1) {
|
|
308
|
-
return applyCaseTransform(fileName, caseType);
|
|
309
|
-
}
|
|
310
|
-
const nameWithoutExt = fileName.substring(0, lastDotIndex);
|
|
311
|
-
const extension = fileName.substring(lastDotIndex);
|
|
312
|
-
const transformedName = applyCaseTransform(nameWithoutExt, caseType);
|
|
313
|
-
return transformedName + extension;
|
|
314
|
-
}
|
|
315
|
-
function matchesAnyPattern(str, patterns) {
|
|
316
|
-
return micromatch.isMatch(str, patterns);
|
|
317
|
-
}
|
|
318
|
-
exports.$LT_EnsureDirectoryExists = $LT_EnsureDirectoryExists;
|
|
319
|
-
exports.$LT_ReadFileContent = $LT_ReadFileContent;
|
|
320
|
-
exports.$LT_ReadJSON = $LT_ReadJSON;
|
|
321
|
-
exports.$LT_RemoveFile = $LT_RemoveFile;
|
|
322
|
-
exports.$LT_WriteFileWithDirs = $LT_WriteFileWithDirs;
|
|
323
|
-
exports.$LT_WriteJSON = $LT_WriteJSON;
|
|
324
|
-
exports.NamespaceCollector = NamespaceCollector;
|
|
325
|
-
exports.TranslationsCollector = TranslationsCollector;
|
|
326
|
-
exports.applyCaseTransform = applyCaseTransform;
|
|
327
|
-
exports.flexibleImportAlgorithm = flexibleImportAlgorithm;
|