1 | 'use strict';
|
2 |
|
3 | var Plugin = require('./lib/plugin');
|
4 | var TemplatePlugin = require('./lib/template-plugin');
|
5 | var JavascriptPlugin = require('./lib/javascript-plugin');
|
6 | var debug = require('debug')('ember-cli:registry');
|
7 |
|
8 | function Registry(plugins, app) {
|
9 | this.registry = {
|
10 | js: [],
|
11 | css: [],
|
12 | 'minify-css': [],
|
13 | template: []
|
14 | };
|
15 |
|
16 | this.instantiatedPlugins = [];
|
17 | this.availablePlugins = plugins;
|
18 | this.app = app;
|
19 | this.pluginTypes = {
|
20 | 'js': JavascriptPlugin,
|
21 | 'template': TemplatePlugin
|
22 | };
|
23 | }
|
24 |
|
25 | module.exports = Registry;
|
26 |
|
27 | Registry.prototype.extensionsForType = function(type) {
|
28 | var registered = this.registeredForType(type);
|
29 |
|
30 | var extensions = registered.reduce(function(memo, plugin) {
|
31 | return memo.concat(plugin.ext);
|
32 | }, [type]).filter(Boolean);
|
33 |
|
34 | extensions = Array.from(new Set(extensions));
|
35 |
|
36 | debug('extensions for type %s: %s', type, extensions);
|
37 |
|
38 | return extensions;
|
39 | };
|
40 |
|
41 | Registry.prototype.load = function(type) {
|
42 | var knownPlugins = this.registeredForType(type);
|
43 | var plugins = knownPlugins.map(function(plugin) {
|
44 | if(this.instantiatedPlugins.indexOf(plugin) > -1 || this.availablePlugins.hasOwnProperty(plugin.name)) {
|
45 | return plugin;
|
46 | }
|
47 | }.bind(this))
|
48 | .filter(Boolean);
|
49 |
|
50 | debug('loading %s: available plugins %s; found plugins %s;', type, knownPlugins.map(function(p) { return p.name; }), plugins.map(function(p) { return p.name; }));
|
51 |
|
52 | return plugins;
|
53 | };
|
54 |
|
55 | Registry.prototype.registeredForType = function(type) {
|
56 | return this.registry[type] = this.registry[type] || [];
|
57 | };
|
58 |
|
59 | Registry.prototype.add = function(type, name, extension, options) {
|
60 | var registered = this.registeredForType(type);
|
61 | var plugin, PluginType;
|
62 |
|
63 |
|
64 | if (typeof name === 'object') {
|
65 | plugin = name;
|
66 | this.instantiatedPlugins.push(plugin);
|
67 | } else {
|
68 | PluginType = this.pluginTypes[type] || Plugin;
|
69 | options = options || {};
|
70 | options.applicationName = this.app.name;
|
71 | options.app = this.app;
|
72 |
|
73 | plugin = new PluginType(name, extension, options);
|
74 | }
|
75 |
|
76 | debug('add type: %s, name: %s, extension:%s, options:%s', type, plugin.name, plugin.ext, options);
|
77 |
|
78 | registered.push(plugin);
|
79 | };
|
80 |
|
81 | Registry.prototype.remove = function(type /* name */) {
|
82 | var registered = this.registeredForType(type);
|
83 | var registeredIndex, name;
|
84 |
|
85 | if (typeof arguments[1] === 'object') {
|
86 | name = arguments[1].name;
|
87 | } else {
|
88 | name = arguments[1];
|
89 | }
|
90 |
|
91 | debug('remove type: %s, name: %s', type, name);
|
92 |
|
93 | for (var i = 0, l = registered.length; i < l; i++) {
|
94 | if (registered[i].name === name) {
|
95 | registeredIndex = i;
|
96 | }
|
97 | }
|
98 |
|
99 | var plugin = registered[registeredIndex];
|
100 | var instantiatedPluginIndex = this.instantiatedPlugins.indexOf(plugin);
|
101 |
|
102 | if (instantiatedPluginIndex > -1) {
|
103 | this.instantiatedPlugins.splice(instantiatedPluginIndex, 1);
|
104 | }
|
105 |
|
106 | if (registeredIndex !== undefined && registeredIndex > -1) {
|
107 | registered.splice(registeredIndex, 1);
|
108 | }
|
109 | };
|