2021-01-19 16:37:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-13 14:32:00 -05:00
|
|
|
const log4js = require('log4js');
|
2021-01-19 16:37:12 +00:00
|
|
|
const plugins = require('./plugins');
|
|
|
|
const hooks = require('./hooks');
|
2020-11-23 13:24:19 -05:00
|
|
|
const request = require('request');
|
2021-02-17 22:53:50 -05:00
|
|
|
const runCmd = require('../../../node/utils/run_cmd');
|
2021-02-15 08:52:38 +01:00
|
|
|
const settings = require('../../../node/utils/Settings');
|
2020-11-13 13:59:20 -05:00
|
|
|
|
2021-02-10 01:34:38 -05:00
|
|
|
const logger = log4js.getLogger('plugins');
|
|
|
|
|
2021-02-15 08:52:38 +01:00
|
|
|
const onAllTasksFinished = async () => {
|
|
|
|
settings.reloadSettings();
|
|
|
|
await hooks.aCallAll('loadSettings', {settings});
|
|
|
|
await hooks.aCallAll('restartServer');
|
2021-01-19 16:37:12 +00:00
|
|
|
};
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
let tasks = 0;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-02-10 01:28:10 -05:00
|
|
|
const wrapTaskCb = (cb) => {
|
2019-02-08 23:20:57 +01:00
|
|
|
tasks++;
|
|
|
|
|
2021-02-10 01:28:10 -05:00
|
|
|
return (...args) => {
|
|
|
|
cb && cb(...args);
|
2013-04-08 16:14:03 +02:00
|
|
|
tasks--;
|
2021-01-19 16:37:12 +00:00
|
|
|
if (tasks === 0) onAllTasksFinished();
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2021-02-10 01:28:10 -05:00
|
|
|
};
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-13 13:59:20 -05:00
|
|
|
exports.uninstall = async (pluginName, cb = null) => {
|
2013-04-08 16:14:03 +02:00
|
|
|
cb = wrapTaskCb(cb);
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.info(`Uninstalling plugin ${pluginName}...`);
|
2020-11-13 13:59:20 -05:00
|
|
|
try {
|
2021-02-17 22:53:50 -05:00
|
|
|
// The --no-save flag prevents npm from creating package.json or package-lock.json.
|
2021-02-15 19:10:55 -05:00
|
|
|
// The --legacy-peer-deps flag is required to work around a bug in npm v7:
|
|
|
|
// https://github.com/npm/cli/issues/2199
|
|
|
|
await runCmd(['npm', 'uninstall', '--no-save', '--legacy-peer-deps', pluginName]);
|
2020-11-13 13:59:20 -05:00
|
|
|
} catch (err) {
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.error(`Failed to uninstall plugin ${pluginName}`);
|
2020-11-13 13:59:20 -05:00
|
|
|
cb(err || new Error(err));
|
|
|
|
throw err;
|
|
|
|
}
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.info(`Successfully uninstalled plugin ${pluginName}`);
|
2021-02-18 02:36:50 -05:00
|
|
|
await hooks.aCallAll('pluginUninstall', {pluginName});
|
|
|
|
await plugins.update();
|
2020-11-13 13:59:20 -05:00
|
|
|
cb(null);
|
2012-03-19 17:16:49 +01:00
|
|
|
};
|
|
|
|
|
2020-11-13 13:59:20 -05:00
|
|
|
exports.install = async (pluginName, cb = null) => {
|
2019-02-08 23:20:57 +01:00
|
|
|
cb = wrapTaskCb(cb);
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.info(`Installing plugin ${pluginName}...`);
|
2020-11-13 13:59:20 -05:00
|
|
|
try {
|
2021-02-17 22:53:50 -05:00
|
|
|
// The --no-save flag prevents npm from creating package.json or package-lock.json.
|
2021-02-15 19:10:55 -05:00
|
|
|
// The --legacy-peer-deps flag is required to work around a bug in npm v7:
|
|
|
|
// https://github.com/npm/cli/issues/2199
|
|
|
|
await runCmd(['npm', 'install', '--no-save', '--legacy-peer-deps', pluginName]);
|
2020-11-13 13:59:20 -05:00
|
|
|
} catch (err) {
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.error(`Failed to install plugin ${pluginName}`);
|
2020-11-13 13:59:20 -05:00
|
|
|
cb(err || new Error(err));
|
|
|
|
throw err;
|
|
|
|
}
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.info(`Successfully installed plugin ${pluginName}`);
|
2021-02-18 02:36:50 -05:00
|
|
|
await hooks.aCallAll('pluginInstall', {pluginName});
|
|
|
|
await plugins.update();
|
2020-11-13 13:59:20 -05:00
|
|
|
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 13:24:19 -05:00
|
|
|
let cacheTimestamp = 0;
|
2012-04-18 13:43:34 +02:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
exports.getAvailablePlugins = (maxCacheAge) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const nowTimestamp = Math.round(Date.now() / 1000);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-01-23 12:24:53 +00: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 13:24:19 -05:00
|
|
|
request('https://static.etherpad.org/plugins.json', (er, response, plugins) => {
|
2019-01-23 12:24:53 +00:00
|
|
|
if (er) return reject(er);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-23 12:24:53 +00:00
|
|
|
try {
|
|
|
|
plugins = JSON.parse(plugins);
|
|
|
|
} catch (err) {
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.error(`error parsing plugins.json: ${err.stack || err}`);
|
2019-01-23 12:24:53 +00:00
|
|
|
plugins = [];
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-23 12:24:53 +00:00
|
|
|
exports.availablePlugins = plugins;
|
|
|
|
cacheTimestamp = nowTimestamp;
|
|
|
|
resolve(plugins);
|
|
|
|
});
|
2013-03-25 16:51:12 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
exports.search = (searchTerm, maxCacheAge) => exports.getAvailablePlugins(maxCacheAge).then(
|
|
|
|
(results) => {
|
|
|
|
const res = {};
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-01-19 16:37:12 +00: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;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-01-19 16:37:12 +00: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') {
|
2021-02-10 01:34:38 -05:00
|
|
|
logger.debug(`plugin without Description: ${results[pluginName].name}`);
|
2021-01-19 16:37:12 +00:00
|
|
|
}
|
2013-09-23 19:55:35 +02:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
continue;
|
2020-11-23 13:24:19 -05:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
res[pluginName] = results[pluginName];
|
2014-07-03 14:24:41 +02:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-01-19 16:37:12 +00:00
|
|
|
return res;
|
2013-03-25 16:51:12 +01:00
|
|
|
}
|
2021-01-19 16:37:12 +00:00
|
|
|
);
|