file.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. const fs = require('fs');
  2. const path = require('path');
  3. const {logger} = require("./logger");
  4. const {rustConfig} = require("../config");
  5. const http = require("./http");
  6. // 检查目录是否存在,不存在新建
  7. function checkPathSync(filePath) {
  8. // 将相对路径转换为绝对路径
  9. const directoryPath = path.resolve(filePath);
  10. // 同步检查目录是否存在
  11. if (!fs.existsSync(directoryPath)) {
  12. // 目录不存在,需要创建它
  13. logger.info('目录不存在,正在创建...');
  14. try {
  15. // 同步创建目录
  16. fs.mkdirSync(directoryPath, {recursive: true});
  17. logger.info('目录已成功创建');
  18. } catch (error) {
  19. // 创建目录失败
  20. logger.error('创建目录失败:', error);
  21. return false
  22. }
  23. } else {
  24. // 目录已存在
  25. logger.info('目录已存在');
  26. }
  27. return true
  28. }
  29. // 检查文件是否存在
  30. function checkFilePath(filePath) {
  31. // 同步检查文件是否存在
  32. const directoryPath = path.resolve(filePath);
  33. if (fs.existsSync(directoryPath)) {
  34. logger.info('文件存在:' + filePath);
  35. return true
  36. } else {
  37. logger.error('文件不存在:' + filePath);
  38. return false
  39. }
  40. }
  41. // 创建文件
  42. function writeFile(filePath, text, breakFun) {
  43. fs.writeFile(filePath, text, (err) => {
  44. if (err) {
  45. logger.error('写入文件时发生错误:', err);
  46. breakFun(err)
  47. } else {
  48. logger.log('文件已成功写入!');
  49. breakFun(null, true)
  50. }
  51. });
  52. }
  53. // 同步复制
  54. function copyFileSync(source, destination) {
  55. try {
  56. // 同步复制文件
  57. fs.copyFileSync(source, destination);
  58. // logger.info("成功");
  59. return true;
  60. } catch (err) {
  61. return false;
  62. // logger.info("失败");
  63. }
  64. }
  65. // // 读取日志(倒叙)指定行数
  66. // function readLastNLines(filePath, n) {
  67. // const content = fs.readFileSync(filePath, 'utf8');
  68. // const lines = content.trim().split('\n');
  69. // const lastNLines = lines.slice(-n).join('\n');
  70. //
  71. // // 此处处理或输出最后n行的内容
  72. // // console.log(lastNLines);
  73. // return lastNLines.split("\n").reverse();
  74. // }
  75. // 下载文件
  76. function dowFile(url, fileName, dowPath, headers) {
  77. return http.dowFile(url, fileName, dowPath, headers)
  78. }
  79. // 删除文件
  80. function delFile() {
  81. }
  82. /***********************************/
  83. function getLastFile(dirPath, number, callback) {
  84. logger.info(`开始读取文件 ---`)
  85. fs.readdir(dirPath, async (err, files) => {
  86. logger.info(`--------01`)
  87. if (err) {
  88. logger.error(`无法列出目录。`, err);
  89. // process.exit(1);
  90. } else {
  91. logger.info(`--------02`, files, dirPath)
  92. let fileList = [];
  93. //提示日志默认是根据日志生成的,拿到的files 也是 按照生成日志排序的,所以
  94. // 按照倒叙获取,如果一个文件内容满足num,就不在继续查询,如果不够,继续获取文件
  95. // for (var i = files.length - 1; i >= 0; i--) {
  96. // var file = files[i];
  97. // logger.info(`--------02..00`, dirPath, file,i,files.length,fileList.length)
  98. // const filePath = path.join(dirPath, file);
  99. // const stats = await fs.promises.stat(filePath);
  100. // fileList.push({name: file, time: stats.mtimeMs});
  101. //
  102. // if (fileList.length > number) {//如果够 number 就不再继续获取文件
  103. // break
  104. // }
  105. // }
  106. for (file of files) {
  107. logger.info(`--------02..00`, dirPath, file,files.length,fileList.length)
  108. const filePath = path.join(dirPath, file);
  109. logger.info(`--------02..111`, filePath)
  110. const stats = await fs.promises.stat(filePath);
  111. logger.info(`--------02..22222`, stats)
  112. fileList.push({name: file, time: stats.mtimeMs});
  113. }
  114. logger.info(`--------03`)
  115. fileList.sort((a, b) => b.time - a.time);
  116. logger.info(`--------04`)
  117. lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
  118. logger.info(`--------05`)
  119. lastFileNameList = lastFileList.map((item) => item.name);
  120. logger.info(`文件读取完毕 ---`)
  121. callback(lastFileNameList.reverse(), lastFileList.reverse());
  122. }
  123. });
  124. }
  125. // 读取日志(倒叙)指定行数
  126. function readLastNLines(dirPath, filePathList, n) {
  127. const fileList = [];
  128. var count = 0;
  129. for(var i = filePathList.length-1;i >=0;i--){
  130. var item = filePathList[i];
  131. var filepath = dirPath + "/" + item
  132. logger.info("日志文件:" + filepath)
  133. const content = fs.readFileSync(`${filepath}`, "utf8");
  134. let lines = content.trim().split("\n");
  135. fileList.push({
  136. filePath: item,
  137. lines: lines,
  138. })
  139. count += lines.length;
  140. if(count > n){
  141. break
  142. }
  143. }
  144. // const fileList = filePathList.map((item) => {
  145. // var filepath = dirPath + "/" + item
  146. //
  147. // logger.info("日志文件:" + filepath)
  148. // const content = fs.readFileSync(`${filepath}`, "utf8");
  149. // let lines = content.trim().split("\n");
  150. // return {
  151. // filePath: item,
  152. // lines: lines,
  153. // };
  154. // });
  155. const allFile = [].concat(...fileList.map((item) => item.lines));
  156. let lastNLines = allFile.slice(-n).join("\n");
  157. return lastNLines.split("\n").reverse();
  158. }
  159. module.exports = {
  160. dowFile,
  161. checkFilePath,
  162. checkPathSync,
  163. writeFile,
  164. copyFileSync,
  165. readLastNLines,
  166. getLastFile
  167. }