file.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. fs.readdir(dirPath, async (err, files) => {
  85. logger.info(`--------01`)
  86. if (err) {
  87. logger.error(`无法列出目录。`, err);
  88. // process.exit(1);
  89. } else {
  90. let fileList = [];
  91. //提示日志默认是根据日志生成的,拿到的files 也是 按照生成日志排序的,所以
  92. // 按照倒叙获取,如果一个文件内容满足num,就不在继续查询,如果不够,继续获取文件
  93. for (file of files) {
  94. const filePath = path.join(dirPath, file);
  95. const stats = await fs.promises.stat(filePath);
  96. fileList.push({name: file, time: stats.mtimeMs});
  97. }
  98. fileList.sort((a, b) => b.time - a.time);
  99. lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
  100. lastFileNameList = lastFileList.map((item) => item.name);
  101. callback(lastFileNameList.reverse(), lastFileList.reverse());
  102. }
  103. });
  104. }
  105. // 读取日志(倒叙)指定行数
  106. function readLastNLines(dirPath, filePathList, n) {
  107. const fileList = [];
  108. var count = 0;
  109. for(var i = filePathList.length-1;i >=0;i--){
  110. var item = filePathList[i];
  111. var filepath = dirPath + "/" + item
  112. logger.info("日志文件:" + filepath)
  113. const content = fs.readFileSync(`${filepath}`, "utf8");
  114. let lines = content.trim().split("\n");
  115. fileList.push({
  116. filePath: item,
  117. lines: lines,
  118. })
  119. count += lines.length;
  120. if(count > n){
  121. break
  122. }
  123. }
  124. fileList.reverse();
  125. const allFile = [].concat(...fileList.map((item) => item.lines));
  126. let lastNLines = allFile.slice(-n).join("\n");
  127. return lastNLines.split("\n").reverse();
  128. }
  129. module.exports = {
  130. dowFile,
  131. checkFilePath,
  132. checkPathSync,
  133. writeFile,
  134. copyFileSync,
  135. readLastNLines,
  136. getLastFile
  137. }