issue #478 - hot-swapping of plugins

v1.18.x
Julian Lam 11 years ago
parent e480b1bace
commit 4353a9da25

12
.gitignore vendored

@ -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

@ -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

@ -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;

@ -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);

Loading…
Cancel
Save