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');
|
2017-06-01 16:24:40 -04:00
|
|
|
var events = require('../events');
|
2014-12-26 18:54:20 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Plugins) {
|
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;
|
2017-04-19 13:23:53 -04:00
|
|
|
Plugins.fireHook(isActive ? 'action:plugin.deactivate' : 'action:plugin.activate', { id: id });
|
2017-01-31 18:38:01 +03:00
|
|
|
setImmediate(next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2017-06-01 16:24:40 -04:00
|
|
|
function (next) {
|
|
|
|
|
events.log({
|
|
|
|
|
type: 'plugin-' + (isActive ? 'deactivate' : 'activate'),
|
|
|
|
|
text: 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);
|
|
|
|
|
}
|
2017-02-18 12:30:49 -07: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) {
|
2017-02-18 12:30:49 -07:00
|
|
|
pubsub.publish('plugins:toggleInstall', { hostname: os.hostname(), id: id, version: version });
|
2014-12-26 20:18:05 -05:00
|
|
|
toggleInstall(id, version, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function toggleInstall(id, version, callback) {
|
2016-11-18 14:03:06 +03:00
|
|
|
var installed;
|
2017-04-19 13:23:53 -04:00
|
|
|
var type;
|
2016-11-18 14:03:06 +03:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
Plugins.isInstalled(id, next);
|
|
|
|
|
},
|
|
|
|
|
function (_installed, next) {
|
|
|
|
|
installed = _installed;
|
2017-04-19 13:23:53 -04:00
|
|
|
type = installed ? 'uninstall' : 'install';
|
2016-11-18 14:03:06 +03:00
|
|
|
Plugins.isActive(id, next);
|
|
|
|
|
},
|
|
|
|
|
function (active, next) {
|
|
|
|
|
if (active) {
|
2017-02-18 15:05:36 -07:00
|
|
|
Plugins.toggleActive(id, function (err) {
|
2016-11-18 14:03:06 +03:00
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
return;
|
2015-01-15 12:34:11 -05:00
|
|
|
}
|
2017-01-31 18:38:01 +03:00
|
|
|
setImmediate(next);
|
2016-11-18 14:03:06 +03:00
|
|
|
},
|
|
|
|
|
function (next) {
|
2017-04-19 13:23:53 -04:00
|
|
|
runNpmCommand(type, id, version || 'latest', next);
|
2016-11-18 14:03:06 +03:00
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
Plugins.get(id, next);
|
|
|
|
|
},
|
|
|
|
|
function (pluginData, next) {
|
2017-04-19 13:23:53 -04:00
|
|
|
Plugins.fireHook('action:plugin.' + type, { id: id, version: version });
|
2017-01-31 18:38:01 +03:00
|
|
|
setImmediate(next, null, pluginData);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-18 14:03:06 +03:00
|
|
|
], 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) {
|
2017-06-01 14:12:25 -06:00
|
|
|
require('child_process').execFile((process.platform === 'win32') ? 'npm.cmd' : 'npm', [command, pkgName + (command === 'install' ? '@' + version : ''), '--no-save'], 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-12-15 16:08:32 +03:00
|
|
|
});
|
2016-01-12 13:53:26 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Plugins.upgrade = function (id, version, callback) {
|
2017-02-18 12:30:49 -07:00
|
|
|
pubsub.publish('plugins:upgrade', { hostname: os.hostname(), id: id, version: version });
|
2014-12-26 20:18:05 -05:00
|
|
|
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);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
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);
|
|
|
|
|
};
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|