Skip to content

Node.js based forum software built for the modern web

issue #478 - hot-swapping of plugins

Julian Lam4353a9da25d2
diff --git a/.gitignore b/.gitignore
index 40ea2a6a86..478600f89f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,3 @@
-#################
-## npm
-#################
-
npm-debug.log
node_modules/
sftp-config.json
@@ -9,12 +5,10 @@ config.json
public/src/nodebb.min.js
public/config.json
public/css/*.css
-public/themes/*
-!/public/themes/vanilla
-!/public/themes/cerulean
-!/public/themes/modern
*.sublime-project
*.sublime-workspace
-plugins/*
.project
*.swp
+Vagrantfile
+.vagrant
+provision.sh
\ No newline at end of file
diff --git a/src/plugins.js b/src/plugins.js
index fe63e8f35c..4ce5104d9a 100644
--- a/src/plugins.js
+++ b/src/plugins.js
@@ -17,8 +17,31 @@ var fs = require('fs'),
if (this.initialized) return;
if (global.env === 'development') winston.info('[plugins] Initializing plugins system');
+ this.reload(function(err) {
+ if (err) {
+ if (global.env === 'development') winston.info('[plugins] NodeBB encountered a problem while loading plugins', err.message);
+ return;
+ }
+
+ if (global.env === 'development') winston.info('[plugins] Plugins OK');
+
+ plugins.initialized = true;
+ plugins.readyEvent.emit('ready');
+ });
+ },
+ ready: function(callback) {
+ if (!this.initialized) this.readyEvent.once('ready', callback);
+ else callback();
+ },
+ initialized: false,
+ reload: function(callback) {
var _self = this;
+ // Resetting all local plugin data
+ this.loadedHooks = {};
+ this.staticDirs = {};
+ this.cssFiles.length = 0;
+
// Read the list of activated plugins and require their libraries
async.waterfall([
function(next) {
@@ -47,23 +70,8 @@ var fs = require('fs'),
next();
}
- ], function(err) {
- if (err) {
- if (global.env === 'development') winston.info('[plugins] NodeBB encountered a problem while loading plugins', err.message);
- return;
- }
-
- if (global.env === 'development') winston.info('[plugins] Plugins OK');
-
- _self.initialized = true;
- _self.readyEvent.emit('ready');
- });
- },
- ready: function(callback) {
- if (!this.initialized) this.readyEvent.once('ready', callback);
- else callback();
+ ], callback);
},
- initialized: false,
loadPlugin: function(pluginPath, callback) {
var _self = this;
@@ -80,16 +88,25 @@ var fs = require('fs'),
fs.exists(libraryPath, function(exists) {
if (exists) {
- _self.libraries[pluginData.id] = require(libraryPath);
+ if (!_self.libraries[pluginData.id]) {
+ _self.libraries[pluginData.id] = require(libraryPath);
+ }
+ // Register hooks for this plugin
if (pluginData.hooks && Array.isArray(pluginData.hooks) && pluginData.hooks.length > 0) {
async.each(pluginData.hooks, function(hook, next) {
_self.registerHook(pluginData.id, hook, next);
}, next);
}
+ } else {
+ winston.warn('[plugins.reload] Library not found for plugin: ' + pluginData.id);
+ next();
}
});
- } else next();
+ } else {
+ winston.warn('[plugins.reload] Library not found for plugin: ' + pluginData.id);
+ next();
+ }
},
function(next) {
// Static Directories for Plugins
diff --git a/src/routes/plugins.js b/src/routes/plugins.js
new file mode 100644
index 0000000000..33e384e8d2
--- /dev/null
+++ b/src/routes/plugins.js
@@ -0,0 +1,25 @@
+var nconf = require('nconf'),
+ path = require('path'),
+ fs = require('fs'),
+ Plugins = require('../plugins'),
+
+ PluginRoutes = function(app) {
+ // Static Assets
+ app.get('/plugins/:id/*', function(req, res) {
+ var relPath = req.url.replace('/plugins/' + req.params.id, '');
+ if (Plugins.staticDirs[req.params.id]) {
+ var fullPath = path.join(Plugins.staticDirs[req.params.id], relPath);
+ fs.exists(fullPath, function(exists) {
+ if (exists) {
+ res.sendfile(fullPath);
+ } else {
+ res.redirect('/404');
+ }
+ })
+ } else {
+ res.redirect('/404');
+ }
+ });
+ };
+
+module.exports = PluginRoutes;
\ No newline at end of file
diff --git a/src/webserver.js b/src/webserver.js
index f82c01fa29..bf847d960f 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -151,19 +151,6 @@ var express = require('express'),
},
function(next) {
async.parallel([
- function(next) {
- // Static Directories for NodeBB Plugins
- plugins.ready(function () {
- for (d in plugins.staticDirs) {
- app.use(nconf.get('relative_path') + '/plugins/' + d, express.static(plugins.staticDirs[d]));
- if (process.env.NODE_ENV === 'development') {
- winston.info('Static directory routed for plugin: ' + d);
- }
- }
-
- next();
- });
- },
function(next) {
// Theme configuration
RDB.hmget('config', 'theme:type', 'theme:id', 'theme:staticDir', 'theme:templates', function(err, themeData) {
@@ -701,6 +688,9 @@ var express = require('express'),
});
});
+ // Other routes
+ require('./routes/plugins')(app);
+
// Debug routes
if (process.env.NODE_ENV === 'development') {
require('./routes/debug')(app);