diff --git a/lib/core.js b/lib/core.js index e54548399..693f7bd49 100644 --- a/lib/core.js +++ b/lib/core.js @@ -944,19 +944,63 @@ for(var key in r.data) { var v = r.data[key]; + // TODO: work with arrays? if (v !== '' && v !== null && ((typeof v === 'string' && !/^\s+$/.test(v)) || typeof v === 'number' || typeof v === 'object')) { // Check meta plugins order. allResults.vars._sources = allResults.vars._sources || {}; - var prevOrder = null, nextOrder = null, pluginId = allResults.vars._sources[key]; + var prevOrder = null, nextOrder = null, prevPluginId = allResults.vars._sources[key]; - if (pluginId && plugins[pluginId] && plugins[r.method.pluginId]) { - prevOrder = plugins[pluginId].order; + if (prevPluginId && plugins[prevPluginId] && plugins[r.method.pluginId]) { + prevOrder = plugins[prevPluginId].order; nextOrder = plugins[r.method.pluginId].order; } - if (!prevOrder || !nextOrder || prevOrder < nextOrder) { + var oldValue = allResults.vars[key]; + + var newValueOverrides = !prevOrder || !nextOrder || prevOrder < nextOrder; + + if (typeof v === 'object' && typeof oldValue === 'object' || Array.isArray(oldValue)) { + + var isArrayMode = Array.isArray(v) || Array.isArray(oldValue); + + if (isArrayMode) { + // Join arrays mode. + if (!Array.isArray(v)) { + v = [v]; + } + if (!Array.isArray(oldValue)) { + oldValue = [oldValue]; + } + + // Keep order by priority. + if (newValueOverrides) { + allResults.vars[key] = oldValue.concat(v); + } else { + allResults.vars[key] = v.concat(oldValue); + } + + } else { + // Merge object mode. + if (newValueOverrides) { + // Extend existing object. + Object.assign(oldValue, v); + } else { + // Extend with old values on top. + allResults.vars[key] = Object.assign(v, oldValue); + } + } + + if (!Array.isArray(allResults.vars._sources[key])) { + // Convert sources list to array. + allResults.vars._sources[key] = [allResults.vars._sources[key]]; + } + allResults.vars._sources[key].push(r.method.pluginId); + + } else if (newValueOverrides) { + + // Assign new non object value. allResults.vars[key] = v; allResults.vars._sources[key] = r.method.pluginId; } diff --git a/lib/loader/pluginLoader.js b/lib/loader/pluginLoader.js index 55bf9473d..e9b7b807c 100644 --- a/lib/loader/pluginLoader.js +++ b/lib/loader/pluginLoader.js @@ -269,6 +269,23 @@ return; } + // Log custom plugin attributes. + // for(var plugin_key in plugin) { + // if (PLUGINS_FIELDS.indexOf(plugin_key) === -1 + // && PLUGIN_METHODS.indexOf(plugin_key) === -1 + // && ['provides', + // 'listed', + // 'tests', + // 're', + // 'notPlugin', + // 'highestPriority', + // 'lowestPriority', + // 'startIteration', + // 'finishIteration'].indexOf(plugin_key) === -1) { + // console.log('--- unknown plugin attr', getFileName(bits[bits.length - 1]), plugin_key) + // } + // } + // Check post plugin. // TODO: only prapereLink method abailable. // TODO: exclude prapereLink method from other plugins.