blob: acabba92dda942ae3ae631fc17daf0d8a1550efc (
plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
var Immutable = require('immutable');
var TemplateBlock = require('./templateBlock');
var PluginDependency = require('./pluginDependency');
var THEME_PREFIX = require('../constants/themePrefix');
var DEFAULT_VERSION = '*';
var Plugin = Immutable.Record({
name: String(),
// Requirement version (ex: ">1.0.0")
version: String(DEFAULT_VERSION),
// Path to load this plugin
path: String(),
// Depth of this plugin in the dependency tree
depth: Number(0),
// Parent depending on this plugin
parent: String(),
// Content of the "package.json"
package: Immutable.Map(),
// Content of the package itself
content: Immutable.Map()
}, 'Plugin');
Plugin.prototype.getName = function() {
return this.get('name');
};
Plugin.prototype.getPath = function() {
return this.get('path');
};
Plugin.prototype.getVersion = function() {
return this.get('version');
};
Plugin.prototype.getPackage = function() {
return this.get('package');
};
Plugin.prototype.getContent = function() {
return this.get('content');
};
Plugin.prototype.getDepth = function() {
return this.get('depth');
};
Plugin.prototype.getParent = function() {
return this.get('parent');
};
/**
* Return the ID on NPM for this plugin
* @return {String}
*/
Plugin.prototype.getNpmID = function() {
return PluginDependency.nameToNpmID(this.getName());
};
/**
* Check if a plugin is loaded
* @return {Boolean}
*/
Plugin.prototype.isLoaded = function() {
return Boolean(this.getPackage().size > 0);
};
/**
* Check if a plugin is a theme given its name
* @return {Boolean}
*/
Plugin.prototype.isTheme = function() {
var name = this.getName();
return (name && name.indexOf(THEME_PREFIX) === 0);
};
/**
* Return map of hooks
* @return {Map<String:Function>}
*/
Plugin.prototype.getHooks = function() {
return this.getContent().get('hooks') || Immutable.Map();
};
/**
* Return infos about resources for a specific type
* @param {String} type
* @return {Map<String:Mixed>}
*/
Plugin.prototype.getResources = function(type) {
if (type != 'website' && type != 'ebook') {
throw new Error('Invalid assets type ' + type);
}
var content = this.getContent();
return (content.get(type)
|| (type == 'website'? content.get('book') : null)
|| Immutable.Map());
};
/**
* Return map of filters
* @return {Map<String:Function>}
*/
Plugin.prototype.getFilters = function() {
return this.getContent().get('filters');
};
/**
* Return map of blocks
* @return {Map<String:TemplateBlock>}
*/
Plugin.prototype.getBlocks = function() {
var blocks = this.getContent().get('blocks');
blocks = blocks || Immutable.Map();
return blocks
.map(function(block, blockName) {
return TemplateBlock.create(blockName, block);
});
};
/**
* Return a specific hook
* @param {String} name
* @return {Function|undefined}
*/
Plugin.prototype.getHook = function(name) {
return this.getHooks().get(name);
};
/**
* Create a plugin from a string
* @param {String}
* @return {Plugin}
*/
Plugin.createFromString = function(s) {
var parts = s.split('@');
var name = parts[0];
var version = parts.slice(1).join('@');
return new Plugin({
name: name,
version: version || DEFAULT_VERSION
});
};
/**
* Create a plugin from a dependency
* @param {PluginDependency}
* @return {Plugin}
*/
Plugin.createFromDep = function(dep) {
return new Plugin({
name: dep.getName(),
version: dep.getVersion()
});
};
Plugin.nameToNpmID = PluginDependency.nameToNpmID;
module.exports = Plugin;
|