mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
plugins: async
ify more functions
This commit is contained in:
parent
9f575ebc84
commit
8a918fbc46
2 changed files with 44 additions and 64 deletions
|
@ -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 hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
|
||||||
var npm = require("npm");
|
var npm = require("npm");
|
||||||
var request = require("request");
|
var request = require("request");
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
var npmIsLoaded = false;
|
let npmIsLoaded = false;
|
||||||
var withNpm = function(npmfn) {
|
const loadNpm = async () => {
|
||||||
if (npmIsLoaded) return npmfn();
|
if (npmIsLoaded) return;
|
||||||
|
await util.promisify(npm.load)({});
|
||||||
npm.load({}, function(er) {
|
npmIsLoaded = true;
|
||||||
if (er) return npmfn(er);
|
npm.on('log', (message) => console.log('npm: ', message));
|
||||||
|
};
|
||||||
npmIsLoaded = true;
|
|
||||||
npm.on("log", function(message) {
|
|
||||||
console.log('npm: ',message)
|
|
||||||
});
|
|
||||||
npmfn();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var tasks = 0
|
var tasks = 0
|
||||||
|
|
||||||
|
@ -34,44 +28,32 @@ function onAllTasksFinished() {
|
||||||
hooks.aCallAll("restartServer", {}, function() {});
|
hooks.aCallAll("restartServer", {}, function() {});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
exports.uninstall = async (pluginName, 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.uninstall = function(plugin_name, cb) {
|
|
||||||
cb = wrapTaskCb(cb);
|
cb = wrapTaskCb(cb);
|
||||||
|
try {
|
||||||
withNpm(function(er) {
|
await loadNpm();
|
||||||
if (er) return cb && cb(er);
|
await util.promisify(npm.commands.uninstall)([pluginName]);
|
||||||
|
await hooks.aCallAll('pluginUninstall', {pluginName});
|
||||||
npm.commands.uninstall([plugin_name], function(er) {
|
await plugins.update();
|
||||||
if (er) return cb && cb(er);
|
} catch (err) {
|
||||||
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name})
|
cb(err || new Error(err));
|
||||||
.then(plugins.update)
|
throw err;
|
||||||
.then(function() { cb(null) })
|
}
|
||||||
.catch(function(er) { cb(er) });
|
cb(null);
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
exports.install = async (pluginName, 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) {
|
|
||||||
cb = wrapTaskCb(cb);
|
cb = wrapTaskCb(cb);
|
||||||
|
try {
|
||||||
withNpm(function(er) {
|
await loadNpm();
|
||||||
if (er) return cb && cb(er);
|
await util.promisify(npm.commands.install)([pluginName]);
|
||||||
|
await hooks.aCallAll('pluginInstall', {pluginName});
|
||||||
npm.commands.install([plugin_name], function(er) {
|
await plugins.update();
|
||||||
if (er) return cb && cb(er);
|
} catch (err) {
|
||||||
hooks.aCallAll("pluginInstall", {plugin_name: plugin_name})
|
cb(err || new Error(err));
|
||||||
.then(plugins.update)
|
throw err;
|
||||||
.then(function() { cb(null) })
|
}
|
||||||
.catch(function(er) { cb(er) });
|
cb(null);
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.availablePlugins = null;
|
exports.availablePlugins = null;
|
||||||
|
|
|
@ -44,17 +44,17 @@ exports.formatHooks = function (hook_set_name) {
|
||||||
return "<dl>" + res.join("\n") + "</dl>";
|
return "<dl>" + res.join("\n") + "</dl>";
|
||||||
};
|
};
|
||||||
|
|
||||||
const callInit = () => {
|
const callInit = async () => {
|
||||||
let p = Object.keys(defs.plugins).map(function(plugin_name) {
|
await Promise.all(Object.keys(defs.plugins).map(async (plugin_name) => {
|
||||||
let plugin = defs.plugins[plugin_name];
|
let plugin = defs.plugins[plugin_name];
|
||||||
let ep_init = path.normalize(path.join(plugin.package.path, ".ep_initialized"));
|
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 fs.writeFile(ep_init, 'done');
|
||||||
await hooks.aCallAll("init_" + plugin_name, {});
|
await hooks.aCallAll("init_" + plugin_name, {});
|
||||||
});
|
}
|
||||||
});
|
}));
|
||||||
|
|
||||||
return Promise.all(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.pathNormalization = function (part, hook_fn_name, hook_name) {
|
exports.pathNormalization = function (part, hook_fn_name, hook_name) {
|
||||||
|
@ -71,16 +71,14 @@ exports.update = async function () {
|
||||||
var plugins = {};
|
var plugins = {};
|
||||||
|
|
||||||
// Load plugin metadata ep.json
|
// Load plugin metadata ep.json
|
||||||
let p = Object.keys(packages).map(function (plugin_name) {
|
await Promise.all(Object.keys(packages).map(
|
||||||
return loadPlugin(packages, plugin_name, plugins, parts);
|
async (pluginName) => await loadPlugin(packages, pluginName, plugins, parts)));
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.all(p).then(function() {
|
defs.plugins = plugins;
|
||||||
defs.plugins = plugins;
|
defs.parts = sortParts(parts);
|
||||||
defs.parts = sortParts(parts);
|
defs.hooks = pluginUtils.extractHooks(defs.parts, 'hooks', exports.pathNormalization);
|
||||||
defs.hooks = pluginUtils.extractHooks(defs.parts, 'hooks', exports.pathNormalization);
|
defs.loaded = true;
|
||||||
defs.loaded = true;
|
await callInit();
|
||||||
}).then(callInit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getPackages = async function () {
|
exports.getPackages = async function () {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue