etherpad-lite/src/static/js/pluginfw/installer.js

122 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-01-19 17:37:12 +01:00
'use strict';
const log4js = require('log4js');
2021-01-19 17:37:12 +01:00
const plugins = require('./plugins');
const hooks = require('./hooks');
2020-11-23 19:24:19 +01:00
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;
npm.on('log', log4js.getLogger('npm').log);
2020-11-13 19:59:20 +01:00
};
2021-01-19 17:37:12 +01:00
const onAllTasksFinished = () => {
hooks.aCallAll('restartServer', {}, () => {});
};
2020-11-23 19:24:19 +01:00
let tasks = 0;
function wrapTaskCb(cb) {
tasks++;
2021-01-19 17:37:12 +01:00
return function (...args) {
cb && cb.apply(this, args);
tasks--;
2021-01-19 17:37:12 +01:00
if (tasks === 0) onAllTasksFinished();
2020-11-23 19:24:19 +01:00
};
}
2020-11-13 19:59:20 +01:00
exports.uninstall = async (pluginName, cb = null) => {
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) => {
cb = wrapTaskCb(cb);
2020-11-13 19:59:20 +01:00
try {
await loadNpm();
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
};
exports.availablePlugins = null;
2020-11-23 19:24:19 +01:00
let cacheTimestamp = 0;
2012-04-18 13:43:34 +02:00
2021-01-19 17:37:12 +01:00
exports.getAvailablePlugins = (maxCacheAge) => {
2020-11-23 19:24:19 +01:00
const nowTimestamp = Math.round(Date.now() / 1000);
2020-11-23 19:24:19 +01:00
return new Promise((resolve, reject) => {
// check cache age before making any request
if (exports.availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
return resolve(exports.availablePlugins);
}
2020-11-23 19:24:19 +01:00
request('https://static.etherpad.org/plugins.json', (er, response, plugins) => {
if (er) return reject(er);
try {
plugins = JSON.parse(plugins);
} catch (err) {
console.error('error parsing plugins.json:', err);
plugins = [];
}
exports.availablePlugins = plugins;
cacheTimestamp = nowTimestamp;
resolve(plugins);
});
});
};
2021-01-19 17:37:12 +01:00
exports.search = (searchTerm, maxCacheAge) => exports.getAvailablePlugins(maxCacheAge).then(
(results) => {
const res = {};
2021-01-19 17:37:12 +01:00
if (searchTerm) {
searchTerm = searchTerm.toLowerCase();
}
for (const pluginName in results) {
// for every available plugin
// TODO: Also search in keywords here!
if (pluginName.indexOf(plugins.prefix) !== 0) continue;
2021-01-19 17:37:12 +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);
}
2021-01-19 17:37:12 +01:00
continue;
2020-11-23 19:24:19 +01:00
}
2021-01-19 17:37:12 +01:00
res[pluginName] = results[pluginName];
}
2021-01-19 17:37:12 +01:00
return res;
}
2021-01-19 17:37:12 +01:00
);