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

168 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-01-19 16:37:12 +00:00
'use strict';
const log4js = require('log4js');
2021-01-19 16:37:12 +00:00
const plugins = require('./plugins');
const hooks = require('./hooks');
const runCmd = require('../../../node/utils/run_cmd');
2021-02-15 08:52:38 +01:00
const settings = require('../../../node/utils/Settings');
const axios = require('axios');
const {PluginManager} = require('live-plugin-manager-pnpm');
const {promises: fs} = require('fs');
const path = require('path');
const {findEtherpadRoot} = require('../../../node/utils/AbsolutePaths');
const logger = log4js.getLogger('plugins');
exports.manager = new PluginManager();
const installedPluginsPath = path.join(settings.root, 'var/installed_plugins.json');
2021-02-15 08:52:38 +01:00
const onAllTasksFinished = async () => {
await plugins.update();
await persistInstalledPlugins();
2021-02-15 08:52:38 +01:00
settings.reloadSettings();
await hooks.aCallAll('loadSettings', {settings});
await hooks.aCallAll('restartServer');
2021-01-19 16:37:12 +00:00
};
const headers = {
'User-Agent': `Etherpad/${settings.getEpVersion()}`,
};
2020-11-23 13:24:19 -05:00
let tasks = 0;
const wrapTaskCb = (cb) => {
tasks++;
return (...args) => {
cb && cb(...args);
tasks--;
2021-01-19 16:37:12 +00:00
if (tasks === 0) onAllTasksFinished();
2020-11-23 13:24:19 -05:00
};
};
const migratePluginsFromNodeModules = async () => {
logger.info('start migration of plugins in node_modules');
// Notes:
// * Do not pass `--prod` otherwise `npm ls` will fail if there is no `package.json`.
// * The `--no-production` flag is required (or the `NODE_ENV` environment variable must be
// unset or set to `development`) because otherwise `npm ls` will not mention any packages
// that are not included in `package.json` (which is expected to not exist).
const cmd = ['pnpm', 'ls', '--long', '--json', '--depth=0', '--no-production'];
const [{dependencies = {}}] = JSON.parse(await runCmd(cmd,
{stdio: [null, 'string']}));
await Promise.all(Object.entries(dependencies)
.filter(([pkg, info]) => pkg.startsWith(plugins.prefix) && pkg !== 'ep_etherpad-lite')
.map(async ([pkg, info]) => {
if (!info._resolved) {
// Install from node_modules directory
await exports.manager.installFromPath(`${findEtherpadRoot()}/node_modules/${pkg}`);
} else {
await exports.manager.install(pkg);
}
}));
await persistInstalledPlugins();
2024-01-30 22:27:31 +01:00
};
exports.checkForMigration = async () => {
2024-01-30 22:27:31 +01:00
logger.info('check installed plugins for migration');
2020-11-13 13:59:20 -05:00
try {
2024-01-30 22:27:31 +01:00
await fs.access(installedPluginsPath, fs.constants.F_OK);
2020-11-13 13:59:20 -05:00
} catch (err) {
await migratePluginsFromNodeModules();
}
const fileContent = await fs.readFile(installedPluginsPath);
const installedPlugins = JSON.parse(fileContent.toString());
for (const plugin of installedPlugins.plugins) {
if (plugin.name.startsWith(plugins.prefix) && plugin.name !== 'ep_etherpad-lite') {
await exports.manager.install(plugin.name, plugin.version);
}
}
};
const persistInstalledPlugins = async () => {
const installedPlugins = {plugins: []};
for (const pkg of Object.values(await plugins.getPackages())) {
installedPlugins.plugins.push({
name: pkg.name,
version: pkg.version,
});
2020-11-13 13:59:20 -05:00
}
installedPlugins.plugins = [...new Set(installedPlugins.plugins)];
await fs.writeFile(installedPluginsPath, JSON.stringify(installedPlugins));
};
exports.uninstall = async (pluginName, cb = null) => {
cb = wrapTaskCb(cb);
logger.info(`Uninstalling plugin ${pluginName}...`);
await exports.manager.uninstall(pluginName);
logger.info(`Successfully uninstalled plugin ${pluginName}`);
await hooks.aCallAll('pluginUninstall', {pluginName});
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) => {
cb = wrapTaskCb(cb);
logger.info(`Installing plugin ${pluginName}...`);
await exports.manager.install(pluginName);
logger.info(`Successfully installed plugin ${pluginName}`);
await hooks.aCallAll('pluginInstall', {pluginName});
2020-11-13 13:59:20 -05:00
cb(null);
2012-03-19 17:16:49 +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);
return new Promise(async (resolve, reject) => {
// check cache age before making any request
if (exports.availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
return resolve(exports.availablePlugins);
}
await axios.get('https://static.etherpad.org/plugins.json', {headers})
.then((pluginsLoaded) => {
exports.availablePlugins = pluginsLoaded.data;
cacheTimestamp = nowTimestamp;
resolve(exports.availablePlugins);
})
.catch(async (err) => reject(err));
});
};
2021-01-19 16:37:12 +00:00
exports.search = (searchTerm, maxCacheAge) => exports.getAvailablePlugins(maxCacheAge).then(
(results) => {
const res = {};
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;
2021-01-19 16:37:12 +00:00
if (searchTerm && !~results[pluginName].name.toLowerCase().indexOf(searchTerm) &&
2024-01-30 22:27:31 +01:00
(typeof results[pluginName].description !== 'undefined' &&
!~results[pluginName].description.toLowerCase().indexOf(searchTerm))
2021-01-19 16:37:12 +00:00
) {
if (typeof results[pluginName].description === 'undefined') {
logger.debug(`plugin without Description: ${results[pluginName].name}`);
2021-01-19 16:37:12 +00:00
}
2021-01-19 16:37:12 +00:00
continue;
2020-11-23 13:24:19 -05:00
}
2021-01-19 16:37:12 +00:00
res[pluginName] = results[pluginName];
}
2021-01-19 16:37:12 +00:00
return res;
}
2021-01-19 16:37:12 +00:00
);