@lang-tag/cli 0.15.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/algorithms/case-utils.d.ts +12 -0
- package/algorithms/config-generation/path-based-config-generator.d.ts +3 -2
- package/algorithms/import/flexible-import-algorithm.d.ts +232 -0
- package/algorithms/import/index.d.ts +2 -1
- package/algorithms/import/simple-mapping-import-algorithm.d.ts +120 -0
- package/algorithms/index.cjs +71 -32
- package/algorithms/index.d.ts +4 -1
- package/algorithms/index.js +68 -12
- package/config.d.ts +39 -30
- package/flexible-import-algorithm-C-S1c742.js +311 -0
- package/flexible-import-algorithm-Fa-l4jWj.cjs +327 -0
- package/index.cjs +192 -121
- package/index.js +186 -115
- package/package.json +1 -1
- package/templates/config/init-config.mustache +1 -0
- package/templates/import/imported-tag.mustache +14 -0
- package/namespace-collector-DCruv_PK.js +0 -95
- package/namespace-collector-DRnZvkDR.cjs +0 -94
- /package/{template → templates/tag}/base-app.mustache +0 -0
- /package/{template → templates/tag}/base-library.mustache +0 -0
- /package/{template → templates/tag}/placeholder.mustache +0 -0
package/config.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { LangTagTranslationsConfig } from 'lang-tag';
|
|
2
2
|
import { LangTagCLILogger } from './logger.ts';
|
|
3
|
-
import { TranslationsCollector } from './algorithms
|
|
3
|
+
import { TranslationsCollector } from './algorithms';
|
|
4
4
|
export interface LangTagCLIConfig {
|
|
5
5
|
/**
|
|
6
6
|
* Tag name used to mark translations in code.
|
|
@@ -121,7 +121,7 @@ export interface LangTagCLIConfig {
|
|
|
121
121
|
* A function to customize the generated file name and export name for imported library tags.
|
|
122
122
|
* Allows controlling how imported tags are organized and named within the generated files.
|
|
123
123
|
*/
|
|
124
|
-
onImport: (
|
|
124
|
+
onImport: (event: LangTagCLIImportEvent) => void;
|
|
125
125
|
/**
|
|
126
126
|
* A function called after all lang-tags were imported
|
|
127
127
|
*/
|
|
@@ -136,34 +136,6 @@ export interface LangTagCLIConfig {
|
|
|
136
136
|
translationArgPosition: 1 | 2;
|
|
137
137
|
debug?: boolean;
|
|
138
138
|
}
|
|
139
|
-
/**
|
|
140
|
-
* Parameters passed to the `onImport` configuration function.
|
|
141
|
-
*/
|
|
142
|
-
export interface LangTagCLIOnImportParams {
|
|
143
|
-
/** The name of the package from which the tag is being imported. */
|
|
144
|
-
packageName: string;
|
|
145
|
-
/** The relative path to the source file within the imported package. */
|
|
146
|
-
importedRelativePath: string;
|
|
147
|
-
/** The original variable name assigned to the lang tag in the source library file, if any. */
|
|
148
|
-
originalExportName: string | undefined;
|
|
149
|
-
/** Parsed JSON translation object from the imported tag. */
|
|
150
|
-
translations: Record<string, any>;
|
|
151
|
-
/** Configuration object associated with the imported tag. */
|
|
152
|
-
config: LangTagTranslationsConfig;
|
|
153
|
-
/** A mutable object that can be used to pass data between multiple `onImport` calls for the same generated file. */
|
|
154
|
-
fileGenerationData: any;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Actions that can be performed within the onImport callback.
|
|
158
|
-
*/
|
|
159
|
-
export interface LangTagCLIOnImportActions {
|
|
160
|
-
/** Sets the desired file for the generated import. */
|
|
161
|
-
setFile: (file: string) => void;
|
|
162
|
-
/** Sets the desired export name for the imported tag. */
|
|
163
|
-
setExportName: (name: string) => void;
|
|
164
|
-
/** Sets the configuration for the currently imported tag. */
|
|
165
|
-
setConfig: (config: LangTagTranslationsConfig) => void;
|
|
166
|
-
}
|
|
167
139
|
type Validity = 'ok' | 'invalid-param-1' | 'invalid-param-2' | 'translations-not-found';
|
|
168
140
|
export interface LangTagCLIProcessedTag {
|
|
169
141
|
fullMatch: string;
|
|
@@ -191,6 +163,43 @@ export interface LangTagCLIConflict {
|
|
|
191
163
|
tagB: LangTagCLITagConflictInfo;
|
|
192
164
|
conflictType: 'path_overwrite' | 'type_mismatch';
|
|
193
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
|
+
}
|
|
194
203
|
export interface LangTagCLIConfigGenerationEvent {
|
|
195
204
|
/** The absolute path to the source file being processed. */
|
|
196
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
|
+
};
|