file.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. console.error('写入文件时发生错误:', err);
  46. breakFun(err)
  47. } else {
  48. console.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. if (err) {
  86. console.error(`Could not list the directory.`, err);
  87. process.exit(1);
  88. }
  89. let fileList = [];
  90. for (file of files) {
  91. const filePath = path.join(dirPath, file);
  92. const stats = await fs.promises.stat(filePath);
  93. fileList.push({name: file, time: stats.mtimeMs});
  94. }
  95. fileList.sort((a, b) => b.time - a.time);
  96. lastFileList = fileList.slice(0, number != -1 ? number : fileList.length);
  97. lastFileNameList = lastFileList.map((item) => item.name);
  98. callback(lastFileNameList.reverse(), lastFileList.reverse());
  99. });
  100. }
  101. // 读取日志(倒叙)指定行数
  102. function readLastNLines(dirPath, filePathList, n) {
  103. const fileList = filePathList.map((item) => {
  104. var filepath = dirPath + "/" + item
  105. // 将相对路径转换为绝对路径
  106. const directoryPath = path.resolve(filepath);
  107. logger.info("日志文件:" + directoryPath)
  108. let lines = [""]
  109. if (checkFilePath(directoryPath)) {
  110. const content = fs.readFileSync(`${filepath}`, "utf8");
  111. lines = content.trim().split("\n");
  112. }
  113. return {
  114. filePath: item,
  115. lines: lines,
  116. };
  117. });
  118. const allFile = [].concat(...fileList.map((item) => item.lines));
  119. const lastNLines = allFile.slice(-n).join("\n");
  120. return lastNLines.split("\n").reverse();
  121. }
  122. module.exports = {
  123. dowFile,
  124. checkFilePath,
  125. checkPathSync,
  126. writeFile,
  127. copyFileSync,
  128. readLastNLines,
  129. getLastFile
  130. }