@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.
package/config.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { LangTagTranslationsConfig } from 'lang-tag';
2
2
  import { LangTagCLILogger } from './logger.ts';
3
+ import { TranslationsCollector } from './algorithms';
3
4
  export interface LangTagCLIConfig {
4
5
  /**
5
6
  * Tag name used to mark translations in code.
@@ -17,11 +18,37 @@ export interface LangTagCLIConfig {
17
18
  */
18
19
  excludes: string[];
19
20
  /**
20
- * Output directory for generated translation namespace files (e.g., common.json, errors.json).
21
- * @default 'locales/en'
21
+ * Root directory for translation files.
22
+ * The actual file structure depends on the collector implementation used.
23
+ * @default 'locales'
24
+ * @example With baseLanguageCode='en' and localesDirectory='locales':
25
+ * - NamespaceCollector (default): locales/en/common.json, locales/en/errors.json
26
+ * - DictionaryCollector: locales/en.json (all translations in one file)
22
27
  */
23
- outputDir: string;
28
+ localesDirectory: string;
29
+ /**
30
+ * The language in which translation values/messages are written in the codebase.
31
+ * This determines the source language for your translations.
32
+ * @default 'en'
33
+ * @example 'en' - Translation values are in English: lang({ helloWorld: 'Hello World' })
34
+ * @example 'pl' - Translation values are in Polish: lang({ helloWorld: 'Witaj Świecie' })
35
+ */
36
+ baseLanguageCode: string;
37
+ /**
38
+ * Indicates whether this configuration is for a translation library.
39
+ * If true, generates an exports file (`.lang-tag.exports.json`) instead of locale files.
40
+ * @default false
41
+ */
42
+ isLibrary: boolean;
24
43
  collect?: {
44
+ /**
45
+ * Translation collector that defines how translation tags are organized into output files.
46
+ * If not specified, NamespaceCollector is used by default.
47
+ * @default NamespaceCollector
48
+ * @example DictionaryCollector - All translations in single file per language
49
+ * @example NamespaceCollector - Separate files per namespace within language directory
50
+ */
51
+ collector?: TranslationsCollector;
25
52
  /**
26
53
  * @default 'common'
27
54
  */
@@ -50,50 +77,6 @@ export interface LangTagCLIConfig {
50
77
  */
51
78
  onCollectFinish?: (event: LangTagCLICollectFinishEvent) => void;
52
79
  };
53
- import: {
54
- /**
55
- * Output directory for generated files containing imported library tags.
56
- * @default 'src/lang-libraries'
57
- */
58
- dir: string;
59
- /**
60
- * The import statement used in generated library files to import the project's `lang` tag function.
61
- * @default 'import { lang } from "@/my-lang-tag-path"'
62
- */
63
- tagImportPath: string;
64
- /**
65
- * A function to customize the generated file name and export name for imported library tags.
66
- * Allows controlling how imported tags are organized and named within the generated files.
67
- */
68
- onImport: (params: LangTagCLIOnImportParams, actions: LangTagCLIOnImportActions) => void;
69
- /**
70
- * A function called after all lang-tags were imported
71
- */
72
- onImportFinish?: () => void;
73
- };
74
- /**
75
- * Determines the position of the translation argument in the `lang()` function.
76
- * If `1`, translations are in the first argument (`lang(translations, options)`).
77
- * If `2`, translations are in the second argument (`lang(options, translations)`).
78
- * @default 1
79
- */
80
- translationArgPosition: 1 | 2;
81
- /**
82
- * Primary language used for the library's translations.
83
- * Affects default language settings when used in library mode.
84
- * @default 'en'
85
- */
86
- language: string;
87
- /**
88
- * Indicates whether this configuration is for a translation library.
89
- * If true, generates an exports file (`.lang-tag.exports.json`) instead of locale files.
90
- * @default false
91
- */
92
- isLibrary: boolean;
93
- /**
94
- * Whether to flatten the translation keys. (Currently unused)
95
- * @default false
96
- */
97
80
  /**
98
81
  * A function called for each found lang tag before processing.
99
82
  * Allows dynamic modification of the tag's configuration (namespace, path, etc.)
@@ -123,36 +106,36 @@ export interface LangTagCLIConfig {
123
106
  * ```
124
107
  */
125
108
  onConfigGeneration: (event: LangTagCLIConfigGenerationEvent) => Promise<void>;
109
+ import: {
110
+ /**
111
+ * Output directory for generated files containing imported library tags.
112
+ * @default 'src/lang-libraries'
113
+ */
114
+ dir: string;
115
+ /**
116
+ * The import statement used in generated library files to import the project's `lang` tag function.
117
+ * @default 'import { lang } from "@/my-lang-tag-path"'
118
+ */
119
+ tagImportPath: string;
120
+ /**
121
+ * A function to customize the generated file name and export name for imported library tags.
122
+ * Allows controlling how imported tags are organized and named within the generated files.
123
+ */
124
+ onImport: (event: LangTagCLIImportEvent) => void;
125
+ /**
126
+ * A function called after all lang-tags were imported
127
+ */
128
+ onImportFinish?: () => void;
129
+ };
130
+ /**
131
+ * Determines the position of the translation argument in the `lang()` function.
132
+ * If `1`, translations are in the first argument (`lang(translations, options)`).
133
+ * If `2`, translations are in the second argument (`lang(options, translations)`).
134
+ * @default 1
135
+ */
136
+ translationArgPosition: 1 | 2;
126
137
  debug?: boolean;
127
138
  }
128
- /**
129
- * Parameters passed to the `onImport` configuration function.
130
- */
131
- export interface LangTagCLIOnImportParams {
132
- /** The name of the package from which the tag is being imported. */
133
- packageName: string;
134
- /** The relative path to the source file within the imported package. */
135
- importedRelativePath: string;
136
- /** The original variable name assigned to the lang tag in the source library file, if any. */
137
- originalExportName: string | undefined;
138
- /** Parsed JSON translation object from the imported tag. */
139
- translations: Record<string, any>;
140
- /** Configuration object associated with the imported tag. */
141
- config: LangTagTranslationsConfig;
142
- /** A mutable object that can be used to pass data between multiple `onImport` calls for the same generated file. */
143
- fileGenerationData: any;
144
- }
145
- /**
146
- * Actions that can be performed within the onImport callback.
147
- */
148
- export interface LangTagCLIOnImportActions {
149
- /** Sets the desired file for the generated import. */
150
- setFile: (file: string) => void;
151
- /** Sets the desired export name for the imported tag. */
152
- setExportName: (name: string) => void;
153
- /** Sets the configuration for the currently imported tag. */
154
- setConfig: (config: LangTagTranslationsConfig) => void;
155
- }
156
139
  type Validity = 'ok' | 'invalid-param-1' | 'invalid-param-2' | 'translations-not-found';
157
140
  export interface LangTagCLIProcessedTag {
158
141
  fullMatch: string;
@@ -180,6 +163,43 @@ export interface LangTagCLIConflict {
180
163
  tagB: LangTagCLITagConflictInfo;
181
164
  conflictType: 'path_overwrite' | 'type_mismatch';
182
165
  }
166
+ export interface LangTagCLIImportManager {
167
+ importTag(pathRelativeToImportDir: string, tag: LangTagCLIImportedTag): void;
168
+ getImportedFiles(): LangTagCLIImportedTagsFile[];
169
+ getImportedFilesCount(): number;
170
+ hasImportedFiles(): boolean;
171
+ }
172
+ export interface LangTagCLIImportedTag {
173
+ variableName: string;
174
+ translations: any;
175
+ config: any | null;
176
+ }
177
+ export interface LangTagCLIImportedTagsFile {
178
+ pathRelativeToImportDir: string;
179
+ tags: LangTagCLIImportedTag[];
180
+ }
181
+ export interface LangTagCLIExportData {
182
+ baseLanguageCode: string;
183
+ files: LangTagCLIExportDataFile[];
184
+ }
185
+ export interface LangTagCLIExportDataFile {
186
+ relativeFilePath: string;
187
+ tags: LangTagCLIExportDataTag[];
188
+ }
189
+ export interface LangTagCLIExportDataTag {
190
+ variableName: string | undefined;
191
+ translations: object;
192
+ config: object | undefined;
193
+ }
194
+ export interface LangTagCLIImportEvent {
195
+ exports: {
196
+ packageJSON: any;
197
+ exportData: LangTagCLIExportData;
198
+ }[];
199
+ langTagConfig: LangTagCLIConfig;
200
+ logger: LangTagCLILogger;
201
+ importManager: LangTagCLIImportManager;
202
+ }
183
203
  export interface LangTagCLIConfigGenerationEvent {
184
204
  /** The absolute path to the source file being processed. */
185
205
  readonly absolutePath: string;
@@ -0,0 +1,311 @@
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
+ };