Allow module to provide resources and automatically expose them

This commit is contained in:
Nick O'Leary
2021-03-15 21:06:10 +00:00
parent 827f8d4d51
commit 8543613563
8 changed files with 94 additions and 5 deletions

View File

@@ -301,6 +301,17 @@ module.exports = {
*/
getNodeExampleFlowPath: library.getExampleFlowPath,
/**
* Gets the full path to a module's resource file
* @param {String} module - the name of the module providing the resource file
* @param {String} path - the relative path of the resource file
* @return {String} the full path to the resource file
*
* @function
* @memberof @node-red/registry
*/
getModuleResource: registry.getModuleResource,
checkFlowDependencies: externalModules.checkFlowDependencies,
registerPlugin: plugins.registerPlugin,
@@ -309,7 +320,8 @@ module.exports = {
getPluginList: plugins.getPluginList,
getPluginConfigs: plugins.getPluginConfigs,
exportPluginSettings: plugins.exportPluginSettings,
deprecated: require("./deprecated")
};

View File

@@ -262,6 +262,14 @@ function getModuleNodeFiles(module) {
result.examples = {path:examplesDir};
} catch(err) {
}
var resourcesDir = path.join(moduleDir,"resources");
try {
fs.statSync(resourcesDir)
result.resources = {path:resourcesDir};
} catch(err) {
}
return result;
}
@@ -406,6 +414,7 @@ function convertModuleFileListToObject(moduleFiles,seedObject) {
user: moduleFile.user||false,
nodes: {},
plugins: {},
resources: nodeModuleFiles.resources,
icons: nodeModuleFiles.icons,
examples: nodeModuleFiles.examples
};

View File

@@ -15,8 +15,8 @@
**/
//var UglifyJS = require("uglify-js");
var path = require("path");
var fs = require("fs");
const path = require("path");
const fs = require("fs");
var library = require("./library");
const {events} = require("@node-red/util")
@@ -680,7 +680,6 @@ function getNodeIconPath(module,icon) {
function getNodeIcons() {
var iconList = {};
for (var module in moduleConfigs) {
if (moduleConfigs.hasOwnProperty(module)) {
if (moduleConfigs[module].icons) {
@@ -692,6 +691,21 @@ function getNodeIcons() {
return iconList;
}
function getModuleResource(module, resourcePath) {
let mod = moduleConfigs[module];
if (mod && mod.resources) {
let basePath = mod.resources.path;
let fullPath = path.join(basePath,resourcePath);
if (/^\.\./.test(path.relative(basePath,fullPath))) {
return null;
}
if (fs.existsSync(fullPath)) {
return fullPath;
}
}
return null;
}
var registry = module.exports = {
init: init,
load: load,
@@ -722,6 +736,8 @@ var registry = module.exports = {
getNodeIconPath: getNodeIconPath,
getNodeIcons: getNodeIcons,
getModuleResource: getModuleResource,
/**
* Gets all of the node template configs
* @return all of the node templates in a single string