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

193 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const ClientDb = require("../../db/clientDb")
const { getPackageForBuilder, buildPage } = require("../../utilities/builder")
2020-05-19 03:22:09 +12:00
const newid = require("../../db/newid")
2020-05-15 02:12:30 +12:00
const env = require("../../environment")
const instanceController = require("./instance")
const { resolve, join } = require("path")
2020-06-04 02:43:37 +12:00
const { copy, exists, readFile, writeFile } = require("fs-extra")
2020-06-04 08:23:56 +12:00
const { budibaseAppsDir } = require("../../utilities/budibaseDir")
const { exec } = require("child_process")
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")
2020-04-08 04:25:09 +12:00
2020-06-30 03:49:16 +12:00
exports.fetch = async function(ctx) {
const db = new CouchDB(ClientDb.name(getClientId(ctx)))
const body = await db.query("client/by_type", {
2020-04-10 03:53:48 +12:00
include_docs: true,
2020-05-07 21:53:34 +12:00
key: ["app"],
})
2020-04-09 03:57:27 +12:00
2020-05-07 21:53:34 +12:00
ctx.body = body.rows.map(row => row.doc)
}
2020-04-08 04:25:09 +12:00
2020-06-30 03:49:16 +12:00
exports.fetchAppPackage = async function(ctx) {
const clientId = await lookupClientId(ctx.params.applicationId)
const db = new CouchDB(ClientDb.name(clientId))
const application = await db.get(ctx.params.applicationId)
2020-05-07 21:53:34 +12:00
ctx.body = await getPackageForBuilder(ctx.config, application)
2020-06-19 03:59:31 +12: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-06-30 03:49:16 +12:00
exports.create = async function(ctx) {
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
const masterDb = new CouchDB("client_app_lookup")
2020-06-19 03:59:31 +12:00
await masterDb.put({
_id: appId,
clientId,
})
2020-06-19 03:59:31 +12:00
const db = new CouchDB(ClientDb.name(clientId))
2020-05-15 02:12:30 +12:00
const newApplication = {
_id: appId,
type: "app",
instances: [],
userInstanceMap: {},
componentLibraries: [
"@budibase/standard-components",
2020-05-07 21:53:34 +12:00
"@budibase/materialdesign-components",
],
name: ctx.request.body.name,
description: ctx.request.body.description,
2020-04-10 03:53:48 +12:00
}
2020-05-15 02:12:30 +12:00
2020-06-19 03:59:31 +12:00
const { rev } = await db.put(newApplication)
2020-05-15 02:12:30 +12:00
newApplication._rev = rev
const createInstCtx = {
2020-06-19 03:59:31 +12:00
user: {
appId: newApplication._id,
},
request: {
body: { name: `dev-${clientId}` },
},
}
await instanceController.create(createInstCtx)
2020-06-19 03:59:31 +12:00
newApplication.instances.push(createInstCtx.body)
2020-05-27 21:54:34 +12:00
if (ctx.isDev) {
const newAppFolder = await createEmptyAppPackage(ctx, newApplication)
await runNpmInstall(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 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
}
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)
if (await exists(newAppFolder)) {
ctx.throw(400, "App folder already exists for this application")
}
await copy(templateFolder, newAppFolder)
2020-06-04 02:43:37 +12:00
await updateJsonFile(join(appsFolder, app._id, "package.json"), {
name: npmFriendlyAppName(app.name),
})
const mainJson = await updateJsonFile(
2020-06-04 02:43:37 +12:00
join(appsFolder, app._id, "pages", "main", "page.json"),
app
)
await buildPage(ctx.config, app._id, "main", { page: mainJson })
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,
})
return newAppFolder
}
const lookupClientId = async appId => {
const masterDb = new CouchDB("client_app_lookup")
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) {
ctx.throw(400, "ClientId not suplied")
}
return clientId
}
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 runNpmInstall = async newAppFolder => {
return new Promise((resolve, reject) => {
const cmd = `cd ${newAppFolder} && npm install`
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(error)
}
resolve(stdout ? stdout : stderr)
})
})
}
const npmFriendlyAppName = name =>
name
.replace(/_/g, "")
.replace(/./g, "")
.replace(/ /g, "")
.toLowerCase()