FileUtil.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. text += "\r\n\t".concat(key.name, ":").concat(key.type).concat(arrStr).concat(index < outPutData.headInfo.keys.length - 1 ? ',' : '', " //").concat(key.des);
  138. });
  139. text += '\r\n}\r\n';
  140. text += "export let ".concat(outPutData.sheetname, ": { [key: string]: I").concat(outPutData.sheetname, " }=\r\n{");
  141. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  142. text = StringUtil_1.default.getTsFileString(jsobj, outPutData.sheetname, text, outPutData.headInfo);
  143. }
  144. text += '\r\n}\r\n';
  145. this.saveText(text, "".concat(outPutData.sheetname, ".ts"));
  146. };
  147. /**
  148. * 保存为单独的js格式文件
  149. * @param outPutData
  150. */
  151. FileUtil.saveJsAlone = function (outPutData) {
  152. var text = '/* eslint-disable */\r\n';
  153. var jsobj = outPutData.jsobj;
  154. text += "module.exports =\r\n{";
  155. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  156. text = StringUtil_1.default.getTsFileString(jsobj, outPutData.sheetname, text, outPutData.headInfo);
  157. }
  158. text += '\r\n}\r\n';
  159. this.saveText(text, "".concat(outPutData.sheetname, ".js"));
  160. };
  161. /**
  162. * 保存为ts格式文件
  163. * @param outPutData
  164. */
  165. FileUtil.saveTs = function (outPutData) {
  166. var text = '/* eslint-disable */\r\n';
  167. for (var i = 0; i < outPutData.length; i++) {
  168. var jsobj = outPutData[i].jsobj;
  169. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  170. text = StringUtil_1.default.getTsClassString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo);
  171. }
  172. }
  173. text += 'export class Config\r\n{';
  174. for (var i = 0; i < outPutData.length; i++) {
  175. var jsobj = outPutData[i].jsobj;
  176. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  177. text = StringUtil_1.default.getTsFileString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo);
  178. }
  179. }
  180. text += '\r\n}\r\n';
  181. this.saveText(text, 'Config.ts');
  182. };
  183. FileUtil.saveTsJson = function (outPutData, isMin) {
  184. if (isMin === void 0) { isMin = false; }
  185. var text = '/* eslint-disable */\r\n';
  186. for (var i = 0; i < outPutData.length; i++) {
  187. var jsobj = outPutData[i].jsobj;
  188. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  189. text = StringUtil_1.default.getTsClassString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo);
  190. }
  191. }
  192. text += 'export class Config\r\n{';
  193. for (var i = 0; i < outPutData.length; i++) {
  194. var jsobj = outPutData[i].jsobj;
  195. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  196. text = StringUtil_1.default.getTsFileHead(outPutData[i].sheetname, text);
  197. StringUtil_1.default.getTsFileString3(jsobj, outPutData[i].headInfo);
  198. var out = '';
  199. if (isMin) {
  200. out = JSON.stringify(jsobj);
  201. }
  202. else {
  203. out = JSON.stringify(jsobj, null, '\t');
  204. }
  205. // out = StringUtil.replaceAll(out, '\\"\\[', '[');
  206. // out = StringUtil.replaceAll(out, '\\]\\"', ']');
  207. out = out.replace(/\\\\/g, '\\');
  208. this.saveText(out, "".concat(outPutData[i].sheetname, ".json"), path.join(Config_1.Config.outPutDir, Config_1.Config.jsonOutputDir));
  209. }
  210. }
  211. text += '\r\n}\r\n';
  212. this.saveText(text, 'Config.ts', path.join(Config_1.Config.outPutDir, Config_1.Config.tsOutputDir));
  213. };
  214. /**
  215. * 保存为js格式文件
  216. * @param outPutData
  217. */
  218. FileUtil.saveJs = function (outPutData) {
  219. var text = '/* eslint-disable */\r\nmodule.exports = {\r\n';
  220. for (var i = 0; i < outPutData.length; i++) {
  221. var jsobj = outPutData[i].jsobj;
  222. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  223. text = StringUtil_1.default.getJsFileString(jsobj, outPutData[i].sheetname, text, outPutData[i].headInfo);
  224. }
  225. }
  226. text += '}\r\n';
  227. this.saveText(text, 'Config.js');
  228. };
  229. FileUtil.saveJsJson = function (outPutData) {
  230. for (var i = 0; i < outPutData.length; i++) {
  231. var jsobj = outPutData[i].jsobj;
  232. if (jsobj.length > 0 && JSON.stringify(jsobj[0]) != '{}') {
  233. this.saveText(JSON.stringify(jsobj, null, '\t'), "".concat(outPutData[i].sheetname, ".json"));
  234. }
  235. }
  236. };
  237. /**
  238. * 保存为csv格式文件
  239. * @param outPutData
  240. */
  241. FileUtil.saveCsv = function (outPutData) {
  242. for (var i = 0; i < outPutData.length; i++) {
  243. var csvobj = outPutData[i].csvobj;
  244. if (csvobj.length > 0 && JSON.stringify(csvobj[0]) != '{}') {
  245. this.saveText(StringUtil_1.default.getCsvFileString(csvobj), "".concat(outPutData[i].sheetname, ".csv"), path.join(Config_1.Config.outPutDir, 'csv'));
  246. }
  247. }
  248. };
  249. /**
  250. * 保存为hsv格式文件
  251. * @param outPutData
  252. */
  253. FileUtil.saveHsv = function (outPutData) {
  254. for (var i = 0; i < outPutData.length; i++) {
  255. var csvobj = outPutData[i].csvobj;
  256. if (csvobj.length > 0 && JSON.stringify(csvobj[0]) != '{}') {
  257. this.saveText(StringUtil_1.default.getHsvFileString(csvobj), "".concat(outPutData[i].sheetname, ".hsv"), path.join(Config_1.Config.outPutDir, 'hsv'));
  258. }
  259. }
  260. };
  261. /**
  262. * 将进度信息打印到同一行
  263. * @param text
  264. */
  265. FileUtil.printSameLine = function (text) {
  266. readline.clearLine(process.stdout, 0); //删除光标所在行
  267. readline.cursorTo(process.stdout, 0); //移动光标到行首
  268. process.stdout.write(text, 'utf-8');
  269. };
  270. FileUtil.printlog = function (val) {
  271. var index = Math.floor(val / Config_1.Config.part);
  272. // console.log(val, index)
  273. if (index > Config_1.Config.partIndex) {
  274. Config_1.Config.partIndex = index;
  275. console.log(Config_1.Config.part * index * 100 + '%');
  276. }
  277. };
  278. return FileUtil;
  279. }());
  280. exports.default = FileUtil;