eljson
Version:
## 介绍
123 lines (122 loc) • 4.13 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, type) => {
// 根据表头取对应的数据
let sheetJson = {};
for (const key in sheetObj) {
if (Object.prototype.hasOwnProperty.call(sheetObj, key)) {
const element = sheetObj[key];
let item = {};
// 上报事件
if (element.hasOwnProperty(format['event'])) {
if (element.hasOwnProperty(format['control_num'])) {
item['cn'] = element[format['control_num']];
}
if (element.hasOwnProperty(format['screen_num'])) {
item['sn'] = element[format['screen_num']];
}
const page_id = element[format['screen_num']];
const element_id = element[format['control_num']];
const eType = getEventType(element[format['event']]);
if (eType) {
if (!sheetJson[eType])
sheetJson[eType] = {};
const id = element_id ? `${element_id}_${page_id}` : page_id;
if (!sheetJson[eType][id])
sheetJson[eType][id] = {};
sheetJson[eType][id] = item;
}
;
}
}
}
return sheetJson;
};
const getEventType = (type) => {
if (type == '页面操作') {
return 'view';
}
if (type == '曝光') {
return 'exposure';
}
if (type == '点击') {
return 'click';
}
if (type == '结果') {
return 'result';
}
if (type == '输入框') {
return 'input';
}
return '';
};
const writeJson = async (langJson) => {
if (Object.keys(langJson).length == 0)
return;
await (0, fs_extra_1.ensureDir)(utils_1.OUTPUT_DIR);
const fileName = `v4.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, '/');
const templateFiles = (0, fast_glob_1.sync)((0, path_1.join)(templatePath, '**', '*').replace(/\\/g, '/'));
templateFiles.forEach((filePath) => {
if (filePath) {
const sheetObj = (0, excel_1.excelToObj)(filePath, comds.name);
if (Object.keys(sheetObj).length > 0) {
const jsonData = objToJson(sheetObj, {
event: comds.event || 'E',
control_num: comds.control_num || 'J',
screen_num: comds.screen_num || 'K'
}, comds.type);
writeJson(jsonData);
}
}
});
};
const PROMPTS = [
{
type: 'input',
name: 'name',
message: '请输入工作表名称',
},
{
type: 'input',
name: 'event',
message: '请输入上报事件列位置(默认值:E 列)',
},
{
type: 'input',
name: 'control_num',
message: '请输入坑位编号列位置(默认值:J 列)',
},
{
type: 'input',
name: 'screen_num',
message: '请输入页面编号列位置(默认值:K 列)',
},
];
async function run() {
const comds = await (0, inquirer_1.prompt)(PROMPTS);
try {
getInput(comds);
}
catch (e) {
consola_1.default.error(e);
}
}
exports.default = run;