main
Last change
on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | import { invokeArgs } from 'ramda-adjunct';
|
---|
| 2 | import PluginError from "../errors/PluginError.mjs";
|
---|
| 3 | /**
|
---|
| 4 | * Filters the given plugins, returning only the ones return `true` for the given method.
|
---|
| 5 | */
|
---|
| 6 | export const filter = async (method, file, plugins) => {
|
---|
| 7 | const pluginResults = await Promise.all(plugins.map(invokeArgs([method], [file])));
|
---|
| 8 | return plugins.filter((plugin, index) => pluginResults[index]);
|
---|
| 9 | };
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Runs the specified method of the given plugins, in order,
|
---|
| 13 | * until one of them returns a successful result.
|
---|
| 14 | * Each method can return a synchronous value or a Promise.
|
---|
| 15 | * If the promise resolves successfully then the result
|
---|
| 16 | * is immediately returned and no further plugins are called.
|
---|
| 17 | * If the promise rejects then the next plugin is called.
|
---|
| 18 | * If ALL plugins fail, then the last error is thrown.
|
---|
| 19 | */
|
---|
| 20 | export const run = async (method, parameters, plugins) => {
|
---|
| 21 | let lastError;
|
---|
| 22 | for (const plugin of plugins) {
|
---|
| 23 | try {
|
---|
| 24 | // @ts-ignore
|
---|
| 25 | const result = await plugin[method].call(plugin, ...parameters); // eslint-disable-line no-await-in-loop
|
---|
| 26 | return {
|
---|
| 27 | plugin,
|
---|
| 28 | result
|
---|
| 29 | };
|
---|
| 30 | } catch (error) {
|
---|
| 31 | lastError = new PluginError('Error while running plugin', {
|
---|
| 32 | cause: error,
|
---|
| 33 | plugin
|
---|
| 34 | });
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | return Promise.reject(lastError);
|
---|
| 38 | }; |
---|
Note:
See
TracBrowser
for help on using the repository browser.