/
index.js
105 lines (91 loc) · 3.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const {identity, isPlainObject, omit, castArray, isNil, isString} = require('lodash');
const AggregateError = require('aggregate-error');
const getError = require('../get-error');
const PLUGINS_DEFINITIONS = require('../definitions/plugins');
const {validatePlugin, validateStep, loadPlugin, parseConfig} = require('./utils');
const pipeline = require('./pipeline');
const normalize = require('./normalize');
module.exports = async (context, pluginsPath) => {
let {options, logger} = context;
const errors = [];
const plugins = options.plugins
? await castArray(options.plugins).reduce(async (plugins, plugin) => {
const plugins2 = await plugins;
if (validatePlugin(plugin)) {
const [name, config] = parseConfig(plugin);
plugin = isString(name) ? await loadPlugin(context, name, pluginsPath) : name;
console.log({plugins: options.plugins})
console.dir(plugin, {depth: null})
console.log(isPlainObject(plugin))
if (isPlainObject(plugin)) {
Object.entries(plugin).forEach(([type, func]) => {
console.log({type, name})
if (PLUGINS_DEFINITIONS[type]) {
Reflect.defineProperty(func, 'pluginName', {
value: isPlainObject(name) ? 'Inline plugin' : name,
writable: false,
enumerable: true,
});
plugins2[type] = [...(plugins2[type] || []), [func, config]];
}
});
} else {
errors.push(getError('EPLUGINSCONF', {plugin}));
}
} else {
errors.push(getError('EPLUGINSCONF', {plugin}));
}
return plugins2;
}, {})
: [];
if (errors.length > 0) {
throw new AggregateError(errors);
}
options = {...plugins, ...options};
const pluginsConf = await Object.entries(PLUGINS_DEFINITIONS).reduce(
async (pluginsConf, [type, {
required,
default: def,
pipelineConfig,
postprocess = identity,
preprocess = identity
}]) => {
let pluginOptions;
const pluginsConf2 = await pluginsConf;
if (isNil(options[type]) && def) {
pluginOptions = def;
} else {
// If an object is passed and the path is missing, merge it with step options
if (isPlainObject(options[type]) && !options[type].path) {
options[type] = castArray(plugins[type]).map((plugin) =>
plugin ? [plugin[0], Object.assign(plugin[1], options[type])] : plugin
);
}
if (!validateStep({required}, options[type])) {
errors.push(getError('EPLUGINCONF', {type, required, pluginConf: options[type]}));
return pluginsConf2;
}
pluginOptions = options[type];
}
const steps = await Promise.all(castArray(pluginOptions).map(async (pluginOpt) =>
await normalize(
{...context, options: omit(options, Object.keys(PLUGINS_DEFINITIONS), 'plugins')},
type,
pluginOpt,
pluginsPath
)
));
pluginsConf2[type] = async (input) =>
postprocess(
await pipeline(steps, pipelineConfig && pipelineConfig(pluginsConf2, logger))(await preprocess(input)),
input
);
return pluginsConf2;
},
plugins
);
if (errors.length > 0) {
throw new AggregateError(errors);
}
return pluginsConf;
};