1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
4 | exports.__esModule = true;
|
---|
5 | exports.run = exports.filter = void 0;
|
---|
6 | var _ramdaAdjunct = require("ramda-adjunct");
|
---|
7 | var _PluginError = _interopRequireDefault(require("../errors/PluginError.cjs"));
|
---|
8 | /**
|
---|
9 | * Filters the given plugins, returning only the ones return `true` for the given method.
|
---|
10 | */
|
---|
11 | const filter = async (method, file, plugins) => {
|
---|
12 | const pluginResults = await Promise.all(plugins.map((0, _ramdaAdjunct.invokeArgs)([method], [file])));
|
---|
13 | return plugins.filter((plugin, index) => pluginResults[index]);
|
---|
14 | };
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Runs the specified method of the given plugins, in order,
|
---|
18 | * until one of them returns a successful result.
|
---|
19 | * Each method can return a synchronous value or a Promise.
|
---|
20 | * If the promise resolves successfully then the result
|
---|
21 | * is immediately returned and no further plugins are called.
|
---|
22 | * If the promise rejects then the next plugin is called.
|
---|
23 | * If ALL plugins fail, then the last error is thrown.
|
---|
24 | */
|
---|
25 | exports.filter = filter;
|
---|
26 | const run = async (method, parameters, plugins) => {
|
---|
27 | let lastError;
|
---|
28 | for (const plugin of plugins) {
|
---|
29 | try {
|
---|
30 | // @ts-ignore
|
---|
31 | const result = await plugin[method].call(plugin, ...parameters); // eslint-disable-line no-await-in-loop
|
---|
32 | return {
|
---|
33 | plugin,
|
---|
34 | result
|
---|
35 | };
|
---|
36 | } catch (error) {
|
---|
37 | lastError = new _PluginError.default('Error while running plugin', {
|
---|
38 | cause: error,
|
---|
39 | plugin
|
---|
40 | });
|
---|
41 | }
|
---|
42 | }
|
---|
43 | return Promise.reject(lastError);
|
---|
44 | };
|
---|
45 | exports.run = run; |
---|