mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.7 KiB
61 lines
1.7 KiB
'use strict';
|
|
|
|
const { visit } = require('../xast.js');
|
|
|
|
/**
|
|
* Plugins engine.
|
|
*
|
|
* @module plugins
|
|
*
|
|
* @param {Object} ast input ast
|
|
* @param {Object} info extra information
|
|
* @param {Array} plugins plugins object from config
|
|
* @return {Object} output ast
|
|
*/
|
|
const invokePlugins = (ast, info, plugins, overrides, globalOverrides) => {
|
|
for (const plugin of plugins) {
|
|
const override = overrides?.[plugin.name];
|
|
if (override === false) {
|
|
continue;
|
|
}
|
|
const params = { ...plugin.params, ...globalOverrides, ...override };
|
|
|
|
const visitor = plugin.fn(ast, params, info);
|
|
if (visitor != null) {
|
|
visit(ast, visitor);
|
|
}
|
|
}
|
|
};
|
|
exports.invokePlugins = invokePlugins;
|
|
|
|
const createPreset = ({ name, plugins }) => {
|
|
return {
|
|
name,
|
|
fn: (ast, params, info) => {
|
|
const { floatPrecision, overrides } = params;
|
|
const globalOverrides = {};
|
|
if (floatPrecision != null) {
|
|
globalOverrides.floatPrecision = floatPrecision;
|
|
}
|
|
if (overrides) {
|
|
const pluginNames = plugins.map(({ name }) => name);
|
|
for (const pluginName of Object.keys(overrides)) {
|
|
if (!pluginNames.includes(pluginName)) {
|
|
console.warn(
|
|
`You are trying to configure ${pluginName} which is not part of ${name}.\n` +
|
|
`Try to put it before or after, for example\n\n` +
|
|
`plugins: [\n` +
|
|
` {\n` +
|
|
` name: '${name}',\n` +
|
|
` },\n` +
|
|
` '${pluginName}'\n` +
|
|
`]\n`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
invokePlugins(ast, info, plugins, overrides, globalOverrides);
|
|
},
|
|
};
|
|
};
|
|
exports.createPreset = createPreset;
|
|
|