read-txt.js 962 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * 读取根目录下的文本文件
  3. */
  4. const path = require('path')
  5. const fs = require('fs')
  6. /** 构建绝对路径:绝对路径直接返回,否则相对于 folderPath 或项目根 */
  7. function buildFilePath(filePath, folderPath) {
  8. if (filePath.startsWith('/') || filePath.includes(':')) return filePath
  9. return folderPath ? path.join(folderPath, filePath) : filePath
  10. }
  11. /** 读取文本文件(供 ef-compiler 调用) */
  12. function readTextFile(absoluteFilePath) {
  13. const content = fs.readFileSync(absoluteFilePath, 'utf8')
  14. return { content }
  15. }
  16. /** 执行读取文本文件 */
  17. async function executeReadTxt({ filePath, folderPath }) {
  18. if (!filePath) return { success: false, error: 'read-txt 缺少 filePath 参数' }
  19. const absoluteFilePath = buildFilePath(filePath, folderPath)
  20. const result = readTextFile(absoluteFilePath)
  21. return { success: true, content: result.content || '' }
  22. }
  23. module.exports = { executeReadTxt, readTextFile }