2012-03-15 18:25:06 +01:00
|
|
|
var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
|
|
|
|
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
|
|
|
|
var npm = require("npm");
|
2012-10-28 18:34:20 +01:00
|
|
|
|
2013-03-25 17:22:51 +01:00
|
|
|
var npmIsLoaded = false;
|
|
|
|
var withNpm = function (npmfn) {
|
|
|
|
if(npmIsLoaded) return npmfn();
|
2012-03-15 18:25:06 +01:00
|
|
|
npm.load({}, function (er) {
|
2013-03-25 16:51:12 +01:00
|
|
|
if (er) return cb(er);
|
2013-03-25 17:22:51 +01:00
|
|
|
npmIsLoaded = true;
|
2012-03-19 17:16:49 +01:00
|
|
|
npm.on("log", function (message) {
|
2013-03-25 16:51:12 +01:00
|
|
|
console.log('npm: ',message)
|
2012-03-19 17:16:49 +01:00
|
|
|
});
|
2013-03-25 16:51:12 +01:00
|
|
|
npmfn();
|
2012-03-19 17:16:49 +01:00
|
|
|
});
|
2012-03-15 18:25:06 +01:00
|
|
|
}
|
|
|
|
|
2012-03-19 17:16:49 +01:00
|
|
|
exports.uninstall = function(plugin_name, cb) {
|
2013-03-25 16:51:12 +01:00
|
|
|
withNpm(function () {
|
|
|
|
npm.commands.uninstall([plugin_name], function (er) {
|
|
|
|
if (er) return cb && cb(er);
|
|
|
|
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name}, function (er, data) {
|
2012-03-19 17:16:49 +01:00
|
|
|
if (er) return cb(er);
|
2013-03-25 16:51:12 +01:00
|
|
|
plugins.update(cb);
|
|
|
|
cb && cb();
|
|
|
|
hooks.aCallAll("restartServer", {}, function () {});
|
2012-03-19 17:16:49 +01:00
|
|
|
});
|
2013-03-25 16:51:12 +01:00
|
|
|
});
|
|
|
|
});
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|
|
|
|
|
2012-03-15 18:25:06 +01:00
|
|
|
exports.install = function(plugin_name, cb) {
|
2013-03-25 16:51:12 +01:00
|
|
|
withNpm(function () {
|
2012-03-19 17:16:49 +01:00
|
|
|
npm.commands.install([plugin_name], function (er) {
|
2013-03-25 16:51:12 +01:00
|
|
|
if (er) return cb && cb(er);
|
2012-03-19 17:16:49 +01:00
|
|
|
hooks.aCallAll("pluginInstall", {plugin_name: plugin_name}, function (er, data) {
|
|
|
|
if (er) return cb(er);
|
|
|
|
plugins.update(cb);
|
2013-03-25 16:51:12 +01:00
|
|
|
cb && cb();
|
|
|
|
hooks.aCallAll("restartServer", {}, function () {});
|
2012-03-19 17:16:49 +01:00
|
|
|
});
|
2012-03-15 18:25:06 +01:00
|
|
|
});
|
2013-03-25 16:51:12 +01:00
|
|
|
});
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|
2012-03-15 18:25:06 +01:00
|
|
|
|
2013-03-25 16:51:12 +01:00
|
|
|
exports.availablePlugins = null;
|
2013-03-25 12:45:23 +01:00
|
|
|
var cacheTimestamp = 0;
|
2012-04-18 13:43:34 +02:00
|
|
|
|
2013-03-25 16:51:12 +01:00
|
|
|
exports.getAvailablePlugins = function(maxCacheAge, cb) {
|
|
|
|
withNpm(function () {
|
|
|
|
if(exports.availablePlugins && maxCacheAge && Math.round(+new Date/1000)-cacheTimestamp <= maxCacheAge) {
|
|
|
|
return cb && cb(null, exports.availablePlugins)
|
|
|
|
}
|
|
|
|
npm.commands.search(['ep_'], function(er, results) {
|
|
|
|
if(er) return cb && cb(er);
|
|
|
|
exports.availablePlugins = results;
|
|
|
|
cacheTimestamp = Math.round(+new Date/1000);
|
|
|
|
cb && cb(null, results)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.search = function(searchTerm, maxCacheAge, cb) {
|
|
|
|
exports.getAvailablePlugins(maxCacheAge, function(er, results) {
|
|
|
|
if(er) return cb && cb(er);
|
|
|
|
var res = {};
|
|
|
|
searchTerm = searchTerm.toLowerCase();
|
|
|
|
for (var pluginName in results) { // for every available plugin
|
|
|
|
if (pluginName.indexOf(plugins.prefix) != 0) continue; // TODO: Also search in keywords here!
|
|
|
|
if(pluginName.indexOf(searchTerm) < 0 && results[pluginName].description.indexOf(searchTerm) < 0) continue;
|
|
|
|
res[pluginName] = results[pluginName];
|
|
|
|
}
|
|
|
|
cb && cb(null, res)
|
|
|
|
})
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|