2020-11-13 20:32:00 +01:00
|
|
|
const log4js = require('log4js');
|
2020-11-23 19:24:19 +01:00
|
|
|
const plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins');
|
|
|
|
const hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
|
|
|
|
const npm = require('npm');
|
|
|
|
const request = require('request');
|
2020-11-13 19:59:20 +01:00
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
let npmIsLoaded = false;
|
|
|
|
const loadNpm = async () => {
|
|
|
|
if (npmIsLoaded) return;
|
|
|
|
await util.promisify(npm.load)({});
|
|
|
|
npmIsLoaded = true;
|
2020-11-13 20:32:00 +01:00
|
|
|
npm.on('log', log4js.getLogger('npm').log);
|
2020-11-13 19:59:20 +01:00
|
|
|
};
|
2012-03-15 18:25:06 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let 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++;
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
return function () {
|
2013-04-08 16:14:03 +02:00
|
|
|
cb && cb.apply(this, arguments);
|
|
|
|
tasks--;
|
2019-02-08 23:20:57 +01:00
|
|
|
if (tasks == 0) onAllTasksFinished();
|
2020-11-23 19:24:19 +01:00
|
|
|
};
|
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() {
|
2020-11-23 19:24:19 +01:00
|
|
|
hooks.aCallAll('restartServer', {}, () => {});
|
2013-04-08 16:14:03 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 19:59:20 +01:00
|
|
|
exports.uninstall = async (pluginName, cb = null) => {
|
2013-04-08 16:14:03 +02:00
|
|
|
cb = wrapTaskCb(cb);
|
2020-11-13 19:59:20 +01:00
|
|
|
try {
|
|
|
|
await loadNpm();
|
|
|
|
await util.promisify(npm.commands.uninstall)([pluginName]);
|
|
|
|
await hooks.aCallAll('pluginUninstall', {pluginName});
|
|
|
|
await plugins.update();
|
|
|
|
} catch (err) {
|
|
|
|
cb(err || new Error(err));
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
cb(null);
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|
|
|
|
|
2020-11-13 19:59:20 +01:00
|
|
|
exports.install = async (pluginName, cb = null) => {
|
2019-02-08 23:20:57 +01:00
|
|
|
cb = wrapTaskCb(cb);
|
2020-11-13 19:59:20 +01:00
|
|
|
try {
|
|
|
|
await loadNpm();
|
2020-12-05 15:08:02 +01:00
|
|
|
await util.promisify(npm.commands.install)([`${pluginName}@latest`]);
|
2020-11-13 19:59:20 +01:00
|
|
|
await hooks.aCallAll('pluginInstall', {pluginName});
|
|
|
|
await plugins.update();
|
|
|
|
} catch (err) {
|
|
|
|
cb(err || new Error(err));
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
cb(null);
|
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;
|
2020-11-23 19:24:19 +01:00
|
|
|
let cacheTimestamp = 0;
|
2012-04-18 13:43:34 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
exports.getAvailablePlugins = function (maxCacheAge) {
|
|
|
|
const nowTimestamp = Math.round(Date.now() / 1000);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-01-23 13:24:53 +01:00
|
|
|
// 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
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
request('https://static.etherpad.org/plugins.json', (er, response, plugins) => {
|
2019-01-23 13:24:53 +01:00
|
|
|
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
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
exports.search = function (searchTerm, maxCacheAge) {
|
|
|
|
return exports.getAvailablePlugins(maxCacheAge).then((results) => {
|
|
|
|
const 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
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
for (const pluginName in results) {
|
2019-02-08 23:20:57 +01:00
|
|
|
// 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
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (searchTerm && !~results[pluginName].name.toLowerCase().indexOf(searchTerm) &&
|
|
|
|
(typeof results[pluginName].description !== 'undefined' && !~results[pluginName].description.toLowerCase().indexOf(searchTerm))
|
|
|
|
) {
|
|
|
|
if (typeof results[pluginName].description === 'undefined') {
|
|
|
|
console.debug('plugin without Description: %s', results[pluginName].name);
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
continue;
|
2014-07-03 14:24:41 +02:00
|
|
|
}
|
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
|
|
|
};
|