2019-09-06 14:04:23 +02:00
|
|
|
const {
|
2019-09-07 07:50:35 +02:00
|
|
|
readJSON, exists
|
2019-09-06 14:04:23 +02:00
|
|
|
} = require("fs-extra");
|
2019-09-07 07:50:35 +02:00
|
|
|
const {
|
|
|
|
resolve, join , dirname
|
|
|
|
} = require("path");
|
2019-09-06 14:04:23 +02:00
|
|
|
|
2019-09-07 07:50:35 +02: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-06 14:04:23 +02:00
|
|
|
|
|
|
|
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 {
|
2019-09-09 06:24:14 +02:00
|
|
|
const components = await readJSON(componentsPath);
|
2019-09-11 06:18:00 +02:00
|
|
|
const namespacedComponents = {_lib:components._lib};
|
2019-09-09 06:24:14 +02:00
|
|
|
for(let cname in components) {
|
2019-10-07 07:03:41 +02:00
|
|
|
if(cname === "_lib" || cname == "_generators") continue;
|
2019-09-09 06:24:14 +02:00
|
|
|
const namespacedName = `${libname}/${cname}`;
|
|
|
|
components[cname].name = namespacedName;
|
|
|
|
namespacedComponents[namespacedName] = components[cname];
|
|
|
|
}
|
2019-10-07 07:03:41 +02: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-09-06 14:04:23 +02:00
|
|
|
return ({
|
2019-09-09 06:24:14 +02:00
|
|
|
components: namespacedComponents,
|
2019-10-07 07:03:41 +02:00
|
|
|
generators: namespacedGenerators,
|
2019-09-06 14:04:23 +02:00
|
|
|
libDir,
|
|
|
|
componentsPath
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
const err = `could not parse JSON - ${componentsPath} : ${e.message}`;
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}
|