1
0
Fork 0
mirror of synced 2024-07-12 17:56:07 +12:00
budibase/packages/server/utilities/builder/componentLibraryInfo.js
2019-09-06 13:04:23 +01:00

37 lines
1 KiB
JavaScript

const {
readJSON, exists
} = require("fs-extra");
const { resolve } = require("path");
const getLibDir = (appPath, libname) => require.resolve(libname, {
basedir: appPath
});
const getComponentsFilepath = libPath =>
resolve(libPath, "components.json");
module.exports.componentsFilepath = (appPath, libname) =>
getComponentsFilepath(getLibDir(appPath, libname));
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 {
return ({
components: await readJSON(componentsPath),
libDir,
componentsPath
});
} catch(e) {
const err = `could not parse JSON - ${componentsPath} : ${e.message}`;
throw new Error(err);
}
}