const fs = require('fs'); const path = require('path'); const {logger} = require("./logger"); const {rustConfig} = require("../config"); const http = require("./http"); // 检查目录是否存在,不存在新建 function checkPathSync(filePath) { // 将相对路径转换为绝对路径 const directoryPath = path.resolve(filePath); // 同步检查目录是否存在 if (!fs.existsSync(directoryPath)) { // 目录不存在,需要创建它 logger.info('目录不存在,正在创建...'); try { // 同步创建目录 fs.mkdirSync(directoryPath, {recursive: true}); logger.info('目录已成功创建'); } catch (error) { // 创建目录失败 logger.error('创建目录失败:', error); return false } } else { // 目录已存在 logger.info('目录已存在'); } return true } // 检查文件是否存在 function checkFilePath(filePath) { // 同步检查文件是否存在 const directoryPath = path.resolve(filePath); if (fs.existsSync(directoryPath)) { logger.info('文件存在:' + filePath); return true } else { logger.error('文件不存在:' + filePath); return false } } // 创建文件 function writeFile(filePath, text, breakFun) { fs.writeFile(filePath, text, (err) => { if (err) { logger.error('写入文件时发生错误:', err); breakFun(err) } else { logger.log('文件已成功写入!'); breakFun(null, true) } }); } // 同步复制 function copyFileSync(source, destination) { try { // 同步复制文件 fs.copyFileSync(source, destination); // logger.info("成功"); return true; } catch (err) { return false; // logger.info("失败"); } } // // 读取日志(倒叙)指定行数 // function readLastNLines(filePath, n) { // const content = fs.readFileSync(filePath, 'utf8'); // const lines = content.trim().split('\n'); // const lastNLines = lines.slice(-n).join('\n'); // // // 此处处理或输出最后n行的内容 // // console.log(lastNLines); // return lastNLines.split("\n").reverse(); // } // 下载文件 function dowFile(url, fileName, dowPath, headers) { return http.dowFile(url, fileName, dowPath, headers) } // 删除文件 function delFile() { } /***********************************/ function getLastFile(dirPath, number, callback) { logger.info(`开始读取文件 ---`) fs.readdir(dirPath, async (err, files) => { logger.info(`--------01`) if (err) { logger.error(`无法列出目录。`, err); // process.exit(1); } else { logger.info(`--------02`, files, dirPath) let fileList = []; //提示日志默认是根据日志生成的,拿到的files 也是 按照生成日志排序的,所以 // 按照倒叙获取,如果一个文件内容满足num,就不在继续查询,如果不够,继续获取文件 // for (var i = files.length - 1; i >= 0; i--) { // var file = files[i]; // logger.info(`--------02..00`, dirPath, file,i,files.length,fileList.length) // const filePath = path.join(dirPath, file); // const stats = await fs.promises.stat(filePath); // fileList.push({name: file, time: stats.mtimeMs}); // // if (fileList.length > number) {//如果够 number 就不再继续获取文件 // break // } // } for (file of files) { logger.info(`--------02..00`, dirPath, file,files.length,fileList.length) const filePath = path.join(dirPath, file); logger.info(`--------02..111`, filePath) const stats = await fs.promises.stat(filePath); logger.info(`--------02..22222`, stats) fileList.push({name: file, time: stats.mtimeMs}); } logger.info(`--------03`) fileList.sort((a, b) => b.time - a.time); logger.info(`--------04`) lastFileList = fileList.slice(0, number != -1 ? number : fileList.length); logger.info(`--------05`) lastFileNameList = lastFileList.map((item) => item.name); logger.info(`文件读取完毕 ---`) callback(lastFileNameList.reverse(), lastFileList.reverse()); } }); } // 读取日志(倒叙)指定行数 function readLastNLines(dirPath, filePathList, n) { const fileList = []; var count = 0; for(var i = filePathList.length-1;i >=0;i--){ var item = filePathList[i]; var filepath = dirPath + "/" + item logger.info("日志文件:" + filepath) const content = fs.readFileSync(`${filepath}`, "utf8"); let lines = content.trim().split("\n"); fileList.push({ filePath: item, lines: lines, }) count += lines.length; if(count > n){ break } } // const fileList = filePathList.map((item) => { // var filepath = dirPath + "/" + item // // logger.info("日志文件:" + filepath) // const content = fs.readFileSync(`${filepath}`, "utf8"); // let lines = content.trim().split("\n"); // return { // filePath: item, // lines: lines, // }; // }); const allFile = [].concat(...fileList.map((item) => item.lines)); let lastNLines = allFile.slice(-n).join("\n"); return lastNLines.split("\n").reverse(); } module.exports = { dowFile, checkFilePath, checkPathSync, writeFile, copyFileSync, readLastNLines, getLastFile }