@ -1,66 +1,63 @@
const { ensureDir , constants , copyFile , writeFile } = require ( "fs-extra" )
const {
ensureDir ,
constants ,
copyFile ,
writeFile ,
readdir ,
readFile ,
} = require ( "fs-extra" )
const { join } = require ( "../centralPath" )
const { budibaseAppsDir } = require ( "../budibaseDir" )
const CouchDB = require ( "../../db" )
const { getScreenParams , getLayoutParams } = require ( "../../db/utils" )
async function getAppPackage ( appId ) {
const db = new CouchDB ( appId )
let params = {
include_docs : true ,
}
let [ screens , layouts ] = await Promise . all ( [
db . allDocs ( getScreenParams ( null , params ) ) ,
db . allDocs ( getLayoutParams ( null , params ) ) ,
] )
screens = screens . rows . map ( row => row . doc )
layouts = layouts . rows . map ( row => row . doc )
if ( ! screens ) {
screens = [ ]
}
if ( ! layouts ) {
layouts = [ ]
}
return { screens , layouts }
}
const CSS_DIRECTORY = "css"
/ * *
* Compile all the non - db static web assets that are required for the running of
* a budibase application . This includes CSS , the JSON structure of the DOM and
* the client library , a script responsible for reading the JSON structure
* and rendering the application .
* @ param { string } appId - id of the application we want to compile static assets for
* @ param { string } appId id of the application we want to compile static assets for
* @ param { array | object } assets a list of screens or screen layouts for which the CSS should be extracted and stored .
* /
module . exports = async appId => {
module . exports = async ( appId , assets ) => {
const publicPath = join ( budibaseAppsDir ( ) , appId , "public" )
const pkg = await getAppPackage ( appId )
await ensureDir ( publicPath )
await buildCssBundle ( publicPath , pkg )
await copyClientLib ( publicPath )
for ( let asset of Array . isArray ( assets ) ? assets : [ assets ] ) {
await buildCssBundle ( publicPath , asset )
await copyClientLib ( publicPath )
// remove props that shouldn't be present when written to DB
if ( asset . _ css ) {
delete asset . _ css
}
}
return assets
}
/ * *
* Reads the _ css property of all screens and the screen layouts , and creates a singular CSS
* bundle for the app at < appId > / p u b l i c / b u n d l e . c s s
* @ param { String } publicPath - path to the public assets directory of the budibase application
* @ param { Object } pkg - app package information
* @ param { Object } asset a single screen or screen layout which is being updated
* /
const buildCssBundle = async ( publicPath , pkg ) => {
const buildCssBundle = async ( publicPath , asset ) => {
const cssPath = join ( publicPath , CSS_DIRECTORY )
let cssString = ""
for ( let screen of pkg . screens || [ ] ) {
if ( ! screen . _ css ) continue
if ( screen . _ css . trim ( ) . length === 0 ) {
delete screen . _ css
continue
}
cssString += screen . _ css
// create a singular CSS file for this asset
const assetCss = asset . _ css ? asset . _ css . trim ( ) : ""
if ( assetCss . length !== 0 ) {
await ensureDir ( cssPath )
await writeFile ( join ( cssPath , asset . _ id ) , assetCss )
}
if ( pkg . layout . _ css ) cssString += pkg . layout . _ css
// bundle up all the CSS in the directory into one top level CSS file
const cssFiles = await readdir ( cssPath )
for ( let filename of cssFiles ) {
const css = await readFile ( filename )
cssString += css
}
writeFile ( join ( publicPath , "bundle.css" ) , cssString )
await writeFile ( join ( publicPath , "bundle.css" ) , cssString )
}
/ * *