@scoutello/i18n-magic 0.16.0 → 0.19.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.
Files changed (64) hide show
  1. package/README.md +207 -19
  2. package/dist/cli.d.ts +2 -0
  3. package/dist/cli.d.ts.map +1 -0
  4. package/dist/cli.js +101 -0
  5. package/dist/cli.js.map +1 -0
  6. package/dist/commands/check-missing.d.ts +1 -0
  7. package/dist/commands/check-missing.d.ts.map +1 -0
  8. package/dist/commands/check-missing.js +13 -0
  9. package/dist/commands/check-missing.js.map +1 -0
  10. package/dist/commands/clean.d.ts +1 -0
  11. package/dist/commands/clean.d.ts.map +1 -0
  12. package/dist/commands/clean.js +82 -0
  13. package/dist/commands/clean.js.map +1 -0
  14. package/dist/commands/create-pruned-namespace-automated.d.ts +20 -0
  15. package/dist/commands/create-pruned-namespace-automated.d.ts.map +1 -0
  16. package/dist/commands/create-pruned-namespace-automated.js +98 -0
  17. package/dist/commands/create-pruned-namespace-automated.js.map +1 -0
  18. package/dist/commands/create-pruned-namespace.d.ts +3 -0
  19. package/dist/commands/create-pruned-namespace.d.ts.map +1 -0
  20. package/dist/commands/create-pruned-namespace.js +123 -0
  21. package/dist/commands/create-pruned-namespace.js.map +1 -0
  22. package/dist/commands/replace.d.ts +1 -0
  23. package/dist/commands/replace.d.ts.map +1 -0
  24. package/dist/commands/replace.js +58 -0
  25. package/dist/commands/replace.js.map +1 -0
  26. package/dist/commands/scan.d.ts +1 -0
  27. package/dist/commands/scan.d.ts.map +1 -0
  28. package/dist/commands/scan.js +70 -0
  29. package/dist/commands/scan.js.map +1 -0
  30. package/dist/commands/sync-locales.d.ts +1 -0
  31. package/dist/commands/sync-locales.d.ts.map +1 -0
  32. package/dist/commands/sync-locales.js +78 -0
  33. package/dist/commands/sync-locales.js.map +1 -0
  34. package/dist/i18n-magic.cjs.development.js +348 -130
  35. package/dist/i18n-magic.cjs.development.js.map +1 -1
  36. package/dist/i18n-magic.cjs.production.min.js +1 -1
  37. package/dist/i18n-magic.cjs.production.min.js.map +1 -1
  38. package/dist/i18n-magic.esm.js +339 -130
  39. package/dist/i18n-magic.esm.js.map +1 -1
  40. package/dist/index.d.ts +11 -1
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +22 -9
  43. package/dist/index.js.map +1 -0
  44. package/dist/lib/languges.d.ts +1 -0
  45. package/dist/lib/languges.d.ts.map +1 -0
  46. package/dist/lib/languges.js +146 -0
  47. package/dist/lib/languges.js.map +1 -0
  48. package/dist/lib/types.d.ts +7 -1
  49. package/dist/lib/types.d.ts.map +1 -0
  50. package/dist/lib/types.js +3 -0
  51. package/dist/lib/types.js.map +1 -0
  52. package/dist/lib/utils.d.ts +20 -1
  53. package/dist/lib/utils.d.ts.map +1 -0
  54. package/dist/lib/utils.js +264 -0
  55. package/dist/lib/utils.js.map +1 -0
  56. package/package.json +38 -14
  57. package/src/cli.ts +117 -0
  58. package/src/commands/clean.ts +4 -1
  59. package/src/commands/create-pruned-namespace-automated.ts +165 -0
  60. package/src/commands/create-pruned-namespace.ts +168 -0
  61. package/src/commands/scan.ts +12 -0
  62. package/src/index.ts +23 -113
  63. package/src/lib/types.ts +7 -1
  64. package/src/lib/utils.ts +59 -2
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var commander = require('commander');
4
- var dotenv = require('dotenv');
5
- var OpenAI = require('openai');
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
6
5
  var glob = require('fast-glob');
