1
0
Fork 0
mirror of synced 2024-06-28 02:50:50 +12:00
budibase/packages/server/src/api/controllers/component.js
Martin McKeaveney 15c35ac0ca fix typo
2020-07-27 17:48:35 +01:00

56 lines
1.4 KiB
JavaScript

const CouchDB = require("../../db")
const ClientDb = require("../../db/clientDb")
const { resolve, join } = require("path")
const {
budibaseTempDir,
budibaseAppsDir,
} = require("../../utilities/budibaseDir")
exports.fetchAppComponentDefinitions = async function(ctx) {
const masterDb = new CouchDB("client_app_lookup")
const { clientId } = await masterDb.get(ctx.params.appId)
const db = new CouchDB(ClientDb.name(clientId))
const app = await db.get(ctx.params.appId)
const componentDefinitions = app.componentLibraries.reduce(
(acc, componentLibrary) => {
let appDirectory = resolve(
budibaseAppsDir(),
ctx.params.appId,
"node_modules"
)
if (ctx.isDev) {
appDirectory = budibaseTempDir()
}
const componentJson = require(join(
appDirectory,
componentLibrary,
ctx.isDev ? "" : "package",
"components.json"
))
const result = {}
// map over the components.json and add the library identifier as a key
// button -> @budibase/standard-components/button
for (key in componentJson) {
const fullComponentName = `${componentLibrary}/${key}`
result[fullComponentName] = {
_component: fullComponentName,
...componentJson[key],
}
}
return {
...acc,
...result,
}
},
{}
)
ctx.body = componentDefinitions
}