1
0
Fork 0
mirror of synced 2024-08-07 22:28:15 +12:00
budibase/packages/server/utilities/builder/buildApp.js

126 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-09-07 00:04:23 +12:00
const {
appPackageFolder
} = require("../createAppPackage");
const { componentLibraryInfo } = require("./componentLibraryInfo");
const {
stat, ensureDir, pathExists,
constants, copyFile, writeFile,
readFile
} = require("fs-extra");
const { join } = require("path");
const sqrl = require('squirrelly');
2019-09-07 17:50:35 +12:00
module.exports = async (config, appname, pages, appdefinition) => {
2019-09-07 00:04:23 +12:00
const appPath = appPackageFolder(config, appname);
await buildClientAppDefinition(
config, appname,
appdefinition, componentLibraries,
2019-09-07 17:50:35 +12:00
appPath, pages, "main");
await buildClientAppDefinition(
config, appname,
appdefinition, componentLibraries,
appPath, pages, "unauthenticated")
2019-09-07 00:04:23 +12:00
await buildIndexHtml(
config, appname, appPath,
2019-09-07 17:50:35 +12:00
pages, "main");
await buildIndexHtml(
config, appname, appPath,
pages, "unauthenticated");
await copyClientLib(appPath, "main");
await copyClientLib(appPath, "unauthenticated");
2019-09-07 00:04:23 +12:00
}
2019-09-07 17:50:35 +12:00
const publicPath = async (appPath, pageName) => join(appPath, "public", pageName);
2019-09-07 00:04:23 +12:00
const rootPath = (config, appname) => config.useAppRootPath ? `/${appname}` : "";
2019-09-07 17:50:35 +12:00
const copyClientLib = async (appPath, pageName) => {
2019-09-07 00:04:23 +12:00
var sourcepath = require.resolve("budibase-client",{
2019-09-07 17:50:35 +12:00
paths: [appPath]
2019-09-07 00:04:23 +12:00
});
2019-09-07 17:50:35 +12:00
var destPath = join(publicPath(appPath, pageName), "budibase-client.js");
2019-09-07 00:04:23 +12:00
await copyFile(sourcepath, destPath, constants.COPYFILE_FICLONE);
}
2019-09-07 17:50:35 +12:00
const buildIndexHtml = async (config, appname, appPath, pages, pageName) => {
2019-09-07 00:04:23 +12:00
2019-09-07 17:50:35 +12:00
const appPublicPath = publicPath(appPath, pageName);
2019-09-07 00:04:23 +12:00
const appRootPath = rootPath(config, appname);
const stylesheetUrl = s =>
s.indexOf('http://') === 0 || s.indexOf('https://') === 0
? s
: `/${rootPath(config, appname)}/${s}`;
const templateObj = {
2019-09-07 17:50:35 +12:00
title: pages[pageName].index.title || "Budibase App",
favicon: `${appRootPath}/${pages[pageName].index.favicon || "/assets/favicon.png"}`,
stylesheets: pages.stylesheets.map(stylesheetUrl),
2019-09-07 00:04:23 +12:00
appRootPath
}
const indexHtmlTemplate = await readFile(
resolve(__dirname, "index.template.html"));
const indexHtmlPath = join(appPublicPath, "index.html");
await writeFile(
indexHtmlPath,
sqrl.Render(indexHtmlTemplate, templateObj),
{flag:"w+"});
}
2019-09-07 17:50:35 +12:00
const buildClientAppDefinition = async (config, appname, appdefinition, appPath, pages, pageName) => {
2019-09-07 00:04:23 +12:00
2019-09-07 17:50:35 +12:00
const appPublicPath = publicPath(appPath, pageName);
2019-09-07 00:04:23 +12:00
const appRootPath = rootPath(config, appname);
2019-09-07 17:50:35 +12:00
const componentLibraries = [];
2019-09-07 00:04:23 +12:00
for(let lib of pages.componentLibraries) {
const info = await componentLibraryInfo(appPath, lib);
const source = join(info.libDir, info.components._lib);
const destDir = join(appPublicPath, "lib", info.libDir.replace(appPath, ""));
await ensureDir(destDir);
const destPath = join(destDir, info.components._lib);
componentLibraries.push(destPath);
let shouldCopy = !(await pathExists(destPath));
if(!shouldCopy) {
const destStat = await stat(destPath);
const sourceStat = await stat(source);
shouldCopy = destStat.ctimeMs !== sourceStat.ctimeMs;
}
if(shouldCopy) {
await copyFile(source, destPath, constants.COPYFILE_FICLONE);
}
}
const filename = join(appPublicPath, "clientAppDefinition.js");
2019-09-07 17:50:35 +12:00
const clientAppDefObj = {
hierarchy: appdefinition.hierarchy,
componentLibraries: pages.componentLibraries,
appRootPath: appRootPath,
props: appdefinition.props[pageName]
}
2019-09-07 00:04:23 +12:00
await writeFile(filename,
2019-09-07 17:50:35 +12:00
`window['##BUDIBASE_APPDEFINITION##'] = ${JSON.stringify(clientAppDefObj)}`);
2019-09-07 00:04:23 +12:00
}