forked from adopted-ember-addons/ember-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (66 loc) · 2 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
/* jshint node: true */
'use strict';
var Funnel = require('broccoli-funnel');
var path = require('path');
function normalize(name) {
if (typeof name === 'string') {
name = path.basename(name, '.js');
return name.replace(/([a-z](?=[A-Z]))/g, '$1-').toLowerCase();
}
}
function uniqueStrings(arr) {
var out = [];
var dict = {};
for (var i=0;i<arr.length;i++) {
var obj = arr[i];
if (typeof obj === 'string' && !dict[obj]) {
out.push(obj);
dict[obj] = true;
}
}
return out;
}
function getEach(arr, propName) {
var out = [];
for (var i=0;i<arr.length;i++) {
var obj = arr[i];
if (typeof obj === 'object' && obj[propName]) {
out.push(obj[propName]);
}
}
return out;
}
module.exports = {
name: 'ember-metrics',
included: function(app, parentAddon) {
this._super.included.apply(this, arguments);
var target = parentAddon || app;
// allow addon to be nested - see: https://github.com/ember-cli/ember-cli/issues/3718
if (target.app) {
target = target.app;
}
var config = target.project.config(target.env) || {};
var addonConfig = config[this.name] || {};
var discovered = ['base'];
if (addonConfig.includeAdapters) {
discovered = discovered.concat(addonConfig.includeAdapters);
}
if (config.metricsAdapters) {
discovered = discovered.concat(getEach(config.metricsAdapters, 'name'));
}
this.whitelisted = uniqueStrings(discovered.map(normalize));
},
treeForAddon: function() {
// see: https://github.com/ember-cli/ember-cli/issues/4463
var tree = this._super.treeForAddon.apply(this, arguments);
return this.filterAdapters(tree, new RegExp('^(?:modules\/)?' + this.name + '\/metrics\-adapters\/', 'i'));
},
filterAdapters: function(tree, regex) {
var whitelisted = this.whitelisted;
return new Funnel(tree, {
exclude: [function(name) {
return regex.test(name) && whitelisted.indexOf(path.basename(name, '.js')) === -1;
}]
});
}
};