2020-09-16 15:00:04 +02:00
|
|
|
const sendEmail = require("./steps/sendEmail")
|
|
|
|
const saveRecord = require("./steps/saveRecord")
|
|
|
|
const deleteRecord = require("./steps/deleteRecord")
|
|
|
|
const createUser = require("./steps/createUser")
|
2020-09-18 17:50:52 +02:00
|
|
|
const environment = require("../environment")
|
|
|
|
const download = require("download")
|
|
|
|
const fetch = require("node-fetch")
|
|
|
|
const path = require("path")
|
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 15:37:09 +02:00
|
|
|
const DEFAULT_BUCKET = "https://prod-budi-automations.s3-eu-west-1.amazonaws.com"
|
|
|
|
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,
|
|
|
|
SAVE_RECORD: saveRecord.run,
|
|
|
|
DELETE_RECORD: deleteRecord.run,
|
|
|
|
CREATE_USER: createUser.run,
|
|
|
|
}
|
|
|
|
const BUILTIN_DEFINITIONS = {
|
|
|
|
SEND_EMAIL: sendEmail.definition,
|
|
|
|
SAVE_RECORD: saveRecord.definition,
|
|
|
|
DELETE_RECORD: deleteRecord.definition,
|
|
|
|
CREATE_USER: createUser.definition,
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 15:37:09 +02:00
|
|
|
let AUTOMATION_BUCKET = environment.AUTOMATION_BUCKET
|
|
|
|
let AUTOMATION_DIRECTORY = environment.AUTOMATION_DIRECTORY
|
2020-09-18 17:50:52 +02:00
|
|
|
let MANIFEST = null
|
|
|
|
|
2020-09-22 15:07:22 +02:00
|
|
|
function buildBundleName(pkgName, version) {
|
|
|
|
return `${pkgName}@${version}.min.js`
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-22 15:37:09 +02:00
|
|
|
return require(path.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]
|
|
|
|
}
|
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-09-22 15:37:09 +02:00
|
|
|
return require(path.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) {
|
|
|
|
AUTOMATION_DIRECTORY = path.join(os.homedir(), DEFAULT_DIRECTORY)
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
Sentry.captureException(err)
|
|
|
|
}
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
2020-09-16 15:00:04 +02:00
|
|
|
|
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
|