1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

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,6 +304,7 @@ module.exports = {
getPluginsByType: plugins.getPluginsByType, getPluginsByType: plugins.getPluginsByType,
getPluginList: plugins.getPluginList, getPluginList: plugins.getPluginList,
getPluginConfigs: plugins.getPluginConfigs, getPluginConfigs: plugins.getPluginConfigs,
exportPluginSettings: plugins.exportPluginSettings,
deprecated: require("./deprecated") deprecated: require("./deprecated")

View File

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

View File

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

View File

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