FileUtil.js 10 KB

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