1
0
Fork 0
mirror of synced 2024-06-30 12:00:31 +12:00
budibase/packages/server/src/api/controllers/component.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const { resolve, join } = require("path")
2020-05-12 02:42:42 +12:00
const {
budibaseTempDir,
budibaseAppsDir,
} = require("../../utilities/budibaseDir")
exports.fetchAppComponentDefinitions = async function(ctx) {
2020-05-07 21:53:34 +12:00
const db = new CouchDB(`client-${ctx.params.clientId}`)
const app = await db.get(ctx.params.appId)
2020-05-07 21:53:34 +12:00
const componentDefinitions = app.componentLibraries.reduce(
(acc, componentLibrary) => {
let appDirectory = resolve(
2020-05-12 02:42:42 +12:00
budibaseAppsDir(),
2020-05-07 21:53:34 +12:00
ctx.params.appId,
"node_modules"
)
2020-05-12 03:01:02 +12:00
if (ctx.isDev) {
2020-05-12 02:42:42 +12:00
appDirectory = budibaseTempDir()
2020-05-07 21:53:34 +12:00
}
const componentJson = require(join(
appDirectory,
componentLibrary,
"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
}