eljson
Version:
## 介绍
146 lines (145 loc) • 4.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const consola_1 = __importDefault(require("consola"));
const inquirer_1 = require("inquirer");
const fs_extra_1 = require("fs-extra");
const utils_1 = require("./utils");
const path_1 = require("path");
const fast_glob_1 = require("fast-glob");
const excel_1 = require("./excel");
const objToJson = (sheetObj, format) => {
// 根据表头取对应的数据
let sheetJson = [];
for (const key in sheetObj) {
if (Object.prototype.hasOwnProperty.call(sheetObj, key)) {
const element = sheetObj[key];
let item = {};
if (element.hasOwnProperty(format["zh"])) {
item["zh"] = element[format["zh"]];
}
if (element.hasOwnProperty(format["en"])) {
item["en"] = element[format["en"]];
}
if (element.hasOwnProperty(format["th"])) {
item["th"] = element[format["th"]];
}
if (Object.keys(item).length > 0) {
sheetJson.push(item);
}
}
}
return sheetJson;
};
const createJson = (jsonData) => {
const zhJson = {};
const enJson = {};
const thJson = {};
for (let i = 0; i < jsonData.length; i++) {
const item = jsonData[i];
const key = item["zh"];
if (key) {
zhJson[key] = key;
enJson[key] = item["en"] || "";
thJson[key] = item["th"] || "";
}
}
return {
zh: zhJson,
en: enJson,
th: thJson,
};
};
const writeJson = async (langJson) => {
if (Object.keys(langJson).length == 0)
return;
await (0, fs_extra_1.ensureDir)(utils_1.OUTPUT_DIR);
for (const key in langJson) {
if (Object.prototype.hasOwnProperty.call(langJson, key)) {
const element = langJson[key];
const fileName = `${key}.json`;
const templatePath = (0, path_1.join)(utils_1.OUTPUT_DIR, fileName);
(0, fs_extra_1.writeFileSync)(templatePath, JSON.stringify(element, null, 2));
consola_1.default.success(`成功创建 ${fileName} 文件`);
}
}
consola_1.default.success("编译成功 😁😘");
};
const getInput = (comds) => {
const templatePath = (0, path_1.join)(utils_1.INPUT_DIR).replace(/\\/g, "/");
let templateFiles = (0, fast_glob_1.sync)((0, path_1.join)(templatePath, "**", "*").replace(/\\/g, "/"));
if (comds.file) {
templateFiles = templateFiles.filter((path) => {
const fileName = path.split("/").pop();
return fileName.includes(comds.file);
});
}
let langJson = {
zh: {},
th: {},
en: {},
};
templateFiles.forEach((filePath) => {
if (filePath) {
const sheetObj = (0, excel_1.excelToObj)(filePath, comds.name);
if (Object.keys(sheetObj).length > 0) {
const jsonData = objToJson(sheetObj, {
zh: comds.zh || "B",
th: comds.th || "C",
en: comds.en || "D",
});
const curLangJson = createJson(jsonData);
langJson = {
zh: Object.assign(Object.assign({}, langJson.zh), curLangJson.zh),
en: Object.assign(Object.assign({}, langJson.en), curLangJson.en),
th: Object.assign(Object.assign({}, langJson.th), curLangJson.th),
};
}
}
});
if (Object.keys(langJson.zh).length > 0 ||
Object.keys(langJson.th).length > 0 ||
Object.keys(langJson.en).length > 0) {
writeJson(langJson);
}
};
const PROMPTS = [
{
type: "input",
name: "file",
message: "请输入翻译文件名称",
},
{
type: "input",
name: "name",
message: "请输入工作表名称",
},
{
type: "input",
name: "zh",
message: "请输入中文翻译列位置(默认值:B 列)",
},
{
type: "input",
name: "th",
message: "请输入泰文翻译列位置(默认值:C 列)",
},
{
type: "input",
name: "en",
message: "请输入英文翻译列位置(默认值:D 列)",
},
];
async function run() {
const comds = await (0, inquirer_1.prompt)(PROMPTS);
try {
getInput(comds);
}
catch (e) {
consola_1.default.error(e);
}
}
exports.default = run;