diff --git a/plugins/content/bower/index.js b/plugins/content/bower/index.js index 666484811b..ecc4accec8 100644 --- a/plugins/content/bower/index.js +++ b/plugins/content/bower/index.js @@ -803,93 +803,114 @@ function addPackage (plugin, packageInfo, options, cb) { return addCb(err); } - // don't duplicate component.name, component.version - db.retrieve(plugin.type, { name: package.name, version: package.version }, function (err, results) { + const targetAttribute = pkgMeta.targetAttribute; + + async.some([ 'componenttype', 'extensiontype', 'menutype', 'themetype' ], (type, asyncCallback) => { + if (!targetAttribute) return asyncCallback(); + + db.retrieve(type, { targetAttribute: targetAttribute }, (err, results) => { + asyncCallback(err, results && results.length); + }); + }, (err, targetAttributeExists) => { if (err) { logger.log('error', err); return addCb(err); } - if (results && 0 !== results.length) { - // don't add duplicate - if (options.strict) { - return addCb(new PluginPackageError(app.polyglot.t('app.versionexists'))); - } - return addCb(null); + if (targetAttributeExists) { + return addCb(new PluginPackageError(app.polyglot.t('app.targetattributeexists', { + targetAttribute + }))); } - db.create(plugin.type, package, function (err, newPlugin) { + // don't duplicate component.name, component.version + db.retrieve(plugin.type, { name: package.name, version: package.version }, function (err, results) { if (err) { + logger.log('error', err); + return addCb(err); + } + + if (results && 0 !== results.length) { + // don't add duplicate if (options.strict) { - return addCb(err); + return addCb(new PluginPackageError(app.polyglot.t('app.versionexists'))); } - - logger.log('error', 'Failed to add package: ' + package.name, err); return addCb(null); } - logger.log('info', 'Added package: ' + package.name); - - // #509 update content targeted by previous versions of this package - logger.log('info', 'searching old package types ... '); - db.retrieve(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err, results) { - + db.create(plugin.type, package, function (err, newPlugin) { if (err) { - // strictness doesn't matter at this point - logger.log('error', 'Failed to retrieve previous packages: ' + err.message, err); + if (options.strict) { + return addCb(err); + } + + logger.log('error', 'Failed to add package: ' + package.name, err); + return addCb(null); } - if (results && results.length) { - // found previous versions to update - // only update content using the id of the most recent version - var oldPlugin = false; - results.forEach(function (item) { - if (!oldPlugin) { - oldPlugin = item; - } else if (semver.gt(item.version, oldPlugin.version)) { - oldPlugin = item; - } - }); + logger.log('info', 'Added package: ' + package.name); - // Persist the _isAvailableInEditor flag. - db.update(plugin.type, {_id: newPlugin._id}, {_isAvailableInEditor: oldPlugin._isAvailableInEditor}, function(err, results) { - if (err) { - logger.log('error', err); - return addCb(err); - } + // #509 update content targeted by previous versions of this package + logger.log('info', 'searching old package types ... '); + db.retrieve(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err, results) { + + if (err) { + // strictness doesn't matter at this point + logger.log('error', 'Failed to retrieve previous packages: ' + err.message, err); + } + + if (results && results.length) { + // found previous versions to update + // only update content using the id of the most recent version + var oldPlugin = false; + results.forEach(function (item) { + if (!oldPlugin) { + oldPlugin = item; + } else if (semver.gt(item.version, oldPlugin.version)) { + oldPlugin = item; + } + }); - plugin.updateLegacyContent(newPlugin, oldPlugin, function (err) { + // Persist the _isAvailableInEditor flag. + db.update(plugin.type, {_id: newPlugin._id}, {_isAvailableInEditor: oldPlugin._isAvailableInEditor}, function(err, results) { if (err) { logger.log('error', err); return addCb(err); } - // Remove older versions of this plugin - db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) { + plugin.updateLegacyContent(newPlugin, oldPlugin, function (err) { if (err) { logger.log('error', err); return addCb(err); } - logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version); - return addCb(null, newPlugin); + // Remove older versions of this plugin + db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) { + if (err) { + logger.log('error', err); + return addCb(err); + } + + logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version); + return addCb(null, newPlugin); + }); }); }); - }); - } else { - // nothing to do! - // Remove older versions of this plugin - db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) { - if (err) { - logger.log('error', err); - return addCb(err); - } + } else { + // nothing to do! + // Remove older versions of this plugin + db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) { + if (err) { + logger.log('error', err); + return addCb(err); + } - logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version); + logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version); - return addCb(null, newPlugin); - }); - } + return addCb(null, newPlugin); + }); + } + }); }); }); }); diff --git a/routes/lang/en-application.json b/routes/lang/en-application.json index 41a9899f13..0fb8d29ad4 100644 --- a/routes/lang/en-application.json +++ b/routes/lang/en-application.json @@ -415,6 +415,7 @@ "app.unrecognisedplugin": "Unrecognised plugin - a plugin should have a bower.json file.", "app.unrecognisedpluginforpackage": "Unrecognised plugin type for package %{package}.", "app.incompatibleframework": "This plugin is incompatible with version %{framework} of the Adapt framework.", + "app.targetattributeexists": "There is a plugin already installed with a target attribute of '%{targetAttribute}'.", "app.versionexists": "You already have this version of the plugin installed.", "app.unknownuser": "Unknown User" }