"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var path = require("path"); var fs = require("fs"); var StringUtil_1 = require("./StringUtil"); var Config_1 = require("./Config"); var readline = require("readline"); var FileUtil = /** @class */ (function () { function FileUtil() { } /** * 递归删除文件夹及文件 * @param dirPath 需要删除的文件夹路径 */ FileUtil.clearDir = function (dirPath) { var _this = this; if (fs.existsSync(dirPath)) { fs.readdirSync(dirPath).forEach(function (file) { var curPath = path.join(dirPath, file); if (fs.statSync(curPath).isDirectory()) { _this.clearDir(curPath); } else { fs.unlinkSync(curPath); } }); } }; /** * 检测一个文件夹路径是否存在,根据条件自动创建 * @param dirPath */ FileUtil.checkDir = function (dirPath, isAutoMake) { if (isAutoMake === void 0) { isAutoMake = false; } if (!fs.existsSync(dirPath)) { if (isAutoMake) { fs.mkdirSync(dirPath); return true; } return false; } else { return true; } }; FileUtil.mkdirsSync = function (dirname) { if (fs.existsSync(dirname)) { return true; } else if (this.mkdirsSync(path.dirname(dirname))) { fs.mkdirSync(dirname); return true; } return false; }; /** * 检测一个文件夹路径是否存在,如果没有就自动创建 * @param dirPath */ FileUtil.checkAndMakeDir = function (dirPath) { return this.mkdirsSync(dirPath); }; /** * 获取一个目录下当前目录的全部文件名 * @param dirPath * @param filter 扩展名筛选 * @param isFilterTempFile 临时文件筛选 */ FileUtil.getFileList = function (dirPath, filter, isFilterTempFile) { if (isFilterTempFile === void 0) { isFilterTempFile = true; } var ret = []; var files = fs.readdirSync(dirPath); for (var i = 0; i < files.length; i++) { var file = files[i]; if (filter) { for (var j = 0; j < filter.length; j++) { if (isFilterTempFile && file.indexOf('~$') != -1) { break; } if (file.lastIndexOf(filter[j]) != -1) { ret.push(file); break; } } } else { ret.push(file); } } return ret; }; /** * 保存文本文件(流方式) * @param text * @param fileName * @param outPutDir */ FileUtil.saveTextByStream = function (text, fileName, outPutDir) { if (outPutDir === void 0) { outPutDir = Config_1.Config.outPutDir; } var savePath = path.join(outPutDir, fileName); var writerStream = fs.createWriteStream(savePath); writerStream.write(text, 'utf-8'); writerStream.on('finish', function () { console.log("".concat(savePath, " is finished")); writerStream.close(); }); }; /** * 保存文本文件 * @param text * @param fileName * @param outPutDir */ FileUtil.saveText = function (text, fileName, outPutDir) { if (outPutDir === void 0) { outPutDir = Config_1.Config.outPutDir; } var savePath = path.join(outPutDir, fileName); if (fs.existsSync(savePath)) { fs.unlinkSync(savePath); } fs.writeFileSync(savePath, text); console.log("".concat(savePath, " is finished")); }; // ---------------------------------------------- /** * 保存为单独的ts格式文件 * @param outPutData */ FileUtil.saveTsAlone = function (outPutData) { var text = '/* eslint-disable */\r\n'; var jsobj = outPutData.jsobj; text += "export interface I".concat(outPutData.sheetname, " {"); outPutData.headInfo.keys.forEach(function (key, index) { var arrStr = ''; for (var i = 1; i < key.arrayNum; i++) { arrStr += '[]'; } if (key.outputclient) text += "\r\n\t".concat(key.name, ":").concat(key.type).concat(arrStr).concat(index < outPutData.headInfo.keys.length - 1 ? ',' : '', " //").concat(key.des.replace(/\s/g, '')); }); text += '\r\n}\r\n'; text += "export let ".concat(outPutData.sheetname, ": { [key: string]: I").concat(outPutData.sheetname, " }=\r\n{"); if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') { text = StringUtil_1.default.getTsFileString(jsobj, outPutData.sheetname, text, outPutData.headInfo); } text += '\r\n}\r\n'; this.saveText(text, "".concat(outPutData.sheetname, ".ts")); }; /** * 保存为单独的js格式文件 * @param outPutData */ FileUtil.saveJsAlone = function (outPutData) { var text = '/* eslint-disable */\r\n'; var jsobj = outPutData.jsobj; text += "module.exports =\r\n{"; if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') { text = StringUtil_1.default.getTsFileString(jsobj, outPutData.sheetname, text, outPutData.headInfo); } text += '\r\n}\r\n'; this.saveText(text, "".concat(outPutData.sheetname, ".js")); }; /** * 保存为ts格式文件 * @param outPutData */ FileUtil.saveTs = function (outPutData) { var text = '/* eslint-disable */\r\n'; for (var i = 0; i < outPutData.length; i++) { var jsobj = outPutData[i].jsobj; if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') { text = StringUtil_1.default.getTsClassString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo); } } text += 'export class Config\r\n{'; for (var i = 0; i < outPutData.length; i++) { var jsobj = outPutData[i].jsobj; if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') { text = StringUtil_1.default.getTsFileString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo); } } text += '\r\n}\r\n'; this.saveText(text, 'Config.ts'); }; FileUtil.saveTsJson = function (outPutData, isMin) { if (isMin === void 0) { isMin = false; } var text = '/* eslint-disable */\r\n'; for (var i = 0; i < outPutData.length; i++) { var jsobj = outPutData[i].jsobj; if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') { text = StringUtil_1.default.getTsClassString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo); } } text += 'export class Config\r\n{'; for (var i = 0; i < outPutData.length; i++) { var jsobj = outPutData[i].jsobj; if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') { text = StringUtil_1.default.getTsFileHead(outPutData[i].sheetname, text); StringUtil_1.default.getTsFileString3(jsobj, outPutData[i].headInfo); var out = ''; if (isMin) { out = JSON.stringify(jsobj); } else { out = JSON.stringify(jsobj, null, '\t'); } // out = StringUtil.replaceAll(out, '\\"\\[', '['); // out = StringUtil.replaceAll(out, '\\]\\"', ']'); out = out.replace(/\\\\/g, '\\'); this.saveText(out, "".concat(outPutData[i].sheetname, ".json"), path.join(Config_1.Config.outPutDir, Config_1.Config.jsonOutputDir)); } } text += '\r\n}\r\n'; this.saveText(text, 'Config.ts', path.join(Config_1.Config.outPutDir, Config_1.Config.tsOutputDir)); }; /** * 保存为js格式文件 * @param outPutData */ FileUtil.saveJs = function (outPutData) { var text = '/* eslint-disable */\r\nmodule.exports = {\r\n'; for (var i = 0; i < outPutData.length; i++) { var jsobj = outPutData[i].jsobj; if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') { text = StringUtil_1.default.getJsFileString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo); } } text += '}\r\n'; this.saveText(text, 'Config.js'); }; FileUtil.saveJsJson = function (outPutData) { for (var i = 0; i < outPutData.length; i++) { var jsobj = outPutData[i].jsobj; if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') { this.saveText(JSON.stringify(jsobj, null, '\t'), "".concat(outPutData[i].sheetname, ".json")); } } }; /** * 保存为csv格式文件 * @param outPutData */ FileUtil.saveCsv = function (outPutData) { for (var i = 0; i < outPutData.length; i++) { var csvobj = outPutData[i].csvobj; if (csvobj.length > 0 && JSON.stringify(csvobj[0]) != '{}') { this.saveText(StringUtil_1.default.getCsvFileString(csvobj), "".concat(outPutData[i].sheetname, ".csv"), path.join(Config_1.Config.outPutDir, 'csv')); } } }; /** * 保存为hsv格式文件 * @param outPutData */ FileUtil.saveHsv = function (outPutData) { for (var i = 0; i < outPutData.length; i++) { var csvobj = outPutData[i].csvobj; if (csvobj.length > 0 && JSON.stringify(csvobj[0]) != '{}') { this.saveText(StringUtil_1.default.getHsvFileString(csvobj), "".concat(outPutData[i].sheetname, ".hsv"), path.join(Config_1.Config.outPutDir, 'hsv')); } } }; /** * 将进度信息打印到同一行 * @param text */ FileUtil.printSameLine = function (text) { readline.clearLine(process.stdout, 0); //删除光标所在行 readline.cursorTo(process.stdout, 0); //移动光标到行首 process.stdout.write(text, 'utf-8'); }; FileUtil.printlog = function (val) { var index = Math.floor(val / Config_1.Config.part); // console.log(val, index) if (index > Config_1.Config.partIndex) { Config_1.Config.partIndex = index; console.log(Config_1.Config.part * index * 100 + '%'); } }; return FileUtil; }()); exports.default = FileUtil;