2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
2020-11-02 15:53:51 +01:00
|
|
|
const { buildPage } = require("../../utilities/builder")
|
2020-05-14 16:12:30 +02:00
|
|
|
const env = require("../../environment")
|
2020-10-13 18:02:59 +02:00
|
|
|
const { copy, existsSync, readFile, writeFile } = require("fs-extra")
|
2020-06-03 22:23:56 +02:00
|
|
|
const { budibaseAppsDir } = require("../../utilities/budibaseDir")
|
2020-06-03 16:43:37 +02:00
|
|
|
const sqrl = require("squirrelly")
|
2020-06-18 17:59:31 +02:00
|
|
|
const setBuilderToken = require("../../utilities/builder/setBuilderToken")
|
2020-07-07 14:44:05 +02:00
|
|
|
const fs = require("fs-extra")
|
2020-10-09 11:00:57 +02:00
|
|
|
const { join, resolve } = require("../../utilities/centralPath")
|
2020-07-14 22:07:53 +02:00
|
|
|
const { promisify } = require("util")
|
|
|
|
const chmodr = require("chmodr")
|
2020-10-21 16:28:30 +02:00
|
|
|
const packageJson = require("../../../package.json")
|
2020-10-29 11:21:06 +01:00
|
|
|
const { createLinkView } = require("../../db/linkedRows")
|
|
|
|
const { downloadTemplate } = require("../../utilities/templates")
|
2020-11-02 15:53:51 +01:00
|
|
|
const {
|
|
|
|
generateAppID,
|
|
|
|
DocumentTypes,
|
|
|
|
SEPARATOR,
|
|
|
|
getPageParams,
|
|
|
|
generatePageID,
|
2020-11-03 18:42:54 +01:00
|
|
|
generateScreenID,
|
2020-11-02 15:53:51 +01:00
|
|
|
} = require("../../db/utils")
|
2020-07-14 22:10:51 +02:00
|
|
|
const {
|
|
|
|
downloadExtractComponentLibraries,
|
|
|
|
} = require("../../utilities/createAppPackage")
|
2020-11-03 18:42:54 +01:00
|
|
|
const { MAIN, UNAUTHENTICATED, PageTypes } = require("../../constants/pages")
|
2020-11-03 17:27:28 +01:00
|
|
|
const { HOME_SCREEN } = require("../../constants/screens")
|
2020-11-03 18:42:54 +01:00
|
|
|
const { cloneDeep } = require("lodash/fp")
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-10-29 11:21:06 +01:00
|
|
|
async function createInstance(template) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const appId = generateAppID()
|
2020-10-29 11:21:06 +01:00
|
|
|
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(appId)
|
2020-10-29 11:21:06 +01:00
|
|
|
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
|
2020-10-29 11:28:27 +01:00
|
|
|
await createLinkView(appId)
|
2020-10-29 11:21:06 +01:00
|
|
|
|
|
|
|
// 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."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:28:27 +01:00
|
|
|
return { _id: appId }
|
2020-10-29 11:21:06 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-10-28 21:35:06 +01:00
|
|
|
let allDbs = await CouchDB.allDbs()
|
|
|
|
const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
|
2020-10-29 12:48:01 +01:00
|
|
|
const apps = appDbNames.map(db => new CouchDB(db).get(db))
|
2020-10-28 21:35:06 +01:00
|
|
|
if (apps.length === 0) {
|
|
|
|
ctx.body = []
|
|
|
|
} else {
|
|
|
|
ctx.body = await Promise.all(apps)
|
|
|
|
}
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.fetchAppPackage = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const application = await db.get(ctx.params.appId)
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-11-03 18:42:54 +01:00
|
|
|
let pages = await db.allDocs(
|
2020-11-02 15:53:51 +01:00
|
|
|
getPageParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2020-11-03 18:42:54 +01:00
|
|
|
pages = pages.rows.map(row => row.doc)
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-11-03 18:42:54 +01:00
|
|
|
const mainPage = pages.filter(page => page.name === PageTypes.MAIN)[0]
|
|
|
|
const unauthPage = pages.filter(
|
|
|
|
page => page.name === PageTypes.UNAUTHENTICATED
|
|
|
|
)[0]
|
2020-11-02 15:53:51 +01:00
|
|
|
ctx.body = {
|
|
|
|
application,
|
2020-11-03 18:42:54 +01:00
|
|
|
pages: {
|
|
|
|
main: mainPage,
|
|
|
|
unauthenticated: unauthPage,
|
|
|
|
},
|
2020-11-02 15:53:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:46:08 +01:00
|
|
|
await setBuilderToken(ctx, ctx.params.appId, application.version)
|
2020-04-20 17:17:11 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.create = async function(ctx) {
|
2020-10-29 11:21:06 +01:00
|
|
|
const instance = await createInstance(ctx.request.body.template)
|
2020-10-29 11:28:27 +01:00
|
|
|
const appId = instance._id
|
2020-11-05 13:43:03 +01:00
|
|
|
const version = packageJson.version
|
2020-05-14 16:12:30 +02:00
|
|
|
const newApplication = {
|
2020-10-29 11:28:27 +01:00
|
|
|
_id: appId,
|
2020-04-20 17:17:11 +02:00
|
|
|
type: "app",
|
2020-10-21 16:28:30 +02:00
|
|
|
version: packageJson.version,
|
2020-08-14 12:21:52 +02:00
|
|
|
componentLibraries: ["@budibase/standard-components"],
|
2020-05-26 17:29:16 +02:00
|
|
|
name: ctx.request.body.name,
|
2020-09-25 15:47:42 +02:00
|
|
|
template: ctx.request.body.template,
|
2020-10-29 11:21:06 +01:00
|
|
|
instance: instance,
|
2020-04-09 17:53:48 +02:00
|
|
|
}
|
2020-10-29 11:28:27 +01:00
|
|
|
const instanceDb = new CouchDB(appId)
|
2020-10-28 21:35:06 +01:00
|
|
|
await instanceDb.put(newApplication)
|
2020-05-14 16:12:30 +02:00
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
if (env.NODE_ENV !== "jest") {
|
2020-05-26 17:29:16 +02:00
|
|
|
const newAppFolder = await createEmptyAppPackage(ctx, newApplication)
|
2020-07-14 22:07:53 +02:00
|
|
|
await downloadExtractComponentLibraries(newAppFolder)
|
2020-05-26 17:29:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 13:43:03 +01:00
|
|
|
await setBuilderToken(ctx, appId, version)
|
2020-06-10 22:39:30 +02:00
|
|
|
ctx.status = 200
|
2020-05-14 16:12:30 +02:00
|
|
|
ctx.body = newApplication
|
|
|
|
ctx.message = `Application ${ctx.request.body.name} created successfully`
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.update = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const application = await db.get(ctx.params.appId)
|
2020-06-29 17:18:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:44:05 +02:00
|
|
|
exports.delete = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const app = await db.get(ctx.params.appId)
|
2020-10-28 21:35:06 +01:00
|
|
|
const result = await db.destroy()
|
2020-10-13 17:31:14 +02:00
|
|
|
|
|
|
|
// remove top level directory
|
2020-11-04 13:36:38 +01:00
|
|
|
// TODO: look into why this isn't a callback
|
2020-10-29 11:28:27 +01:00
|
|
|
await fs.rmdir(join(budibaseAppsDir(), ctx.params.appId), {
|
2020-07-07 14:44:05 +02:00
|
|
|
recursive: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = `Application ${app.name} deleted successfully.`
|
|
|
|
ctx.body = result
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:29:16 +02:00
|
|
|
const createEmptyAppPackage = async (ctx, app) => {
|
|
|
|
const templateFolder = resolve(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"utilities",
|
|
|
|
"appDirectoryTemplate"
|
|
|
|
)
|
|
|
|
|
2020-06-03 22:23:56 +02:00
|
|
|
const appsFolder = budibaseAppsDir()
|
2020-10-07 10:58:32 +02:00
|
|
|
const newAppFolder = resolve(appsFolder, app._id)
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2020-11-02 15:53:51 +01:00
|
|
|
const db = new CouchDB(app._id)
|
|
|
|
|
2020-10-13 18:02:59 +02:00
|
|
|
if (existsSync(newAppFolder)) {
|
2020-05-26 17:29:16 +02:00
|
|
|
ctx.throw(400, "App folder already exists for this application")
|
|
|
|
}
|
|
|
|
|
2020-11-03 17:27:28 +01:00
|
|
|
// await fs.ensureDir(join(newAppFolder, "pages", "main", "screens"), 0o777)
|
|
|
|
// await fs.ensureDir(
|
|
|
|
// join(newAppFolder, "pages", "unauthenticated", "screens"),
|
|
|
|
// 0o777
|
|
|
|
// )
|
|
|
|
|
2020-05-26 17:29:16 +02:00
|
|
|
await copy(templateFolder, newAppFolder)
|
|
|
|
|
2020-07-14 08:02:46 +02:00
|
|
|
// 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 :(
|
2020-07-14 22:07:53 +02:00
|
|
|
const chmodrPromise = promisify(chmodr)
|
|
|
|
await chmodrPromise(newAppFolder, 0o777)
|
2020-07-14 08:02:46 +02:00
|
|
|
|
2020-06-03 16:43:37 +02:00
|
|
|
await updateJsonFile(join(appsFolder, app._id, "package.json"), {
|
|
|
|
name: npmFriendlyAppName(app.name),
|
|
|
|
})
|
2020-06-22 22:16:51 +02:00
|
|
|
|
2020-09-28 18:04:08 +02:00
|
|
|
// if this app is being created from a template,
|
|
|
|
// copy the frontend page definition files from
|
|
|
|
// the template directory.
|
2020-09-25 15:47:42 +02:00
|
|
|
if (app.template) {
|
2020-09-28 18:04:08 +02:00
|
|
|
const templatePageDefinitions = join(
|
|
|
|
appsFolder,
|
|
|
|
"templates",
|
|
|
|
app.template.key,
|
|
|
|
"pages"
|
|
|
|
)
|
2020-11-02 15:53:51 +01:00
|
|
|
// TODO: write the template page JSON to couch
|
|
|
|
// iterate over the pages and write them to the db
|
2020-09-25 15:47:42 +02:00
|
|
|
await copy(templatePageDefinitions, join(appsFolder, app._id, "pages"))
|
|
|
|
}
|
|
|
|
|
2020-11-03 18:42:54 +01:00
|
|
|
// const mainJson = await updateJsonFile(
|
|
|
|
// join(appsFolder, app._id, "pages", "main", "page.json"),
|
|
|
|
// app
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// mainJson._id = generatePageID()
|
|
|
|
// await db.put(mainJson)
|
2020-06-22 22:16:51 +02:00
|
|
|
|
2020-11-03 18:42:54 +01:00
|
|
|
// const unauthenticatedJson = await updateJsonFile(
|
|
|
|
// join(appsFolder, app._id, "pages", "unauthenticated", "page.json"),
|
|
|
|
// app
|
|
|
|
// )
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2020-11-02 15:53:51 +01:00
|
|
|
// Write to couch
|
2020-11-03 18:42:54 +01:00
|
|
|
// unauthenticatedJson._id = generatePageID()
|
|
|
|
// await db.put(unauthenticatedJson)
|
|
|
|
|
|
|
|
const mainPage = cloneDeep(MAIN)
|
|
|
|
mainPage._id = generatePageID()
|
|
|
|
mainPage.title = app.name
|
|
|
|
const unauthPage = cloneDeep(UNAUTHENTICATED)
|
|
|
|
unauthPage._id = generatePageID()
|
2020-11-04 17:13:50 +01:00
|
|
|
// TODO: fix - handlebars etc
|
2020-11-03 18:42:54 +01:00
|
|
|
unauthPage.title = app.name
|
2020-11-04 17:13:50 +01:00
|
|
|
unauthPage.props._children[0]._children.title = `Log in to ${app.name}`
|
2020-11-03 18:42:54 +01:00
|
|
|
const homeScreen = cloneDeep(HOME_SCREEN)
|
|
|
|
homeScreen._id = generateScreenID(mainPage._id)
|
2020-11-05 13:43:03 +01:00
|
|
|
const response = await db.bulkDocs([mainPage, unauthPage, homeScreen])
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-11-03 18:42:54 +01:00
|
|
|
await buildPage(app._id, "main", {
|
|
|
|
page: mainPage,
|
|
|
|
screens: [homeScreen],
|
|
|
|
})
|
2020-11-02 15:53:51 +01:00
|
|
|
await buildPage(app._id, "unauthenticated", {
|
2020-11-03 18:42:54 +01:00
|
|
|
page: unauthPage,
|
|
|
|
screens: [],
|
2020-06-22 22:16:51 +02:00
|
|
|
})
|
|
|
|
|
2020-05-26 17:29:16 +02:00
|
|
|
return newAppFolder
|
|
|
|
}
|
|
|
|
|
2020-11-03 18:42:54 +01: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-07-09 15:47:54 +02:00
|
|
|
|
2020-06-03 16:43:37 +02:00
|
|
|
const updateJsonFile = async (filePath, app) => {
|
|
|
|
const json = await readFile(filePath, "utf8")
|
|
|
|
const newJson = sqrl.Render(json, app)
|
|
|
|
await writeFile(filePath, newJson, "utf8")
|
2020-06-22 22:16:51 +02:00
|
|
|
return JSON.parse(newJson)
|
2020-06-03 16:43:37 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:29:16 +02:00
|
|
|
const npmFriendlyAppName = name =>
|
|
|
|
name
|
|
|
|
.replace(/_/g, "")
|
|
|
|
.replace(/./g, "")
|
|
|
|
.replace(/ /g, "")
|
|
|
|
.toLowerCase()
|