forked from DockYard/ember-composable-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (89 loc) · 2.96 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
/* eslint-env node */
'use strict';
var Funnel = require('broccoli-funnel');
var path = require('path');
var intersection = require('./lib/intersection');
var difference = require('./lib/difference');
module.exports = {
name: 'ember-composable-helpers',
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;
var addonOptions = (this.parent && this.parent.options) || (this.app && this.app.options) || {};
var config = addonOptions[this.name] || {};
this.whitelist = this.generateWhitelist(config);
this.blacklist = this.generateBlacklist(config);
},
treeForAddon: function() {
// see: https://github.com/ember-cli/ember-cli/issues/4463
var tree = this._super.treeForAddon.apply(this, arguments);
var moduleRegexp = '(^modules\/)?' + this.name + '\/helpers\/';
return this.filterHelpers(tree, new RegExp(moduleRegexp, 'i'));
},
filterHelpers: function(tree, regex) {
var whitelist = this.whitelist;
var blacklist = this.blacklist;
var _this = this;
// exit early if no opts defined
if ((!whitelist || whitelist.length === 0) && (!blacklist || blacklist.length === 0)) {
return new Funnel(tree);
}
return new Funnel(tree, {
exclude: [function(name) {
return _this.exclusionFilter(name, regex, {
whitelist: whitelist,
blacklist: blacklist
});
}]
});
},
exclusionFilter: function(name, regex, lists) {
var whitelist = lists.whitelist || [];
var blacklist = lists.blacklist || [];
var isAddonHelper = regex.test(name);
var helperName = path.basename(name, '.js');
var isWhitelisted = whitelist.indexOf(helperName) !== -1;
var 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) {
var only = addonConfig.only || [];
var except = addonConfig.except || [];
if (except && except.length) {
return difference(only, except);
}
return only;
},
generateBlacklist: function(addonConfig) {
var only = addonConfig.only || [];
var except = addonConfig.except || [];
if (only && only.length) {
return intersection(except, only);
}
return except;
}
};