lint: src/static/js/pluginfw/shared.js

This commit is contained in:
Richard Hansen 2021-02-03 18:31:42 -05:00 committed by John McLear
parent 2c80c1f2da
commit 99ca57f3ab

View file

@ -1,5 +1,5 @@
'use strict'; 'use strict';
const _ = require('underscore');
const defs = require('./plugin_defs'); const defs = require('./plugin_defs');
const disabledHookReasons = { const disabledHookReasons = {
@ -27,54 +27,49 @@ const loadFn = (path, hookName) => {
let fn = require(path); let fn = require(path);
functionName = functionName ? functionName : hookName; functionName = functionName ? functionName : hookName;
_.each(functionName.split('.'), (name) => { for (const name of functionName.split('.')) {
fn = fn[name]; fn = fn[name];
}); }
return fn; return fn;
}; };
const extractHooks = (parts, hook_set_name, normalizer) => { const extractHooks = (parts, hookSetName, normalizer) => {
const hooks = {}; const hooks = {};
_.each(parts, (part) => { for (const part of parts) {
_.chain(part[hook_set_name] || {}) for (const [hookName, regHookFnName] of Object.entries(part[hookSetName] || {})) {
.keys() /* On the server side, you can't just
.each((hook_name) => {
let hook_fn_name = part[hook_set_name][hook_name];
/* On the server side, you can't just
* require("pluginname/whatever") if the plugin is installed as * require("pluginname/whatever") if the plugin is installed as
* a dependency of another plugin! Bah, pesky little details of * a dependency of another plugin! Bah, pesky little details of
* npm... */ * npm... */
if (normalizer) { const hookFnName = normalizer ? normalizer(part, regHookFnName, hookName) : regHookFnName;
hook_fn_name = normalizer(part, hook_fn_name, hook_name);
}
const disabledReason = (disabledHookReasons[hook_set_name] || {})[hook_name]; const disabledReason = (disabledHookReasons[hookSetName] || {})[hookName];
if (disabledReason) { if (disabledReason) {
console.error( console.error(`Hook ${hookSetName}/${hookName} is disabled. Reason: ${disabledReason}`);
`Hook ${hook_set_name}/${hook_name} is disabled. Reason: ${disabledReason}`); console.error(`The hook function ${hookFnName} from plugin ${part.plugin} ` +
console.error(`The hook function ${hook_fn_name} from plugin ${part.plugin} ` + 'will never be called, which may cause the plugin to fail');
'will never be called, which may cause the plugin to fail'); console.error(`Please update the ${part.plugin} plugin to not use the ${hookName} hook`);
console.error( return;
`Please update the ${part.plugin} plugin to not use the ${hook_name} hook`); }
return; let hookFn;
} try {
let hook_fn; hookFn = loadFn(hookFnName, hookName);
try { if (!hookFn) throw new Error('Not a function');
hook_fn = loadFn(hook_fn_name, hook_name); } catch (exc) {
if (!hook_fn) { console.error(`Failed to load '${hookFnName}' for ` +
throw new Error('Not a function'); `'${part.full_name}/${hookSetName}/${hookName}': ${exc.toString()}`);
} }
} catch (exc) { if (hookFn) {
console.error(`Failed to load '${hook_fn_name}' for ` + if (hooks[hookName] == null) hooks[hookName] = [];
`'${part.full_name}/${hook_set_name}/${hook_name}': ${exc.toString()}`); hooks[hookName].push({
} hook_name: hookName,
if (hook_fn) { hook_fn: hookFn,
if (hooks[hook_name] == null) hooks[hook_name] = []; hook_fn_name: hookFnName,
hooks[hook_name].push({hook_name, hook_fn, hook_fn_name, part}); part,
}
}); });
}); }
}
}
return hooks; return hooks;
}; };
@ -93,11 +88,8 @@ exports.extractHooks = extractHooks;
* Some plugins: [ 'ep_adminpads', 'ep_add_buttons', 'ep_activepads' ] * Some plugins: [ 'ep_adminpads', 'ep_add_buttons', 'ep_activepads' ]
*/ */
exports.clientPluginNames = () => { exports.clientPluginNames = () => {
const client_plugin_names = _.uniq( const clientPluginNames = defs.parts
defs.parts .filter((part) => Object.prototype.hasOwnProperty.call(part, 'client_hooks'))
.filter((part) => Object.prototype.hasOwnProperty.call(part, 'client_hooks')) .map((part) => `plugin-${part.plugin}`);
.map((part) => `plugin-${part.plugin}`) return [...new Set(clientPluginNames)];
);
return client_plugin_names;
}; };