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");
|
2014-11-04 18:06:47 +01:00
|
|
|
var request = require("request");
|
2012-10-28 18:34:20 +01:00
|
|
|
|
2013-03-25 17:22:51 +01:00
|
|
|
var npmIsLoaded = false;
|
2019-02-08 23:20:57 +01:00
|
|
|
var withNpm = function(npmfn) {
|
|
|
|
if (npmIsLoaded) return npmfn();
|
|
|
|
|
|
|
|
npm.load({}, function(er) {
|
2013-03-26 11:20:12 +01:00
|
|
|
if (er) return npmfn(er);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2013-03-25 17:22:51 +01:00
|
|
|
npmIsLoaded = true;
|
2019-02-08 23:20:57 +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
|
|
|
}
|
|
|
|
|
2013-04-08 16:14:03 +02:00
|
|
|
var tasks = 0
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2013-04-08 16:14:03 +02:00
|
|
|
function wrapTaskCb(cb) {
|
2019-02-08 23:20:57 +01:00
|
|
|
tasks++;
|
|
|
|
|
2013-04-08 16:14:03 +02:00
|
|
|
return function() {
|
|
|
|
cb && cb.apply(this, arguments);
|
|
|
|
tasks--;
|
2019-02-08 23:20:57 +01:00
|
|
|
if (tasks == 0) onAllTasksFinished();
|
2013-04-08 16:14:03 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2013-04-08 16:14:03 +02:00
|
|
|
function onAllTasksFinished() {
|
2019-02-08 23:20:57 +01:00
|
|
|
hooks.aCallAll("restartServer", {}, function() {});
|
2013-04-08 16:14:03 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:10:48 +01:00
|
|
|
/*
|
|
|
|
* We cannot use arrow functions in this file, because code in /src/static
|
|
|
|
* can end up being loaded in browsers, and we still support IE11.
|
|
|
|
*/
|
2012-03-19 17:16:49 +01:00
|
|
|
exports.uninstall = function(plugin_name, cb) {
|
2013-04-08 16:14:03 +02:00
|
|
|
cb = wrapTaskCb(cb);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
withNpm(function(er) {
|
2013-03-26 11:20:12 +01:00
|
|
|
if (er) return cb && cb(er);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
npm.commands.uninstall([plugin_name], function(er) {
|
2013-03-25 16:51:12 +01:00
|
|
|
if (er) return cb && cb(er);
|
2019-01-18 17:10:48 +01:00
|
|
|
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name})
|
|
|
|
.then(plugins.update)
|
|
|
|
.then(function() { cb(null) })
|
|
|
|
.catch(function(er) { cb(er) });
|
2013-03-25 16:51:12 +01:00
|
|
|
});
|
|
|
|
});
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|
|
|
|
|
2019-01-18 17:10:48 +01:00
|
|
|
/*
|
|
|
|
* We cannot use arrow functions in this file, because code in /src/static
|
|
|
|
* can end up being loaded in browsers, and we still support IE11.
|
|
|
|
*/
|
2012-03-15 18:25:06 +01:00
|
|
|
exports.install = function(plugin_name, cb) {
|
2019-02-08 23:20:57 +01:00
|
|
|
cb = wrapTaskCb(cb);
|
|
|
|
|
|
|
|
withNpm(function(er) {
|
2013-03-26 11:20:12 +01:00
|
|
|
if (er) return cb && cb(er);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
npm.commands.install([plugin_name], function(er) {
|
2013-03-26 11:20:12 +01:00
|
|
|
if (er) return cb && cb(er);
|
2019-01-18 17:10:48 +01:00
|
|
|
hooks.aCallAll("pluginInstall", {plugin_name: plugin_name})
|
|
|
|
.then(plugins.update)
|
|
|
|
.then(function() { cb(null) })
|
|
|
|
.catch(function(er) { cb(er) });
|
2013-03-26 11:20:12 +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
|
|
|
|
2019-01-23 13:24:53 +01:00
|
|
|
exports.getAvailablePlugins = function(maxCacheAge) {
|
|
|
|
var nowTimestamp = Math.round(Date.now() / 1000);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-23 13:24:53 +01:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
// check cache age before making any request
|
|
|
|
if (exports.availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
|
|
|
|
return resolve(exports.availablePlugins);
|
2013-03-25 16:51:12 +01:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-23 13:24:53 +01:00
|
|
|
request("https://static.etherpad.org/plugins.json", function(er, response, plugins) {
|
|
|
|
if (er) return reject(er);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-23 13:24:53 +01:00
|
|
|
try {
|
|
|
|
plugins = JSON.parse(plugins);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('error parsing plugins.json:', err);
|
|
|
|
plugins = [];
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-23 13:24:53 +01:00
|
|
|
exports.availablePlugins = plugins;
|
|
|
|
cacheTimestamp = nowTimestamp;
|
|
|
|
resolve(plugins);
|
|
|
|
});
|
2013-03-25 16:51:12 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-01-23 13:24:53 +01:00
|
|
|
exports.search = function(searchTerm, maxCacheAge) {
|
|
|
|
return exports.getAvailablePlugins(maxCacheAge).then(function(results) {
|
2013-03-25 16:51:12 +01:00
|
|
|
var res = {};
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
if (searchTerm) {
|
2013-09-10 19:46:10 +02:00
|
|
|
searchTerm = searchTerm.toLowerCase();
|
2019-02-08 23:20:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var pluginName in results) {
|
|
|
|
// for every available plugin
|
2013-03-25 16:51:12 +01:00
|
|
|
if (pluginName.indexOf(plugins.prefix) != 0) continue; // TODO: Also search in keywords here!
|
2013-09-23 19:55:35 +02:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
if (searchTerm && !~results[pluginName].name.toLowerCase().indexOf(searchTerm)
|
2014-07-03 14:24:41 +02:00
|
|
|
&& (typeof results[pluginName].description != "undefined" && !~results[pluginName].description.toLowerCase().indexOf(searchTerm) )
|
2019-02-08 23:20:57 +01:00
|
|
|
) {
|
|
|
|
if (typeof results[pluginName].description === "undefined") {
|
2014-07-03 14:24:41 +02:00
|
|
|
console.debug('plugin without Description: %s', results[pluginName].name);
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2014-07-03 14:24:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2013-03-25 16:51:12 +01:00
|
|
|
res[pluginName] = results[pluginName];
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-23 13:24:53 +01:00
|
|
|
return res;
|
2019-02-08 23:20:57 +01:00
|
|
|
});
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|