echo-parser.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /** 语句:echo 打印信息(写入 log + UI) */
  2. const types = ['echo']
  3. function parse(action, parseContext) {
  4. const { extractVarName } = parseContext
  5. const parsed = { type: 'echo' }
  6. if (action.inVars && Array.isArray(action.inVars)) {
  7. parsed.inVars = action.inVars.map(v => extractVarName(v))
  8. } else parsed.inVars = []
  9. if (action.value) parsed.value = action.value
  10. return Object.assign({}, action, parsed)
  11. }
  12. async function execute(action, ctx) {
  13. const { folderPath, variableContext, replaceVariablesInString, logMessage } = ctx
  14. let message = ''
  15. if (action.inVars && action.inVars.length > 0) {
  16. message = action.inVars.map((v) => (v != null ? String(v) : '')).join(' ')
  17. } else if (action.value) {
  18. message = replaceVariablesInString(action.value, variableContext)
  19. const doubleBracePattern = /\{\{([\w-]+)\}\}/g
  20. let match
  21. const variablesInValue = []
  22. while ((match = doubleBracePattern.exec(action.value)) !== null) variablesInValue.push(match[1])
  23. if (!message || message === action.value) {
  24. const missingVars = variablesInValue.filter(vn => {
  25. const v = variableContext[vn]
  26. return v === undefined || v === null || v === ''
  27. })
  28. if (missingVars.length > 0) message = `${action.value} [vars undefined or empty: ${missingVars.join(', ')}]`
  29. }
  30. }
  31. const now = new Date()
  32. 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')}`
  33. const messageWithTime = message ? `[echo] ${message} [time: ${timeStr}]` : `[echo] [empty message] [time: ${timeStr}]`
  34. await logMessage(messageWithTime, folderPath)
  35. if (typeof window !== 'undefined') {
  36. const logEvent = new CustomEvent('log-message', { detail: { message } })
  37. window.dispatchEvent(logEvent)
  38. }
  39. return { success: true }
  40. }
  41. module.exports = { types, parse, execute }