plugins: asyncify more functions

This commit is contained in:
Richard Hansen 2020-11-13 13:59:20 -05:00 committed by John McLear
parent 9f575ebc84
commit 8a918fbc46
2 changed files with 44 additions and 64 deletions

View file

@ -2,21 +2,15 @@ var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
var npm = require("npm");
var request = require("request");
const util = require('util');
var npmIsLoaded = false;
var withNpm = function(npmfn) {
if (npmIsLoaded) return npmfn();
npm.load({}, function(er) {
if (er) return npmfn(er);
npmIsLoaded = true;
npm.on("log", function(message) {
console.log('npm: ',message)
});
npmfn();
});
}
let npmIsLoaded = false;
const loadNpm = async () => {
if (npmIsLoaded) return;
await util.promisify(npm.load)({});
npmIsLoaded = true;
npm.on('log', (message) => console.log('npm: ', message));
};
var tasks = 0
@ -34,44 +28,32 @@ function onAllTasksFinished() {
hooks.aCallAll("restartServer", {}, function() {});
}
/*
* 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.
*/
exports.uninstall = function(plugin_name, cb) {
exports.uninstall = async (pluginName, cb = null) => {
cb = wrapTaskCb(cb);
withNpm(function(er) {
if (er) return cb && cb(er);
npm.commands.uninstall([plugin_name], function(er) {
if (er) return cb && cb(er);
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name})
.then(plugins.update)
.then(function() { cb(null) })
.catch(function(er) { cb(er) });
});
});
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);
};
/*
* 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.
*/
exports.install = function(plugin_name, cb) {
exports.install = async (pluginName, cb = null) => {
cb = wrapTaskCb(cb);
withNpm(function(er) {
if (er) return cb && cb(er);
npm.commands.install([plugin_name], function(er) {
if (er) return cb && cb(er);
hooks.aCallAll("pluginInstall", {plugin_name: plugin_name})
.then(plugins.update)
.then(function() { cb(null) })
.catch(function(er) { cb(er) });
});
});
try {
await loadNpm();
await util.promisify(npm.commands.install)([pluginName]);
await hooks.aCallAll('pluginInstall', {pluginName});
await plugins.update();
} catch (err) {
cb(err || new Error(err));
throw err;
}
cb(null);
};
exports.availablePlugins = null;

View file

@ -44,17 +44,17 @@ exports.formatHooks = function (hook_set_name) {
return "<dl>" + res.join("\n") + "</dl>";
};
const callInit = () => {
let p = Object.keys(defs.plugins).map(function(plugin_name) {
const callInit = async () => {
await Promise.all(Object.keys(defs.plugins).map(async (plugin_name) => {
let plugin = defs.plugins[plugin_name];
let ep_init = path.normalize(path.join(plugin.package.path, ".ep_initialized"));
return fs.stat(ep_init).catch(async function() {
try {
await fs.stat(ep_init);
} catch (err) {
await fs.writeFile(ep_init, 'done');
await hooks.aCallAll("init_" + plugin_name, {});
});
});
return Promise.all(p);
}
}));
}
exports.pathNormalization = function (part, hook_fn_name, hook_name) {
@ -71,16 +71,14 @@ exports.update = async function () {
var plugins = {};
// Load plugin metadata ep.json
let p = Object.keys(packages).map(function (plugin_name) {
return loadPlugin(packages, plugin_name, plugins, parts);
});
await Promise.all(Object.keys(packages).map(
async (pluginName) => await loadPlugin(packages, pluginName, plugins, parts)));
return Promise.all(p).then(function() {
defs.plugins = plugins;
defs.parts = sortParts(parts);
defs.hooks = pluginUtils.extractHooks(defs.parts, 'hooks', exports.pathNormalization);
defs.loaded = true;
}).then(callInit);
defs.plugins = plugins;
defs.parts = sortParts(parts);
defs.hooks = pluginUtils.extractHooks(defs.parts, 'hooks', exports.pathNormalization);
defs.loaded = true;
await callInit();
}
exports.getPackages = async function () {