| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2017-05-26 01:39:40 -06:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-08-21 17:48:58 -04:00
										 |  |  | 			acpLessFiles: function (next) { | 
					
						
							|  |  |  | 				Plugins.data.getFiles(pluginData, 'acpLess', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-05-17 17:25:41 -06:00
										 |  |  | 			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); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-05-23 21:55:23 -06:00
										 |  |  | 			languageData: function (next) { | 
					
						
							|  |  |  | 				Plugins.data.getLanguageData(pluginData, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-05-17 17:25:41 -06:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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); | 
					
						
							| 
									
										
										
										
											2017-08-21 17:48:58 -04:00
										 |  |  | 			add(Plugins.acpLessFiles, results.acpLessFiles); | 
					
						
							| 
									
										
										
										
											2017-05-17 17:25:41 -06:00
										 |  |  | 			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); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-05-23 21:55:23 -06:00
										 |  |  | 			if (results.languageData) { | 
					
						
							|  |  |  | 				Plugins.languageData.languages = _.union(Plugins.languageData.languages, results.languageData.languages); | 
					
						
							|  |  |  | 				Plugins.languageData.namespaces = _.union(Plugins.languageData.namespaces, results.languageData.namespaces); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			Plugins.pluginsData[pluginData.id] = pluginData; | 
					
						
							| 
									
										
										
										
											2017-05-17 17:25:41 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			callback(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-19 14:24:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 01:32:20 -06:00
										 |  |  | 	Plugins.prepareForBuild = function (targets, callback) { | 
					
						
							| 
									
										
										
										
											2019-01-08 19:28:30 -05:00
										 |  |  | 		if (targets.includes('client js bundle')) { | 
					
						
							|  |  |  | 			Plugins.clientScripts.length = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (targets.includes('admin js bundle')) { | 
					
						
							|  |  |  | 			Plugins.acpScripts.length = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (targets.includes('client side styles') || targets.includes('admin control panel styles')) { | 
					
						
							|  |  |  | 			Plugins.cssFiles.length = 0; | 
					
						
							|  |  |  | 			Plugins.lessFiles.length = 0; | 
					
						
							|  |  |  | 			if (targets.includes('admin control panel styles')) { | 
					
						
							|  |  |  | 				Plugins.acpLessFiles.length = 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (targets.includes('sounds')) { | 
					
						
							|  |  |  | 			Plugins.soundpacks.length = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (targets.includes('languages')) { | 
					
						
							|  |  |  | 			Plugins.languageData.languages = []; | 
					
						
							|  |  |  | 			Plugins.languageData.namespaces = []; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-23 15:58:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 01:32:20 -06:00
										 |  |  | 		var map = { | 
					
						
							|  |  |  | 			'plugin static dirs': ['staticDirs'], | 
					
						
							|  |  |  | 			'requirejs modules': ['modules'], | 
					
						
							|  |  |  | 			'client js bundle': ['clientScripts'], | 
					
						
							|  |  |  | 			'admin js bundle': ['acpScripts'], | 
					
						
							|  |  |  | 			'client side styles': ['cssFiles', 'lessFiles'], | 
					
						
							| 
									
										
										
										
											2017-08-21 17:48:58 -04:00
										 |  |  | 			'admin control panel styles': ['cssFiles', 'lessFiles', 'acpLessFiles'], | 
					
						
							| 
									
										
										
										
											2017-05-18 01:32:20 -06:00
										 |  |  | 			sounds: ['soundpack'], | 
					
						
							| 
									
										
										
										
											2017-05-23 21:55:23 -06:00
										 |  |  | 			languages: ['languageData'], | 
					
						
							| 
									
										
										
										
											2017-05-18 01:32:20 -06:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var fields = targets.reduce(function (prev, target) { | 
					
						
							|  |  |  | 			if (!map[target]) { | 
					
						
							|  |  |  | 				return prev; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return prev.concat(map[target]); | 
					
						
							|  |  |  | 		}, []).filter(function (field, i, arr) { | 
					
						
							|  |  |  | 			return arr.indexOf(field) === i; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		winston.verbose('[plugins] loading the following fields from plugin data: ' + fields.join(', ')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-18 01:32:20 -06:00
										 |  |  | 					registerPluginAssets(pluginData, fields, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-09 16:26:28 -06:00
										 |  |  | 	var themeNamePattern = /(@.*?\/)?nodebb-theme-.*$/; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-06-09 16:26:28 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				return callback(themeNamePattern.test(pluginPath) ? null : err); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-18 01:32:20 -06:00
										 |  |  | 					registerPluginAssets(pluginData, ['soundpack'], 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() { | 
					
						
							| 
									
										
										
										
											2018-10-20 14:40:48 -04:00
										 |  |  | 			if (!Plugins.versionWarning.includes(pluginData.id)) { | 
					
						
							| 
									
										
										
										
											2015-08-20 11:01:59 -04:00
										 |  |  | 				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
										 |  |  | }; |