2021-03-26 00:17:04 +13:00
|
|
|
const scriptController = require("../../api/controllers/script")
|
2021-09-09 23:23:52 +12:00
|
|
|
const { buildCtx } = require("./utils")
|
2021-03-26 00:17:04 +13:00
|
|
|
|
2021-09-07 04:53:02 +12:00
|
|
|
exports.definition = {
|
2021-05-27 22:11:44 +12:00
|
|
|
name: "JS Scripting",
|
2021-05-01 02:44:37 +12:00
|
|
|
tagline: "Execute JavaScript Code",
|
2021-03-26 00:17:04 +13:00
|
|
|
icon: "ri-terminal-box-line",
|
|
|
|
description: "Run a piece of JavaScript code in your automation",
|
|
|
|
type: "ACTION",
|
2021-09-14 23:40:19 +12:00
|
|
|
internal: true,
|
2021-03-26 00:17:04 +13:00
|
|
|
stepId: "EXECUTE_SCRIPT",
|
|
|
|
inputs: {},
|
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
|
|
|
code: {
|
2021-03-27 03:56:34 +13:00
|
|
|
type: "string",
|
|
|
|
customType: "code",
|
2021-03-26 00:17:04 +13:00
|
|
|
title: "Code",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["code"],
|
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
properties: {
|
2021-05-01 02:44:37 +12:00
|
|
|
value: {
|
|
|
|
type: "string",
|
2021-09-09 23:23:52 +12:00
|
|
|
description: "The result of the return statement",
|
2021-05-01 02:44:37 +12:00
|
|
|
},
|
2021-03-26 00:17:04 +13:00
|
|
|
success: {
|
|
|
|
type: "boolean",
|
|
|
|
description: "Whether the action was successful",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-27 03:56:34 +13:00
|
|
|
required: ["success"],
|
2021-03-26 00:17:04 +13:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-07 04:53:02 +12:00
|
|
|
exports.run = async function ({ inputs, appId, context, emitter }) {
|
2021-03-26 00:17:04 +13:00
|
|
|
if (inputs.code == null) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
response: {
|
|
|
|
message: "Invalid inputs",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:23:52 +12:00
|
|
|
const ctx = buildCtx(appId, emitter, {
|
|
|
|
body: {
|
|
|
|
script: inputs.code,
|
|
|
|
context,
|
2021-03-26 00:17:04 +13:00
|
|
|
},
|
2021-09-09 23:23:52 +12:00
|
|
|
})
|
2021-03-26 00:17:04 +13:00
|
|
|
|
|
|
|
try {
|
|
|
|
await scriptController.execute(ctx)
|
|
|
|
return {
|
2021-09-09 23:23:52 +12:00
|
|
|
success: true,
|
2021-05-01 02:44:37 +12:00
|
|
|
value: ctx.body,
|
2021-03-26 00:17:04 +13:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
response: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|