FileUtil.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var path = require("path");
  4. var fs = require("fs");
  5. var StringUtil_1 = require("./StringUtil");
  6. var Config_1 = require("./Config");
  7. var readline = require("readline");
  8. var FileUtil = /** @class */ (function () {
  9. function FileUtil() {
  10. }
  11. /**
  12. * 递归删除文件夹及文件
  13. * @param dirPath 需要删除的文件夹路径
  14. */
  15. FileUtil.clearDir = function (dirPath) {
  16. var _this = this;
  17. if (fs.existsSync(dirPath)) {
  18. fs.readdirSync(dirPath).forEach(function (file) {
  19. var curPath = path.join(dirPath, file);
  20. if (fs.statSync(curPath).isDirectory()) {
  21. _this.clearDir(curPath);
  22. }
  23. else {
  24. fs.unlinkSync(curPath);
  25. }
  26. });
  27. }
  28. };
  29. /**
  30. * 检测一个文件夹路径是否存在,根据条件自动创建
  31. * @param dirPath
  32. */
  33. FileUtil.checkDir = function (dirPath, isAutoMake) {
  34. if (isAutoMake === void 0) { isAutoMake = false; }
  35. if (!fs.existsSync(dirPath)) {
  36. if (isAutoMake) {
  37. fs.mkdirSync(dirPath);
  38. return true;
  39. }
  40. return false;
  41. }
  42. else {
  43. return true;
  44. }
  45. };
  46. FileUtil.mkdirsSync = function (dirname) {
  47. if (fs.existsSync(dirname)) {
  48. return true;
  49. }
  50. else if (this.mkdirsSync(path.dirname(dirname))) {
  51. fs.mkdirSync(dirname);
  52. return true;
  53. }
  54. return false;
  55. };
  56. /**
  57. * 检测一个文件夹路径是否存在,如果没有就自动创建
  58. * @param dirPath
  59. */
  60. FileUtil.checkAndMakeDir = function (dirPath) {
  61. return this.mkdirsSync(dirPath);
  62. };
  63. /**
  64. * 获取一个目录下当前目录的全部文件名
  65. * @param dirPath
  66. * @param filter 扩展名筛选
  67. * @param isFilterTempFile 临时文件筛选
  68. */
  69. FileUtil.getFileList = function (dirPath, filter, isFilterTempFile) {
  70. if (isFilterTempFile === void 0) { isFilterTempFile = true; }
  71. var ret = [];
  72. var files = fs.readdirSync(dirPath);
  73. for (var i = 0; i < files.length; i++) {
  74. var file = files[i];
  75. if (filter) {
  76. for (var j = 0; j < filter.length; j++) {
  77. if (isFilterTempFile && file.indexOf('~$') != -1) {
  78. break;
  79. }
  80. if (file.lastIndexOf(filter[j]) != -1) {
  81. ret.push(file);
  82. break;
  83. }
  84. }
  85. }
  86. else {
  87. ret.push(file);
  88. }
  89. }
  90. return ret;
  91. };
  92. /**
  93. * 保存文本文件(流方式)
  94. * @param text
  95. * @param fileName
  96. * @param outPutDir
  97. */
  98. FileUtil.saveTextByStream = function (text, fileName, outPutDir) {
  99. if (outPutDir === void 0) { outPutDir = Config_1.Config.outPutDir; }
  100. var savePath = path.join(outPutDir, fileName);
  101. var writerStream = fs.createWriteStream(savePath);
  102. writerStream.write(text, 'utf-8');
  103. writerStream.on('finish', function () {
  104. console.log("".concat(savePath, " is finished"));
  105. writerStream.close();
  106. });
  107. };
  108. /**
  109. * 保存文本文件
  110. * @param text
  111. * @param fileName
  112. * @param outPutDir
  113. */
  114. FileUtil.saveText = function (text, fileName, outPutDir) {
  115. if (outPutDir === void 0) { outPutDir = Config_1.Config.outPutDir; }
  116. var savePath = path.join(outPutDir, fileName);
  117. if (fs.existsSync(savePath)) {
  118. fs.unlinkSync(savePath);
  119. }
  120. fs.writeFileSync(savePath, text);
  121. console.log("".concat(savePath, " is finished"));
  122. };
  123. // ----------------------------------------------
  124. /**
  125. * 保存为单独的ts格式文件
  126. * @param outPutData
  127. */
  128. FileUtil.saveTsAlone = function (outPutData) {
  129. var text = '/* eslint-disable */\r\n';
  130. var jsobj = outPutData.jsobj;
  131. text += "export interface I".concat(outPutData.sheetname, " {");
  132. outPutData.headInfo.keys.forEach(function (key, index) {
  133. var arrStr = '';
  134. for (var i = 1; i < key.arrayNum; i++) {
  135. arrStr += '[]';
  136. }
  137. if (key.outputclient)
  138. 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, ''));
  139. });
  140. text += '\r\n}\r\n';
  141. text += "export let ".concat(outPutData.sheetname, ": { [key: string]: I").concat(outPutData.sheetname, " }=\r\n{");
  142. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  143. text = StringUtil_1.default.getTsFileString(jsobj, outPutData.sheetname, text, outPutData.headInfo);
  144. }
  145. text += '\r\n}\r\n';
  146. this.saveText(text, "".concat(outPutData.sheetname, ".ts"));
  147. };
  148. /**
  149. * 保存为单独的js格式文件
  150. * @param outPutData
  151. */
  152. FileUtil.saveJsAlone = function (outPutData) {
  153. var text = '/* eslint-disable */\r\n';
  154. var jsobj = outPutData.jsobj;
  155. text += "module.exports =\r\n{";
  156. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  157. text = StringUtil_1.default.getTsFileString(jsobj, outPutData.sheetname, text, outPutData.headInfo);
  158. }
  159. text += '\r\n}\r\n';
  160. this.saveText(text, "".concat(outPutData.sheetname, ".js"));
  161. };
  162. /**
  163. * 保存为ts格式文件
  164. * @param outPutData
  165. */
  166. FileUtil.saveTs = function (outPutData) {
  167. var text = '/* eslint-disable */\r\n';
  168. for (var i = 0; i < outPutData.length; i++) {
  169. var jsobj = outPutData[i].jsobj;
  170. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  171. text = StringUtil_1.default.getTsClassString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo);
  172. }
  173. }
  174. text += 'export class Config\r\n{';
  175. for (var i = 0; i < outPutData.length; i++) {
  176. var jsobj = outPutData[i].jsobj;
  177. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  178. text = StringUtil_1.default.getTsFileString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo);
  179. }
  180. }
  181. text += '\r\n}\r\n';
  182. this.saveText(text, 'Config.ts');
  183. };
  184. FileUtil.saveTsJson = function (outPutData, isMin) {
  185. if (isMin === void 0) { isMin = false; }
  186. var text = '/* eslint-disable */\r\n';
  187. for (var i = 0; i < outPutData.length; i++) {
  188. var jsobj = outPutData[i].jsobj;
  189. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  190. text = StringUtil_1.default.getTsClassString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo);
  191. }
  192. }
  193. text += 'export class Config\r\n{';
  194. for (var i = 0; i < outPutData.length; i++) {
  195. var jsobj = outPutData[i].jsobj;
  196. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  197. text = StringUtil_1.default.getTsFileHead(outPutData[i].sheetname, text);
  198. StringUtil_1.default.getTsFileString3(jsobj, outPutData[i].headInfo);
  199. var out = '';
  200. if (isMin) {
  201. out = JSON.stringify(jsobj);
  202. }
  203. else {
  204. out = JSON.stringify(jsobj, null, '\t');
  205. }
  206. // out = StringUtil.replaceAll(out, '\\"\\[', '[');
  207. // out = StringUtil.replaceAll(out, '\\]\\"', ']');
  208. out = out.replace(/\\\\/g, '\\');
  209. this.saveText(out, "".concat(outPutData[i].sheetname, ".json"), path.join(Config_1.Config.outPutDir, Config_1.Config.jsonOutputDir));
  210. }
  211. }
  212. text += '\r\n}\r\n';
  213. this.saveText(text, 'Config.ts', path.join(Config_1.Config.outPutDir, Config_1.Config.tsOutputDir));
  214. };
  215. /**
  216. * 保存为js格式文件
  217. * @param outPutData
  218. */
  219. FileUtil.saveJs = function (outPutData) {
  220. var text = '/* eslint-disable */\r\nmodule.exports = {\r\n';
  221. for (var i = 0; i < outPutData.length; i++) {
  222. var jsobj = outPutData[i].jsobj;
  223. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  224. text = StringUtil_1.default.getJsFileString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo);
  225. }
  226. }
  227. text += '}\r\n';
  228. this.saveText(text, 'Config.js');
  229. };
  230. FileUtil.saveJsJson = function (outPutData) {
  231. for (var i = 0; i < outPutData.length; i++) {
  232. var jsobj = outPutData[i].jsobj;
  233. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  234. this.saveText(JSON.stringify(jsobj, null, '\t'), "".concat(outPutData[i].sheetname, ".json"));
  235. }
  236. }
  237. };
  238. /**
  239. * 保存为csv格式文件
  240. * @param outPutData
  241. */
  242. FileUtil.saveCsv = function (outPutData) {
  243. for (var i = 0; i < outPutData.length; i++) {
  244. var csvobj = outPutData[i].csvobj;
  245. if (csvobj.length > 0 && JSON.stringify(csvobj[0]) != '{}') {
  246. this.saveText(StringUtil_1.default.getCsvFileString(csvobj), "".concat(outPutData[i].sheetname, ".csv"), path.join(Config_1.Config.outPutDir, 'csv'));
  247. }
  248. }
  249. };
  250. /**
  251. * 保存为hsv格式文件
  252. * @param outPutData
  253. */
  254. FileUtil.saveHsv = function (outPutData) {
  255. for (var i = 0; i < outPutData.length; i++) {
  256. var csvobj = outPutData[i].csvobj;
  257. if (csvobj.length > 0 && JSON.stringify(csvobj[0]) != '{}') {
  258. this.saveText(StringUtil_1.default.getHsvFileString(csvobj), "".concat(outPutData[i].sheetname, ".hsv"), path.join(Config_1.Config.outPutDir, 'hsv'));
  259. }
  260. }
  261. };
  262. /**
  263. * 将进度信息打印到同一行
  264. * @param text
  265. */
  266. FileUtil.printSameLine = function (text) {
  267. readline.clearLine(process.stdout, 0); //删除光标所在行
  268. readline.cursorTo(process.stdout, 0); //移动光标到行首
  269. process.stdout.write(text, 'utf-8');
  270. };
  271. FileUtil.printlog = function (val) {
  272. var index = Math.floor(val / Config_1.Config.part);
  273. // console.log(val, index)
  274. if (index > Config_1.Config.partIndex) {
  275. Config_1.Config.partIndex = index;
  276. console.log(Config_1.Config.part * index * 100 + '%');
  277. }
  278. };
  279. return FileUtil;
  280. }());
  281. exports.default = FileUtil;