@lang-tag/cli 0.13.1 → 0.15.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/collector/dictionary-collector.d.ts +17 -0
- package/algorithms/collector/index.d.ts +10 -0
- package/algorithms/collector/namespace-collector.d.ts +12 -0
- package/algorithms/collector/type.d.ts +12 -0
- package/algorithms/config-generation/index.d.ts +1 -0
- package/algorithms/config-generation/path-based-config-generator.d.ts +25 -0
- package/algorithms/config-generation/prepend-namespace-to-path.d.ts +28 -0
- package/algorithms/index.cjs +182 -5
- package/algorithms/index.d.ts +6 -3
- package/algorithms/index.js +194 -16
- package/config.d.ts +58 -47
- package/index.cjs +86 -122
- package/index.js +82 -118
- package/namespace-collector-DCruv_PK.js +95 -0
- package/namespace-collector-DRnZvkDR.cjs +94 -0
- package/package.json +1 -1
- package/template/base-app.mustache +11 -16
- package/template/base-library.mustache +24 -33
package/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { program } from "commander";
|
|
3
3
|
import fs, { readFileSync, existsSync } from "fs";
|
|
4
|
-
import { writeFile,
|
|
4
|
+
import { writeFile, readFile } from "fs/promises";
|
|
5
5
|
import JSON5 from "json5";
|
|
6
6
|
import * as path from "path";
|
|
7
|
-
import path__default, { sep,
|
|
7
|
+
import path__default, { sep, join, dirname as dirname$1 } from "path";
|
|
8
8
|
import { globby } from "globby";
|
|
9
|
-
import path$1, { resolve, dirname
|
|
9
|
+
import path$1, { resolve, dirname } from "pathe";
|
|
10
10
|
import { pathToFileURL, fileURLToPath } from "url";
|
|
11
|
+
import { N as NamespaceCollector, $ as $LT_ReadFileContent, a as $LT_ReadJSON, b as $LT_WriteJSON, c as $LT_EnsureDirectoryExists, d as $LT_WriteFileWithDirs } from "./namespace-collector-DCruv_PK.js";
|
|
11
12
|
import * as process$1 from "node:process";
|
|
12
13
|
import process__default from "node:process";
|
|
13
14
|
import * as acorn from "acorn";
|
|
@@ -60,31 +61,33 @@ class $LT_TagProcessor {
|
|
|
60
61
|
while (i < fileContent.length && (fileContent[i] === " " || fileContent[i] === "\n" || fileContent[i] === " ")) {
|
|
61
62
|
i++;
|
|
62
63
|
}
|
|
63
|
-
if (i >= fileContent.length || fileContent[i]
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
braceCount = 1;
|
|
68
|
-
const secondParamStart = i;
|
|
69
|
-
i++;
|
|
70
|
-
while (i < fileContent.length && braceCount > 0) {
|
|
71
|
-
if (fileContent[i] === "{") braceCount++;
|
|
72
|
-
if (fileContent[i] === "}") braceCount--;
|
|
73
|
-
i++;
|
|
74
|
-
}
|
|
75
|
-
if (braceCount !== 0) {
|
|
76
|
-
currentIndex = matchStartIndex + 1;
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
parameter2Text = fileContent.substring(secondParamStart, i);
|
|
80
|
-
while (i < fileContent.length && (fileContent[i] === " " || fileContent[i] === "\n" || fileContent[i] === " ")) {
|
|
81
|
-
i++;
|
|
82
|
-
}
|
|
83
|
-
if (i < fileContent.length && fileContent[i] === ",") {
|
|
64
|
+
if (i >= fileContent.length || fileContent[i] === ")") ;
|
|
65
|
+
else if (fileContent[i] === "{") {
|
|
66
|
+
braceCount = 1;
|
|
67
|
+
const secondParamStart = i;
|
|
84
68
|
i++;
|
|
69
|
+
while (i < fileContent.length && braceCount > 0) {
|
|
70
|
+
if (fileContent[i] === "{") braceCount++;
|
|
71
|
+
if (fileContent[i] === "}") braceCount--;
|
|
72
|
+
i++;
|
|
73
|
+
}
|
|
74
|
+
if (braceCount !== 0) {
|
|
75
|
+
currentIndex = matchStartIndex + 1;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
parameter2Text = fileContent.substring(secondParamStart, i);
|
|
85
79
|
while (i < fileContent.length && (fileContent[i] === " " || fileContent[i] === "\n" || fileContent[i] === " ")) {
|
|
86
80
|
i++;
|
|
87
81
|
}
|
|
82
|
+
if (i < fileContent.length && fileContent[i] === ",") {
|
|
83
|
+
i++;
|
|
84
|
+
while (i < fileContent.length && (fileContent[i] === " " || fileContent[i] === "\n" || fileContent[i] === " ")) {
|
|
85
|
+
i++;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
currentIndex = matchStartIndex + 1;
|
|
90
|
+
continue;
|
|
88
91
|
}
|
|
89
92
|
} else if (fileContent[i] !== ")") {
|
|
90
93
|
currentIndex = matchStartIndex + 1;
|
|
@@ -479,10 +482,13 @@ const CONFIG_FILE_NAME = ".lang-tag.config.js";
|
|
|
479
482
|
const EXPORTS_FILE_NAME = ".lang-tag.exports.json";
|
|
480
483
|
const LANG_TAG_DEFAULT_CONFIG = {
|
|
481
484
|
tagName: "lang",
|
|
485
|
+
isLibrary: false,
|
|
482
486
|
includes: ["src/**/*.{js,ts,jsx,tsx}"],
|
|
483
487
|
excludes: ["node_modules", "dist", "build"],
|
|
484
|
-
|
|
488
|
+
localesDirectory: "locales",
|
|
489
|
+
baseLanguageCode: "en",
|
|
485
490
|
collect: {
|
|
491
|
+
collector: new NamespaceCollector(),
|
|
486
492
|
defaultNamespace: "common",
|
|
487
493
|
ignoreConflictsWithMatchingValues: true,
|
|
488
494
|
onCollectConfigFix: ({ config, langTagConfig }) => {
|
|
@@ -509,8 +515,6 @@ const LANG_TAG_DEFAULT_CONFIG = {
|
|
|
509
515
|
actions.setExportName(`translations${exportIndex}`);
|
|
510
516
|
}
|
|
511
517
|
},
|
|
512
|
-
isLibrary: false,
|
|
513
|
-
language: "en",
|
|
514
518
|
translationArgPosition: 1,
|
|
515
519
|
onConfigGeneration: async (event) => {
|
|
516
520
|
}
|
|
@@ -527,10 +531,10 @@ async function $LT_ReadConfig(projectPath) {
|
|
|
527
531
|
}
|
|
528
532
|
const userConfig = configModule.default || {};
|
|
529
533
|
const tn = (userConfig.tagName || "").toLowerCase().replace(/[-_\s]/g, "");
|
|
530
|
-
if (tn
|
|
531
|
-
throw new Error('Custom tagName cannot
|
|
534
|
+
if (tn === "langtag" || tn === "lang-tag") {
|
|
535
|
+
throw new Error('Custom tagName cannot be "lang-tag" or "langtag"! (It is not recommended for use with libraries)\n');
|
|
532
536
|
}
|
|
533
|
-
|
|
537
|
+
const config = {
|
|
534
538
|
...LANG_TAG_DEFAULT_CONFIG,
|
|
535
539
|
...userConfig,
|
|
536
540
|
import: {
|
|
@@ -542,39 +546,14 @@ async function $LT_ReadConfig(projectPath) {
|
|
|
542
546
|
...userConfig.collect
|
|
543
547
|
}
|
|
544
548
|
};
|
|
549
|
+
if (!config.collect.collector) {
|
|
550
|
+
throw new Error("Collector not found! (config.collect.collector)");
|
|
551
|
+
}
|
|
552
|
+
return config;
|
|
545
553
|
} catch (error) {
|
|
546
554
|
throw error;
|
|
547
555
|
}
|
|
548
556
|
}
|
|
549
|
-
async function $LT_EnsureDirectoryExists(filePath) {
|
|
550
|
-
await mkdir(filePath, { recursive: true });
|
|
551
|
-
}
|
|
552
|
-
async function $LT_RemoveDirectory(dirPath) {
|
|
553
|
-
try {
|
|
554
|
-
await rm(dirPath, { recursive: true, force: true });
|
|
555
|
-
} catch (error) {
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
async function $LT_WriteJSON(filePath, data) {
|
|
559
|
-
await writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
|
|
560
|
-
}
|
|
561
|
-
async function $LT_ReadJSON(filePath) {
|
|
562
|
-
const content = await readFile(filePath, "utf-8");
|
|
563
|
-
return JSON.parse(content);
|
|
564
|
-
}
|
|
565
|
-
async function $LT_WriteFileWithDirs(filePath, content) {
|
|
566
|
-
const dir = dirname(filePath);
|
|
567
|
-
try {
|
|
568
|
-
await mkdir(dir, { recursive: true });
|
|
569
|
-
} catch (error) {
|
|
570
|
-
}
|
|
571
|
-
await writeFile(filePath, content, "utf-8");
|
|
572
|
-
}
|
|
573
|
-
async function $LT_ReadFileContent(relativeFilePath) {
|
|
574
|
-
const cwd = process.cwd();
|
|
575
|
-
const absolutePath = resolve$1(cwd, relativeFilePath);
|
|
576
|
-
return await readFile(absolutePath, "utf-8");
|
|
577
|
-
}
|
|
578
557
|
function parseObjectAST(code) {
|
|
579
558
|
const nodes = [];
|
|
580
559
|
try {
|
|
@@ -976,6 +955,8 @@ function $LT_CreateDefaultLogger(debugMode, translationArgPosition = 1) {
|
|
|
976
955
|
async function $LT_GetCommandEssentials() {
|
|
977
956
|
const config = await $LT_ReadConfig(process__default.cwd());
|
|
978
957
|
const logger = $LT_CreateDefaultLogger(config.debug, config.translationArgPosition);
|
|
958
|
+
config.collect.collector.config = config;
|
|
959
|
+
config.collect.collector.logger = logger;
|
|
979
960
|
return {
|
|
980
961
|
config,
|
|
981
962
|
logger
|
|
@@ -1038,36 +1019,26 @@ function deepMergeTranslations(target, source) {
|
|
|
1038
1019
|
}
|
|
1039
1020
|
return changed;
|
|
1040
1021
|
}
|
|
1041
|
-
async function $
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
await $LT_RemoveDirectory(config.outputDir);
|
|
1046
|
-
}
|
|
1047
|
-
await $LT_EnsureDirectoryExists(config.outputDir);
|
|
1048
|
-
for (let namespace of Object.keys(namespaces)) {
|
|
1022
|
+
async function $LT_WriteToCollections({ config, collections, logger, clean }) {
|
|
1023
|
+
await config.collect.collector.preWrite(clean);
|
|
1024
|
+
const changedCollections = [];
|
|
1025
|
+
for (let namespace of Object.keys(collections)) {
|
|
1049
1026
|
if (!namespace) {
|
|
1050
1027
|
continue;
|
|
1051
1028
|
}
|
|
1052
|
-
const filePath =
|
|
1053
|
-
process__default.cwd(),
|
|
1054
|
-
config.outputDir,
|
|
1055
|
-
namespace + ".json"
|
|
1056
|
-
);
|
|
1029
|
+
const filePath = await config.collect.collector.resolveCollectionFilePath(namespace);
|
|
1057
1030
|
let originalJSON = {};
|
|
1058
1031
|
try {
|
|
1059
1032
|
originalJSON = await $LT_ReadJSON(filePath);
|
|
1060
1033
|
} catch (e) {
|
|
1061
|
-
|
|
1062
|
-
logger.warn(`Original namespace file "{namespace}.json" not found. A new one will be created.`, { namespace });
|
|
1063
|
-
}
|
|
1034
|
+
await config.collect.collector.onMissingCollection(namespace);
|
|
1064
1035
|
}
|
|
1065
|
-
if (deepMergeTranslations(originalJSON,
|
|
1066
|
-
|
|
1036
|
+
if (deepMergeTranslations(originalJSON, collections[namespace])) {
|
|
1037
|
+
changedCollections.push(namespace);
|
|
1067
1038
|
await $LT_WriteJSON(filePath, originalJSON);
|
|
1068
1039
|
}
|
|
1069
1040
|
}
|
|
1070
|
-
|
|
1041
|
+
await config.collect.collector.postWrite(changedCollections);
|
|
1071
1042
|
}
|
|
1072
1043
|
async function $LT_CollectCandidateFilesWithTags(props) {
|
|
1073
1044
|
const { config, logger } = props;
|
|
@@ -1119,30 +1090,32 @@ async function $LT_WriteAsExportFile({ config, logger, files }) {
|
|
|
1119
1090
|
};
|
|
1120
1091
|
}
|
|
1121
1092
|
const data = {
|
|
1122
|
-
language: config.
|
|
1093
|
+
language: config.baseLanguageCode,
|
|
1123
1094
|
packageName: packageJson.name || "",
|
|
1124
1095
|
files: langTagFiles
|
|
1125
1096
|
};
|
|
1126
1097
|
await $LT_WriteJSON(EXPORTS_FILE_NAME, data);
|
|
1127
1098
|
logger.success(`Written {file}`, { file: EXPORTS_FILE_NAME });
|
|
1128
1099
|
}
|
|
1129
|
-
async function $
|
|
1100
|
+
async function $LT_GroupTagsToCollections({ logger, files, config }) {
|
|
1130
1101
|
let totalTags = 0;
|
|
1131
|
-
const
|
|
1132
|
-
function
|
|
1133
|
-
const
|
|
1134
|
-
|
|
1135
|
-
|
|
1102
|
+
const collections = {};
|
|
1103
|
+
function getTranslationsCollection(namespace) {
|
|
1104
|
+
const collectionName = config.collect.collector.aggregateCollection(namespace);
|
|
1105
|
+
const collection = collections[collectionName] || {};
|
|
1106
|
+
if (!(collectionName in collections)) {
|
|
1107
|
+
collections[collectionName] = collection;
|
|
1136
1108
|
}
|
|
1137
|
-
return
|
|
1109
|
+
return collection;
|
|
1138
1110
|
}
|
|
1139
1111
|
const allConflicts = [];
|
|
1140
1112
|
const existingValuesByNamespace = /* @__PURE__ */ new Map();
|
|
1141
1113
|
for (const file of files) {
|
|
1142
1114
|
totalTags += file.tags.length;
|
|
1143
|
-
for (const
|
|
1115
|
+
for (const _tag of file.tags) {
|
|
1116
|
+
const tag = config.collect.collector.transformTag(_tag);
|
|
1144
1117
|
const tagConfig = tag.parameterConfig;
|
|
1145
|
-
const
|
|
1118
|
+
const collection = getTranslationsCollection(tagConfig.namespace);
|
|
1146
1119
|
let existingValues = existingValuesByNamespace.get(tagConfig.namespace);
|
|
1147
1120
|
if (!existingValues) {
|
|
1148
1121
|
existingValues = /* @__PURE__ */ new Map();
|
|
@@ -1185,7 +1158,7 @@ async function $LT_GroupTagsToNamespaces({ logger, files, config }) {
|
|
|
1185
1158
|
};
|
|
1186
1159
|
const target = await ensureNestedObject(
|
|
1187
1160
|
tagConfig.path,
|
|
1188
|
-
|
|
1161
|
+
collection,
|
|
1189
1162
|
valueTracker,
|
|
1190
1163
|
addConflict
|
|
1191
1164
|
);
|
|
@@ -1205,7 +1178,7 @@ async function $LT_GroupTagsToNamespaces({ logger, files, config }) {
|
|
|
1205
1178
|
let shouldContinue = true;
|
|
1206
1179
|
config.collect.onCollectFinish({
|
|
1207
1180
|
totalTags,
|
|
1208
|
-
namespaces,
|
|
1181
|
+
namespaces: collections,
|
|
1209
1182
|
conflicts: allConflicts,
|
|
1210
1183
|
logger,
|
|
1211
1184
|
exit() {
|
|
@@ -1216,11 +1189,11 @@ async function $LT_GroupTagsToNamespaces({ logger, files, config }) {
|
|
|
1216
1189
|
throw new Error(`LangTagConflictResolution:Processing stopped due to collect finish handler`);
|
|
1217
1190
|
}
|
|
1218
1191
|
}
|
|
1219
|
-
return
|
|
1192
|
+
return collections;
|
|
1220
1193
|
}
|
|
1221
|
-
async function ensureNestedObject(path2,
|
|
1222
|
-
if (!path2 || !path2.trim()) return
|
|
1223
|
-
let current =
|
|
1194
|
+
async function ensureNestedObject(path2, rootCollection, valueTracker, addConflict) {
|
|
1195
|
+
if (!path2 || !path2.trim()) return rootCollection;
|
|
1196
|
+
let current = rootCollection;
|
|
1224
1197
|
let currentPath = "";
|
|
1225
1198
|
for (const key of path2.split(".")) {
|
|
1226
1199
|
currentPath = currentPath ? `${currentPath}.${key}` : key;
|
|
@@ -1320,19 +1293,10 @@ async function $LT_CMD_Collect(options) {
|
|
|
1320
1293
|
return;
|
|
1321
1294
|
}
|
|
1322
1295
|
try {
|
|
1323
|
-
const
|
|
1296
|
+
const collections = await $LT_GroupTagsToCollections({ logger, files, config });
|
|
1324
1297
|
const totalTags = files.reduce((sum, file) => sum + file.tags.length, 0);
|
|
1325
1298
|
logger.debug("Found {totalTags} translation tags", { totalTags });
|
|
1326
|
-
|
|
1327
|
-
if (!changedNamespaces?.length) {
|
|
1328
|
-
logger.info("No changes were made based on the current configuration and files");
|
|
1329
|
-
return;
|
|
1330
|
-
}
|
|
1331
|
-
const n = changedNamespaces.map((n2) => `"${n2}.json"`).join(", ");
|
|
1332
|
-
logger.success("Updated namespaces {outputDir} ({namespaces})", {
|
|
1333
|
-
outputDir: config.outputDir,
|
|
1334
|
-
namespaces: n
|
|
1335
|
-
});
|
|
1299
|
+
await $LT_WriteToCollections({ config, collections, logger, clean: options?.clean });
|
|
1336
1300
|
} catch (e) {
|
|
1337
1301
|
const prefix = "LangTagConflictResolution:";
|
|
1338
1302
|
if (e.message.startsWith(prefix)) {
|
|
@@ -1398,15 +1362,8 @@ async function handleFile(config, logger, cwdRelativeFilePath, event) {
|
|
|
1398
1362
|
const absoluteFilePath = path__default.join(cwd, cwdRelativeFilePath);
|
|
1399
1363
|
await checkAndRegenerateFileLangTags(config, logger, absoluteFilePath, cwdRelativeFilePath);
|
|
1400
1364
|
const files = await $LT_CollectCandidateFilesWithTags({ filesToScan: [cwdRelativeFilePath], config, logger });
|
|
1401
|
-
const namespaces = await $
|
|
1402
|
-
|
|
1403
|
-
if (changedNamespaces.length > 0) {
|
|
1404
|
-
const n = changedNamespaces.map((n2) => `"${n2}.json"`).join(", ");
|
|
1405
|
-
logger.success("Updated namespaces {outputDir} ({namespaces})", {
|
|
1406
|
-
outputDir: config.outputDir,
|
|
1407
|
-
namespaces: n
|
|
1408
|
-
});
|
|
1409
|
-
}
|
|
1365
|
+
const namespaces = await $LT_GroupTagsToCollections({ logger, files, config });
|
|
1366
|
+
await $LT_WriteToCollections({ config, collections: namespaces, logger });
|
|
1410
1367
|
}
|
|
1411
1368
|
async function detectModuleSystem() {
|
|
1412
1369
|
const packageJsonPath = join(process.cwd(), "package.json");
|
|
@@ -1446,7 +1403,13 @@ const generationAlgorithm = pathBasedConfigGenerator({
|
|
|
1446
1403
|
// },
|
|
1447
1404
|
// admin: {
|
|
1448
1405
|
// '>': 'management', // rename "admin" to "management"
|
|
1449
|
-
// users: false // ignore "users"
|
|
1406
|
+
// users: false // ignore "users",
|
|
1407
|
+
// ui: {
|
|
1408
|
+
// '>>': { // 'redirect' - ignore everything, jump to 'ui' namespace and prefix all paths with 'admin'
|
|
1409
|
+
// namespace: 'ui',
|
|
1410
|
+
// pathPrefix: 'admin'
|
|
1411
|
+
// }
|
|
1412
|
+
// }
|
|
1450
1413
|
// }
|
|
1451
1414
|
// }
|
|
1452
1415
|
// }
|
|
@@ -1459,7 +1422,8 @@ const config = {
|
|
|
1459
1422
|
isLibrary: false,
|
|
1460
1423
|
includes: ['src/**/*.{js,ts,jsx,tsx}'],
|
|
1461
1424
|
excludes: ['node_modules', 'dist', 'build', '**/*.test.ts'],
|
|
1462
|
-
|
|
1425
|
+
localesDirectory: 'public/locales',
|
|
1426
|
+
baseLanguageCode: 'en',
|
|
1463
1427
|
onConfigGeneration: async event => {
|
|
1464
1428
|
// We do not modify imported configurations
|
|
1465
1429
|
if (event.isImportedLibrary) return;
|
|
@@ -1588,7 +1552,7 @@ async function $LT_ImportLibraries(config, logger) {
|
|
|
1588
1552
|
const content = `${config.import.tagImportPath}
|
|
1589
1553
|
|
|
1590
1554
|
${exports}`;
|
|
1591
|
-
await $LT_EnsureDirectoryExists(dirname
|
|
1555
|
+
await $LT_EnsureDirectoryExists(dirname(filePath));
|
|
1592
1556
|
await writeFile(filePath, content, "utf-8");
|
|
1593
1557
|
logger.success('Imported node_modules file: "{fileName}"', { fileName });
|
|
1594
1558
|
}
|
|
@@ -1606,7 +1570,7 @@ function renderTemplate(template, data) {
|
|
|
1606
1570
|
}
|
|
1607
1571
|
function loadTemplate(templateName) {
|
|
1608
1572
|
const __filename = fileURLToPath(import.meta.url);
|
|
1609
|
-
const __dirname = dirname(__filename);
|
|
1573
|
+
const __dirname = dirname$1(__filename);
|
|
1610
1574
|
const templatePath = join(__dirname, "template", `${templateName}.mustache`);
|
|
1611
1575
|
try {
|
|
1612
1576
|
return readFileSync(templatePath, "utf-8");
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { mkdir, writeFile, readFile, rm } from "fs/promises";
|
|
2
|
+
import { dirname, resolve } from "path";
|
|
3
|
+
import path, { resolve as resolve$1 } from "pathe";
|
|
4
|
+
import process__default from "node:process";
|
|
5
|
+
class TranslationsCollector {
|
|
6
|
+
config;
|
|
7
|
+
logger;
|
|
8
|
+
}
|
|
9
|
+
async function $LT_EnsureDirectoryExists(filePath) {
|
|
10
|
+
await mkdir(filePath, { recursive: true });
|
|
11
|
+
}
|
|
12
|
+
async function $LT_RemoveDirectory(dirPath) {
|
|
13
|
+
try {
|
|
14
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
15
|
+
} catch (error) {
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function $LT_RemoveFile(filePath) {
|
|
19
|
+
try {
|
|
20
|
+
await rm(filePath, { force: true });
|
|
21
|
+
} catch (error) {
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async function $LT_WriteJSON(filePath, data) {
|
|
25
|
+
await writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
|
|
26
|
+
}
|
|
27
|
+
async function $LT_ReadJSON(filePath) {
|
|
28
|
+
const content = await readFile(filePath, "utf-8");
|
|
29
|
+
return JSON.parse(content);
|
|
30
|
+
}
|
|
31
|
+
async function $LT_WriteFileWithDirs(filePath, content) {
|
|
32
|
+
const dir = dirname(filePath);
|
|
33
|
+
try {
|
|
34
|
+
await mkdir(dir, { recursive: true });
|
|
35
|
+
} catch (error) {
|
|
36
|
+
}
|
|
37
|
+
await writeFile(filePath, content, "utf-8");
|
|
38
|
+
}
|
|
39
|
+
async function $LT_ReadFileContent(relativeFilePath) {
|
|
40
|
+
const cwd = process.cwd();
|
|
41
|
+
const absolutePath = resolve(cwd, relativeFilePath);
|
|
42
|
+
return await readFile(absolutePath, "utf-8");
|
|
43
|
+
}
|
|
44
|
+
class NamespaceCollector extends TranslationsCollector {
|
|
45
|
+
clean;
|
|
46
|
+
languageDirectory;
|
|
47
|
+
aggregateCollection(namespace) {
|
|
48
|
+
return namespace;
|
|
49
|
+
}
|
|
50
|
+
transformTag(tag) {
|
|
51
|
+
return tag;
|
|
52
|
+
}
|
|
53
|
+
async preWrite(clean) {
|
|
54
|
+
this.clean = clean;
|
|
55
|
+
this.languageDirectory = path.join(this.config.localesDirectory, this.config.baseLanguageCode);
|
|
56
|
+
if (clean) {
|
|
57
|
+
this.logger.info("Cleaning output directory...");
|
|
58
|
+
await $LT_RemoveDirectory(this.languageDirectory);
|
|
59
|
+
}
|
|
60
|
+
await $LT_EnsureDirectoryExists(this.languageDirectory);
|
|
61
|
+
}
|
|
62
|
+
async resolveCollectionFilePath(collectionName) {
|
|
63
|
+
return resolve$1(
|
|
64
|
+
process__default.cwd(),
|
|
65
|
+
this.languageDirectory,
|
|
66
|
+
collectionName + ".json"
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
async onMissingCollection(collectionName) {
|
|
70
|
+
if (!this.clean) {
|
|
71
|
+
this.logger.warn(`Original namespace file "{namespace}.json" not found. A new one will be created.`, { namespace: collectionName });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async postWrite(changedCollections) {
|
|
75
|
+
if (!changedCollections?.length) {
|
|
76
|
+
this.logger.info("No changes were made based on the current configuration and files");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const n = changedCollections.map((n2) => `"${n2}.json"`).join(", ");
|
|
80
|
+
this.logger.success("Updated namespaces {outputDir} ({namespaces})", {
|
|
81
|
+
outputDir: this.config.localesDirectory,
|
|
82
|
+
namespaces: n
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
$LT_ReadFileContent as $,
|
|
88
|
+
NamespaceCollector as N,
|
|
89
|
+
TranslationsCollector as T,
|
|
90
|
+
$LT_ReadJSON as a,
|
|
91
|
+
$LT_WriteJSON as b,
|
|
92
|
+
$LT_EnsureDirectoryExists as c,
|
|
93
|
+
$LT_WriteFileWithDirs as d,
|
|
94
|
+
$LT_RemoveFile as e
|
|
95
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
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
|
+
class TranslationsCollector {
|
|
7
|
+
config;
|
|
8
|
+
logger;
|
|
9
|
+
}
|
|
10
|
+
async function $LT_EnsureDirectoryExists(filePath) {
|
|
11
|
+
await promises.mkdir(filePath, { recursive: true });
|
|
12
|
+
}
|
|
13
|
+
async function $LT_RemoveDirectory(dirPath) {
|
|
14
|
+
try {
|
|
15
|
+
await promises.rm(dirPath, { recursive: true, force: true });
|
|
16
|
+
} catch (error) {
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function $LT_RemoveFile(filePath) {
|
|
20
|
+
try {
|
|
21
|
+
await promises.rm(filePath, { force: true });
|
|
22
|
+
} catch (error) {
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function $LT_WriteJSON(filePath, data) {
|
|
26
|
+
await promises.writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
|
|
27
|
+
}
|
|
28
|
+
async function $LT_ReadJSON(filePath) {
|
|
29
|
+
const content = await promises.readFile(filePath, "utf-8");
|
|
30
|
+
return JSON.parse(content);
|
|
31
|
+
}
|
|
32
|
+
async function $LT_WriteFileWithDirs(filePath, content) {
|
|
33
|
+
const dir = path.dirname(filePath);
|
|
34
|
+
try {
|
|
35
|
+
await promises.mkdir(dir, { recursive: true });
|
|
36
|
+
} catch (error) {
|
|
37
|
+
}
|
|
38
|
+
await promises.writeFile(filePath, content, "utf-8");
|
|
39
|
+
}
|
|
40
|
+
async function $LT_ReadFileContent(relativeFilePath) {
|
|
41
|
+
const cwd = process.cwd();
|
|
42
|
+
const absolutePath = path.resolve(cwd, relativeFilePath);
|
|
43
|
+
return await promises.readFile(absolutePath, "utf-8");
|
|
44
|
+
}
|
|
45
|
+
class NamespaceCollector extends TranslationsCollector {
|
|
46
|
+
clean;
|
|
47
|
+
languageDirectory;
|
|
48
|
+
aggregateCollection(namespace) {
|
|
49
|
+
return namespace;
|
|
50
|
+
}
|
|
51
|
+
transformTag(tag) {
|
|
52
|
+
return tag;
|
|
53
|
+
}
|
|
54
|
+
async preWrite(clean) {
|
|
55
|
+
this.clean = clean;
|
|
56
|
+
this.languageDirectory = path$1.join(this.config.localesDirectory, this.config.baseLanguageCode);
|
|
57
|
+
if (clean) {
|
|
58
|
+
this.logger.info("Cleaning output directory...");
|
|
59
|
+
await $LT_RemoveDirectory(this.languageDirectory);
|
|
60
|
+
}
|
|
61
|
+
await $LT_EnsureDirectoryExists(this.languageDirectory);
|
|
62
|
+
}
|
|
63
|
+
async resolveCollectionFilePath(collectionName) {
|
|
64
|
+
return path$1.resolve(
|
|
65
|
+
process$1.cwd(),
|
|
66
|
+
this.languageDirectory,
|
|
67
|
+
collectionName + ".json"
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
async onMissingCollection(collectionName) {
|
|
71
|
+
if (!this.clean) {
|
|
72
|
+
this.logger.warn(`Original namespace file "{namespace}.json" not found. A new one will be created.`, { namespace: collectionName });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async postWrite(changedCollections) {
|
|
76
|
+
if (!changedCollections?.length) {
|
|
77
|
+
this.logger.info("No changes were made based on the current configuration and files");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const n = changedCollections.map((n2) => `"${n2}.json"`).join(", ");
|
|
81
|
+
this.logger.success("Updated namespaces {outputDir} ({namespaces})", {
|
|
82
|
+
outputDir: this.config.localesDirectory,
|
|
83
|
+
namespaces: n
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.$LT_EnsureDirectoryExists = $LT_EnsureDirectoryExists;
|
|
88
|
+
exports.$LT_ReadFileContent = $LT_ReadFileContent;
|
|
89
|
+
exports.$LT_ReadJSON = $LT_ReadJSON;
|
|
90
|
+
exports.$LT_RemoveFile = $LT_RemoveFile;
|
|
91
|
+
exports.$LT_WriteFileWithDirs = $LT_WriteFileWithDirs;
|
|
92
|
+
exports.$LT_WriteJSON = $LT_WriteJSON;
|
|
93
|
+
exports.NamespaceCollector = NamespaceCollector;
|
|
94
|
+
exports.TranslationsCollector = TranslationsCollector;
|
package/package.json
CHANGED
|
@@ -33,7 +33,6 @@ export function {{tagName}}{{#isTypeScript}}<T extends LangTagTranslations>{{/is
|
|
|
33
33
|
// transform: ({ path, params }) => t(path, params),
|
|
34
34
|
// });
|
|
35
35
|
// };
|
|
36
|
-
//
|
|
37
36
|
// // SSR
|
|
38
37
|
// const initT = async (language?: string) => {
|
|
39
38
|
// const { t } = await initTranslations({ language, namespaces: [namespace] });
|
|
@@ -51,22 +50,18 @@ export function {{tagName}}{{#isTypeScript}}<T extends LangTagTranslations>{{/is
|
|
|
51
50
|
});
|
|
52
51
|
};
|
|
53
52
|
|
|
54
|
-
const client = () => {
|
|
55
|
-
{{#isReact}}
|
|
56
|
-
return useMemo(() => createTranslations(), []);
|
|
57
|
-
{{/isReact}}
|
|
58
|
-
{{^isReact}}
|
|
59
|
-
return createTranslations();
|
|
60
|
-
{{/isReact}}
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const server = () => {
|
|
64
|
-
return createTranslations();
|
|
65
|
-
};
|
|
66
|
-
|
|
67
53
|
return {
|
|
68
|
-
client
|
|
69
|
-
|
|
54
|
+
client: () => {
|
|
55
|
+
{{#isReact}}
|
|
56
|
+
return useMemo(() => createTranslations(), []);
|
|
57
|
+
{{/isReact}}
|
|
58
|
+
{{^isReact}}
|
|
59
|
+
return createTranslations();
|
|
60
|
+
{{/isReact}}
|
|
61
|
+
},
|
|
62
|
+
server: () => {
|
|
63
|
+
return createTranslations();
|
|
64
|
+
},
|
|
70
65
|
{{#isTypeScript}}
|
|
71
66
|
Type: {} as CallableTranslations<T>,
|
|
72
67
|
{{/isTypeScript}}
|
|
@@ -40,45 +40,36 @@ export function {{tagName}}<T extends LangTagTranslations>(
|
|
|
40
40
|
|
|
41
41
|
{{#isReact}}
|
|
42
42
|
const Context = createContext{{#isTypeScript}}<CallableTranslations<T> | null>{{/isTypeScript}}(null);
|
|
43
|
-
|
|
44
|
-
const useTranslations = () => {
|
|
45
|
-
const contextTranslations = useContext(Context);
|
|
46
|
-
return useMemo(() => createTranslationHelper(contextTranslations), [contextTranslations]);
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const initTranslations = (translations{{#isTypeScript}}?: PartialFlexibleTranslations<T>{{/isTypeScript}}) => {
|
|
50
|
-
const normalized = useMemo(
|
|
51
|
-
() => translations ? normalizeTranslations(translations) : null,
|
|
52
|
-
[translations]
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
return useMemo(() => createTranslationHelper(normalized), [normalized]);
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
function Provider({translations, children}{{#isTypeScript}}: { translations?: PartialFlexibleTranslations<T>; children: ReactNode }{{/isTypeScript}}){{#isTypeScript}}: ReactNode{{/isTypeScript}} {
|
|
59
|
-
const normalized = useMemo(
|
|
60
|
-
() => translations ? normalizeTranslations(translations) : null,
|
|
61
|
-
[translations]
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
return <Context.Provider value={normalized}>{children}</Context.Provider>;
|
|
65
|
-
}
|
|
66
|
-
{{/isReact}}
|
|
67
|
-
{{^isReact}}
|
|
68
|
-
const initTranslations = (translations{{#isTypeScript}}?: PartialFlexibleTranslations<T>{{/isTypeScript}}) => {
|
|
69
|
-
const normalized = translations ? normalizeTranslations(translations) : null;
|
|
70
|
-
return createTranslationHelper(normalized);
|
|
71
|
-
};
|
|
72
43
|
{{/isReact}}
|
|
73
44
|
|
|
74
45
|
return {
|
|
75
46
|
{{#isReact}}
|
|
76
|
-
useTranslations
|
|
77
|
-
|
|
78
|
-
|
|
47
|
+
useTranslations: () => {
|
|
48
|
+
const contextTranslations = useContext(Context);
|
|
49
|
+
return useMemo(() => createTranslationHelper(contextTranslations), [contextTranslations]);
|
|
50
|
+
},
|
|
51
|
+
initTranslations: (translations{{#isTypeScript}}?: PartialFlexibleTranslations<T>{{/isTypeScript}}) => {
|
|
52
|
+
const normalized = useMemo(
|
|
53
|
+
() => translations ? normalizeTranslations(translations) : null,
|
|
54
|
+
[translations]
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
return useMemo(() => createTranslationHelper(normalized), [normalized]);
|
|
58
|
+
},
|
|
59
|
+
Provider({translations, children}{{#isTypeScript}}: { translations?: PartialFlexibleTranslations<T>; children: ReactNode }{{/isTypeScript}}){{#isTypeScript}}: ReactNode{{/isTypeScript}} {
|
|
60
|
+
const normalized = useMemo(
|
|
61
|
+
() => translations ? normalizeTranslations(translations) : null,
|
|
62
|
+
[translations]
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return <Context.Provider value={normalized}>{children}</Context.Provider>;
|
|
66
|
+
},
|
|
79
67
|
{{/isReact}}
|
|
80
68
|
{{^isReact}}
|
|
81
|
-
initTranslations
|
|
69
|
+
initTranslations: (translations{{#isTypeScript}}?: PartialFlexibleTranslations<T>{{/isTypeScript}}) => {
|
|
70
|
+
const normalized = translations ? normalizeTranslations(translations) : null;
|
|
71
|
+
return createTranslationHelper(normalized);
|
|
72
|
+
};,
|
|
82
73
|
{{/isReact}}
|
|
83
74
|
{{#isTypeScript}}
|
|
84
75
|
InputType: {} as PartialFlexibleTranslations<T>,
|