| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /**
- * 保存字符串为文本文件
- * 支持将字符串内容保存到根目录下的文本文件
- */
- export const tagName = 'save-txt';
- export const schema = {
- description: '将字符串内容保存到根目录下的文本文件。',
- inputs: {
- filePath: '文件路径(相对于项目根目录,如 "output.txt" 或 "data/output.txt")',
- content: '要保存的内容(字符串,可以是变量)',
- },
- outputs: {
- success: '保存是否成功(boolean)',
- },
- };
- /**
- * 执行保存文本文件
- * @param {Object} params - 参数对象
- * @param {string} params.filePath - 文件路径(相对于项目根目录)
- * @param {string} params.content - 要保存的内容(字符串)
- * @param {string} params.folderPath - 工作流文件夹路径(用于构建绝对路径)
- * @returns {Promise<{success: boolean, error?: string}>}
- */
- export async function executeSaveTxt({ filePath, content, folderPath }) {
- try {
- if (!filePath) {
- return { success: false, error: 'save-txt 缺少 filePath 参数' };
- }
- if (content === undefined || content === null) {
- return { success: false, error: 'save-txt 缺少 content 参数' };
- }
- if (!window.electronAPI || !window.electronAPI.writeTextFile) {
- return { success: false, error: '写入文本文件 API 不可用' };
- }
- // 构建文件路径
- // 如果 filePath 是绝对路径,直接使用
- // 如果提供了 folderPath(工作流目录),相对于工作流目录
- // 否则,相对于项目根目录
- let absoluteFilePath = filePath;
-
- // 如果是相对路径(不以 / 开头且不包含 :)
- if (!filePath.startsWith('/') && !filePath.includes(':')) {
- // 如果提供了工作流目录,相对于工作流目录
- if (folderPath) {
- // folderPath 格式可能是:static/processing/微信聊天自动发送工作流
- // 需要构建相对于项目根目录的完整路径
- if (folderPath.startsWith('static/processing/')) {
- const folderName = folderPath.replace('static/processing/', '');
- absoluteFilePath = `static/processing/${folderName}/${filePath}`;
- } else {
- absoluteFilePath = `${folderPath}/${filePath}`;
- }
- } else {
- // 没有工作流目录,相对于项目根目录
- absoluteFilePath = filePath;
- }
- }
- // 将内容转换为字符串
- const contentString = typeof content === 'string' ? content : String(content);
- // 调用主进程的 writeTextFile API
- // 主进程会将相对路径解析为相对于项目根目录的绝对路径
- const result = await window.electronAPI.writeTextFile(absoluteFilePath, contentString);
- if (!result.success) {
- return { success: false, error: `保存文件失败: ${result.error}` };
- }
- return { success: true };
- } catch (error) {
- return { success: false, error: error.message || '保存文本文件失败' };
- }
- }
|