Add support for settings object in plugin definition

This commit is contained in:
Nick O'Leary 2021-02-19 11:59:49 +00:00
parent 3f9a29730f
commit c744af161d
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 61 additions and 8 deletions

View File

@ -304,7 +304,8 @@ module.exports = {
getPluginsByType: plugins.getPluginsByType,
getPluginList: plugins.getPluginList,
getPluginConfigs: plugins.getPluginConfigs,
exportPluginSettings: plugins.exportPluginSettings,
deprecated: require("./deprecated")
};

View File

@ -1,11 +1,13 @@
const registry = require("./registry");
const {events} = require("@node-red/util")
const {events} = require("@node-red/util");
const clone = require("clone");
var pluginConfigCache = {};
var pluginToId = {};
var plugins = {};
var pluginsByType = {};
var settings;
let pluginConfigCache = {};
let pluginToId = {};
let plugins = {};
let pluginsByType = {};
let pluginSettings = {};
let settings;
function init(_settings) {
settings = _settings;
@ -13,6 +15,7 @@ function init(_settings) {
pluginConfigCache = {};
pluginToId = {};
pluginsByType = {};
pluginSettings = {};
}
function registerPlugin(nodeSetId,id,definition) {
@ -32,6 +35,11 @@ function registerPlugin(nodeSetId,id,definition) {
pluginsByType[definition.type] = pluginsByType[definition.type] || [];
pluginsByType[definition.type].push(definition);
}
if (definition.settings) {
pluginSettings[id] = definition.settings;
}
if (definition.onadd && typeof definition.onadd === 'function') {
definition.onadd();
}
@ -93,11 +101,53 @@ function getPluginList() {
return list;
}
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;
}
module.exports = {
init,
registerPlugin,
getPlugin,
getPluginsByType,
getPluginConfigs,
getPluginList
getPluginList,
exportPluginSettings
}

View File

@ -130,6 +130,7 @@ var api = module.exports = {
safeSettings.flowEncryptionType = runtime.nodes.getCredentialKeyType();
runtime.settings.exportNodeSettings(safeSettings);
runtime.plugins.exportPluginSettings(safeSettings);
}
return safeSettings;

View File

@ -7,4 +7,5 @@ module.exports = {
getPluginsByType: registry.getPluginsByType,
getPluginList: registry.getPluginList,
getPluginConfigs: registry.getPluginConfigs,
exportPluginSettings: registry.exportPluginSettings
}