2020-02-03 10:24:25 +01:00
|
|
|
const { appPackageFolder, appsFolder } = require("../createAppPackage")
|
|
|
|
const {
|
|
|
|
readJSON,
|
|
|
|
writeJSON,
|
|
|
|
readdir,
|
|
|
|
stat,
|
|
|
|
ensureDir,
|
|
|
|
rename,
|
|
|
|
unlink,
|
|
|
|
rmdir,
|
|
|
|
} = require("fs-extra")
|
|
|
|
const { join, dirname } = require("path")
|
|
|
|
const { $ } = require("@budibase/core").common
|
|
|
|
const { keyBy, intersection, map } = require("lodash/fp")
|
|
|
|
const { merge } = require("lodash")
|
|
|
|
|
|
|
|
const { componentLibraryInfo } = require("./componentLibraryInfo")
|
|
|
|
const savePackage = require("./savePackage")
|
|
|
|
const buildApp = require("./buildApp")
|
|
|
|
|
|
|
|
module.exports.savePackage = savePackage
|
|
|
|
|
|
|
|
const getPages = async appPath => await readJSON(`${appPath}/pages.json`)
|
|
|
|
const getAppDefinition = async appPath =>
|
|
|
|
await readJSON(`${appPath}/appDefinition.json`)
|
2019-09-29 07:40:06 +02:00
|
|
|
|
2019-07-13 11:35:57 +02:00
|
|
|
module.exports.getPackageForBuilder = async (config, appname) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const appPath = appPackageFolder(config, appname)
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const pages = await getPages(appPath)
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return {
|
|
|
|
appDefinition: await getAppDefinition(appPath),
|
2019-07-13 11:35:57 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
accessLevels: await readJSON(`${appPath}/access_levels.json`),
|
2019-07-25 08:31:54 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
pages,
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
components: await getComponents(appPath, pages),
|
2019-07-13 11:35:57 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
screens: keyBy("name")(await fetchscreens(appPath)),
|
|
|
|
}
|
2019-07-13 11:35:57 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 18:14:23 +02:00
|
|
|
module.exports.getApps = async (config, master) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const dirs = await readdir(appsFolder(config))
|
2019-10-11 18:14:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return $(master.listApplications(), [map(a => a.name), intersection(dirs)])
|
2019-10-11 18:14:23 +02:00
|
|
|
}
|
2019-07-14 08:46:36 +02:00
|
|
|
|
2019-07-26 18:08:59 +02:00
|
|
|
const componentPath = (appPath, name) =>
|
2020-02-03 10:24:25 +01:00
|
|
|
join(appPath, "components", name + ".json")
|
2019-07-26 18:08:59 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
module.exports.saveScreen = async (config, appname, component) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const appPath = appPackageFolder(config, appname)
|
|
|
|
const compPath = componentPath(appPath, component.name)
|
|
|
|
await ensureDir(dirname(compPath))
|
|
|
|
await writeJSON(compPath, component, {
|
|
|
|
encoding: "utf8",
|
|
|
|
flag: "w",
|
|
|
|
spaces: 2,
|
|
|
|
})
|
2019-07-26 18:08:59 +02:00
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
module.exports.renameScreen = async (config, appname, oldName, newName) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const appPath = appPackageFolder(config, appname)
|
2019-07-26 18:08:59 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const oldComponentPath = componentPath(appPath, oldName)
|
2019-07-26 18:08:59 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const newComponentPath = componentPath(appPath, newName)
|
2019-07-26 18:08:59 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
await ensureDir(dirname(newComponentPath))
|
|
|
|
await rename(oldComponentPath, newComponentPath)
|
2019-07-26 18:08:59 +02:00
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
module.exports.deleteScreen = async (config, appname, name) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const appPath = appPackageFolder(config, appname)
|
|
|
|
const componentFile = componentPath(appPath, 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
|
|
|
}
|
|
|
|
|
2019-09-09 06:24:14 +02:00
|
|
|
module.exports.componentLibraryInfo = async (config, appname, lib) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const appPath = appPackageFolder(config, appname)
|
|
|
|
return await componentLibraryInfo(appPath, lib)
|
|
|
|
}
|
2019-08-19 22:18:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const getComponents = async (appPath, pages, lib) => {
|
|
|
|
let libs
|
|
|
|
if (!lib) {
|
|
|
|
pages = pages || (await readJSON(`${appPath}/pages.json`))
|
2019-08-19 22:18:23 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (!pages.componentLibraries) return []
|
2019-07-25 08:31:54 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
libs = pages.componentLibraries
|
|
|
|
} else {
|
|
|
|
libs = [lib]
|
|
|
|
}
|
2019-07-25 08:31:54 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const components = {}
|
|
|
|
const generators = {}
|
2019-07-25 08:31:54 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
for (let l of libs) {
|
|
|
|
const info = await componentLibraryInfo(appPath, l)
|
|
|
|
merge(components, info.components)
|
|
|
|
merge(generators, info.generators)
|
|
|
|
}
|
2019-10-07 07:03:41 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (components._lib) delete components._lib
|
|
|
|
if (components._generators) delete components._generators
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return { components, generators }
|
2019-07-26 16:13:15 +02:00
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
const fetchscreens = async (appPath, relativePath = "") => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const currentDir = join(appPath, "components", relativePath)
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const contents = await readdir(currentDir)
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const components = []
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
for (let item of contents) {
|
|
|
|
const itemRelativePath = join(relativePath, item)
|
|
|
|
const itemFullPath = join(currentDir, item)
|
|
|
|
const stats = await stat(itemFullPath)
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (stats.isFile()) {
|
|
|
|
if (!item.endsWith(".json")) continue
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const component = await readJSON(itemFullPath)
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
component.name = itemRelativePath
|
|
|
|
.substring(0, itemRelativePath.length - 5)
|
|
|
|
.replace(/\\/g, "/")
|
2019-08-07 10:03:49 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
component.props = component.props || {}
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
components.push(component)
|
|
|
|
} else {
|
|
|
|
const childComponents = await fetchscreens(
|
|
|
|
appPath,
|
|
|
|
join(relativePath, item)
|
|
|
|
)
|
|
|
|
|
|
|
|
for (let c of childComponents) {
|
|
|
|
components.push(c)
|
|
|
|
}
|
2019-07-26 16:13:15 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2019-07-26 16:13:15 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return components
|
2019-07-25 08:31:54 +02:00
|
|
|
}
|
2019-07-14 08:46:36 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
module.exports.getComponents = getComponents
|