@scoutello/i18n-magic
Version:
Intelligent CLI toolkit that automates internationalization workflows with AI-powered translations for JavaScript/TypeScript projects
123 lines • 5.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPrunedNamespace = void 0;
const fast_glob_1 = __importDefault(require("fast-glob"));
const i18next_scanner_1 = require("i18next-scanner");
const node_fs_1 = __importDefault(require("node:fs"));
const prompts_1 = __importDefault(require("prompts"));
const utils_1 = require("../lib/utils");
const createPrunedNamespace = async (config) => {
const { namespaces, loadPath, savePath, locales, defaultNamespace } = config;
// Step 1: Ask for source namespace
const sourceNamespaceResponse = await (0, prompts_1.default)({
type: "select",
name: "value",
message: "Select source namespace to create pruned version from:",
choices: namespaces.map((namespace) => ({
title: namespace,
value: namespace,
})),
onState: (state) => {
if (state.aborted) {
process.nextTick(() => {
process.exit(0);
});
}
},
});
const sourceNamespace = sourceNamespaceResponse.value;
// Step 2: Ask for new namespace name
const newNamespaceResponse = await (0, prompts_1.default)({
type: "text",
name: "value",
message: "Enter the name for the new namespace:",
validate: (value) => {
if (!value)
return "Namespace name cannot be empty";
if (namespaces.includes(value))
return "Namespace already exists";
return true;
},
onState: (state) => {
if (state.aborted) {
process.nextTick(() => {
process.exit(0);
});
}
},
});
const newNamespace = newNamespaceResponse.value;
// Step 3: Ask for glob patterns to find relevant keys
const allPatterns = config.globPatterns.map((pattern) => typeof pattern === "string" ? pattern : pattern.pattern);
const globPatternsResponse = await (0, prompts_1.default)({
type: "list",
name: "value",
message: "Enter glob patterns to find relevant keys (comma separated):",
initial: allPatterns.join(","),
separator: ",",
onState: (state) => {
if (state.aborted) {
process.nextTick(() => {
process.exit(0);
});
}
},
});
const selectedGlobPatterns = globPatternsResponse.value;
console.log(`Finding keys used in files matching: ${selectedGlobPatterns.join(", ")}`);
// Extract keys from files matching the glob patterns
const parser = new i18next_scanner_1.Parser({
nsSeparator: false,
keySeparator: false,
});
const files = await (0, fast_glob_1.default)([...selectedGlobPatterns, "!**/node_modules/**"]);
console.log(`Found ${files.length} files to scan`);
const extractedKeys = [];
for (const file of files) {
const content = node_fs_1.default.readFileSync(file, "utf-8");
parser.parseFuncFromString(content, { list: ["t"] }, (key) => {
extractedKeys.push(key);
});
}
const uniqueExtractedKeys = (0, utils_1.removeDuplicatesFromArray)(extractedKeys);
console.log(`Found ${uniqueExtractedKeys.length} unique translation keys`);
// Filter keys that belong to the source namespace
const relevantKeys = [];
for (const key of uniqueExtractedKeys) {
const pureKey = (0, utils_1.getPureKey)(key, sourceNamespace, sourceNamespace === defaultNamespace);
if (pureKey) {
relevantKeys.push(pureKey);
}
}
console.log(`Found ${relevantKeys.length} keys from namespace '${sourceNamespace}'`);
if (relevantKeys.length === 0) {
console.log("No relevant keys found. Exiting...");
return;
}
// Get translations from source namespace and create new namespace files
for (const locale of locales) {
try {
// Load source namespace translations
const sourceTranslations = await (0, utils_1.loadLocalesFile)(loadPath, locale, sourceNamespace);
// Create new namespace with only the keys used in the glob pattern files
const newNamespaceTranslations = {};
for (const key of relevantKeys) {
if (sourceTranslations[key]) {
newNamespaceTranslations[key] = sourceTranslations[key];
}
}
// Write the new namespace file
await (0, utils_1.writeLocalesFile)(savePath, locale, newNamespace, newNamespaceTranslations);
console.log(`Created pruned namespace '${newNamespace}' for locale '${locale}' with ${Object.keys(newNamespaceTranslations).length} keys`);
}
catch (error) {
console.error(`Error creating pruned namespace for locale '${locale}':`, error);
}
}
console.log(`✅ Successfully created pruned namespace '${newNamespace}'`);
};
exports.createPrunedNamespace = createPrunedNamespace;
//# sourceMappingURL=create-pruned-namespace.js.map