UNPKG

@erda-ui/cli

Version:

Command line interface for rapid Erda UI development

106 lines (105 loc) 5.96 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const inquirer_1 = __importDefault(require("inquirer")); const chalk_1 = __importDefault(require("chalk")); const ora_1 = __importDefault(require("ora")); const google_translate_1 = require("./util/google-translate"); const youdao_translate_1 = require("./util/youdao-translate"); const log_1 = require("./util/log"); const env_1 = require("./util/env"); const i18n_utils_1 = require("./util/i18n-utils"); const path_1 = __importDefault(require("path")); const configFilePath = path_1.default.resolve(process.cwd(), '.translaterc'); exports.default = ({ isSwitchNs = false, isExternal = false }) => __awaiter(void 0, void 0, void 0, function* () { try { (0, env_1.isCwdInRoot)({ alert: true }); const originalResources = (0, i18n_utils_1.prepareEnv)(isExternal, isSwitchNs); if (isSwitchNs) { if (isExternal) { (0, log_1.logError)('external module has only one namespace, no need to switch'); return; } yield (0, i18n_utils_1.batchSwitchNamespace)(originalResources); return; } const untranslatedWords = new Set(); const translatedWords = {}; yield (0, i18n_utils_1.extractAllI18nD)(isExternal, originalResources, translatedWords, untranslatedWords); if (untranslatedWords.size === 0 && Object.keys(translatedWords).length === 0) { (0, log_1.logInfo)('sort current locale files & remove unused translation'); yield (0, i18n_utils_1.writeLocaleFiles)(isExternal); (0, log_1.logInfo)('no content needs to be translated is found, program exits'); return; } if (Object.keys(translatedWords).length > 0) { yield inquirer_1.default.prompt({ name: 'confirm', type: 'confirm', message: `Please carefully check whether the existing translation of ${chalk_1.default.green('[temp-translated-words.json]')} is suitable, if you are not satisfied, please move the content into ${chalk_1.default.green('[temp-zh-words.json]')}, no problem or after manual modification press enter to continue`, }); } const tempWords = JSON.parse(fs_1.default.readFileSync(i18n_utils_1.tempFilePath, { encoding: 'utf-8' })); const _untranslatedWords = Object.keys(tempWords); if (_untranslatedWords.length > 0) { const isUsingYoudao = fs_1.default.existsSync(configFilePath); const spinner = (0, ora_1.default)(`${isUsingYoudao ? 'youdao' : 'google'} automatic translating...`).start(); const translateMethod = isUsingYoudao ? youdao_translate_1.doTranslate : google_translate_1.doTranslate; yield translateMethod(); spinner.stop(); (0, log_1.logSuccess)('automatic translation completed'); yield inquirer_1.default.prompt({ name: 'confirm', type: 'confirm', message: `Please double check whether the automatic translation of ${chalk_1.default.green('[temp-zh-words.json]')} is suitable, no problem or after manual modification then press enter to continue`, }); } const reviewedZhMap = JSON.parse(fs_1.default.readFileSync(i18n_utils_1.tempFilePath, { encoding: 'utf-8' })); let translatedMap = {}; if (Object.keys(translatedWords).length > 0) { translatedMap = JSON.parse(fs_1.default.readFileSync(i18n_utils_1.tempTranslatedWordPath, { encoding: 'utf-8' })); } let ns = ''; if (reviewedZhMap && Object.keys(reviewedZhMap).length && !isExternal) { const nsList = Object.values(originalResources).reduce((acc, resource) => { const [zhResource] = resource; return acc.concat(Object.keys(zhResource)); }, []); const { targetNs } = yield inquirer_1.default.prompt({ name: 'targetNs', type: 'list', message: 'please select the new namespace name', choices: nsList.map((_ns) => ({ value: _ns, name: _ns })), }); (0, log_1.logInfo)('Specify the namespace as', targetNs); ns = targetNs; } const spinner = (0, ora_1.default)('replacing source file...').start(); yield (0, i18n_utils_1.writeI18nTToSourceFile)(isExternal, ns, translatedMap, reviewedZhMap); spinner.stop(); (0, log_1.logSuccess)('replacing source file completed'); if (reviewedZhMap && Object.keys(reviewedZhMap).length > 0) { yield (0, i18n_utils_1.writeLocaleFiles)(isExternal); } } finally { if (!isSwitchNs) { fs_1.default.unlinkSync(i18n_utils_1.tempFilePath); fs_1.default.unlinkSync(i18n_utils_1.tempTranslatedWordPath); (0, log_1.logSuccess)('clearing of temporary files completed'); } } (0, log_1.logInfo)('i18n process is completed, see you👋'); });