mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-31 19:02:59 +01:00
lint: src/static/js/pluginfw/shared.js
This commit is contained in:
parent
2c80c1f2da
commit
99ca57f3ab
1 changed files with 38 additions and 46 deletions
|
@ -1,5 +1,5 @@
|
|||
'use strict';
|
||||
const _ = require('underscore');
|
||||
|
||||
const defs = require('./plugin_defs');
|
||||
|
||||
const disabledHookReasons = {
|
||||
|
@ -27,54 +27,49 @@ const loadFn = (path, hookName) => {
|
|||
let fn = require(path);
|
||||
functionName = functionName ? functionName : hookName;
|
||||
|
||||
_.each(functionName.split('.'), (name) => {
|
||||
for (const name of functionName.split('.')) {
|
||||
fn = fn[name];
|
||||
});
|
||||
}
|
||||
return fn;
|
||||
};
|
||||
|
||||
const extractHooks = (parts, hook_set_name, normalizer) => {
|
||||
const extractHooks = (parts, hookSetName, normalizer) => {
|
||||
const hooks = {};
|
||||
_.each(parts, (part) => {
|
||||
_.chain(part[hook_set_name] || {})
|
||||
.keys()
|
||||
.each((hook_name) => {
|
||||
let hook_fn_name = part[hook_set_name][hook_name];
|
||||
|
||||
for (const part of parts) {
|
||||
for (const [hookName, regHookFnName] of Object.entries(part[hookSetName] || {})) {
|
||||
/* On the server side, you can't just
|
||||
* require("pluginname/whatever") if the plugin is installed as
|
||||
* a dependency of another plugin! Bah, pesky little details of
|
||||
* npm... */
|
||||
if (normalizer) {
|
||||
hook_fn_name = normalizer(part, hook_fn_name, hook_name);
|
||||
}
|
||||
const hookFnName = normalizer ? normalizer(part, regHookFnName, hookName) : regHookFnName;
|
||||
|
||||
const disabledReason = (disabledHookReasons[hook_set_name] || {})[hook_name];
|
||||
const disabledReason = (disabledHookReasons[hookSetName] || {})[hookName];
|
||||
if (disabledReason) {
|
||||
console.error(
|
||||
`Hook ${hook_set_name}/${hook_name} is disabled. Reason: ${disabledReason}`);
|
||||
console.error(`The hook function ${hook_fn_name} from plugin ${part.plugin} ` +
|
||||
console.error(`Hook ${hookSetName}/${hookName} is disabled. Reason: ${disabledReason}`);
|
||||
console.error(`The hook function ${hookFnName} from plugin ${part.plugin} ` +
|
||||
'will never be called, which may cause the plugin to fail');
|
||||
console.error(
|
||||
`Please update the ${part.plugin} plugin to not use the ${hook_name} hook`);
|
||||
console.error(`Please update the ${part.plugin} plugin to not use the ${hookName} hook`);
|
||||
return;
|
||||
}
|
||||
let hook_fn;
|
||||
let hookFn;
|
||||
try {
|
||||
hook_fn = loadFn(hook_fn_name, hook_name);
|
||||
if (!hook_fn) {
|
||||
throw new Error('Not a function');
|
||||
}
|
||||
hookFn = loadFn(hookFnName, hookName);
|
||||
if (!hookFn) throw new Error('Not a function');
|
||||
} catch (exc) {
|
||||
console.error(`Failed to load '${hook_fn_name}' for ` +
|
||||
`'${part.full_name}/${hook_set_name}/${hook_name}': ${exc.toString()}`);
|
||||
console.error(`Failed to load '${hookFnName}' for ` +
|
||||
`'${part.full_name}/${hookSetName}/${hookName}': ${exc.toString()}`);
|
||||
}
|
||||
if (hookFn) {
|
||||
if (hooks[hookName] == null) hooks[hookName] = [];
|
||||
hooks[hookName].push({
|
||||
hook_name: hookName,
|
||||
hook_fn: hookFn,
|
||||
hook_fn_name: hookFnName,
|
||||
part,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (hook_fn) {
|
||||
if (hooks[hook_name] == null) hooks[hook_name] = [];
|
||||
hooks[hook_name].push({hook_name, hook_fn, hook_fn_name, part});
|
||||
}
|
||||
});
|
||||
});
|
||||
return hooks;
|
||||
};
|
||||
|
||||
|
@ -93,11 +88,8 @@ exports.extractHooks = extractHooks;
|
|||
* Some plugins: [ 'ep_adminpads', 'ep_add_buttons', 'ep_activepads' ]
|
||||
*/
|
||||
exports.clientPluginNames = () => {
|
||||
const client_plugin_names = _.uniq(
|
||||
defs.parts
|
||||
const clientPluginNames = defs.parts
|
||||
.filter((part) => Object.prototype.hasOwnProperty.call(part, 'client_hooks'))
|
||||
.map((part) => `plugin-${part.plugin}`)
|
||||
);
|
||||
|
||||
return client_plugin_names;
|
||||
.map((part) => `plugin-${part.plugin}`);
|
||||
return [...new Set(clientPluginNames)];
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue