From 19297e2d2254026135491bfbb49942860db90274 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 7 Nov 2014 13:46:16 -0500 Subject: [PATCH] plugin installation takes a version now, and queries nbbpm, #2363 better error handling for #2363 upgrades now also call the suggestion route from nbbpm upgrade button will show alert now if you try to upgrade past the suggested version, #2363 --- public/src/admin/extend/plugins.js | 116 ++++++++++++++++++++--------- src/meta/configs.js | 4 +- src/plugins.js | 8 +- src/socket.io/admin.js | 8 +- 4 files changed, 93 insertions(+), 43 deletions(-) diff --git a/public/src/admin/extend/plugins.js b/public/src/admin/extend/plugins.js index 28262f22fb..d818b724ac 100644 --- a/public/src/admin/extend/plugins.js +++ b/public/src/admin/extend/plugins.js @@ -33,35 +33,16 @@ define('admin/extend/plugins', function() { pluginsList.on('click', 'button[data-action="toggleInstall"]', function() { pluginID = $(this).parents('li').attr('data-plugin-id'); - var btn = $(this); - var activateBtn = btn.siblings('[data-action="toggleActive"]'); - btn.html(btn.html() + 'ing') - .attr('disabled', true) - .find('i').attr('class', 'fa fa-refresh fa-spin'); - - socket.emit('admin.plugins.toggleInstall', pluginID, function(err, status) { - if (err) { - return app.alertError(err.message); - } - - if (status.installed) { - btn.html(' Uninstall'); + Plugins.suggest(pluginID, function(err, payload) { + if (!err) { + Plugins.toggleInstall(pluginID, payload.version); } else { - btn.html(' Install'); - + bootbox.confirm('

NodeBB could not reach the package manager, proceed with installation of latest version?

Server returned (' + err.status + '): ' + err.responseText + '
', function(confirm) { + if (confirm) { + Plugins.toggleInstall(pluginID, 'latest'); + } + }); } - activateBtn.toggleClass('hidden', !status.installed); - - btn.toggleClass('btn-danger', status.installed).toggleClass('btn-success', !status.installed) - .attr('disabled', false); - - app.alert({ - alert_id: 'plugin_toggled', - title: 'Plugin ' + (status.installed ? 'Installed' : 'Uninstalled'), - message: status.installed ? 'Plugin successfully installed, please activate the plugin.' : 'The plugin has been successfully deactivated and uninstalled.', - type: 'info', - timeout: 5000 - }); }); }); @@ -70,15 +51,30 @@ define('admin/extend/plugins', function() { var parent = btn.parents('li'); pluginID = parent.attr('data-plugin-id'); - btn.attr('disabled', true).find('i').attr('class', 'fa fa-refresh fa-spin'); - socket.emit('admin.plugins.upgrade', pluginID, function(err) { - if (err) { - return app.alertError(err.message); + Plugins.suggest(pluginID, function(err, payload) { + if (!err) { + require(['semver'], function(semver) { + if (semver.gt(payload.version, parent.find('.currentVersion').text())) { + btn.attr('disabled', true).find('i').attr('class', 'fa fa-refresh fa-spin'); + socket.emit('admin.plugins.upgrade', { + id: pluginID, + version: payload.version + }, function(err) { + if (err) { + return app.alertError(err.message); + } + parent.find('.fa-exclamation-triangle').remove(); + parent.find('.currentVersion').text(payload.version); + btn.remove(); + }); + } else { + bootbox.alert('

Your version of NodeBB (v' + app.config.version + ') is only cleared to upgrade to v' + payload.version + ' of this plugin. Please update your NodeBB if you wish to install a newer version of this plugin.'); + } + }); + } else { + bootbox.alert('

NodeBB could not reach the package manager, an upgrade is not suggested at this time.

'); } - parent.find('.fa-exclamation-triangle').remove(); - parent.find('.currentVersion').text(parent.find('.latestVersion').text()); - btn.remove(); }); }); @@ -94,6 +90,58 @@ define('admin/extend/plugins', function() { } else { pluginsList.append('
  • No plugins found.

  • '); } + }, + toggleInstall: function(pluginID, version, callback) { + var btn = $('li[data-plugin-id="' + pluginID + '"] button[data-action="toggleInstall"]'); + var activateBtn = btn.siblings('[data-action="toggleActive"]'); + btn.html(btn.html() + 'ing') + .attr('disabled', true) + .find('i').attr('class', 'fa fa-refresh fa-spin'); + + socket.emit('admin.plugins.toggleInstall', { + id: pluginID, + version: version + }, function(err, status) { + if (err) { + return app.alertError(err.message); + } + + if (status.installed) { + btn.html(' Uninstall'); + } else { + btn.html(' Install'); + + } + activateBtn.toggleClass('hidden', !status.installed); + + btn.toggleClass('btn-danger', status.installed).toggleClass('btn-success', !status.installed) + .attr('disabled', false); + + app.alert({ + alert_id: 'plugin_toggled', + title: 'Plugin ' + (status.installed ? 'Installed' : 'Uninstalled'), + message: status.installed ? 'Plugin successfully installed, please activate the plugin.' : 'The plugin has been successfully deactivated and uninstalled.', + type: 'info', + timeout: 5000 + }); + + if (typeof callback === 'function') { + callback.apply(this, arguments); + } + }); + }, + suggest: function(pluginId, callback) { + var nbbVersion = app.config.version; + $.ajax('https://packages.nodebb.org/api/v1/suggest', { + type: 'GET', + data: { + package: pluginId, + version: nbbVersion + }, + dataType: 'json' + }).done(function(payload) { + callback(undefined, payload); + }).fail(callback); } }; diff --git a/src/meta/configs.js b/src/meta/configs.js index 39198f57ec..65fb0de032 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -2,7 +2,8 @@ 'use strict'; var winston = require('winston'), - db = require('../database'); + db = require('../database'), + pkg = require('../../package.json'); module.exports = function(Meta) { @@ -25,6 +26,7 @@ module.exports = function(Meta) { Meta.configs.list = function (callback) { db.getObject('config', function (err, config) { + config.version = pkg.version; callback(err, config || {}); }); }; diff --git a/src/plugins.js b/src/plugins.js index 978da93e53..bc287d008f 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -550,7 +550,7 @@ var fs = require('fs'), }); }; - Plugins.toggleInstall = function(id, callback) { + Plugins.toggleInstall = function(id, version, callback) { Plugins.isInstalled(id, function(err, installed) { if (err) { return callback(err); @@ -573,7 +573,7 @@ var fs = require('fs'), npm.load({}, next); }, function(res, next) { - npm.commands[installed ? 'uninstall' : 'install'](installed ? id : [id], next); + npm.commands[installed ? 'uninstall' : 'install'](installed ? id : [id + '@' + (version || 'latest')], next); } ], function(err) { callback(err, { @@ -584,13 +584,13 @@ var fs = require('fs'), }); }; - Plugins.upgrade = function(id, callback) { + Plugins.upgrade = function(id, version, callback) { async.waterfall([ function(next) { npm.load({}, next); }, function(res, next) { - npm.commands.install([id], next); + npm.commands.install([id + '@' + (version || 'latest')], next); } ], callback); }; diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 0a84ddffa1..f4bec7ef43 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -97,12 +97,12 @@ SocketAdmin.plugins.toggleActive = function(socket, plugin_id, callback) { plugins.toggleActive(plugin_id, callback); }; -SocketAdmin.plugins.toggleInstall = function(socket, plugin_id, callback) { - plugins.toggleInstall(plugin_id, callback); +SocketAdmin.plugins.toggleInstall = function(socket, data, callback) { + plugins.toggleInstall(data.id, data.version, callback); }; -SocketAdmin.plugins.upgrade = function(socket, plugin_id, callback) { - plugins.upgrade(plugin_id, callback); +SocketAdmin.plugins.upgrade = function(socket, data, callback) { + plugins.upgrade(data.id, data.version, callback); }; SocketAdmin.widgets.set = function(socket, data, callback) {