-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugins.js
121 lines (108 loc) · 3.07 KB
/
plugins.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { getPluginsRoot, cfs } = require('./fs-helpers');
const isPluginListDefined = (plugins) =>
plugins && Array.isArray(plugins) && plugins.length;
function validatePluginDefinition(plugin) {
const isNameUndefined = !plugin.name;
if (isNameUndefined) {
const error = new Error(
'Plugin validation error: name is not defined, please check plugin definition'
);
throw error;
}
const requiredOptions = ['init', 'process', 'end'];
for (let option of requiredOptions) {
const isOptionDefined = plugin[option];
if (!isOptionDefined) {
const error = new Error(
'Plugin validation error: ' +
option +
' is not defined, please check plugin definition'
);
throw error;
}
const isOptionNotAFunction = typeof plugin[option] !== 'function';
if (isOptionNotAFunction) {
const error = new Error(
'Plugin validation error: ' +
option +
' is not a function, please check plugin definition'
);
throw error;
}
}
}
const getPluginPath = ({ plugin, root }) => root + '/' + plugin + '.js';
function getPluginDefinition({ plugin, root }) {
return new Promise(function getDefinition(resolve, reject) {
try {
const getPlugin = require(getPluginPath({ plugin, root }));
const isFunction = typeof getPlugin === 'function';
if (isFunction) {
const definition = getPlugin();
resolve(definition);
} else {
throw new Error('Exported entity for plugin ' + plugin + ' is not a function.');
}
} catch (error) {
reject(error);
}
});
}
async function appendPluginDefinition(config) {
const { plugins } = config;
if (!isPluginListDefined(plugins)) {
config.plugins = [];
return config;
}
const root = getPluginsRoot(config);
config.plugins = (
await Promise.all(
plugins.map(async (plugin) => {
let pluginDefinition;
try {
pluginDefinition = await getPluginDefinition({ plugin, root });
} catch (error) {
console.error('Error loading plugin ', plugin);
console.trace(error);
return null;
}
try {
validatePluginDefinition(pluginDefinition);
return pluginDefinition;
} catch (error) {
console.error(
'Invalid definition for plugin: ' + plugin + '. Skipping it.'
);
console.trace(error);
return null;
}
})
)
).filter((_) => _);
return config;
}
function initializePlugin(plugin, config) {
plugin.init(config);
}
function applyPlugin({ plugin, content, config }) {
const result = plugin.process(content, config);
if (!result) {
const error = new Error(
'Error processing content: ' +
plugin.name +
' process function should return valid value, please check plugin definition'
);
console.error(error);
return content;
}
return result;
}
function cleanupPlugin(plugin) {
plugin.end();
}
module.exports = {
appendPluginDefinition,
initializePlugin,
applyPlugin,
cleanupPlugin,
};