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

This commit is contained in:
Richard Hansen 2021-02-03 18:31:42 -05:00 committed by John McLear
parent 9a86ebec2a
commit 2b32bc1840

View file

@ -6,7 +6,6 @@ const readInstalled = require('./read-installed.js');
const path = require('path'); const path = require('path');
const tsort = require('./tsort'); const tsort = require('./tsort');
const util = require('util'); const util = require('util');
const _ = require('underscore');
const settings = require('../../../node/utils/Settings'); const settings = require('../../../node/utils/Settings');
const pluginUtils = require('./shared'); const pluginUtils = require('./shared');
@ -16,49 +15,42 @@ exports.prefix = 'ep_';
exports.formatPlugins = () => Object.keys(defs.plugins).join(', '); exports.formatPlugins = () => Object.keys(defs.plugins).join(', ');
exports.formatPluginsWithVersion = () => { exports.formatPluginsWithVersion = () => Object.values(defs.plugins)
const plugins = []; .filter((plugin) => plugin.package.name !== 'ep_etherpad-lite')
_.forEach(defs.plugins, (plugin) => { .map((plugin) => `${plugin.package.name}@${plugin.package.version}`)
if (plugin.package.name !== 'ep_etherpad-lite') { .join(', ');
const pluginStr = `${plugin.package.name}@${plugin.package.version}`;
plugins.push(pluginStr);
}
});
return plugins.join(', ');
};
exports.formatParts = () => _.map(defs.parts, (part) => part.full_name).join('\n'); exports.formatParts = () => defs.parts.map((part) => part.full_name).join('\n');
exports.formatHooks = (hook_set_name) => { exports.formatHooks = (hookSetName) => {
const res = []; const res = [];
const hooks = pluginUtils.extractHooks(defs.parts, hook_set_name || 'hooks'); const hooks = pluginUtils.extractHooks(defs.parts, hookSetName || 'hooks');
for (const registeredHooks of Object.values(hooks)) {
_.chain(hooks).keys().forEach((hook_name) => { for (const hook of registeredHooks) {
_.forEach(hooks[hook_name], (hook) => {
res.push(`<dt>${hook.hook_name}</dt><dd>${hook.hook_fn_name} ` + res.push(`<dt>${hook.hook_name}</dt><dd>${hook.hook_fn_name} ` +
`from ${hook.part.full_name}</dd>`); `from ${hook.part.full_name}</dd>`);
}); }
}); }
return `<dl>${res.join('\n')}</dl>`; return `<dl>${res.join('\n')}</dl>`;
}; };
const callInit = async () => { const callInit = async () => {
await Promise.all(Object.keys(defs.plugins).map(async (plugin_name) => { await Promise.all(Object.keys(defs.plugins).map(async (pluginName) => {
const plugin = defs.plugins[plugin_name]; const plugin = defs.plugins[pluginName];
const ep_init = path.normalize(path.join(plugin.package.path, '.ep_initialized')); const epInit = path.normalize(path.join(plugin.package.path, '.ep_initialized'));
try { try {
await fs.stat(ep_init); await fs.stat(epInit);
} catch (err) { } catch (err) {
await fs.writeFile(ep_init, 'done'); await fs.writeFile(epInit, 'done');
await hooks.aCallAll(`init_${plugin_name}`, {}); await hooks.aCallAll(`init_${pluginName}`, {});
} }
})); }));
}; };
exports.pathNormalization = (part, hook_fn_name, hook_name) => { exports.pathNormalization = (part, hookFnName, hookName) => {
const tmp = hook_fn_name.split(':'); // hook_fn_name might be something like 'C:\\foo.js:myFunc'. const tmp = hookFnName.split(':'); // hookFnName might be something like 'C:\\foo.js:myFunc'.
// If there is a single colon assume it's 'filename:funcname' not 'C:\\filename'. // If there is a single colon assume it's 'filename:funcname' not 'C:\\filename'.
const functionName = (tmp.length > 1 ? tmp.pop() : null) || hook_name; const functionName = (tmp.length > 1 ? tmp.pop() : null) || hookName;
const moduleName = tmp.join(':') || part.plugin; const moduleName = tmp.join(':') || part.plugin;
const packageDir = path.dirname(defs.plugins[part.plugin].package.path); const packageDir = path.dirname(defs.plugins[part.plugin].package.path);
const fileName = path.normalize(path.join(packageDir, moduleName)); const fileName = path.normalize(path.join(packageDir, moduleName));
@ -89,15 +81,15 @@ exports.getPackages = async () => {
const packages = {}; const packages = {};
const flatten = (deps) => { const flatten = (deps) => {
_.chain(deps).keys().each((name) => { for (const [name, dep] of Object.entries(deps)) {
if (name.indexOf(exports.prefix) === 0) { if (name.indexOf(exports.prefix) === 0) {
packages[name] = _.clone(deps[name]); packages[name] = {...dep};
// Delete anything that creates loops so that the plugin // Delete anything that creates loops so that the plugin
// list can be sent as JSON to the web client // list can be sent as JSON to the web client
delete packages[name].dependencies; delete packages[name].dependencies;
delete packages[name].parent; delete packages[name].parent;
} }
}); }
}; };
const tmp = {}; const tmp = {};
@ -106,40 +98,40 @@ exports.getPackages = async () => {
return packages; return packages;
}; };
const loadPlugin = async (packages, plugin_name, plugins, parts) => { const loadPlugin = async (packages, pluginName, plugins, parts) => {
const plugin_path = path.resolve(packages[plugin_name].path, 'ep.json'); const pluginPath = path.resolve(packages[pluginName].path, 'ep.json');
try { try {
const data = await fs.readFile(plugin_path); const data = await fs.readFile(pluginPath);
try { try {
const plugin = JSON.parse(data); const plugin = JSON.parse(data);
plugin.package = packages[plugin_name]; plugin.package = packages[pluginName];
plugins[plugin_name] = plugin; plugins[pluginName] = plugin;
_.each(plugin.parts, (part) => { for (const part of plugin.parts) {
part.plugin = plugin_name; part.plugin = pluginName;
part.full_name = `${plugin_name}/${part.name}`; part.full_name = `${pluginName}/${part.name}`;
parts[part.full_name] = part; parts[part.full_name] = part;
}); }
} catch (ex) { } catch (ex) {
console.error(`Unable to parse plugin definition file ${plugin_path}: ${ex.toString()}`); console.error(`Unable to parse plugin definition file ${pluginPath}: ${ex.toString()}`);
} }
} catch (er) { } catch (er) {
console.error(`Unable to load plugin definition file ${plugin_path}`); console.error(`Unable to load plugin definition file ${pluginPath}`);
} }
}; };
const partsToParentChildList = (parts) => { const partsToParentChildList = (parts) => {
const res = []; const res = [];
_.chain(parts).keys().forEach((name) => { for (const name of Object.keys(parts)) {
_.each(parts[name].post || [], (child_name) => { for (const childName of parts[name].post || []) {
res.push([name, child_name]); res.push([name, childName]);
}); }
_.each(parts[name].pre || [], (parent_name) => { for (const parentName of parts[name].pre || []) {
res.push([parent_name, name]); res.push([parentName, name]);
}); }
if (!parts[name].pre && !parts[name].post) { if (!parts[name].pre && !parts[name].post) {
res.push([name, `:${name}`]); // Include apps with no dependency info res.push([name, `:${name}`]); // Include apps with no dependency info
} }
}); }
return res; return res;
}; };