UNPKG

@c-sheep/i18n-extract-cli

Version:

这是一款能够自动将代码里的中文转成i18n国际化标记的命令行工具。当然,你也可以用它实现将中文语言包自动翻译成其他语言。适用于vue2、vue3和react

110 lines (109 loc) 4.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteFolderRecursive = exports.getOutputPath = exports.formatCode = exports.getSourceFilePaths = exports.getI18nConfig = exports.getUserConfig = void 0; const glob_1 = __importDefault(require("glob")); const getAbsolutePath_1 = require("./getAbsolutePath"); const fs_extra_1 = __importDefault(require("fs-extra")); const log_1 = __importDefault(require("./log")); const assertType_1 = require("./assertType"); const prettier_1 = __importDefault(require("prettier")); const path_1 = __importDefault(require("path")); var config_1 = require("./config"); Object.defineProperty(exports, "getUserConfig", { enumerable: true, get: function () { return config_1.getUserConfig; } }); Object.defineProperty(exports, "getI18nConfig", { enumerable: true, get: function () { return config_1.getI18nConfig; } }); function getPrettierParser(ext) { switch (ext) { case "vue": return "vue"; case "ts": case "tsx": return "babel-ts"; default: return "babel"; } } function isValidInput(input) { const inputPath = (0, getAbsolutePath_1.getAbsolutePath)(process.cwd(), input); if (!fs_extra_1.default.existsSync(inputPath)) { log_1.default.error(`路径${inputPath}不存在,请重新设置input参数`); process.exit(1); } if (!fs_extra_1.default.statSync(inputPath).isDirectory()) { log_1.default.error(`路径${inputPath}不是一个目录,请重新设置input参数`); process.exit(1); } return true; } function getOnlyAddIncludes(exclude, includes) { return includes.reduce((data, item) => { const result = glob_1.default.sync(item, { ignore: exclude }); return [...data, ...result]; }, []); } function getSourceFilePaths(input, exclude, includes) { if (isValidInput(input)) { if (!includes) { return glob_1.default.sync(`${input}/**/*.{cjs,mjs,js,ts,tsx,jsx,vue}`, { ignore: exclude }); } else { return getOnlyAddIncludes(exclude, includes); } } else { return []; } } exports.getSourceFilePaths = getSourceFilePaths; function formatCode(code, ext, prettierConfig) { let stylizedCode = code; if ((0, assertType_1.isObject)(prettierConfig)) { stylizedCode = prettier_1.default.format(code, { ...prettierConfig, parser: getPrettierParser(ext) }); log_1.default.verbose(`格式化代码完成`); } return stylizedCode; } exports.formatCode = formatCode; function getOutputPath(input, output, sourceFilePath) { let outputPath; if (output) { const filePath = sourceFilePath.replace(input + "/", ""); outputPath = (0, getAbsolutePath_1.getAbsolutePath)(process.cwd(), output, filePath); fs_extra_1.default.ensureFileSync(outputPath); } else { outputPath = (0, getAbsolutePath_1.getAbsolutePath)(process.cwd(), sourceFilePath); } return outputPath; } exports.getOutputPath = getOutputPath; function deleteFolderRecursive(folderPath) { //判断文件夹是否存在 if (fs_extra_1.default.existsSync(folderPath)) { //读取文件夹下的文件目录,以数组形式输出 fs_extra_1.default.readdirSync(folderPath).forEach((file) => { //拼接路径 const curPath = path_1.default.join(folderPath, file); //判断是不是文件夹,如果是,继续递归 if (fs_extra_1.default.lstatSync(curPath).isDirectory()) { deleteFolderRecursive(curPath); } else { //删除文件或文件夹 fs_extra_1.default.unlinkSync(curPath); } }); //仅可用于删除空目录 fs_extra_1.default.rmdirSync(folderPath); } } exports.deleteFolderRecursive = deleteFolderRecursive;