| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- /** 语句:echo 打印信息(写入 log + UI) */
- const types = ['echo']
- function parse(action, parseContext) {
- const { extractVarName } = parseContext
- const parsed = { type: 'echo' }
- if (action.inVars && Array.isArray(action.inVars)) {
- parsed.inVars = action.inVars.map(v => extractVarName(v))
- } else parsed.inVars = []
- if (action.value) parsed.value = action.value
- return Object.assign({}, action, parsed)
- }
- async function execute(action, ctx) {
- const { folderPath, variableContext, extractVarName, replaceVariablesInString, logMessage } = ctx
- let message = ''
- if (action.inVars && action.inVars.length > 0) {
- const messages = action.inVars.map(varWithBraces => {
- const varName = extractVarName(varWithBraces)
- const varValue = variableContext[varName]
- if (varWithBraces.startsWith('{') && varWithBraces.endsWith('}')) {
- return varValue !== undefined ? String(varValue) : varWithBraces
- }
- return varWithBraces
- })
- message = messages.join(' ')
- } else if (action.value) {
- message = replaceVariablesInString(action.value, variableContext)
- const doubleBracePattern = /\{\{([\w-]+)\}\}/g
- let match
- const variablesInValue = []
- while ((match = doubleBracePattern.exec(action.value)) !== null) variablesInValue.push(match[1])
- if (!message || message === action.value) {
- const missingVars = variablesInValue.filter(vn => {
- const v = variableContext[vn]
- return v === undefined || v === null || v === ''
- })
- if (missingVars.length > 0) message = `${action.value} [变量未定义或为空: ${missingVars.join(', ')}]`
- }
- }
- const now = new Date()
- const timeStr = `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`
- const messageWithTime = message ? `${message} [系统时间: ${timeStr}]` : `[空消息] [系统时间: ${timeStr}]`
- await logMessage(messageWithTime, folderPath)
- if (typeof window !== 'undefined') {
- const logEvent = new CustomEvent('log-message', { detail: { message } })
- window.dispatchEvent(logEvent)
- }
- return { success: true }
- }
- module.exports = { types, parse, execute }
|