7
6
  var i18nextScanner = require('i18next-scanner');
8
7
  var fs = require('node:fs');
9
8
  var path = require('node:path');
10
9
  var prompts = require('prompts');
10
+ var console$1 = require('console');
11
11
 
12
12
  function _construct(t, e, r) {
13
13
  if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
@@ -1077,6 +1077,293 @@ var removeUnusedKeys = /*#__PURE__*/function () {
1077
1077
  };
1078
1078
  }();
1079
1079
 
1080
+ var createPrunedNamespace = /*#__PURE__*/function () {
1081
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
1082
+ var namespaces, loadPath, savePath, locales, defaultNamespace, sourceNamespaceResponse, sourceNamespace, newNamespaceResponse, newNamespace, globPatternsResponse, selectedGlobPatterns, parser, files, extractedKeys, _iterator, _step, file, content, uniqueExtractedKeys, relevantKeys, _iterator2, _step2, key, pureKey, _iterator3, _step3, locale, sourceTranslations, newNamespaceTranslations, _iterator4, _step4, _key;
1083
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1084
+ while (1) switch (_context.prev = _context.next) {
1085
+ case 0:
1086
+ namespaces = config.namespaces, loadPath = config.loadPath, savePath = config.savePath, locales = config.locales, defaultNamespace = config.defaultNamespace; // Step 1: Ask for source namespace
1087
+ _context.next = 3;
1088
+ return prompts({
1089
+ type: "select",
1090
+ name: "value",
1091
+ message: "Select source namespace to create pruned version from:",
1092
+ choices: namespaces.map(function (namespace) {
1093
+ return {
1094
+ title: namespace,
1095
+ value: namespace
1096
+ };
1097
+ }),
1098
+ onState: function onState(state) {
1099
+ if (state.aborted) {
1100
+ process.nextTick(function () {
1101
+ process.exit(0);
1102
+ });
1103
+ }
1104
+ }
1105
+ });
1106
+ case 3:
1107
+ sourceNamespaceResponse = _context.sent;
1108
+ sourceNamespace = sourceNamespaceResponse.value; // Step 2: Ask for new namespace name
1109
+ _context.next = 7;
1110
+ return prompts({
1111
+ type: "text",
1112
+ name: "value",
1113
+ message: "Enter the name for the new namespace:",
1114
+ validate: function validate(value) {
1115
+ if (!value) return "Namespace name cannot be empty";
1116
+ if (namespaces.includes(value)) return "Namespace already exists";
1117
+ return true;
1118
+ },
1119
+ onState: function onState(state) {
1120
+ if (state.aborted) {
1121
+ process.nextTick(function () {
1122
+ process.exit(0);
1123
+ });
1124
+ }
1125
+ }
1126
+ });
1127
+ case 7:
1128
+ newNamespaceResponse = _context.sent;
1129
+ newNamespace = newNamespaceResponse.value; // Step 3: Ask for glob patterns to find relevant keys
1130
+ _context.next = 11;
1131
+ return prompts({
1132
+ type: "list",
1133
+ name: "value",
1134
+ message: "Enter glob patterns to find relevant keys (comma separated):",
1135
+ initial: config.globPatterns.join(","),
1136
+ separator: ",",
1137
+ onState: function onState(state) {
1138
+ if (state.aborted) {
1139
+ process.nextTick(function () {
1140
+ process.exit(0);
1141
+ });
1142
+ }
1143
+ }
1144
+ });
1145
+ case 11:
1146
+ globPatternsResponse = _context.sent;
1147
+ selectedGlobPatterns = globPatternsResponse.value;
1148
+ console.log("Finding keys used in files matching: " + selectedGlobPatterns.join(", "));
1149
+ // Extract keys from files matching the glob patterns
1150
+ parser = new i18nextScanner.Parser({
1151
+ nsSeparator: false,
1152
+ keySeparator: false
1153
+ });
1154
+ _context.next = 17;
1155
+ return glob([].concat(selectedGlobPatterns, ["!**/node_modules/**"]));
1156
+ case 17:
1157
+ files = _context.sent;
1158
+ console.log("Found " + files.length + " files to scan");
1159
+ extractedKeys = [];
1160
+ for (_iterator = _createForOfIteratorHelperLoose(files); !(_step = _iterator()).done;) {
1161
+ file = _step.value;
1162
+ content = fs.readFileSync(file, "utf-8");
1163
+ parser.parseFuncFromString(content, {
1164
+ list: ["t"]
1165
+ }, function (key) {
1166
+ extractedKeys.push(key);
1167
+ });
1168
+ }
1169
+ uniqueExtractedKeys = removeDuplicatesFromArray(extractedKeys);
1170
+ console.log("Found " + uniqueExtractedKeys.length + " unique translation keys");
1171
+ // Filter keys that belong to the source namespace
1172
+ relevantKeys = [];
1173
+ for (_iterator2 = _createForOfIteratorHelperLoose(uniqueExtractedKeys); !(_step2 = _iterator2()).done;) {
1174
+ key = _step2.value;
1175
+ pureKey = getPureKey(key, sourceNamespace, sourceNamespace === defaultNamespace);
1176
+ if (pureKey) {
1177
+ relevantKeys.push(pureKey);
1178
+ }
1179
+ }
1180
+ console.log("Found " + relevantKeys.length + " keys from namespace '" + sourceNamespace + "'");
1181
+ if (!(relevantKeys.length === 0)) {
1182
+ _context.next = 29;
1183
+ break;
1184
+ }
1185
+ console.log("No relevant keys found. Exiting...");
1186
+ return _context.abrupt("return");
1187
+ case 29:
1188
+ _iterator3 = _createForOfIteratorHelperLoose(locales);
1189
+ case 30:
1190
+ if ((_step3 = _iterator3()).done) {
1191
+ _context.next = 48;
1192
+ break;
1193
+ }
1194
+ locale = _step3.value;
1195
+ _context.prev = 32;
1196
+ _context.next = 35;
1197
+ return loadLocalesFile(loadPath, locale, sourceNamespace);
1198
+ case 35:
1199
+ sourceTranslations = _context.sent;
1200
+ // Create new namespace with only the keys used in the glob pattern files
1201
+ newNamespaceTranslations = {};
1202
+ for (_iterator4 = _createForOfIteratorHelperLoose(relevantKeys); !(_step4 = _iterator4()).done;) {
1203
+ _key = _step4.value;
1204
+ if (sourceTranslations[_key]) {
1205
+ newNamespaceTranslations[_key] = sourceTranslations[_key];
1206
+ }
1207
+ }
1208
+ // Write the new namespace file
1209
+ _context.next = 40;
1210
+ return writeLocalesFile(savePath, locale, newNamespace, newNamespaceTranslations);
1211
+ case 40:
1212
+ console.log("Created pruned namespace '" + newNamespace + "' for locale '" + locale + "' with " + Object.keys(newNamespaceTranslations).length + " keys");
1213
+ _context.next = 46;
1214
+ break;
1215
+ case 43:
1216
+ _context.prev = 43;
1217
+ _context.t0 = _context["catch"](32);
1218
+ console.error("Error creating pruned namespace for locale '" + locale + "':", _context.t0);
1219
+ case 46:
1220
+ _context.next = 30;
1221
+ break;
1222
+ case 48:
1223
+ console.log("\u2705 Successfully created pruned namespace '" + newNamespace + "'");
1224
+ case 49:
1225
+ case "end":
1226
+ return _context.stop();
1227
+ }
1228
+ }, _callee, null, [[32, 43]]);
1229
+ }));
1230
+ return function createPrunedNamespace(_x) {
1231
+ return _ref.apply(this, arguments);
1232
+ };
1233
+ }();
1234
+
1235
+ var createPrunedNamespaceAutomated = /*#__PURE__*/function () {
1236
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, options) {
1237
+ var namespaces, loadPath, savePath, locales, defaultNamespace, sourceNamespace, newNamespace, globPatterns, parser, files, extractedKeys, _iterator, _step, file, content, uniqueExtractedKeys, relevantKeys, _iterator2, _step2, key, pureKey, results, _iterator3, _step3, locale, sourceTranslations, newNamespaceTranslations, _iterator4, _step4, _key, keyCount;
1238
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1239
+ while (1) switch (_context.prev = _context.next) {
1240
+ case 0:
1241
+ namespaces = config.namespaces, loadPath = config.loadPath, savePath = config.savePath, locales = config.locales, defaultNamespace = config.defaultNamespace;
1242
+ sourceNamespace = options.sourceNamespace, newNamespace = options.newNamespace, globPatterns = options.globPatterns; // Validate inputs
1243
+ if (namespaces.includes(sourceNamespace)) {
1244
+ _context.next = 4;
1245
+ break;
1246
+ }
1247
+ throw new Error("Source namespace '" + sourceNamespace + "' not found in configuration");
1248
+ case 4:
1249
+ if (!namespaces.includes(newNamespace)) {
1250
+ _context.next = 6;
1251
+ break;
1252
+ }
1253
+ throw new Error("Namespace '" + newNamespace + "' already exists");
1254
+ case 6:
1255
+ console.log("Creating pruned namespace '" + newNamespace + "' from '" + sourceNamespace + "'");
1256
+ console.log("Using glob patterns: " + globPatterns.join(", "));
1257
+ // Extract keys from files matching the glob patterns
1258
+ parser = new i18nextScanner.Parser({
1259
+ nsSeparator: false,
1260
+ keySeparator: false
1261
+ });
1262
+ _context.next = 11;
1263
+ return glob([].concat(globPatterns, ["!**/node_modules/**"]));
1264
+ case 11:
1265
+ files = _context.sent;
1266
+ console.log("Found " + files.length + " files to scan");
1267
+ extractedKeys = [];
1268
+ for (_iterator = _createForOfIteratorHelperLoose(files); !(_step = _iterator()).done;) {
1269
+ file = _step.value;
1270
+ content = fs.readFileSync(file, "utf-8");
1271
+ parser.parseFuncFromString(content, {
1272
+ list: ["t"]
1273
+ }, function (key) {
1274
+ extractedKeys.push(key);
1275
+ });
1276
+ }
1277
+ uniqueExtractedKeys = removeDuplicatesFromArray(extractedKeys);
1278
+ console.log("Found " + uniqueExtractedKeys.length + " unique translation keys");
1279
+ // Filter keys that belong to the source namespace
1280
+ relevantKeys = [];
1281
+ for (_iterator2 = _createForOfIteratorHelperLoose(uniqueExtractedKeys); !(_step2 = _iterator2()).done;) {
1282
+ key = _step2.value;
1283
+ pureKey = getPureKey(key, sourceNamespace, sourceNamespace === defaultNamespace);
1284
+ if (pureKey) {
1285
+ relevantKeys.push(pureKey);
1286
+ }
1287
+ }
1288
+ console.log("Found " + relevantKeys.length + " keys from namespace '" + sourceNamespace + "'");
1289
+ if (!(relevantKeys.length === 0)) {
1290
+ _context.next = 23;
1291
+ break;
1292
+ }
1293
+ console.log("No relevant keys found. Exiting...");
1294
+ return _context.abrupt("return", {
1295
+ success: false,
1296
+ message: "No relevant keys found",
1297
+ keysCount: 0
1298
+ });
1299
+ case 23:
1300
+ // Get translations from source namespace and create new namespace files
1301
+ results = [];
1302
+ _iterator3 = _createForOfIteratorHelperLoose(locales);
1303
+ case 25:
1304
+ if ((_step3 = _iterator3()).done) {
1305
+ _context.next = 46;
1306
+ break;
1307
+ }
1308
+ locale = _step3.value;
1309
+ _context.prev = 27;
1310
+ _context.next = 30;
1311
+ return loadLocalesFile(loadPath, locale, sourceNamespace);
1312
+ case 30:
1313
+ sourceTranslations = _context.sent;
1314
+ // Create new namespace with only the keys used in the glob pattern files
1315
+ newNamespaceTranslations = {};
1316
+ for (_iterator4 = _createForOfIteratorHelperLoose(relevantKeys); !(_step4 = _iterator4()).done;) {
1317
+ _key = _step4.value;
1318
+ if (sourceTranslations[_key]) {
1319
+ newNamespaceTranslations[_key] = sourceTranslations[_key];
1320
+ }
1321
+ }
1322
+ // Write the new namespace file
1323
+ _context.next = 35;
1324
+ return writeLocalesFile(savePath, locale, newNamespace, newNamespaceTranslations);
1325
+ case 35:
1326
+ keyCount = Object.keys(newNamespaceTranslations).length;
1327
+ console.log("Created pruned namespace '" + newNamespace + "' for locale '" + locale + "' with " + keyCount + " keys");
1328
+ results.push({
1329
+ locale: locale,
1330
+ keyCount: keyCount,
1331
+ success: true
1332
+ });
1333
+ _context.next = 44;
1334
+ break;
1335
+ case 40:
1336
+ _context.prev = 40;
1337
+ _context.t0 = _context["catch"](27);
1338
+ console.error("Error creating pruned namespace for locale '" + locale + "':", _context.t0);
1339
+ results.push({
1340
+ locale: locale,
1341
+ keyCount: 0,
1342
+ success: false,
1343
+ error: _context.t0 instanceof Error ? _context.t0.message : String(_context.t0)
1344
+ });
1345
+ case 44:
1346
+ _context.next = 25;
1347
+ break;
1348
+ case 46:
1349
+ console.log("\u2705 Successfully created pruned namespace '" + newNamespace + "'");
1350
+ return _context.abrupt("return", {
1351
+ success: true,
1352
+ message: "Created pruned namespace '" + newNamespace + "' with " + relevantKeys.length + " keys",
1353
+ keysCount: relevantKeys.length,
1354
+ results: results
1355
+ });
1356
+ case 48:
1357
+ case "end":
1358
+ return _context.stop();
1359
+ }
1360
+ }, _callee, null, [[27, 40]]);
1361
+ }));
1362
+ return function createPrunedNamespaceAutomated(_x, _x2) {
1363
+ return _ref.apply(this, arguments);
1364
+ };
1365
+ }();
1366
+
1080
1367
  var getKeyToReplace = /*#__PURE__*/function () {
1081
1368
  var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(keys) {
1082
1369
  var keyToReplace;
@@ -1204,69 +1491,79 @@ var replaceTranslation = /*#__PURE__*/function () {
1204
1491
 
1205
1492
  var translateMissing = /*#__PURE__*/function () {
1206
1493
  var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
1207
- var loadPath, savePath, defaultLocale, namespaces, locales, context, openai, disableTranslation, newKeys, newKeysWithDefaultLocale, _iterator, _step, newKey, answer, newKeysObject, allLocales, _iterator2, _step2, locale, translatedValues, _loop, _iterator3, _step3;
1494
+ var loadPath, savePath, defaultLocale, namespaces, locales, context, openai, disableTranslation, autoClear, newKeys, newKeysWithDefaultLocale, _iterator, _step, newKey, answer, newKeysObject, allLocales, _iterator2, _step2, locale, translatedValues, _loop, _iterator3, _step3;
1208
1495
  return _regeneratorRuntime().wrap(function _callee$(_context2) {
1209
1496
  while (1) switch (_context2.prev = _context2.next) {
1210
1497
  case 0:
1211
- loadPath = config.loadPath, savePath = config.savePath, defaultLocale = config.defaultLocale, namespaces = config.namespaces, locales = config.locales, context = config.context, openai = config.openai, disableTranslation = config.disableTranslation;
1212
- _context2.next = 3;
1498
+ loadPath = config.loadPath, savePath = config.savePath, defaultLocale = config.defaultLocale, namespaces = config.namespaces, locales = config.locales, context = config.context, openai = config.openai, disableTranslation = config.disableTranslation, autoClear = config.autoClear; // Run clean command first if autoClear is enabled
1499
+ if (!autoClear) {
1500
+ _context2.next = 6;
1501
+ break;
1502
+ }
1503
+ console$1.log("🧹 Auto-clearing unused translations before scanning...");
1504
+ _context2.next = 5;
1505
+ return removeUnusedKeys(config);
1506
+ case 5:
1507
+ console$1.log("✅ Auto-clear completed. Now scanning for missing translations...\n");
1508
+ case 6:
1509
+ _context2.next = 8;
1213
1510
  return getMissingKeys(config);
1214
- case 3:
1511
+ case 8:
1215
1512
  newKeys = _context2.sent;
1216
1513
  if (!(newKeys.length === 0)) {
1217
- _context2.next = 9;
1514
+ _context2.next = 14;
1218
1515
  break;
1219
1516
  }
1220
- console.log("No new keys found.");
1221
- _context2.next = 8;
1517
+ console$1.log("No new keys found.");
1518
+ _context2.next = 13;
1222
1519
  return checkAllKeysExist(config);
1223
- case 8:
1520
+ case 13:
1224
1521
  return _context2.abrupt("return");
1225
- case 9:
1226
- console.log(newKeys.length + " keys are missing. Please provide the values for the following keys in " + defaultLocale + ":");
1522
+ case 14:
1523
+ console$1.log(newKeys.length + " keys are missing. Please provide the values for the following keys in " + defaultLocale + ":");
1227
1524
  newKeysWithDefaultLocale = [];
1228
1525
  _iterator = _createForOfIteratorHelperLoose(newKeys);
1229
- case 12:
1526
+ case 17:
1230
1527
  if ((_step = _iterator()).done) {
1231
- _context2.next = 20;
1528
+ _context2.next = 25;
1232
1529
  break;
1233
1530
  }
1234
1531
  newKey = _step.value;
1235
- _context2.next = 16;
1532
+ _context2.next = 21;
1236
1533
  return getTextInput(newKey.key);
1237
- case 16:
1534
+ case 21:
1238
1535
  answer = _context2.sent;
1239
1536
  newKeysWithDefaultLocale.push({
1240
1537
  key: newKey.key,
1241
1538
  namespace: newKey.namespace,
1242
1539
  value: answer
1243
1540
  });
1244
- case 18:
1245
- _context2.next = 12;
1541
+ case 23:
1542
+ _context2.next = 17;
1246
1543
  break;
1247
- case 20:
1544
+ case 25:
1248
1545
  newKeysObject = newKeysWithDefaultLocale.reduce(function (prev, next) {
1249
1546
  prev[next.key] = next.value;
1250
1547
  return prev;
1251
1548
  }, {});
1252
1549
  allLocales = disableTranslation ? [defaultLocale] : locales;
1253
1550
  _iterator2 = _createForOfIteratorHelperLoose(allLocales);
1254
- case 23:
1551
+ case 28:
1255
1552
  if ((_step2 = _iterator2()).done) {
1256
- _context2.next = 43;
1553
+ _context2.next = 48;
1257
1554
  break;
1258
1555
  }
1259
1556
  locale = _step2.value;
1260
1557
  translatedValues = {};
1261
1558
  if (!(locale === defaultLocale)) {
1262
- _context2.next = 30;
1559
+ _context2.next = 35;
1263
1560
  break;
1264
1561
  }
1265
1562
  translatedValues = newKeysObject;
1266
- _context2.next = 33;
1563
+ _context2.next = 38;
1267
1564
  break;
1268
- case 30:
1269
- _context2.next = 32;
1565
+ case 35:
1566
+ _context2.next = 37;
1270
1567
  return translateKey({
1271
1568
  inputLanguage: defaultLocale,
1272
1569
  outputLanguage: locale,
@@ -1275,9 +1572,9 @@ var translateMissing = /*#__PURE__*/function () {
1275
1572
  openai: openai,
1276
1573
  model: config.model
1277
1574
  });
1278
- case 32:
1575
+ case 37:
1279
1576
  translatedValues = _context2.sent;
1280
- case 33:
1577
+ case 38:
1281
1578
  _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
1282
1579
  var namespace, existingKeys, relevantKeys, _iterator4, _step4, key;
1283
1580
  return _regeneratorRuntime().wrap(function _loop$(_context) {
@@ -1309,30 +1606,30 @@ var translateMissing = /*#__PURE__*/function () {
1309
1606
  }, _loop);
1310
1607
  });
1311
1608
  _iterator3 = _createForOfIteratorHelperLoose(namespaces);
1312
- case 35:
1609
+ case 40:
1313
1610
  if ((_step3 = _iterator3()).done) {
1314
- _context2.next = 41;
1611
+ _context2.next = 46;
1315
1612
  break;
1316
1613
  }
1317
- return _context2.delegateYield(_loop(), "t0", 37);
1318
- case 37:
1614
+ return _context2.delegateYield(_loop(), "t0", 42);
1615
+ case 42:
1319
1616
  if (!_context2.t0) {
1320
- _context2.next = 39;
1617
+ _context2.next = 44;
1321
1618
  break;
1322
1619
  }
1323
- return _context2.abrupt("continue", 39);
1324
- case 39:
1325
- _context2.next = 35;
1620
+ return _context2.abrupt("continue", 44);
1621
+ case 44:
1622
+ _context2.next = 40;
1326
1623
  break;
1327
- case 41:
1328
- _context2.next = 23;
1624
+ case 46:
1625
+ _context2.next = 28;
1329
1626
  break;
1330
- case 43:
1331
- _context2.next = 45;
1627
+ case 48:
1628
+ _context2.next = 50;
1332
1629
  return checkAllKeysExist(config);
1333
- case 45:
1334
- console.log("Successfully translated " + newKeys.length + " keys.");
1335
- case 46:
1630
+ case 50:
1631
+ console$1.log("Successfully translated " + newKeys.length + " keys.");
1632
+ case 51:
1336
1633
  case "end":
1337
1634
  return _context2.stop();
1338
1635
  }
@@ -1479,91 +1776,12 @@ var syncLocales = /*#__PURE__*/function () {
1479
1776
  };
1480
1777
  }();
1481
1778
 
1482
- // Only run CLI initialization when this file is executed directly
1483
- var program = /*#__PURE__*/new commander.Command();
1484
- program.name("i18n-magic").description("CLI to help you manage your locales JSON with translations, replacements, etc. with OpenAI.").version("0.2.0").option("-c, --config <path>", "path to config file").option("-e, --env <path>", "path to .env file");
1485
- var commands = [{
1486
- name: "scan",
1487
- description: "Scan for missing translations, get prompted for each, translate it to the other locales and save it to the JSON file.",
1488
- action: translateMissing
1489
- }, {
1490
- name: "replace",
1491
- description: "Replace a translation based on the key, and translate it to the other locales and save it to the JSON file.",
1492
- action: replaceTranslation
1493
- }, {
1494
- name: "check-missing",
1495
- description: "Check if there are any missing translations. Useful for a CI/CD pipeline or husky hook.",
1496
- action: checkMissing
1497
- }, {
1498
- name: "sync",
1499
- description: "Sync the translations from the default locale to the other locales. Useful for a CI/CD pipeline or husky hook.",
1500
- action: syncLocales
1501
- }, {
1502
- name: "clean",
1503
- description: "Remove unused translations from all locales. Useful for a CI/CD pipeline or husky hook.",
1504
- action: removeUnusedKeys
1505
- }];
1506
- var _loop = function _loop() {
1507
- var command = _commands[_i];
1508
- var cmd = program.command(command.name).description(command.description);
1509
- // Add key option to replace command
1510
- if (command.name === "replace") {
1511
- cmd.option("-k, --key <key>", "translation key to replace").allowExcessArguments(true).argument("[key]", "translation key to replace");
1512
- }
1513
- cmd.action( /*#__PURE__*/function () {
1514
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(arg, options) {
1515
- var _config$model;
1516
- var res, config, isGemini, openaiKey, geminiKey, key, keyType, openai, keyToUse;
1517
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1518
- while (1) switch (_context.prev = _context.next) {
1519
- case 0:
1520
- res = dotenv.config({
1521
- path: program.opts().env || ".env"
1522
- });
1523
- _context.next = 3;
1524
- return loadConfig({
1525
- configPath: program.opts().config
1526
- });
1527
- case 3:
1528
- config = _context.sent;
1529
- isGemini = (_config$model = config.model) == null ? void 0 : _config$model.includes("gemini"); // Get API key from environment or config
1530
- openaiKey = res.parsed.OPENAI_API_KEY || config.OPENAI_API_KEY;
1531
- geminiKey = res.parsed.GEMINI_API_KEY || config.GEMINI_API_KEY; // Select appropriate key based on model type
1532
- key = isGemini ? geminiKey : openaiKey;
1533
- if (!key) {
1534
- keyType = isGemini ? "GEMINI_API_KEY" : "OPENAI_API_KEY";
1535
- console.error("Please provide a" + (isGemini ? " Gemini" : "n OpenAI") + " API key in your .env file or config, called " + keyType + ".");
1536
- process.exit(1);
1537
- }
1538
- openai = new OpenAI(_extends({
1539
- apiKey: key
1540
- }, isGemini && {
1541
- baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
1542
- })); // For replace command, check for key in argument or option
1543
- if (command.name === "replace") {
1544
- // If key is provided as positional argument, use that first
1545
- keyToUse = typeof arg === "string" ? arg : options.key;
1546
- command.action(_extends({}, config, {
1547
- openai: openai
1548
- }), keyToUse);
1549
- } else {
1550
- command.action(_extends({}, config, {
1551
- openai: openai
1552
- }));
1553
- }
1554
- case 11:
1555
- case "end":
1556
- return _context.stop();
1557
- }
1558
- }, _callee);
1559
- }));
1560
- return function (_x, _x2) {
1561
- return _ref.apply(this, arguments);
1562
- };
1563
- }());
1564
- };
1565
- for (var _i = 0, _commands = commands; _i < _commands.length; _i++) {
1566
- _loop();
1567
- }
1568
- program.parse(process.argv);
1779
+ exports.checkMissing = checkMissing;
1780
+ exports.createPrunedNamespace = createPrunedNamespace;
1781
+ exports.createPrunedNamespaceAutomated = createPrunedNamespaceAutomated;
1782
+ exports.loadConfig = loadConfig;
1783
+ exports.removeUnusedKeys = removeUnusedKeys;
1784
+ exports.replaceTranslation = replaceTranslation;
1785
+ exports.syncLocales = syncLocales;
1786
+ exports.translateMissing = translateMissing;
1569
1787
  //# sourceMappingURL=i18n-magic.cjs.development.js.map