Remove event passing for icons/examples from the api layer

This commit is contained in:
Nick O'Leary
2017-02-15 22:54:32 +00:00
parent 702e6d3b51
commit 869fdbcc6a
12 changed files with 291 additions and 149 deletions

View File

@@ -25,6 +25,7 @@ var flowUtil = require("./flows/util")
var context = require("./context");
var Node = require("./Node");
var log = require("../log");
var library = require("./library");
var events = require("../events");
@@ -88,6 +89,7 @@ function init(runtime) {
flows.init(runtime);
registry.init(runtime);
context.init(runtime.settings);
library.init(runtime);
}
function disableNode(id) {
@@ -135,6 +137,9 @@ module.exports = {
getNodeConfigs: registry.getNodeConfigs,
getNodeConfig: registry.getNodeConfig,
getNodeIconPath: registry.getNodeIconPath,
getNodeExampleFlows: library.getExampleFlows,
getNodeExampleFlowPath: library.getExampleFlowPath,
clearRegistry: registry.clear,
cleanModuleList: registry.cleanModuleList,

View File

@@ -0,0 +1,108 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var fs = require('fs');
var fspath = require('path');
var when = require('when');
var runtime;
var exampleRoots = {};
var exampleFlows = null;
function getFlowsFromPath(path) {
return when.promise(function(resolve,reject) {
var result = {};
fs.readdir(path,function(err,files) {
var promises = [];
var validFiles = [];
files.forEach(function(file) {
var fullPath = fspath.join(path,file);
var stats = fs.lstatSync(fullPath);
if (stats.isDirectory()) {
validFiles.push(file);
promises.push(getFlowsFromPath(fullPath));
} else if (/\.json$/.test(file)){
validFiles.push(file);
promises.push(when.resolve(file.split(".")[0]))
}
})
var i=0;
when.all(promises).then(function(results) {
results.forEach(function(r) {
if (typeof r === 'string') {
result.f = result.f||[];
result.f.push(r);
} else {
result.d = result.d||{};
result.d[validFiles[i]] = r;
}
i++;
})
resolve(result);
})
});
})
}
function addNodeExamplesDir(module) {
exampleRoots[module.name] = module.path;
getFlowsFromPath(module.path).then(function(result) {
exampleFlows = exampleFlows||{d:{}};
exampleFlows.d[module.name] = result;
});
}
function removeNodeExamplesDir(module) {
delete exampleRoots[module];
if (exampleFlows && exampleFlows.d) {
delete exampleFlows.d[module];
}
if (exampleFlows && Object.keys(exampleFlows.d).length === 0) {
exampleFlows = null;
}
}
function init(_runtime) {
runtime = _runtime;
exampleRoots = {};
exampleFlows = null;
runtime.events.removeListener("node-examples-dir",addNodeExamplesDir);
runtime.events.on("node-examples-dir",addNodeExamplesDir);
runtime.events.removeListener("node-module-uninstalled",removeNodeExamplesDir);
runtime.events.on("node-module-uninstalled",removeNodeExamplesDir);
}
function getExampleFlows() {
return exampleFlows;
}
function getExampleFlowPath(module,path) {
if (exampleRoots[module]) {
return fspath.join(exampleRoots[module],path)+".json";
}
return null;
}
module.exports = {
init: init,
getExampleFlows: getExampleFlows,
getExampleFlowPath: getExampleFlowPath
}

View File

@@ -70,13 +70,14 @@ module.exports = {
getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig,
getNodeIconPath: registry.getNodeIconPath,
enableNode: enableNodeSet,
disableNode: registry.disableNodeSet,
addModule: addModule,
removeModule: registry.removeModule,
installModule: installer.installModule,
uninstallModule: installer.uninstallModule,

View File

@@ -17,6 +17,9 @@
//var UglifyJS = require("uglify-js");
var util = require("util");
var when = require("when");
var path = require("path");
var fs = require("fs");
var events = require("../../events");
var settings;
@@ -41,6 +44,9 @@ function init(_settings,_loader) {
nodeList = [];
nodeConfigCache = null;
Node = require("../Node");
events.removeListener("node-icon-dir",nodeIconDir);
events.on("node-icon-dir",nodeIconDir);
}
function load() {
@@ -562,6 +568,40 @@ function setModulePendingUpdated(module,version) {
});
}
var icon_paths = {
"node-red":[path.resolve(__dirname + '/../../../../public/icons')]
};
var iconCache = {};
var defaultIcon = path.resolve(__dirname + '/../../../../public/icons/arrow-in.png');
function nodeIconDir(dir) {
icon_paths[dir.name] = icon_paths[dir.name] || [];
icon_paths[dir.name].push(path.resolve(dir.path));
}
function getNodeIconPath(module,icon) {
var iconName = module+"/"+icon;
if (iconCache[iconName]) {
return iconCache[iconName];
} else {
var paths = icon_paths[module];
if (paths) {
for (var p=0;p<paths.length;p++) {
var iconPath = path.join(paths[p],icon);
try {
fs.statSync(iconPath);
iconCache[iconName] = iconPath;
return iconPath;
} catch(err) {
// iconPath doesn't exist
}
}
}
return defaultIcon;
}
}
var registry = module.exports = {
init: init,
load: load,
@@ -583,6 +623,7 @@ var registry = module.exports = {
getModuleList: getModuleList,
getModuleInfo: getModuleInfo,
getNodeIconPath: getNodeIconPath,
/**
* Gets all of the node template configs
* @return all of the node templates in a single string