1
0
Fork 0
mirror of synced 2024-08-04 21:02:01 +12:00
budibase/packages/server/utilities/builder/index.js

193 lines
4.8 KiB
JavaScript
Raw Normal View History

const {
appPackageFolder,
appsFolder
2019-09-07 00:04:23 +12:00
} = require("../createAppPackage");
const {
2019-07-27 04:08:59 +12:00
readJSON,
writeJSON,
readdir,
2019-07-27 04:08:59 +12:00
stat,
ensureDir,
rename,
2019-07-27 18:43:03 +12:00
unlink,
rmdir
2019-07-27 04:08:59 +12:00
} = require("fs-extra");
const {
2019-07-27 04:08:59 +12:00
join,
dirname
} = require("path");
const { $ } = require("budibase-core").common;
const {
keys,
reduce,
2019-08-20 08:18:23 +12:00
keyBy,
filter
} = require("lodash/fp");
const {merge} = require("lodash");
2019-07-13 21:35:57 +12:00
2019-09-07 00:04:23 +12:00
const { componentLibraryInfo } = require("./componentLibraryInfo");
2019-07-13 21:35:57 +12:00
module.exports.getPackageForBuilder = async (config, appname) => {
const appPath = appPackageFolder(config, appname);
2019-07-27 04:08:59 +12:00
const pages = await readJSON(`${appPath}/pages.json`);
2019-07-13 21:35:57 +12:00
return ({
2019-07-27 04:08:59 +12:00
appDefinition: await readJSON(`${appPath}/appDefinition.json`),
2019-07-13 21:35:57 +12:00
2019-07-27 04:08:59 +12:00
accessLevels: await readJSON(`${appPath}/access_levels.json`),
pages,
rootComponents: await getRootComponents(appPath, pages),
2019-07-28 19:03:11 +12:00
derivedComponents: keyBy("name")(
await fetchDerivedComponents(appPath))
2019-07-13 21:35:57 +12:00
})
}
module.exports.savePackage = async (config, appname, pkg) => {
const appPath = appPackageFolder(config, appname);
2019-07-27 04:08:59 +12:00
await writeJSON(
2019-07-13 21:35:57 +12:00
`${appPath}/appDefinition.json`,
2019-08-20 08:18:23 +12:00
pkg.appDefinition,
{spaces:2});
2019-07-13 21:35:57 +12:00
2019-07-27 04:08:59 +12:00
await writeJSON(
2019-07-13 21:35:57 +12:00
`${appPath}/access_levels.json`,
2019-08-20 08:18:23 +12:00
pkg.accessLevels,
{spaces:2});
2019-07-13 21:35:57 +12:00
2019-07-27 04:08:59 +12:00
await writeJSON(
`${appPath}/pages.json`,
2019-08-20 08:18:23 +12:00
pkg.pages,
{spaces:2});
}
module.exports.getApps = async (config) =>
await readdir(appsFolder(config));
2019-07-27 04:08:59 +12:00
const componentPath = (appPath, name) =>
join(appPath, "components", name + ".json");
module.exports.saveDerivedComponent = async (config, appname, component) => {
const appPath = appPackageFolder(config, appname);
await writeJSON(
2019-07-28 19:03:11 +12:00
componentPath(appPath, component.name),
2019-07-27 04:08:59 +12:00
component,
2019-08-20 08:18:23 +12:00
{encoding:"utf8", flag:"w", spaces:2});
2019-07-27 04:08:59 +12:00
}
module.exports.renameDerivedComponent = async (config, appname, oldName, newName) => {
const appPath = appPackageFolder(config, appname);
const oldComponentPath = componentPath(
appPath, oldName);
2019-07-27 18:31:31 +12:00
const newComponentPath = componentPath(
2019-07-27 04:08:59 +12: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 18:43:03 +12:00
const componentFile = componentPath(appPath, name);
await unlink(componentFile);
const dir = dirname(componentFile);
if((await readdir(dir)).length === 0) {
await rmdir(dir);
}
2019-07-27 04:08:59 +12:00
}
2019-09-07 00:04:23 +12:00
module.exports.componentLibraryInfo = componentLibraryInfo;
2019-08-20 08:18:23 +12:00
const getRootComponents = async (appPath, pages ,lib) => {
const componentsInLibrary = async (libname) => {
2019-09-07 00:04:23 +12:00
const { components } = await componentLibraryInfo(appPath, libname);
return $(components, [
keys,
2019-08-20 08:18:23 +12:00
filter(k => k !== "_lib"),
reduce((obj, k) => {
const component = components[k];
component.name = `${libname}/${k}`;
obj[component.name] = component;
return obj;
}, {})
])
}
let libs;
if(!lib) {
2019-07-27 04:08:59 +12:00
pages = pages || await readJSON(
`${appPath}/pages.json`);
if(!pages.componentLibraries) return [];
libs = pages.componentLibraries;
} else {
libs = [lib];
}
const components = {};
for(let l of libs) {
merge(components, await componentsInLibrary(l))
}
return components;
}
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-27 04:08:59 +12:00
const component =
await readJSON(itemFullPath);
2019-07-28 19:03:11 +12:00
component.name = itemRelativePath
.substring(0, itemRelativePath.length - 5)
.replace(/\\/g, "/");
component.props = component.props || {};
component.props._component = component.name;
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;