-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugins.js
74 lines (61 loc) · 1.97 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
'use strict';
const rglob = require('require-glob');
const path = require('path');
const filterEmpty = function(item) {
return Boolean(item);
};
const mapField = function(field) {
return function(item) {
return item[field];
};
};
const load = function(includes, excludes) {
var resolverPackage = require(path.resolve(__dirname, 'package'));
// map include and exclude names to regex patterns for paths
const pathSeps = '[/\\\\]';
var pluginToRegex = function(name) {
var pattern = pathSeps + '(plugins' + pathSeps + '|' + resolverPackage.name + '-)' + name + pathSeps;
return new RegExp(pattern);
};
includes = includes ? includes.map(pluginToRegex) : includes;
excludes = excludes ? excludes.map(pluginToRegex) : excludes;
var plugins = rglob.sync([
// built in plugins
path.join(__dirname, '/plugins/*/index.js'),
// installed sibling plugins
path.join('node_modules', resolverPackage.name + '-*', '/index.js')
],
{
cwd: process.cwd(),
reducer: function(options, result, file, i, files) {
// don't actually reduce, because that's incredibly annoying
// for our case
return files;
}
}).filter(function(item) {
// includes take precedence over excludes
var test = function(regex) {
return regex.test(item.path);
};
var retVal = true;
if (includes && includes.length) {
retVal = includes.some(test);
} else if (excludes && excludes.length) {
retVal = !excludes.some(test);
}
console.log((retVal ? 'In' : 'Ex') + 'cluding plugin ' + item.path);
return retVal;
}).map(function(item) {
return item.exports;
});
console.log();
return {
resolvers: plugins.map(mapField('resolver')).filter(filterEmpty),
writers: plugins.map(mapField('writer')).filter(filterEmpty),
updaters: plugins.map(mapField('updater')).filter(filterEmpty),
postResolvers: plugins.map(mapField('postResolver')).filter(filterEmpty)
};
};
module.exports = {
load: load
};