2020-02-03 10:24:25 +01:00
|
|
|
const { appPackageFolder } = require("../createAppPackage")
|
|
|
|
const { componentLibraryInfo } = require("./componentLibraryInfo")
|
2019-09-06 14:04:23 +02:00
|
|
|
const {
|
2020-02-03 10:24:25 +01:00
|
|
|
stat,
|
|
|
|
ensureDir,
|
|
|
|
pathExists,
|
|
|
|
constants,
|
|
|
|
copyFile,
|
|
|
|
writeFile,
|
|
|
|
readFile,
|
2020-02-10 22:35:51 +01:00
|
|
|
writeJSON,
|
2020-02-03 10:24:25 +01:00
|
|
|
} = require("fs-extra")
|
|
|
|
const { join, resolve, dirname } = require("path")
|
|
|
|
const sqrl = require("squirrelly")
|
2020-02-10 16:51:09 +01:00
|
|
|
const { convertCssToFiles } = require("./convertCssToFiles")
|
2020-02-10 22:35:51 +01:00
|
|
|
const publicPath = require("./publicPath")
|
2019-09-06 14:04:23 +02:00
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
module.exports = async (config, appname, pageName, pkg) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const appPath = appPackageFolder(config, appname)
|
|
|
|
|
2020-02-12 13:45:24 +01:00
|
|
|
pkg.screens = pkg.screens || []
|
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
await convertCssToFiles(publicPath(appPath, pageName), pkg)
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
await buildIndexHtml(config, appname, pageName, appPath, pkg)
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
await buildFrontendAppDefinition(config, appname, pageName, pkg, appPath)
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
await copyClientLib(appPath, pageName)
|
|
|
|
|
|
|
|
await savePageJson(appPath, pageName, pkg)
|
2019-09-06 14:04:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const rootPath = (config, appname) =>
|
|
|
|
config.useAppRootPath ? `/${appname}` : ""
|
2019-09-06 14:04:23 +02:00
|
|
|
|
2019-09-07 07:50:35 +02:00
|
|
|
const copyClientLib = async (appPath, pageName) => {
|
2020-02-10 16:51:09 +01:00
|
|
|
const sourcepath = require.resolve("@budibase/client")
|
|
|
|
const destPath = join(publicPath(appPath, pageName), "budibase-client.js")
|
2020-02-03 10:24:25 +01:00
|
|
|
|
|
|
|
await copyFile(sourcepath, destPath, constants.COPYFILE_FICLONE)
|
2020-02-10 16:51:09 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
await copyFile(
|
|
|
|
sourcepath + ".map",
|
|
|
|
destPath + ".map",
|
|
|
|
constants.COPYFILE_FICLONE
|
|
|
|
)
|
2019-09-06 14:04:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
const buildIndexHtml = async (config, appname, pageName, appPath, pkg) => {
|
|
|
|
const appPublicPath = publicPath(appPath, pageName)
|
2020-02-03 10:24:25 +01:00
|
|
|
const appRootPath = rootPath(config, appname)
|
2019-09-06 14:04:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const stylesheetUrl = s =>
|
|
|
|
s.indexOf("http://") === 0 || s.indexOf("https://") === 0
|
|
|
|
? s
|
|
|
|
: `/${rootPath(config, appname)}/${s}`
|
2019-09-06 14:04:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const templateObj = {
|
2020-02-10 16:51:09 +01:00
|
|
|
title: pkg.page.title || "Budibase App",
|
|
|
|
favicon: `${appRootPath}/${pkg.page.favicon || "/_shared/favicon.png"}`,
|
|
|
|
stylesheets: (pkg.page.stylesheets || []).map(stylesheetUrl),
|
|
|
|
screenStyles: pkg.screens.filter(s => s._css).map(s => s._css),
|
|
|
|
pageStyle: pkg.page._css,
|
2020-02-03 10:24:25 +01:00
|
|
|
appRootPath,
|
|
|
|
}
|
2019-09-06 14:04:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const indexHtmlTemplate = await readFile(
|
|
|
|
resolve(__dirname, "index.template.html"),
|
|
|
|
"utf8"
|
|
|
|
)
|
2019-09-06 14:04:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const indexHtmlPath = join(appPublicPath, "index.html")
|
2019-09-06 14:04:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const indexHtml = sqrl.Render(indexHtmlTemplate, templateObj)
|
2019-09-09 06:24:14 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
await writeFile(indexHtmlPath, indexHtml, { flag: "w+" })
|
2019-09-06 14:04:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
const buildFrontendAppDefinition = async (config, appname, pageName, pkg) => {
|
2020-02-10 16:51:09 +01:00
|
|
|
const appPath = appPackageFolder(config, appname)
|
2020-02-10 22:35:51 +01:00
|
|
|
const appPublicPath = publicPath(appPath, pageName)
|
2020-02-03 10:24:25 +01:00
|
|
|
const appRootPath = rootPath(config, appname)
|
|
|
|
|
|
|
|
const componentLibraries = []
|
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
for (let lib of pkg.page.componentLibraries) {
|
2020-02-03 10:24:25 +01:00
|
|
|
const info = await componentLibraryInfo(appPath, lib)
|
|
|
|
const libFile = info.components._lib || "index.js"
|
|
|
|
const source = join(info.libDir, libFile)
|
|
|
|
const moduleDir = join(
|
|
|
|
appPublicPath,
|
|
|
|
"lib",
|
|
|
|
info.libDir.replace(appPath, "")
|
|
|
|
)
|
|
|
|
const destPath = join(moduleDir, libFile)
|
|
|
|
|
|
|
|
await ensureDir(dirname(destPath))
|
|
|
|
|
|
|
|
componentLibraries.push({
|
|
|
|
importPath: destPath.replace(appPublicPath, "").replace(/\\/g, "/"),
|
|
|
|
libName: lib,
|
|
|
|
})
|
|
|
|
|
|
|
|
let shouldCopy = !(await pathExists(destPath))
|
|
|
|
if (!shouldCopy) {
|
|
|
|
const destStat = await stat(destPath)
|
|
|
|
const sourceStat = await stat(source)
|
|
|
|
shouldCopy = destStat.ctimeMs !== sourceStat.ctimeMs
|
2019-09-06 14:04:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (shouldCopy) {
|
|
|
|
await copyFile(source, destPath, constants.COPYFILE_FICLONE)
|
2019-09-07 07:50:35 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
const filename = join(appPublicPath, "clientFrontendDefinition.js")
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-02-10 16:51:09 +01:00
|
|
|
if (pkg.page._css) {
|
|
|
|
delete pkg.page._css
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let screen of pkg.screens) {
|
|
|
|
if (screen._css) {
|
|
|
|
delete pkg.page._css
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 22:35:51 +01:00
|
|
|
const clientUiDefinition = JSON.stringify({
|
2020-02-03 10:24:25 +01:00
|
|
|
componentLibraries: componentLibraries,
|
|
|
|
appRootPath: appRootPath,
|
2020-02-10 16:51:09 +01:00
|
|
|
page: pkg.page,
|
|
|
|
screens: pkg.screens,
|
2020-02-10 22:35:51 +01:00
|
|
|
})
|
2020-02-03 10:24:25 +01:00
|
|
|
|
|
|
|
await writeFile(
|
|
|
|
filename,
|
2020-02-12 13:45:24 +01:00
|
|
|
`window['##BUDIBASE_FRONTEND_DEFINITION##'] = ${clientUiDefinition};
|
2020-02-10 22:35:51 +01:00
|
|
|
window['##BUDIBASE_FRONTEND_FUNCTIONS##'] = ${pkg.uiFunctions}`
|
2020-02-03 10:24:25 +01:00
|
|
|
)
|
|
|
|
}
|
2020-02-10 22:35:51 +01:00
|
|
|
|
|
|
|
const savePageJson = async (appPath, pageName, pkg) => {
|
|
|
|
const pageFile = join(appPath, "pages", pageName, "page.json")
|
|
|
|
|
|
|
|
if (pkg.page._css) {
|
|
|
|
delete pkg.page._css
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkg.page.name) {
|
|
|
|
delete pkg.page.name
|
|
|
|
}
|
|
|
|
|
|
|
|
await writeJSON(pageFile, pkg.page, {
|
|
|
|
spaces: 2,
|
|
|
|
})
|
|
|
|
}
|