|
|
@@ -0,0 +1,85 @@
|
|
|
+# ef-compiler 新增结点说明
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 一、action 下添加结点
|
|
|
+
|
|
|
+在 `nodejs/ef-compiler/actions/` 下新建 `hello-parser.js`:
|
|
|
+
|
|
|
+```js
|
|
|
+const types = ['hello']
|
|
|
+
|
|
|
+function parse(action, parseContext) {
|
|
|
+ const parsed = { type: 'hello', value: action.value || '' }
|
|
|
+ return Object.assign({}, action, parsed)
|
|
|
+}
|
|
|
+
|
|
|
+async function execute(action, ctx) {
|
|
|
+ const msg = action.value ? String(action.value) : 'hello'
|
|
|
+ if (ctx.logMessage) await ctx.logMessage('[hello] ' + msg, ctx.folderPath)
|
|
|
+ return { success: true }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = { types, parse, execute }
|
|
|
+```
|
|
|
+
|
|
|
+在 `workflow-json-parser.js` 里:`actionModules` 加 `require('./actions/hello-parser.js')`;要中文名则在 `getActionName` 的 `typeNames` 加 `'hello': '打招呼'`。
|
|
|
+
|
|
|
+工作流 JSON:`{ "type": "hello", "value": "世界" }`
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 二、fun 下添加结点
|
|
|
+
|
|
|
+以添加 `test-fun` 为例,共 5 步(都在 `fun-parser.js` 里改,除了第 1 步)。
|
|
|
+
|
|
|
+**1. 实现**
|
|
|
+在 `nodejs/ef-compiler/actions/fun/` 下新建 `test-fun.js`:
|
|
|
+
|
|
|
+```js
|
|
|
+async function executeTestFun() {
|
|
|
+ return { success: true, value: 'ok' }
|
|
|
+}
|
|
|
+module.exports = { executeTestFun }
|
|
|
+```
|
|
|
+
|
|
|
+**2. 登记**
|
|
|
+脚本:`nodejs/ef-compiler/actions/fun-parser.js`
|
|
|
+约第 6~14 行,在 `FUN_REGISTRY_TYPES` 数组里加 `'test-fun'`(可加在 `'string-press',` 后)。
|
|
|
+
|
|
|
+**3. 解析**
|
|
|
+脚本:`fun-parser.js`
|
|
|
+约第 34~118 行,在 `parse()` 的 `switch (action.type)` 里、`default:` 之前加:
|
|
|
+
|
|
|
+```js
|
|
|
+case 'test-fun':
|
|
|
+ parsed.variable = action.outVars?.[0] ? extractVarName(action.outVars[0]) : extractVarName(action.variable)
|
|
|
+ break
|
|
|
+```
|
|
|
+
|
|
|
+**4. 挂载**
|
|
|
+脚本:`fun-parser.js`
|
|
|
+约第 183~189 行,在 `get()` 的 `case 'io':` 的 mod 对象里加一行(例如在 `executeSaveTxt` 那行后面):
|
|
|
+`executeTestFun: require(path.join(funcDir, 'test-fun.js')).executeTestFun`。(funcDir 已指向 `actions/fun`)
|
|
|
+
|
|
|
+**5. 执行**
|
|
|
+脚本:`fun-parser.js`
|
|
|
+约第 229 行起,在 `run()` 的 `switch (actionType)` 里加:
|
|
|
+
|
|
|
+```js
|
|
|
+case 'test-fun': {
|
|
|
+ const { executeTestFun } = get(funcDir, 'io')
|
|
|
+ const result = await executeTestFun()
|
|
|
+ if (!result.success) return result
|
|
|
+ const varName = action.outVars?.[0] ? extractVarName(action.outVars[0]) : extractVarName(action.variable)
|
|
|
+ if (varName && result.value != null) variableContext[varName] = String(result.value)
|
|
|
+ await logOutVars(action, variableContext, folderPath)
|
|
|
+ return { success: true }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+**显示名(可选)**
|
|
|
+脚本:`nodejs/ef-compiler/workflow-json-parser.js`
|
|
|
+约第 48~84 行,在 `getActionName` 的 `typeNames` 对象里加 `'test-fun': '测试'`。
|
|
|
+
|
|
|
+工作流里写:`{ "type": "test-fun", "variable": "out" }` 即可。
|