2014-12-26 18:54:20 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-10-14 21:48:38 +03:00
|
|
|
var path = require('path');
|
|
|
|
|
var semver = require('semver');
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var winston = require('winston');
|
|
|
|
|
var nconf = require('nconf');
|
2014-12-26 18:54:20 -05:00
|
|
|
|
2017-05-17 17:25:41 -06:00
|
|
|
var meta = require('../meta');
|
2014-12-26 18:54:20 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Plugins) {
|
2017-05-17 17:25:41 -06:00
|
|
|
function registerPluginAssets(pluginData, fields, callback) {
|
|
|
|
|
function add(dest, arr) {
|
|
|
|
|
dest.push.apply(dest, arr || []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var handlers = {
|
|
|
|
|
staticDirs: function (next) {
|
|
|
|
|
Plugins.data.getStaticDirectories(pluginData, next);
|
2016-11-19 14:24:37 -05:00
|
|
|
},
|
2017-05-17 17:25:41 -06:00
|
|
|
cssFiles: function (next) {
|
|
|
|
|
Plugins.data.getFiles(pluginData, 'css', next);
|
|
|
|
|
},
|
|
|
|
|
lessFiles: function (next) {
|
|
|
|
|
Plugins.data.getFiles(pluginData, 'less', next);
|
|
|
|
|
},
|
|
|
|
|
clientScripts: function (next) {
|
|
|
|
|
Plugins.data.getScripts(pluginData, 'client', next);
|
|
|
|
|
},
|
|
|
|
|
acpScripts: function (next) {
|
|
|
|
|
Plugins.data.getScripts(pluginData, 'acp', next);
|
|
|
|
|
},
|
|
|
|
|
modules: function (next) {
|
|
|
|
|
Plugins.data.getModules(pluginData, next);
|
|
|
|
|
},
|
|
|
|
|
soundpack: function (next) {
|
|
|
|
|
Plugins.data.getSoundpack(pluginData, next);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var methods;
|
|
|
|
|
if (Array.isArray(fields)) {
|
|
|
|
|
methods = fields.reduce(function (prev, field) {
|
|
|
|
|
prev[field] = handlers[field];
|
|
|
|
|
return prev;
|
|
|
|
|
}, {});
|
|
|
|
|
} else {
|
|
|
|
|
methods = handlers;
|
|
|
|
|
}
|
2016-11-19 14:24:37 -05:00
|
|
|
|
2017-05-17 17:25:41 -06:00
|
|
|
async.parallel(methods, function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2016-11-19 14:24:37 -05:00
|
|
|
|
2017-05-17 17:25:41 -06:00
|
|
|
Object.assign(Plugins.staticDirs, results.staticDirs || {});
|
|
|
|
|
add(Plugins.cssFiles, results.cssFiles);
|
|
|
|
|
add(Plugins.lessFiles, results.lessFiles);
|
|
|
|
|
add(Plugins.clientScripts, results.clientScripts);
|
|
|
|
|
add(Plugins.acpScripts, results.acpScripts);
|
|
|
|
|
Object.assign(meta.js.scripts.modules, results.modules || {});
|
|
|
|
|
if (results.soundpack) {
|
|
|
|
|
Plugins.soundpacks.push(results.soundpack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-19 14:24:37 -05:00
|
|
|
|
|
|
|
|
Plugins.prepareForBuild = function (callback) {
|
2016-12-21 15:18:43 +03:00
|
|
|
Plugins.cssFiles.length = 0;
|
|
|
|
|
Plugins.lessFiles.length = 0;
|
|
|
|
|
Plugins.clientScripts.length = 0;
|
|
|
|
|
Plugins.acpScripts.length = 0;
|
2017-02-17 11:19:30 -07:00
|
|
|
Plugins.soundpacks.length = 0;
|
2016-12-23 15:58:40 +03:00
|
|
|
|
2016-11-19 14:24:37 -05:00
|
|
|
async.waterfall([
|
2017-05-17 17:25:41 -06:00
|
|
|
Plugins.data.getActive,
|
2016-11-20 13:33:35 +03:00
|
|
|
function (plugins, next) {
|
|
|
|
|
async.each(plugins, function (pluginData, next) {
|
2017-05-17 17:25:41 -06:00
|
|
|
// TODO: only load the data that's needed for the build
|
|
|
|
|
registerPluginAssets(pluginData, true, next);
|
2016-11-19 14:24:37 -05:00
|
|
|
}, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-19 14:24:37 -05:00
|
|
|
], callback);
|
|
|
|
|
};
|
2014-12-26 18:54:20 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Plugins.loadPlugin = function (pluginPath, callback) {
|
2017-05-17 17:25:41 -06:00
|
|
|
Plugins.data.loadPluginInfo(pluginPath, function (err, pluginData) {
|
2014-12-26 18:54:20 -05:00
|
|
|
if (err) {
|
2015-03-11 18:04:27 -04:00
|
|
|
if (err.message === '[[error:parse-error]]') {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2014-12-26 18:54:20 -05:00
|
|
|
return callback(pluginPath.match('nodebb-theme') ? null : err);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 11:01:59 -04:00
|
|
|
checkVersion(pluginData);
|
2014-12-26 18:54:20 -05:00
|
|
|
|
|
|
|
|
async.parallel([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2017-05-17 17:25:41 -06:00
|
|
|
registerHooks(pluginData, next);
|
2014-12-26 18:54:20 -05:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2017-05-17 17:25:41 -06:00
|
|
|
// TODO: change this from `true` to `['soundpack']`
|
|
|
|
|
// this will skip several build-only plugin loading methods
|
|
|
|
|
// and only load soundpacks, which will speed up startup
|
|
|
|
|
registerPluginAssets(pluginData, true, next);
|
2017-02-16 22:56:25 -07:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
], function (err) {
|
2014-12-26 18:54:20 -05:00
|
|
|
if (err) {
|
|
|
|
|
winston.verbose('[plugins] Could not load plugin : ' + pluginData.id);
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
winston.verbose('[plugins] Loaded plugin: ' + pluginData.id);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-20 11:01:59 -04:00
|
|
|
function checkVersion(pluginData) {
|
|
|
|
|
function add() {
|
|
|
|
|
if (Plugins.versionWarning.indexOf(pluginData.id) === -1) {
|
|
|
|
|
Plugins.versionWarning.push(pluginData.id);
|
|
|
|
|
}
|
2015-03-07 00:59:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pluginData.nbbpm && pluginData.nbbpm.compatibility && semver.validRange(pluginData.nbbpm.compatibility)) {
|
2015-08-20 10:29:58 -04:00
|
|
|
if (!semver.satisfies(nconf.get('version'), pluginData.nbbpm.compatibility)) {
|
2015-08-20 11:01:59 -04:00
|
|
|
add();
|
2015-03-07 00:59:03 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2015-08-20 11:01:59 -04:00
|
|
|
add();
|
2015-03-07 00:59:03 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 17:25:41 -06:00
|
|
|
function registerHooks(pluginData, callback) {
|
2014-12-26 18:54:20 -05:00
|
|
|
if (!pluginData.library) {
|
2015-06-02 17:35:41 -04:00
|
|
|
return callback();
|
2014-12-26 18:54:20 -05:00
|
|
|
}
|
|
|
|
|
|
2017-05-17 17:25:41 -06:00
|
|
|
var libraryPath = path.join(pluginData.path, pluginData.library);
|
2014-12-26 18:54:20 -05:00
|
|
|
|
2015-01-12 23:24:19 -05:00
|
|
|
try {
|
2014-12-26 18:54:20 -05:00
|
|
|
if (!Plugins.libraries[pluginData.id]) {
|
2015-01-06 23:29:48 -05:00
|
|
|
Plugins.requireLibrary(pluginData.id, libraryPath);
|
2014-12-26 18:54:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(pluginData.hooks) && pluginData.hooks.length > 0) {
|
2016-10-13 11:43:39 +02:00
|
|
|
async.each(pluginData.hooks, function (hook, next) {
|
2014-12-26 18:54:20 -05:00
|
|
|
Plugins.registerHook(pluginData.id, hook, next);
|
|
|
|
|
}, callback);
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2017-02-18 01:52:56 -07:00
|
|
|
} catch (err) {
|
2015-01-12 23:24:19 -05:00
|
|
|
winston.error(err.stack);
|
2015-06-02 17:35:41 -04:00
|
|
|
winston.warn('[plugins] Unable to parse library for: ' + pluginData.id);
|
|
|
|
|
callback();
|
2015-01-12 23:24:19 -05:00
|
|
|
}
|
2014-12-26 18:54:20 -05:00
|
|
|
}
|
2016-08-24 17:48:04 -04:00
|
|
|
};
|