1
0
Fork 0
mirror of synced 2024-06-28 11:00:55 +12:00
budibase/packages/server/api/controllers/component.js
2020-05-06 10:33:30 +01:00

40 lines
1.1 KiB
JavaScript

const CouchDB = require("../../db");
const { homedir } = require("os");
const { resolve, join } = require("path");
const isDev = process.env.NODE_ENV !== "production";
exports.fetchAppComponentDefinitions = async function(ctx) {
const db = new CouchDB(`client-${ctx.params.clientId}`);
const app = await db.get(ctx.params.appId)
const componentDefinitions = app.componentLibraries.reduce((acc, componentLibrary) => {
let appDirectory = resolve(homedir(), ".budibase", ctx.params.appId, "node_modules");
if (isDev) {
appDirectory = "/tmp/.budibase";
}
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;
};