| 1234567891011121314151617181920212223242526272829 |
- /**
- * 保存字符串为文本文件
- */
- const path = require('path')
- const fs = require('fs')
- /** 构建绝对路径:绝对路径直接返回,否则相对于 folderPath 或项目根 */
- function buildFilePath(filePath, folderPath) {
- if (filePath.startsWith('/') || filePath.includes(':')) return filePath
- return folderPath ? path.join(folderPath, filePath) : filePath
- }
- /** 写入文本文件(供 ef-compiler 调用) */
- function writeTextFile(absoluteFilePath, contentStr) {
- fs.writeFileSync(absoluteFilePath, contentStr, 'utf8')
- }
- /** 执行保存文本文件 */
- async function executeSaveTxt({ filePath, content, folderPath }) {
- if (!filePath) return { success: false, error: 'save-txt 缺少 filePath 参数' }
- if (content === undefined || content === null) return { success: false, error: 'save-txt 缺少 content 参数' }
- const absoluteFilePath = buildFilePath(filePath, folderPath)
- const contentStr = typeof content === 'string' ? content : String(content)
- writeTextFile(absoluteFilePath, contentStr)
- return { success: true }
- }
- module.exports = { executeSaveTxt, writeTextFile }
|