@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.
@@ -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;
@@ -1 +0,0 @@
1
- -