1
0
Fork 0
mirror of synced 2024-06-16 09:25:12 +12:00
budibase/packages/server/src/api/controllers/application.js

220 lines
6.4 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const { getPackageForBuilder, buildPage } = require("../../utilities/builder")
2020-05-15 02:12:30 +12:00
const env = require("../../environment")
2020-10-14 05:02:59 +13:00
const { copy, existsSync, readFile, writeFile } = require("fs-extra")
2020-06-04 08:23:56 +12:00
const { budibaseAppsDir } = require("../../utilities/budibaseDir")
2020-06-04 02:43:37 +12:00
const sqrl = require("squirrelly")
2020-06-19 03:59:31 +12:00
const setBuilderToken = require("../../utilities/builder/setBuilderToken")
const fs = require("fs-extra")
const { join, resolve } = require("../../utilities/centralPath")
const { promisify } = require("util")
const chmodr = require("chmodr")
const packageJson = require("../../../package.json")
const { createLinkView } = require("../../db/linkedRows")
const { downloadTemplate } = require("../../utilities/templates")
const { generateAppID, DocumentTypes, SEPARATOR } = require("../../db/utils")
2020-07-15 08:10:51 +12:00
const {
downloadExtractComponentLibraries,
} = require("../../utilities/createAppPackage")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
2020-04-08 04:25:09 +12:00
async function createInstance(template) {
const appId = generateAppID()
const db = new CouchDB(appId)
await db.put({
_id: "_design/database",
// view collation information, read before writing any complex views:
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
views: {},
})
// add view for linked rows
await createLinkView(appId)
// replicate the template data to the instance DB
if (template) {
const templatePath = await downloadTemplate(...template.key.split("/"))
const dbDumpReadStream = fs.createReadStream(
join(templatePath, "db", "dump.txt")
)
const { ok } = await db.load(dbDumpReadStream)
if (!ok) {
throw "Error loading database dump from template."
}
}
return { _id: appId }
}
2020-06-30 03:49:16 +12:00
exports.fetch = async function(ctx) {
let allDbs = await CouchDB.allDbs()
const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
2020-10-30 00:48:01 +13:00
const apps = appDbNames.map(db => new CouchDB(db).get(db))
if (apps.length === 0) {
ctx.body = []
} else {
ctx.body = await Promise.all(apps)
}
2020-05-07 21:53:34 +12:00
}
2020-04-08 04:25:09 +12:00
2020-06-30 03:49:16 +12:00
exports.fetchAppPackage = async function(ctx) {
const db = new CouchDB(ctx.params.appId)
const application = await db.get(ctx.params.appId)
2020-05-07 21:53:34 +12:00
ctx.body = await getPackageForBuilder(ctx.config, application)
setBuilderToken(ctx, ctx.params.appId, application.version)
}
2020-06-30 03:49:16 +12:00
exports.create = async function(ctx) {
const instance = await createInstance(ctx.request.body.template)
const appId = instance._id
2020-05-15 02:12:30 +12:00
const newApplication = {
_id: appId,
type: "app",
userInstanceMap: {},
version: packageJson.version,
componentLibraries: ["@budibase/standard-components"],
name: ctx.request.body.name,
2020-09-26 01:47:42 +12:00
template: ctx.request.body.template,
instance: instance,
2020-04-10 03:53:48 +12:00
}
const instanceDb = new CouchDB(appId)
await instanceDb.put(newApplication)
2020-05-15 02:12:30 +12:00
if (env.NODE_ENV !== "jest") {
const newAppFolder = await createEmptyAppPackage(ctx, newApplication)
await downloadExtractComponentLibraries(newAppFolder)
}
ctx.status = 200
2020-05-15 02:12:30 +12:00
ctx.body = newApplication
ctx.message = `Application ${ctx.request.body.name} created successfully`
2020-05-07 21:53:34 +12:00
}
2020-06-30 03:49:16 +12:00
exports.update = async function(ctx) {
const db = new CouchDB(ctx.params.appId)
const application = await db.get(ctx.params.appId)
const data = ctx.request.body
const newData = { ...application, ...data }
const response = await db.put(newData)
data._rev = response.rev
ctx.status = 200
ctx.message = `Application ${application.name} updated successfully.`
ctx.body = response
}
exports.delete = async function(ctx) {
const db = new CouchDB(ctx.params.appId)
const app = await db.get(ctx.params.appId)
const result = await db.destroy()
// remove top level directory
await fs.rmdir(join(budibaseAppsDir(), ctx.params.appId), {
recursive: true,
})
ctx.status = 200
ctx.message = `Application ${app.name} deleted successfully.`
ctx.body = result
}
const createEmptyAppPackage = async (ctx, app) => {
const templateFolder = resolve(
__dirname,
"..",
"..",
"utilities",
"appDirectoryTemplate"
)
2020-06-04 08:23:56 +12:00
const appsFolder = budibaseAppsDir()
const newAppFolder = resolve(appsFolder, app._id)
2020-10-14 05:02:59 +13:00
if (existsSync(newAppFolder)) {
ctx.throw(400, "App folder already exists for this application")
}
2020-07-14 19:21:22 +12:00
await fs.ensureDir(join(newAppFolder, "pages", "main", "screens"), 0o777)
await fs.ensureDir(
join(newAppFolder, "pages", "unauthenticated", "screens"),
0o777
)
2020-07-14 19:13:05 +12:00
await copy(templateFolder, newAppFolder)
// this line allows full permission on copied files
// we have an unknown problem without this, whereby the
// files get weird permissions and cant be written to :(
const chmodrPromise = promisify(chmodr)
await chmodrPromise(newAppFolder, 0o777)
2020-06-04 02:43:37 +12:00
await updateJsonFile(join(appsFolder, app._id, "package.json"), {
name: npmFriendlyAppName(app.name),
})
2020-09-29 05:04:08 +13:00
// if this app is being created from a template,
// copy the frontend page definition files from
// the template directory.
2020-09-26 01:47:42 +12:00
if (app.template) {
2020-09-29 05:04:08 +13:00
const templatePageDefinitions = join(
appsFolder,
"templates",
app.template.key,
"pages"
)
2020-09-26 01:47:42 +12:00
await copy(templatePageDefinitions, join(appsFolder, app._id, "pages"))
}
const mainJson = await updateJsonFile(
2020-06-04 02:43:37 +12:00
join(appsFolder, app._id, "pages", "main", "page.json"),
app
)
2020-07-10 02:05:56 +12:00
await buildPage(ctx.config, app._id, "main", {
2020-07-10 01:47:54 +12:00
page: mainJson,
screens: await loadScreens(newAppFolder, "main"),
})
const unauthenticatedJson = await updateJsonFile(
2020-06-04 02:43:37 +12:00
join(appsFolder, app._id, "pages", "unauthenticated", "page.json"),
app
)
await buildPage(ctx.config, app._id, "unauthenticated", {
page: unauthenticatedJson,
2020-07-10 01:47:54 +12:00
screens: await loadScreens(newAppFolder, "unauthenticated"),
})
return newAppFolder
}
2020-07-10 01:47:54 +12:00
const loadScreens = async (appFolder, page) => {
const screensFolder = join(appFolder, "pages", page, "screens")
const screenFiles = (await fs.readdir(screensFolder)).filter(s =>
s.endsWith(".json")
)
let screens = []
for (let file of screenFiles) {
screens.push(await fs.readJSON(join(screensFolder, file)))
}
return screens
}
2020-06-04 02:43:37 +12:00
const updateJsonFile = async (filePath, app) => {
const json = await readFile(filePath, "utf8")
const newJson = sqrl.Render(json, app)
await writeFile(filePath, newJson, "utf8")
return JSON.parse(newJson)
2020-06-04 02:43:37 +12:00
}
const npmFriendlyAppName = name =>
name
.replace(/_/g, "")
.replace(/./g, "")
.replace(/ /g, "")
.toLowerCase()