2020-12-10 17:01:55 +01:00
|
|
|
const registry = require("./registry");
|
2021-02-19 12:59:49 +01:00
|
|
|
const {events} = require("@node-red/util");
|
|
|
|
const clone = require("clone");
|
2020-12-10 17:01:55 +01:00
|
|
|
|
2021-02-19 12:59:49 +01:00
|
|
|
let pluginConfigCache = {};
|
|
|
|
let pluginToId = {};
|
|
|
|
let plugins = {};
|
|
|
|
let pluginsByType = {};
|
|
|
|
let pluginSettings = {};
|
|
|
|
let settings;
|
2020-12-10 17:01:55 +01:00
|
|
|
|
|
|
|
function init(_settings) {
|
|
|
|
settings = _settings;
|
|
|
|
plugins = {};
|
|
|
|
pluginConfigCache = {};
|
|
|
|
pluginToId = {};
|
|
|
|
pluginsByType = {};
|
2021-02-19 12:59:49 +01:00
|
|
|
pluginSettings = {};
|
2020-12-10 17:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function registerPlugin(nodeSetId,id,definition) {
|
|
|
|
var moduleId = registry.getModuleFromSetId(nodeSetId);
|
|
|
|
var pluginId = registry.getNodeFromSetId(nodeSetId);
|
|
|
|
|
|
|
|
definition.id = id;
|
|
|
|
definition.module = moduleId;
|
|
|
|
pluginToId[id] = nodeSetId;
|
|
|
|
plugins[id] = definition;
|
|
|
|
var module = registry.getModule(moduleId);
|
2021-01-26 14:35:40 +01:00
|
|
|
|
|
|
|
definition.path = module.path;
|
|
|
|
|
2020-12-10 17:01:55 +01:00
|
|
|
module.plugins[pluginId].plugins.push(definition);
|
|
|
|
if (definition.type) {
|
|
|
|
pluginsByType[definition.type] = pluginsByType[definition.type] || [];
|
|
|
|
pluginsByType[definition.type].push(definition);
|
|
|
|
}
|
2021-02-19 12:59:49 +01:00
|
|
|
if (definition.settings) {
|
|
|
|
pluginSettings[id] = definition.settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-10 17:01:55 +01:00
|
|
|
if (definition.onadd && typeof definition.onadd === 'function') {
|
|
|
|
definition.onadd();
|
|
|
|
}
|
|
|
|
events.emit("registry:plugin-added",id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPlugin(id) {
|
|
|
|
return plugins[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPluginsByType(type) {
|
|
|
|
return pluginsByType[type] || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPluginConfigs(lang) {
|
|
|
|
if (!pluginConfigCache[lang]) {
|
|
|
|
var result = "";
|
|
|
|
var script = "";
|
|
|
|
var moduleConfigs = registry.getModuleList();
|
|
|
|
for (var module in moduleConfigs) {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (moduleConfigs.hasOwnProperty(module)) {
|
|
|
|
var plugins = moduleConfigs[module].plugins;
|
|
|
|
for (var plugin in plugins) {
|
|
|
|
if (plugins.hasOwnProperty(plugin)) {
|
|
|
|
var config = plugins[plugin];
|
|
|
|
if (config.enabled && !config.err && config.config) {
|
|
|
|
result += "\n<!-- --- [red-plugin:"+config.id+"] --- -->\n";
|
|
|
|
result += config.config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pluginConfigCache[lang] = result;
|
|
|
|
}
|
|
|
|
return pluginConfigCache[lang];
|
|
|
|
}
|
|
|
|
function getPluginList() {
|
|
|
|
var list = [];
|
|
|
|
var moduleConfigs = registry.getModuleList();
|
|
|
|
for (var module in moduleConfigs) {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (moduleConfigs.hasOwnProperty(module)) {
|
|
|
|
var plugins = moduleConfigs[module].plugins;
|
|
|
|
for (var plugin in plugins) {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (plugins.hasOwnProperty(plugin)) {
|
|
|
|
var pluginInfo = registry.filterNodeInfo(plugins[plugin]);
|
|
|
|
pluginInfo.version = moduleConfigs[module].version;
|
|
|
|
// if (moduleConfigs[module].pending_version) {
|
|
|
|
// nodeInfo.pending_version = moduleConfigs[module].pending_version;
|
|
|
|
// }
|
|
|
|
list.push(pluginInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:59:49 +01:00
|
|
|
function exportPluginSettings(safeSettings) {
|
|
|
|
for (let id in pluginSettings) {
|
|
|
|
if (pluginSettings.hasOwnProperty(id)) {
|
|
|
|
if (settings.hasOwnProperty(id) && !safeSettings.hasOwnProperty(id)) {
|
|
|
|
let pluginTypeSettings = pluginSettings[id];
|
|
|
|
let exportedSet = {};
|
|
|
|
let defaultExportable = false;
|
|
|
|
if (pluginTypeSettings['*'] && pluginTypeSettings['*'].hasOwnProperty("exportable")) {
|
|
|
|
defaultExportable = pluginTypeSettings['*'].exportable;
|
|
|
|
}
|
|
|
|
if (defaultExportable) {
|
|
|
|
exportedSet = clone(settings[id]);
|
|
|
|
}
|
|
|
|
for (let property in pluginTypeSettings) {
|
|
|
|
if (pluginTypeSettings.hasOwnProperty(property)) {
|
|
|
|
let setting = pluginTypeSettings[property];
|
|
|
|
if (defaultExportable) {
|
|
|
|
if (setting.exportable === false) {
|
|
|
|
delete exportedSet[property]
|
|
|
|
} else if (!exportedSet.hasOwnProperty(property) && setting.hasOwnProperty('value')) {
|
|
|
|
exportedSet[property] = setting.value;
|
|
|
|
}
|
|
|
|
} else if (setting.exportable) {
|
|
|
|
if (settings[id].hasOwnProperty(property)) {
|
|
|
|
exportedSet[property] = settings[id][property];
|
|
|
|
} else if (setting.hasOwnProperty('value')) {
|
|
|
|
exportedSet[property] = setting.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Object.keys(exportedSet).length > 0) {
|
|
|
|
safeSettings[id] = exportedSet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return safeSettings;
|
|
|
|
}
|
|
|
|
|
2020-12-10 17:01:55 +01:00
|
|
|
module.exports = {
|
|
|
|
init,
|
|
|
|
registerPlugin,
|
|
|
|
getPlugin,
|
|
|
|
getPluginsByType,
|
|
|
|
getPluginConfigs,
|
2021-02-19 12:59:49 +01:00
|
|
|
getPluginList,
|
|
|
|
exportPluginSettings
|
2020-12-10 17:01:55 +01:00
|
|
|
}
|