var fs = require('fs');
var iconv = require('iconv-lite');
var csvParse = require('csv-parse/lib/sync');
// var jsonh = require('./jsonh');
// const zlib = require('zlib');
let clientCfg = null;
function AttributeCount(obj) {
var count = 0;
for(var i in obj) {
if(obj.hasOwnProperty(i)) { // 建议加上判断,如果没有扩展对象属性可以不加
count++;
}
}
return count;
}
function Uint8ArrayToString(fileData){
var dataString = "";
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i]);
}
return dataString;
}
function stringToUint8Array(str){
var arr = [];
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
var tmpUint8Array = new Uint8Array(arr);
return tmpUint8Array;
}
function setOneRow(obj, field, data)
{
let children = field.split(".");
if (children.length < 2)
{
obj[field] = parseData(data);
return;
}
var curNode = obj;
for (var i = 0; i < children.length - 1; i++)
{
let isArray = !isNaN(children[i + 1]);
if (curNode[children[i]] == undefined)
{
if (isArray)
{
curNode[children[i]] = [];
}
else
{
curNode[children[i]] = {};
}
}
curNode = curNode[children[i]];
}
curNode[children[i]] = parseData(data);
//console.log("setobj");
}
//解析单元格
function parseData(data)
{
if (data == "")//空字符串视为字符串类型
{
return data;
}
else if (isNaN(data))
{
if (data.length >= 2 && data[0] == "\"" && data[data.length - 1] == "\"")
{
return data.substr(1, data.length - 2);
}
return data;
}
else if (data.indexOf(".") > 0)
{
return parseFloat(data);
}
else
{
return parseInt(data);
}
}
function checkJsonString(obj)
{
if (obj.hasOwnProperty(""))
{
delete obj[""];
}
let replaceRegex = /\\n/g;
//检查JSON对象的每个字段是否是JSON字串
for (var i in obj)
{
if (obj[i].constructor == String)
{
obj[i] = obj[i].replace(replaceRegex, '\n');
}
try{
//console.log("[checkJsonString] key:" + i + " value:" + obj[i]);
let parseObj = JSON.parse(obj[i]);
switch (parseObj.constructor)
{
case Number:
obj[i] = parseObj;
break;
case Object:
obj[i] = parseObj;
break;
case Array:
obj[i] = parseObj;
break;
}
}
catch (ex)
{
//console.log("[checkJsonString] key:" + i + " value:" + obj[i]);
}
}
}
function convertOneFile(srcPath, desPath, tableName)
{
let data = fs.readFileSync(srcPath);
let content = iconv.decode(data, "gbk");
let regex = /\n,+(\n|\r\n)/g;
let emptyLineIndex = content.search(regex);
if (emptyLineIndex > 0)
{
content = content.substr(0, emptyLineIndex);
}
let records = csvParse(content, {relax_column_count: true, /*skip_empty_lines:true, skip_lines_with_empty_values:true,*/ columns: function(header){
//console.log(header);
let index = header.indexOf("");
if (index > 0)
{
header.splice(index);
}
return header;
}});
//console.log("records", records);
let jsonObject = new Array();
for (let i = 0; i < records.length; i++)
{
checkJsonString(records[i]);
jsonObject.push(records[i]);
}
let jsonStr = jsonMin(jsonObject, desPath);
if (checkClientUse(tableName))
{
clientCfg[tableName] = jsonObject;
}
fs.writeFileSync(desPath, jsonStr, function(err){
if (err != null)
{
console.error("error:", err);
//console.error("parse error:" + srcPath);
}
});
}
// function jsonCompress(jsonStr){
// //let plain = stringToUint8Array(jsonStr);
// // plain = Array.<number> or Uint8Array
// var gzip = new zlib.gzipSync(jsonStr);
// //var compressed = gzip.compress();
// return Uint8ArrayToString(gzip);
// }
function jsonMin(jsonObject, desPath)
{
let jsonStr;
//jsonStr = JSON.stringify(jsonObject);
let original = jsonObject;
let soriginal = JSON.stringify(original);
//let packed = jsonh.pack(original);
//let spacked = JSON.stringify(packed);
jsonStr = soriginal;
return jsonStr;
}
function excel2json(srcDir, files, curpath){
let desDir = curpath
console.log("excel2json;", desDir)
let count = 0;
clientCfg = {};
files.forEach(filename => {
let dotIndex = filename.indexOf(".csv");
let subIndex = filename.indexOf("-");//中文文件名用减号分隔,转换时只使用减号前的文件名DropConfig-掉落 => DropConfig
let splitIndex = dotIndex;
if (subIndex > 0)
{
splitIndex = subIndex;
}
if (splitIndex > 0)
{
let tableName = filename.substr(0, splitIndex);
let desPath = desDir + tableName + ".json";
//console.log("desPath:" + desPath);
convertOneFile(srcDir + filename, desPath, tableName);
count++;
}
});
console.log("covnet complete, count:" + count);
let clientTableCount = AttributeCount(clientCfg);
if (clientTableCount > 0)
{
let clentCfgStr = JSON.stringify(clientCfg);
if (clentCfgStr.length > 0)
{
fs.writeFileSync(desDir + "ClientConfig.json", clentCfgStr, function(err){
if (err != null)
{
console.error("error:", err);
}
});
}
}
console.log("clientTableCount:" + clientTableCount);
clientCfg = null;
}
function testFiles()
{
let srcDir = "tables\\csv\\";
let files = ["HelpConfig-帮助.csv"];
excel2json(srcDir, files);
}
//按字母排序
let clientList = ['BackDropConfig',
'ItemConfig',
'FirstRechargeConfig',
'PkgLevelConfig',
'CharacterAvatarConfig'
];
function checkClientUse(name)
{
//return true;
if (clientList)
{
return clientList.includes(name);
}
return false;
}
function main( ){
fs.readFile('config.json',function(err,data){//读取同目录下的book.json文件
if(err){throw err;}
var jsonObj = JSON.parse(data);//获取json文件对象
// console.log("jsonObj:", jsonObj)
let srcDir = jsonObj.srcDir
// console.log("srcDir:", srcDir)
let files = fs.readdirSync(srcDir);
console.log("files:", files)
excel2json(srcDir, files, jsonObj.desDir);
});
}
main()