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

99 lines
2.5 KiB
JavaScript
Raw Normal View History

const { appPackageFolder } = require("../createAppPackage")
const {
readJSON,
writeJSON,
readdir,
ensureDir,
rename,
unlink,
rmdir,
} = require("fs-extra")
const { join, dirname, resolve } = require("path")
const buildPage = require("./buildPage")
const getPages = require("./getPages")
const listScreens = require("./listScreens")
const deleteCodeMeta = require("./deleteCodeMeta")
module.exports.buildPage = buildPage
module.exports.listScreens = listScreens
const getAppDefinition = async appPath =>
await readJSON(`${appPath}/appDefinition.json`)
module.exports.getPackageForBuilder = async (config, application) => {
const appPath = resolve(config.latestPackagesFolder, application._id);
const pages = await getPages(appPath)
return {
pages,
application,
clientId: process.env.CLIENT_ID
}
2019-07-13 11:35:57 +02:00
}
const screenPath = (appPath, pageName, name) =>
join(appPath, "pages", pageName, "screens", name + ".json")
2019-07-26 18:08:59 +02:00
module.exports.saveScreen = async (config, appname, pagename, screen) => {
const appPath = appPackageFolder(config, appname)
const compPath = screenPath(appPath, pagename, screen.name)
await ensureDir(dirname(compPath))
if (screen._css) {
delete screen._css
}
deleteCodeMeta(screen.props)
await writeJSON(compPath, screen, {
encoding: "utf8",
flag: "w",
spaces: 2,
})
return screen
2019-07-26 18:08:59 +02:00
}
module.exports.renameScreen = async (
config,
appname,
pagename,
oldName,
newName
) => {
const appPath = appPackageFolder(config, appname)
2019-07-26 18:08:59 +02:00
const oldComponentPath = screenPath(appPath, pagename, oldName)
2019-07-26 18:08:59 +02:00
const newComponentPath = screenPath(appPath, pagename, newName)
2019-07-26 18:08:59 +02:00
await ensureDir(dirname(newComponentPath))
await rename(oldComponentPath, newComponentPath)
2019-07-26 18:08:59 +02:00
}
module.exports.deleteScreen = async (config, appname, pagename, name) => {
const appPath = appPackageFolder(config, appname)
const componentFile = screenPath(appPath, pagename, name)
await unlink(componentFile)
const dir = dirname(componentFile)
if ((await readdir(dir)).length === 0) {
await rmdir(dir)
}
2019-07-26 18:08:59 +02:00
}
module.exports.savePage = async (config, appname, pagename, page) => {
const appPath = appPackageFolder(config, appname)
const pageDir = join(appPath, "pages", pagename)
await ensureDir(pageDir)
await writeJSON(join(pageDir, "page.json"), page, {
encoding: "utf8",
flag: "w",
space: 2,
})
const appDefinition = await getAppDefinition(appPath)
await buildPage(config, appname, appDefinition, pagename, page)
2020-05-04 18:13:57 +02:00
}