1
0
Fork 0
mirror of synced 2024-07-13 18:26:06 +12:00
budibase/packages/server/utilities/builder/componentLibraryInfo.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

const { readJSON, exists } = require("fs-extra")
const { resolve, join, dirname } = require("path")
2019-09-07 00:04:23 +12:00
2019-09-07 17:50:35 +12:00
const getLibDir = (appPath, libname) => {
try {
const componentsFile = require.resolve(join(libname, "components.json"), {
paths: [appPath],
})
return dirname(componentsFile)
} catch (e) {
console.log(e)
}
2019-09-07 17:50:35 +12:00
}
2019-09-07 00:04:23 +12:00
const getComponentsFilepath = libPath => resolve(libPath, "components.json")
2019-09-07 00:04:23 +12:00
module.exports.componentsFilepath = (appPath, libname) =>
getComponentsFilepath(getLibDir(appPath, libname))
2019-09-07 00:04:23 +12:00
module.exports.componentLibraryInfo = async (appPath, libname) => {
const libDir = getLibDir(appPath, libname)
const componentsPath = getComponentsFilepath(libDir)
if (!(await exists(componentsPath))) {
const e = new Error(
`could not find components definition file at ${componentsPath}`
)
e.statusCode = 404
throw e
}
try {
const components = await readJSON(componentsPath)
const namespacedComponents = { _lib: components._lib }
for (let cname in components) {
if (cname === "_lib" || cname == "_generators") continue
const namespacedName = `${libname}/${cname}`
components[cname].name = namespacedName
namespacedComponents[namespacedName] = components[cname]
2019-09-07 00:04:23 +12:00
}
const namespacedGenerators = {}
if (components._generators) {
namespacedGenerators._lib = components._generators._lib || "generators.js"
for (let gname in components._generators) {
if (gname === "_lib") continue
const namespacedName = `${libname}/${gname}`
components._generators[gname].name = namespacedName
namespacedGenerators[namespacedName] = components._generators[gname]
}
}
2019-10-07 18:03:41 +13:00
return {
components: namespacedComponents,
generators: namespacedGenerators,
libDir,
componentsPath,
2019-09-07 00:04:23 +12:00
}
} catch (e) {
const err = `could not parse JSON - ${componentsPath} : ${e.message}`
throw new Error(err)
}
}