Adding default options which will be used in the builder if no directory or bucket is specified as environment variables.

This commit is contained in:
mike12345567 2020-09-22 14:37:09 +01:00
parent b1e1e06895
commit 8fe50febc4
1 changed files with 26 additions and 14 deletions

View File

@ -6,8 +6,12 @@ const environment = require("../environment")
const download = require("download") const download = require("download")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const path = require("path") const path = require("path")
const os = require("os")
const fs = require("fs")
const Sentry = require("@sentry/node") const Sentry = require("@sentry/node")
const DEFAULT_BUCKET = "https://prod-budi-automations.s3-eu-west-1.amazonaws.com"
const DEFAULT_DIRECTORY = ".budibase-automations"
const AUTOMATION_MANIFEST = "manifest.json" const AUTOMATION_MANIFEST = "manifest.json"
const BUILTIN_ACTIONS = { const BUILTIN_ACTIONS = {
SEND_EMAIL: sendEmail.run, SEND_EMAIL: sendEmail.run,
@ -22,6 +26,8 @@ const BUILTIN_DEFINITIONS = {
CREATE_USER: createUser.definition, CREATE_USER: createUser.definition,
} }
let AUTOMATION_BUCKET = environment.AUTOMATION_BUCKET
let AUTOMATION_DIRECTORY = environment.AUTOMATION_DIRECTORY
let MANIFEST = null let MANIFEST = null
function buildBundleName(pkgName, version) { function buildBundleName(pkgName, version) {
@ -30,10 +36,10 @@ function buildBundleName(pkgName, version) {
async function downloadPackage(name, version, bundleName) { async function downloadPackage(name, version, bundleName) {
await download( await download(
`${environment.AUTOMATION_BUCKET}/${name}/${version}/${bundleName}`, `${AUTOMATION_BUCKET}/${name}/${version}/${bundleName}`,
environment.AUTOMATION_DIRECTORY AUTOMATION_DIRECTORY
) )
return require(path.join(environment.AUTOMATION_DIRECTORY, bundleName)) return require(path.join(AUTOMATION_DIRECTORY, bundleName))
} }
module.exports.getAction = async function(actionName) { module.exports.getAction = async function(actionName) {
@ -47,28 +53,34 @@ module.exports.getAction = async function(actionName) {
const pkg = MANIFEST.packages[actionName] const pkg = MANIFEST.packages[actionName]
const bundleName = buildBundleName(pkg.stepId, pkg.version) const bundleName = buildBundleName(pkg.stepId, pkg.version)
try { try {
return require(path.join(environment.AUTOMATION_DIRECTORY, bundleName)) return require(path.join(AUTOMATION_DIRECTORY, bundleName))
} catch (err) { } catch (err) {
return downloadPackage(pkg.stepId, pkg.version, bundleName) return downloadPackage(pkg.stepId, pkg.version, bundleName)
} }
} }
module.exports.init = async function() { module.exports.init = async function() {
// 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 })
}
// env setup to get async packages // env setup to get async packages
try { try {
if (environment.AUTOMATION_DIRECTORY && environment.AUTOMATION_BUCKET) { let response = await fetch(`${AUTOMATION_BUCKET}/${AUTOMATION_MANIFEST}`)
let response = await fetch(
`${environment.AUTOMATION_BUCKET}/${AUTOMATION_MANIFEST}`
)
MANIFEST = await response.json() MANIFEST = await response.json()
}
} catch (err) {
Sentry.captureException(err)
}
module.exports.DEFINITIONS = module.exports.DEFINITIONS =
MANIFEST && MANIFEST.packages MANIFEST && MANIFEST.packages
? Object.assign(MANIFEST.packages, BUILTIN_DEFINITIONS) ? Object.assign(MANIFEST.packages, BUILTIN_DEFINITIONS)
: BUILTIN_DEFINITIONS : BUILTIN_DEFINITIONS
} catch (err) {
Sentry.captureException(err)
}
} }
module.exports.DEFINITIONS = BUILTIN_DEFINITIONS module.exports.DEFINITIONS = BUILTIN_DEFINITIONS