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

135 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-09-06 14:04:23 +02:00
const {
appPackageFolder
} = require("../createAppPackage");
const { componentLibraryInfo } = require("./componentLibraryInfo");
const {
stat, ensureDir, pathExists,
constants, copyFile, writeFile,
readFile
} = require("fs-extra");
2019-09-09 06:24:14 +02:00
const { join, resolve } = require("path");
2019-09-06 14:04:23 +02:00
const sqrl = require('squirrelly');
2019-09-07 07:50:35 +02:00
module.exports = async (config, appname, pages, appdefinition) => {
2019-09-06 14:04:23 +02:00
const appPath = appPackageFolder(config, appname);
await buildClientAppDefinition(
config, appname,
2019-09-09 06:24:14 +02:00
appdefinition,
2019-09-07 07:50:35 +02:00
appPath, pages, "main");
await buildClientAppDefinition(
config, appname,
2019-09-09 06:24:14 +02:00
appdefinition,
2019-09-07 07:50:35 +02:00
appPath, pages, "unauthenticated")
2019-09-06 14:04:23 +02:00
await buildIndexHtml(
config, appname, appPath,
2019-09-07 07:50:35 +02:00
pages, "main");
await buildIndexHtml(
config, appname, appPath,
pages, "unauthenticated");
await copyClientLib(appPath, "main");
await copyClientLib(appPath, "unauthenticated");
2019-09-06 14:04:23 +02:00
}
2019-09-09 06:24:14 +02:00
const publicPath = (appPath, pageName) => join(appPath, "public", pageName);
2019-09-06 14:04:23 +02:00
const rootPath = (config, appname) => config.useAppRootPath ? `/${appname}` : "";
2019-09-07 07:50:35 +02:00
const copyClientLib = async (appPath, pageName) => {
2019-09-06 14:04:23 +02:00
var sourcepath = require.resolve("budibase-client",{
2019-09-07 07:50:35 +02:00
paths: [appPath]
2019-09-06 14:04:23 +02:00
});
2019-09-07 07:50:35 +02:00
var destPath = join(publicPath(appPath, pageName), "budibase-client.js");
2019-09-06 14:04:23 +02:00
await copyFile(sourcepath, destPath, constants.COPYFILE_FICLONE);
}
2019-09-07 07:50:35 +02:00
const buildIndexHtml = async (config, appname, appPath, pages, pageName) => {
2019-09-06 14:04:23 +02:00
2019-09-07 07:50:35 +02:00
const appPublicPath = publicPath(appPath, pageName);
2019-09-06 14:04:23 +02: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 07:50:35 +02:00
title: pages[pageName].index.title || "Budibase App",
favicon: `${appRootPath}/${pages[pageName].index.favicon || "/assets/favicon.png"}`,
2019-09-09 06:24:14 +02:00
stylesheets: (pages.stylesheets || []).map(stylesheetUrl),
2019-09-06 14:04:23 +02:00
appRootPath
}
const indexHtmlTemplate = await readFile(
2019-09-09 06:24:14 +02:00
resolve(__dirname, "index.template.html"), "utf8");
2019-09-06 14:04:23 +02:00
const indexHtmlPath = join(appPublicPath, "index.html");
2019-09-09 06:24:14 +02:00
const indexHtml = sqrl.Render(indexHtmlTemplate, templateObj)
2019-09-06 14:04:23 +02:00
await writeFile(
indexHtmlPath,
2019-09-09 06:24:14 +02:00
indexHtml,
2019-09-06 14:04:23 +02:00
{flag:"w+"});
2019-09-09 06:24:14 +02:00
2019-09-06 14:04:23 +02:00
}
2019-09-07 07:50:35 +02:00
const buildClientAppDefinition = async (config, appname, appdefinition, appPath, pages, pageName) => {
2019-09-06 14:04:23 +02:00
2019-09-07 07:50:35 +02:00
const appPublicPath = publicPath(appPath, pageName);
2019-09-06 14:04:23 +02:00
const appRootPath = rootPath(config, appname);
2019-09-07 07:50:35 +02:00
const componentLibraries = [];
2019-09-06 14:04:23 +02:00
for(let lib of pages.componentLibraries) {
const info = await componentLibraryInfo(appPath, lib);
2019-09-09 06:24:14 +02:00
const libFile = info.components._lib || "index.js";
let source;
try {
source = join(info.libDir, libFile);
} catch(e) {
console.log(e);
}
2019-09-06 14:04:23 +02:00
const destDir = join(appPublicPath, "lib", info.libDir.replace(appPath, ""));
await ensureDir(destDir);
2019-09-09 06:24:14 +02:00
const destPath = join(destDir, libFile);
2019-09-06 14:04:23 +02:00
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 07:50:35 +02:00
const clientAppDefObj = {
hierarchy: appdefinition.hierarchy,
componentLibraries: pages.componentLibraries,
appRootPath: appRootPath,
props: appdefinition.props[pageName]
}
2019-09-06 14:04:23 +02:00
await writeFile(filename,
2019-09-07 07:50:35 +02:00
`window['##BUDIBASE_APPDEFINITION##'] = ${JSON.stringify(clientAppDefObj)}`);
2019-09-06 14:04:23 +02:00
}