forked from DockYard/ember-composable-helpers
/
index.js
124 lines (100 loc) · 3.17 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
const Funnel = require('broccoli-funnel');
const path = require('path');
const intersection = require('./lib/intersection');
const difference = require('./lib/difference');
const StripBadReexports = require('./lib/strip-bad-reexports');
module.exports = {
name: require('./package').name,
included: function (app) {
this._super.included.apply(this, arguments);
// see: https://github.com/ember-cli/ember-cli/issues/3718
if (typeof app.import !== 'function' && app.app) {
app = app.app;
}
this.app = app;
const addonOptions =
(this.parent && this.parent.options) ||
(this.app && this.app.options) ||
{};
const config = addonOptions[this.name] || {};
this.whitelist = this.generateWhitelist(config);
this.blacklist = this.generateBlacklist(config);
},
treeForAddon: function (tree) {
tree = this.filterHelpers(tree, new RegExp(/^helpers\//, 'i'));
tree = new StripBadReexports(tree, [`index.js`]);
return this._super.treeForAddon.call(this, tree);
},
treeForApp: function (tree) {
tree = this.filterHelpers(tree, new RegExp(/^helpers\//, 'i'));
return this._super.treeForApp.call(this, tree);
},
filterHelpers: function (tree, regex) {
const whitelist = this.whitelist;
const blacklist = this.blacklist;
const _this = this;
// exit early if no opts defined
if (
(!whitelist || whitelist.length === 0) &&
(!blacklist || blacklist.length === 0)
) {
return tree;
}
return new Funnel(tree, {
exclude: [
function (name) {
return _this.exclusionFilter(name, regex, {
whitelist: whitelist,
blacklist: blacklist,
});
},
],
});
},
exclusionFilter: function (name, regex, lists) {
const whitelist = lists.whitelist || [];
const blacklist = lists.blacklist || [];
const isAddonHelper = regex.test(name);
const helperName = path.basename(name, '.js');
const isWhitelisted = whitelist.indexOf(helperName) !== -1;
const isBlacklisted = blacklist.indexOf(helperName) !== -1;
// non-helper, don't exclude
if (!isAddonHelper) {
return false;
}
// don't exclude if both lists are empty
if (whitelist.length === 0 && blacklist.length === 0) {
return false;
}
// don't exclude if both whitelisted and blacklisted
if (isWhitelisted && isBlacklisted) {
return false;
}
// only whitelist defined
if (whitelist.length && blacklist.length === 0) {
return !isWhitelisted;
}
// only blacklist defined
if (blacklist.length && whitelist.length === 0) {
return isBlacklisted;
}
return !isWhitelisted || isBlacklisted;
},
generateWhitelist: function (addonConfig) {
const only = addonConfig.only || [];
const except = addonConfig.except || [];
if (except && except.length) {
return difference(only, except);
}
return only;
},
generateBlacklist: function (addonConfig) {
const only = addonConfig.only || [];
const except = addonConfig.except || [];
if (only && only.length) {
return intersection(except, only);
}
return except;
},
};