2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
2020-05-18 07:40:29 +02:00
|
|
|
const ClientDb = require("../../db/clientDb")
|
2020-06-22 22:16:51 +02:00
|
|
|
const { getPackageForBuilder, buildPage } = require("../../utilities/builder")
|
2020-05-18 17:22:09 +02:00
|
|
|
const newid = require("../../db/newid")
|
2020-05-14 16:12:30 +02:00
|
|
|
const env = require("../../environment")
|
2020-05-26 17:29:16 +02:00
|
|
|
const instanceController = require("./instance")
|
|
|
|
const { resolve, join } = require("path")
|
2020-06-03 16:43:37 +02:00
|
|
|
const { copy, exists, 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-07-14 22:07:53 +02:00
|
|
|
const { promisify } = require("util")
|
|
|
|
const chmodr = require("chmodr")
|
2020-07-14 22:10:51 +02:00
|
|
|
const {
|
|
|
|
downloadExtractComponentLibraries,
|
|
|
|
} = require("../../utilities/createAppPackage")
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-06-10 22:39:30 +02:00
|
|
|
const db = new CouchDB(ClientDb.name(getClientId(ctx)))
|
2020-05-18 07:40:29 +02:00
|
|
|
const body = await db.query("client/by_type", {
|
2020-04-09 17:53:48 +02:00
|
|
|
include_docs: true,
|
2020-05-07 11:53:34 +02:00
|
|
|
key: ["app"],
|
|
|
|
})
|
2020-04-08 17:57:27 +02:00
|
|
|
|
2020-05-07 11:53:34 +02:00
|
|
|
ctx.body = body.rows.map(row => row.doc)
|
|
|
|
}
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.fetchAppPackage = async function(ctx) {
|
2020-06-10 22:39:30 +02:00
|
|
|
const clientId = await lookupClientId(ctx.params.applicationId)
|
|
|
|
const db = new CouchDB(ClientDb.name(clientId))
|
2020-05-18 07:40:29 +02:00
|
|
|
const application = await db.get(ctx.params.applicationId)
|
2020-05-07 11:53:34 +02:00
|
|
|
ctx.body = await getPackageForBuilder(ctx.config, application)
|
2020-06-18 17:59:31 +02:00
|
|
|
/*
|
|
|
|
instance is hardcoded now - this can only change when we move
|
|
|
|
pages and screens into the database
|
|
|
|
*/
|
|
|
|
const devInstance = application.instances.find(
|
|
|
|
i => i.name === `dev-${clientId}`
|
|
|
|
)
|
|
|
|
setBuilderToken(ctx, ctx.params.applicationId, devInstance._id)
|
2020-04-20 17:17:11 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.create = async function(ctx) {
|
2020-06-10 22:39:30 +02:00
|
|
|
const clientId =
|
|
|
|
(ctx.request.body && ctx.request.body.clientId) || env.CLIENT_ID
|
|
|
|
|
|
|
|
if (!clientId) {
|
|
|
|
ctx.throw(400, "ClientId not suplied")
|
|
|
|
}
|
|
|
|
const appId = newid()
|
|
|
|
// insert an appId -> clientId lookup
|
2020-06-24 00:26:54 +02:00
|
|
|
const masterDb = new CouchDB("client_app_lookup")
|
2020-06-18 17:59:31 +02:00
|
|
|
|
2020-06-10 22:39:30 +02:00
|
|
|
await masterDb.put({
|
|
|
|
_id: appId,
|
|
|
|
clientId,
|
|
|
|
})
|
2020-06-18 17:59:31 +02:00
|
|
|
|
2020-06-10 22:39:30 +02:00
|
|
|
const db = new CouchDB(ClientDb.name(clientId))
|
2020-05-14 16:12:30 +02:00
|
|
|
|
|
|
|
const newApplication = {
|
2020-06-10 22:39:30 +02:00
|
|
|
_id: appId,
|
2020-04-20 17:17:11 +02:00
|
|
|
type: "app",
|
|
|
|
instances: [],
|
2020-05-04 19:07:03 +02:00
|
|
|
userInstanceMap: {},
|
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-04-09 17:53:48 +02:00
|
|
|
}
|
2020-05-14 16:12:30 +02:00
|
|
|
|
2020-06-18 17:59:31 +02:00
|
|
|
const { rev } = await db.put(newApplication)
|
2020-05-14 16:12:30 +02:00
|
|
|
newApplication._rev = rev
|
2020-05-26 17:29:16 +02:00
|
|
|
const createInstCtx = {
|
2020-06-18 17:59:31 +02:00
|
|
|
user: {
|
|
|
|
appId: newApplication._id,
|
2020-05-26 17:29:16 +02:00
|
|
|
},
|
|
|
|
request: {
|
2020-09-25 15:47:42 +02:00
|
|
|
body: {
|
|
|
|
name: `dev-${clientId}`,
|
|
|
|
template: ctx.request.body.template,
|
|
|
|
},
|
2020-05-26 17:29:16 +02:00
|
|
|
},
|
|
|
|
}
|
2020-09-25 15:47:42 +02:00
|
|
|
// TODO: pass template into here from create InstCtx
|
2020-05-26 17:29:16 +02:00
|
|
|
await instanceController.create(createInstCtx)
|
2020-06-18 17:59:31 +02:00
|
|
|
newApplication.instances.push(createInstCtx.body)
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2020-09-25 15:47:42 +02:00
|
|
|
// TODO: if template is passed, create the app package from the template and seed the instance database
|
2020-07-15 14:42:12 +02:00
|
|
|
if (process.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-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-06-29 17:18:43 +02:00
|
|
|
const clientId = await lookupClientId(ctx.params.applicationId)
|
|
|
|
const db = new CouchDB(ClientDb.name(clientId))
|
|
|
|
const application = await db.get(ctx.params.applicationId)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const db = new CouchDB(ClientDb.name(getClientId(ctx)))
|
|
|
|
const app = await db.get(ctx.params.applicationId)
|
|
|
|
const result = await db.remove(app)
|
|
|
|
await fs.rmdir(`${budibaseAppsDir()}/${ctx.params.applicationId}`, {
|
|
|
|
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-05-26 17:29:16 +02:00
|
|
|
const newAppFolder = resolve(appsFolder, app._id)
|
|
|
|
|
|
|
|
if (await exists(newAppFolder)) {
|
|
|
|
ctx.throw(400, "App folder already exists for this application")
|
|
|
|
}
|
|
|
|
|
2020-07-14 09:21:22 +02:00
|
|
|
await fs.ensureDir(join(newAppFolder, "pages", "main", "screens"), 0o777)
|
|
|
|
await fs.ensureDir(
|
|
|
|
join(newAppFolder, "pages", "unauthenticated", "screens"),
|
|
|
|
0o777
|
|
|
|
)
|
2020-07-14 09:13:05 +02:00
|
|
|
|
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-25 15:47:42 +02:00
|
|
|
// Copy the frontend page definition files from the template directory
|
|
|
|
// if this app is being created from a template.
|
|
|
|
if (app.template) {
|
|
|
|
const templatePageDefinitions = join(appsFolder, app.template.name, "pages")
|
|
|
|
await copy(templatePageDefinitions, join(appsFolder, app._id, "pages"))
|
|
|
|
}
|
|
|
|
|
2020-06-22 22:16:51 +02:00
|
|
|
const mainJson = await updateJsonFile(
|
2020-06-03 16:43:37 +02:00
|
|
|
join(appsFolder, app._id, "pages", "main", "page.json"),
|
|
|
|
app
|
|
|
|
)
|
2020-06-22 22:16:51 +02:00
|
|
|
|
2020-07-09 16:05:56 +02:00
|
|
|
await buildPage(ctx.config, app._id, "main", {
|
2020-07-09 15:47:54 +02:00
|
|
|
page: mainJson,
|
|
|
|
screens: await loadScreens(newAppFolder, "main"),
|
|
|
|
})
|
2020-06-22 22:16:51 +02:00
|
|
|
|
|
|
|
const unauthenticatedJson = await updateJsonFile(
|
2020-06-03 16:43:37 +02:00
|
|
|
join(appsFolder, app._id, "pages", "unauthenticated", "page.json"),
|
|
|
|
app
|
|
|
|
)
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2020-06-22 22:16:51 +02:00
|
|
|
await buildPage(ctx.config, app._id, "unauthenticated", {
|
|
|
|
page: unauthenticatedJson,
|
2020-07-09 15:47:54 +02:00
|
|
|
screens: await loadScreens(newAppFolder, "unauthenticated"),
|
2020-06-22 22:16:51 +02:00
|
|
|
})
|
|
|
|
|
2020-05-26 17:29:16 +02:00
|
|
|
return newAppFolder
|
|
|
|
}
|
|
|
|
|
2020-07-09 15:47:54 +02: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-10 22:39:30 +02:00
|
|
|
const lookupClientId = async appId => {
|
2020-06-24 00:26:54 +02:00
|
|
|
const masterDb = new CouchDB("client_app_lookup")
|
2020-06-10 22:39:30 +02:00
|
|
|
const { clientId } = await masterDb.get(appId)
|
|
|
|
return clientId
|
|
|
|
}
|
|
|
|
|
|
|
|
const getClientId = ctx => {
|
|
|
|
const clientId =
|
|
|
|
(ctx.request.body && ctx.request.body.clientId) ||
|
|
|
|
(ctx.query && ctx.query.clientId) ||
|
|
|
|
env.CLIENT_ID
|
|
|
|
|
|
|
|
if (!clientId) {
|
2020-08-05 16:18:28 +02:00
|
|
|
ctx.throw(400, "ClientId not supplied")
|
2020-06-10 22:39:30 +02:00
|
|
|
}
|
|
|
|
return clientId
|
|
|
|
}
|
|
|
|
|
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()
|