| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 14:03:06 +03:00
										 |  |  | var winston = require('winston'); | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | var path = require('path'); | 
					
						
							|  |  |  | var fs = require('fs'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var os = require('os'); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 14:03:06 +03:00
										 |  |  | var db = require('../database'); | 
					
						
							|  |  |  | var meta = require('../meta'); | 
					
						
							|  |  |  | var pubsub = require('../pubsub'); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (Plugins) { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 20:18:05 -05:00
										 |  |  | 	if (nconf.get('isPrimary') === 'true') { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		pubsub.on('plugins:toggleInstall', function (data) { | 
					
						
							| 
									
										
										
										
											2014-12-26 20:18:05 -05:00
										 |  |  | 			if (data.hostname !== os.hostname()) { | 
					
						
							|  |  |  | 				toggleInstall(data.id, data.version); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		pubsub.on('plugins:upgrade', function (data) { | 
					
						
							| 
									
										
										
										
											2014-12-26 20:18:05 -05:00
										 |  |  | 			if (data.hostname !== os.hostname()) { | 
					
						
							|  |  |  | 				upgrade(data.id, data.version); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Plugins.toggleActive = function (id, callback) { | 
					
						
							|  |  |  | 		callback = callback || function () {}; | 
					
						
							| 
									
										
										
										
											2014-12-26 19:17:37 -05:00
										 |  |  | 		var isActive; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2014-12-26 19:17:37 -05:00
										 |  |  | 				Plugins.isActive(id, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (_isActive, next) { | 
					
						
							| 
									
										
										
										
											2014-12-26 19:17:37 -05:00
										 |  |  | 				isActive = _isActive; | 
					
						
							| 
									
										
										
										
											2015-02-23 14:57:22 -05:00
										 |  |  | 				if (isActive) { | 
					
						
							|  |  |  | 					db.sortedSetRemove('plugins:active', id, next); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					db.sortedSetCard('plugins:active', function (err, count) { | 
					
						
							| 
									
										
										
										
											2015-02-23 14:57:22 -05:00
										 |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2015-09-12 15:14:49 -04:00
										 |  |  | 						db.sortedSetAdd('plugins:active', count, id, next); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2015-02-23 14:57:22 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-12-26 19:17:37 -05:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2014-12-26 19:17:37 -05:00
										 |  |  | 				meta.reloadRequired = true; | 
					
						
							|  |  |  | 				Plugins.fireHook(isActive ? 'action:plugin.deactivate' : 'action:plugin.activate', id); | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		], function (err) { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				winston.warn('[plugins] Could not toggle active state on plugin \'' + id + '\''); | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-12-26 19:17:37 -05:00
										 |  |  | 			callback(null, {id: id, active: !isActive}); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Plugins.toggleInstall = function (id, version, callback) { | 
					
						
							| 
									
										
										
										
											2014-12-26 20:18:05 -05:00
										 |  |  | 		pubsub.publish('plugins:toggleInstall', {hostname: os.hostname(), id: id, version: version}); | 
					
						
							|  |  |  | 		toggleInstall(id, version, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function toggleInstall(id, version, callback) { | 
					
						
							| 
									
										
										
										
											2016-11-18 14:03:06 +03:00
										 |  |  | 		var type; | 
					
						
							|  |  |  | 		var installed; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				Plugins.isInstalled(id, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (_installed, next) { | 
					
						
							|  |  |  | 				installed = _installed; | 
					
						
							|  |  |  | 				type = installed ? 'uninstall' : 'install'; | 
					
						
							|  |  |  | 				Plugins.isActive(id, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (active, next) { | 
					
						
							|  |  |  | 				if (active) { | 
					
						
							|  |  |  | 					Plugins.toggleActive(id, function (err, status) { | 
					
						
							|  |  |  | 						next(err); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					return; | 
					
						
							| 
									
										
										
										
											2015-01-15 12:34:11 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-11-18 14:03:06 +03:00
										 |  |  | 				next(); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2016-12-14 09:28:26 -05:00
										 |  |  | 				runNpmCommand(type, id, version || 'latest', next); | 
					
						
							| 
									
										
										
										
											2016-11-18 14:03:06 +03:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				Plugins.get(id, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (pluginData, next) { | 
					
						
							|  |  |  | 				Plugins.fireHook('action:plugin.' + type, id); | 
					
						
							|  |  |  | 				next(null, pluginData); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2014-12-26 20:18:05 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-14 09:28:26 -05:00
										 |  |  | 	function runNpmCommand(command, pkgName, version, callback) { | 
					
						
							|  |  |  | 		require('child_process').execFile('npm', [command, pkgName + (command === 'install' ? '@' + version : '')], function (err, stdout) { | 
					
						
							| 
									
										
										
										
											2016-01-12 13:53:26 +02:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-12-14 09:28:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			winston.verbose('[plugins/' + command + '] ' + stdout); | 
					
						
							| 
									
										
										
										
											2016-11-18 14:03:06 +03:00
										 |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2016-01-12 13:53:26 +02:00
										 |  |  | 		 }); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Plugins.upgrade = function (id, version, callback) { | 
					
						
							| 
									
										
										
										
											2014-12-26 20:18:05 -05:00
										 |  |  | 		pubsub.publish('plugins:upgrade', {hostname: os.hostname(), id: id, version: version}); | 
					
						
							|  |  |  | 		upgrade(id, version, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function upgrade(id, version, callback) { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-12-14 09:28:26 -05:00
										 |  |  | 			async.apply(runNpmCommand, 'install', id, version || 'latest'), | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2015-03-06 13:00:56 -05:00
										 |  |  | 				Plugins.isActive(id, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (isActive, next) { | 
					
						
							| 
									
										
										
										
											2015-03-06 13:00:56 -05:00
										 |  |  | 				meta.reloadRequired = isActive; | 
					
						
							|  |  |  | 				next(null, isActive); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2014-12-26 20:18:05 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Plugins.isInstalled = function (id, callback) { | 
					
						
							| 
									
										
										
										
											2014-12-26 20:18:05 -05:00
										 |  |  | 		var pluginDir = path.join(__dirname, '../../node_modules', id); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		fs.stat(pluginDir, function (err, stats) { | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			callback(null, err ? false : stats.isDirectory()); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Plugins.isActive = function (id, callback) { | 
					
						
							| 
									
										
										
										
											2015-02-23 14:57:22 -05:00
										 |  |  | 		db.isSortedSetMember('plugins:active', id, callback); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2015-02-23 15:55:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Plugins.getActive = function (callback) { | 
					
						
							| 
									
										
										
										
											2015-02-23 15:55:35 -05:00
										 |  |  | 		db.getSortedSetRange('plugins:active', 0, -1, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | }; |