FileUtil.js 10 KB

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