budibase/packages/server/utilities/builder/index.js

169 lines
4.5 KiB
JavaScript
Raw Normal View History

const {
appPackageFolder,
appsFolder
2019-09-06 14:04:23 +02:00
} = require("../createAppPackage");
const {
readJSON, writeJSON, readdir,
stat, ensureDir, rename,
unlink, rmdir
2019-07-26 18:08:59 +02:00
} = require("fs-extra");
const {
join,dirname
} = require("path");
2019-09-10 07:14:45 +02:00
const { $ } = require("@budibase/core").common;
const {
2019-09-09 06:24:14 +02:00
keyBy
} = require("lodash/fp");
const {merge} = require("lodash");
2019-07-13 11:35:57 +02:00
2019-09-06 14:04:23 +02:00
const { componentLibraryInfo } = require("./componentLibraryInfo");
2019-09-07 07:50:35 +02:00
const savePackage = require("./savePackage");
const buildApp = require("./buildApp");
2019-09-07 07:50:35 +02:00
module.exports.savePackage = savePackage;
2019-09-06 14:04:23 +02:00
const getPages = async (appPath) => await readJSON(`${appPath}/pages.json`);
const getAppDefinition = async (appPath) => await readJSON(`${appPath}/appDefinition.json`);
2019-07-13 11:35:57 +02:00
module.exports.getPackageForBuilder = async (config, appname) => {
const appPath = appPackageFolder(config, appname);
const pages = await getPages(appPath);
2019-07-13 11:35:57 +02:00
return ({
appDefinition: await getAppDefinition(appPath),
2019-07-13 11:35:57 +02:00
2019-07-26 18:08:59 +02:00
accessLevels: await readJSON(`${appPath}/access_levels.json`),
pages,
rootComponents: await getRootComponents(appPath, pages),
2019-07-28 09:03:11 +02:00
derivedComponents: keyBy("name")(
await fetchDerivedComponents(appPath))
2019-09-11 07:08:39 +02:00
});
2019-07-13 11:35:57 +02:00
}
module.exports.getApps = async (config) =>
await readdir(appsFolder(config));
2019-07-26 18:08:59 +02:00
const componentPath = (appPath, name) =>
join(appPath, "components", name + ".json");
module.exports.saveDerivedComponent = async (config, appname, component) => {
const appPath = appPackageFolder(config, appname);
2019-09-09 06:24:14 +02:00
const compPath = componentPath(appPath, component.name);
await ensureDir(dirname(compPath));
2019-07-26 18:08:59 +02:00
await writeJSON(
2019-09-09 06:24:14 +02:00
compPath,
2019-07-26 18:08:59 +02:00
component,
2019-08-19 22:18:23 +02:00
{encoding:"utf8", flag:"w", spaces:2});
2019-07-26 18:08:59 +02:00
}
module.exports.renameDerivedComponent = async (config, appname, oldName, newName) => {
const appPath = appPackageFolder(config, appname);
const oldComponentPath = componentPath(
appPath, oldName);
2019-07-27 08:31:31 +02:00
const newComponentPath = componentPath(
2019-07-26 18:08:59 +02:00
appPath, newName);
await ensureDir(dirname(newComponentPath));
await rename(
oldComponentPath,
newComponentPath);
}
module.exports.deleteDerivedComponent = async (config, appname, name) => {
const appPath = appPackageFolder(config, appname);
2019-07-27 08:43:03 +02:00
const componentFile = componentPath(appPath, name);
await unlink(componentFile);
const dir = dirname(componentFile);
if((await readdir(dir)).length === 0) {
await rmdir(dir);
}
2019-07-26 18:08:59 +02:00
}
2019-09-09 06:24:14 +02:00
module.exports.componentLibraryInfo = async (config, appname, lib) => {
const appPath = appPackageFolder(config, appname);
return await componentLibraryInfo(appPath, lib);
};
2019-08-19 22:18:23 +02:00
const getRootComponents = async (appPath, pages ,lib) => {
let libs;
if(!lib) {
2019-07-26 18:08:59 +02:00
pages = pages || await readJSON(
`${appPath}/pages.json`);
if(!pages.componentLibraries) return [];
libs = pages.componentLibraries;
} else {
libs = [lib];
}
const components = {};
2019-10-07 07:03:41 +02:00
const generators = {};
for(let l of libs) {
2019-09-09 06:24:14 +02:00
const info = await componentLibraryInfo(appPath, l);
merge(components, info.components);
2019-10-07 07:03:41 +02:00
merge(generators, info.generators);
}
2019-09-12 07:10:50 +02:00
if(components._lib) delete components._lib;
2019-10-07 07:03:41 +02:00
if(components._generators) delete components._generators;
2019-09-12 07:10:50 +02:00
2019-10-07 07:03:41 +02:00
return {components, generators};
}
const fetchDerivedComponents = async (appPath, relativePath = "") => {
const currentDir = join(appPath, "components", relativePath);
const contents = await readdir(currentDir);
const components = [];
for(let item of contents) {
const itemRelativePath = join(relativePath, item);
const itemFullPath = join(currentDir, item);
const stats = await stat(itemFullPath);
if(stats.isFile()) {
if(!item.endsWith(".json")) continue;
2019-07-26 18:08:59 +02:00
const component =
await readJSON(itemFullPath);
2019-07-28 09:03:11 +02:00
component.name = itemRelativePath
.substring(0, itemRelativePath.length - 5)
.replace(/\\/g, "/");
component.props = component.props || {};
components.push(component);
} else {
const childComponents = await fetchDerivedComponents(
appPath, join(relativePath, item)
);
for(let c of childComponents) {
components.push(c);
}
}
}
return components;
}
module.exports.getRootComponents = getRootComponents;