2020-09-16 15:00:04 +02:00
|
|
|
const sendEmail = require("./steps/sendEmail")
|
2020-10-09 20:10:28 +02:00
|
|
|
const createRow = require("./steps/createRow")
|
|
|
|
const updateRow = require("./steps/updateRow")
|
|
|
|
const deleteRow = require("./steps/deleteRow")
|
2020-09-16 15:00:04 +02:00
|
|
|
const createUser = require("./steps/createUser")
|
2020-10-27 13:33:25 +01:00
|
|
|
const outgoingWebhook = require("./steps/outgoingWebhook")
|
2020-10-28 21:35:06 +01:00
|
|
|
const env = require("../environment")
|
2020-09-18 17:50:52 +02:00
|
|
|
const download = require("download")
|
|
|
|
const fetch = require("node-fetch")
|
2020-10-09 11:00:57 +02:00
|
|
|
const { join } = require("../utilities/centralPath")
|
2020-09-22 15:37:09 +02:00
|
|
|
const os = require("os")
|
|
|
|
const fs = require("fs")
|
2020-09-18 17:50:52 +02:00
|
|
|
const Sentry = require("@sentry/node")
|
2020-09-10 16:00:21 +02:00
|
|
|
|
2020-09-22 16:25:26 +02:00
|
|
|
const DEFAULT_BUCKET =
|
|
|
|
"https://prod-budi-automations.s3-eu-west-1.amazonaws.com"
|
2020-09-22 15:37:09 +02:00
|
|
|
const DEFAULT_DIRECTORY = ".budibase-automations"
|
2020-09-18 17:50:52 +02:00
|
|
|
const AUTOMATION_MANIFEST = "manifest.json"
|
2020-09-16 15:00:04 +02:00
|
|
|
const BUILTIN_ACTIONS = {
|
|
|
|
SEND_EMAIL: sendEmail.run,
|
2020-10-09 20:10:28 +02:00
|
|
|
CREATE_ROW: createRow.run,
|
|
|
|
UPDATE_ROW: updateRow.run,
|
|
|
|
DELETE_ROW: deleteRow.run,
|
2020-09-16 15:00:04 +02:00
|
|
|
CREATE_USER: createUser.run,
|
2020-10-27 13:33:25 +01:00
|
|
|
OUTGOING_WEBHOOK: outgoingWebhook.run,
|
2020-09-16 15:00:04 +02:00
|
|
|
}
|
|
|
|
const BUILTIN_DEFINITIONS = {
|
|
|
|
SEND_EMAIL: sendEmail.definition,
|
2020-10-09 20:10:28 +02:00
|
|
|
CREATE_ROW: createRow.definition,
|
|
|
|
UPDATE_ROW: updateRow.definition,
|
|
|
|
DELETE_ROW: deleteRow.definition,
|
2020-09-16 15:00:04 +02:00
|
|
|
CREATE_USER: createUser.definition,
|
2020-10-27 13:33:25 +01:00
|
|
|
OUTGOING_WEBHOOK: outgoingWebhook.definition,
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
let AUTOMATION_BUCKET = env.AUTOMATION_BUCKET
|
|
|
|
let AUTOMATION_DIRECTORY = env.AUTOMATION_DIRECTORY
|
2020-09-18 17:50:52 +02:00
|
|
|
let MANIFEST = null
|
|
|
|
|
2021-03-11 19:29:48 +01:00
|
|
|
/* instanbul ignore next */
|
2020-09-22 15:07:22 +02:00
|
|
|
function buildBundleName(pkgName, version) {
|
|
|
|
return `${pkgName}@${version}.min.js`
|
|
|
|
}
|
|
|
|
|
2021-03-11 19:29:48 +01:00
|
|
|
/* instanbul ignore next */
|
2020-09-22 15:07:22 +02:00
|
|
|
async function downloadPackage(name, version, bundleName) {
|
2020-09-18 17:50:52 +02:00
|
|
|
await download(
|
2020-09-22 15:37:09 +02:00
|
|
|
`${AUTOMATION_BUCKET}/${name}/${version}/${bundleName}`,
|
|
|
|
AUTOMATION_DIRECTORY
|
2020-09-18 17:50:52 +02:00
|
|
|
)
|
2020-10-07 01:24:56 +02:00
|
|
|
return require(join(AUTOMATION_DIRECTORY, bundleName))
|
2020-09-18 17:50:52 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 16:00:21 +02:00
|
|
|
module.exports.getAction = async function(actionName) {
|
|
|
|
if (BUILTIN_ACTIONS[actionName] != null) {
|
|
|
|
return BUILTIN_ACTIONS[actionName]
|
|
|
|
}
|
2021-02-17 14:40:14 +01:00
|
|
|
// worker pools means that a worker may not have manifest
|
|
|
|
if (env.CLOUD && MANIFEST == null) {
|
|
|
|
MANIFEST = await module.exports.init()
|
|
|
|
}
|
2020-09-18 17:50:52 +02:00
|
|
|
// env setup to get async packages
|
|
|
|
if (!MANIFEST || !MANIFEST.packages || !MANIFEST.packages[actionName]) {
|
|
|
|
return null
|
|
|
|
}
|
2020-09-22 15:07:22 +02:00
|
|
|
const pkg = MANIFEST.packages[actionName]
|
|
|
|
const bundleName = buildBundleName(pkg.stepId, pkg.version)
|
2020-09-18 17:50:52 +02:00
|
|
|
try {
|
2020-10-07 01:24:56 +02:00
|
|
|
return require(join(AUTOMATION_DIRECTORY, bundleName))
|
2020-09-18 17:50:52 +02:00
|
|
|
} catch (err) {
|
2020-09-22 15:07:22 +02:00
|
|
|
return downloadPackage(pkg.stepId, pkg.version, bundleName)
|
2020-09-18 17:50:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.init = async function() {
|
2020-09-22 15:37:09 +02:00
|
|
|
// set defaults
|
|
|
|
if (!AUTOMATION_DIRECTORY) {
|
2020-10-07 01:24:56 +02:00
|
|
|
AUTOMATION_DIRECTORY = join(os.homedir(), DEFAULT_DIRECTORY)
|
2020-09-22 15:37:09 +02:00
|
|
|
}
|
|
|
|
if (!AUTOMATION_BUCKET) {
|
|
|
|
AUTOMATION_BUCKET = DEFAULT_BUCKET
|
|
|
|
}
|
|
|
|
if (!fs.existsSync(AUTOMATION_DIRECTORY)) {
|
|
|
|
fs.mkdirSync(AUTOMATION_DIRECTORY, { recursive: true })
|
|
|
|
}
|
2020-09-18 17:50:52 +02:00
|
|
|
// env setup to get async packages
|
|
|
|
try {
|
2020-09-22 15:37:09 +02:00
|
|
|
let response = await fetch(`${AUTOMATION_BUCKET}/${AUTOMATION_MANIFEST}`)
|
|
|
|
MANIFEST = await response.json()
|
|
|
|
module.exports.DEFINITIONS =
|
|
|
|
MANIFEST && MANIFEST.packages
|
|
|
|
? Object.assign(MANIFEST.packages, BUILTIN_DEFINITIONS)
|
|
|
|
: BUILTIN_DEFINITIONS
|
2020-09-18 17:50:52 +02:00
|
|
|
} catch (err) {
|
2021-02-17 14:40:14 +01:00
|
|
|
console.error(err)
|
2020-09-18 17:50:52 +02:00
|
|
|
Sentry.captureException(err)
|
|
|
|
}
|
2021-02-17 14:40:14 +01:00
|
|
|
return MANIFEST
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
2020-09-16 15:00:04 +02:00
|
|
|
|
2021-03-11 19:29:48 +01:00
|
|
|
// definitions will have downloaded ones added to it, while builtin won't
|
2020-09-18 17:50:52 +02:00
|
|
|
module.exports.DEFINITIONS = BUILTIN_DEFINITIONS
|
2020-09-16 15:00:04 +02:00
|
|
|
module.exports.BUILTIN_DEFINITIONS = BUILTIN_DEFINITIONS
|