eljson
Version:
## 介绍
256 lines (255 loc) • 8.68 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];
const event = getEventType(element[format["report_event"]]);
const page_id = element[format["page_id"]];
const element_id = element[format["element_id"]];
let item = {};
if (element.hasOwnProperty(format["type"])) {
item["type"] = element[format["type"]];
}
if (element.hasOwnProperty(format["report_event"])) {
item["event"] = event;
}
if (element.hasOwnProperty(format["type"])) {
item["type"] = element[format["type"]];
}
if (element.hasOwnProperty(format["element_id"])) {
item["element_id"] = element[format["element_id"]];
}
if (element.hasOwnProperty(format["element_name"])) {
item["element_name"] = element[format["element_name"]];
}
if (element.hasOwnProperty(format["page_id"])) {
item["page_id"] = page_id;
}
if (element.hasOwnProperty(format["page_name"])) {
item["page_name"] = element[format["page_name"]];
}
if (element.hasOwnProperty(format["moudle_id"])) {
item["moudle_id"] = element[format["moudle_id"]];
}
if (element.hasOwnProperty(format["moudle_name"])) {
item["moudle_name"] = element[format["moudle_name"]];
}
if (element.hasOwnProperty(format["position_id"])) {
item["position_id"] = element[format["position_id"]];
}
if (element.hasOwnProperty(format["pop_id"])) {
item["pop_id"] = element[format["pop_id"]];
}
if (element.hasOwnProperty(format["pop_name"])) {
item["pop_name"] = element[format["pop_name"]];
}
if (element.hasOwnProperty(format["api_id"])) {
item["api_id"] = element[format["api_id"]];
}
if (element.hasOwnProperty(format["api_name"])) {
item["api_name"] = element[format["api_name"]];
}
if (event) {
if (!sheetJson[event])
sheetJson[event] = {};
if (event == "WebViewScreen") {
if (page_id) {
if (!sheetJson[event][page_id]) {
sheetJson[event][page_id] = {};
}
sheetJson[event][page_id] = item;
}
}
else {
if (element_id) {
if (!sheetJson[event][element_id]) {
sheetJson[event][element_id] = {};
}
sheetJson[event][element_id] = item;
}
}
}
}
}
return sheetJson;
};
const getEventType = (type) => {
if (type == "浏览") {
return "WebViewScreen";
}
if (type == "曝光") {
return "WebExposure";
}
if (type == "点击") {
return "WebClick";
}
if (type == "弹窗点击") {
return "PopClick";
}
if (type == "弹窗浏览") {
return "PopView";
}
if (type == "输入") {
return "WebInput";
}
if (type == "活动点击") {
return "ActivityWebClick";
}
if (type == "活动浏览") {
return "ActivityWebView";
}
if (type == "活动输入") {
return "ActivityWebInput";
}
return "";
};
const writeJson = async (langJson) => {
if (Object.keys(langJson).length == 0)
return;
await (0, fs_extra_1.ensureDir)(utils_1.OUTPUT_DIR);
const fileName = `sensor.json`;
const templatePath = (0, path_1.join)(utils_1.OUTPUT_DIR, fileName);
(0, fs_extra_1.writeFileSync)(templatePath, JSON.stringify(langJson, 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);
});
}
templateFiles.forEach((filePath) => {
if (filePath) {
const sheetObj = (0, excel_1.excelToObj)(filePath, comds.name);
if (Object.keys(sheetObj).length > 0) {
const jsonData = objToJson(sheetObj, {
type: comds.type || "C",
report_event: comds.report_event || "D",
point_event: comds.point_event || "E",
element_id: comds.element_id || "F",
element_name: comds.element_name || "G",
page_id: comds.page_id || "H",
page_name: comds.page_name || "I",
moudle_id: comds.moudle_id || "J",
moudle_name: comds.moudle_name || "K",
position_id: comds.position_id || "L",
pop_id: comds.pop_id || "N",
pop_name: comds.pop_name || "O",
api_id: comds.api_id || "P",
api_name: comds.api_name || "Q",
});
writeJson(jsonData);
}
}
});
};
const PROMPTS = [
{
type: "input",
name: "file",
message: "请输入上报文件名称",
},
{
type: "input",
name: "name",
message: "请输入工作表名称",
},
{
type: "input",
name: "type",
message: "请输入类型列位置(默认值:C 列)",
},
{
type: "input",
name: "report_event",
message: "请输入上报事件列位置(默认值:D 列)",
},
{
type: "input",
name: "point_event",
message: "请输入埋点事件列位置(默认值:E 列)",
},
{
type: "input",
name: "element_id",
message: "请输入element_id列位置(默认值:F 列)",
},
{
type: "input",
name: "element_name",
message: "请输入element_name列位置(默认值:G 列)",
},
{
type: "input",
name: "page_id",
message: "请输入page_id列位置(默认值:H 列)",
},
{
type: "input",
name: "page_name",
message: "请输入page_name列位置(默认值:I 列)",
},
{
type: "input",
name: "moudle_id",
message: "请输入moudle_id列位置(默认值:J 列)",
},
{
type: "input",
name: "moudle_name",
message: "请输入moudle_name列位置(默认值:K 列)",
},
{
type: "input",
name: "position_id",
message: "请输入position_id列位置(默认值:L 列)",
},
{
type: "input",
name: "pop_id",
message: "请输入pop_id列位置(默认值:N 列)",
},
{
type: "input",
name: "pop_name",
message: "请输入pop_name列位置(默认值:O 列)",
},
{
type: "input",
name: "api_id",
message: "请输入api_id列位置(默认值:P 列)",
},
{
type: "input",
name: "api_name",
message: "请输入api_name列位置(默认值:Q 列)",
},
];
async function run() {
const comds = await (0, inquirer_1.prompt)(PROMPTS);
try {
getInput(comds);
}
catch (e) {
consola_1.default.error(e);
}
}
exports.default = run;