@scoutello/i18n-magic
Version:
Intelligent CLI toolkit that automates internationalization workflows with AI-powered translations for JavaScript/TypeScript projects
95 lines • 4.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const dotenv_1 = __importDefault(require("dotenv"));
const openai_1 = __importDefault(require("openai"));
const check_missing_1 = require("./commands/check-missing");
const clean_1 = require("./commands/clean");
const replace_1 = require("./commands/replace");
const scan_1 = require("./commands/scan");
const sync_locales_1 = require("./commands/sync-locales");
const utils_1 = require("./lib/utils");
const program = new commander_1.Command();
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");
const commands = [
{
name: "scan",
description: "Scan for missing translations, get prompted for each, translate it to the other locales and save it to the JSON file.",
action: scan_1.translateMissing,
},
{
name: "replace",
description: "Replace a translation based on the key, and translate it to the other locales and save it to the JSON file.",
action: replace_1.replaceTranslation,
},
{
name: "check-missing",
description: "Check if there are any missing translations. Useful for a CI/CD pipeline or husky hook.",
action: check_missing_1.checkMissing,
},
{
name: "sync",
description: "Sync the translations from the default locale to the other locales. Useful for a CI/CD pipeline or husky hook.",
action: sync_locales_1.syncLocales,
},
{
name: "clean",
description: "Remove unused translations from all locales. Useful for a CI/CD pipeline or husky hook.",
action: clean_1.removeUnusedKeys,
},
];
for (const command of commands) {
const cmd = program.command(command.name).description(command.description);
// Add key option to replace command
if (command.name === "replace") {
cmd
.option("-k, --key <key>", "translation key to replace")
.allowExcessArguments(true)
.argument("[key]", "translation key to replace");
}
cmd.action(async (arg, options) => {
const res = dotenv_1.default.config({
path: program.opts().env || ".env",
});
const config = await (0, utils_1.loadConfig)({
configPath: program.opts().config,
});
const isGemini = config.model?.includes("gemini");
// Get API key from environment or config
const openaiKey = res.parsed.OPENAI_API_KEY || config.OPENAI_API_KEY;
const geminiKey = res.parsed.GEMINI_API_KEY || config.GEMINI_API_KEY;
// Select appropriate key based on model type
const key = isGemini ? geminiKey : openaiKey;
if (!key) {
const keyType = isGemini ? "GEMINI_API_KEY" : "OPENAI_API_KEY";
console.error(`Please provide a${isGemini ? " Gemini" : "n OpenAI"} API key in your .env file or config, called ${keyType}.`);
process.exit(1);
}
const openai = new openai_1.default({
apiKey: key,
...(isGemini && {
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
}),
});
// For replace command, check for key in argument or option
if (command.name === "replace") {
// If key is provided as positional argument, use that first
const keyToUse = typeof arg === "string" ? arg : options.key;
command.action({ ...config, openai }, keyToUse);
}
else {
command.action({ ...config, openai });
}
});
}
program.parse(process.argv);
//# sourceMappingURL=cli.js.map