|
1
|
|
|
import path from 'path' |
|
2
|
|
|
import fse from 'fs-extra' |
|
3
|
|
|
|
|
4
|
|
|
import { |
|
5
|
|
|
cmsData, |
|
6
|
|
|
config |
|
7
|
|
|
} from '../' |
|
8
|
|
|
|
|
9
|
|
|
let singleton = Symbol() |
|
10
|
|
|
let singletonEnforcer = Symbol() |
|
11
|
|
|
|
|
12
|
|
|
class Plugins { |
|
13
|
|
|
|
|
14
|
|
|
constructor(enforcer) { |
|
15
|
|
|
if(enforcer != singletonEnforcer) throw 'Cannot construct Plugins singleton' |
|
|
|
|
|
|
16
|
|
|
this._plugins = [] |
|
17
|
|
|
this.fn = [] |
|
18
|
|
|
var pluginsDir = path.join(config.root, config.plugins.url) |
|
19
|
|
|
try { |
|
20
|
|
|
var directoryPlugins = fse.lstatSync(pluginsDir) |
|
21
|
|
|
if (directoryPlugins.isDirectory()) { |
|
22
|
|
|
|
|
23
|
|
|
this._plugins = cmsData.file.getFolders(pluginsDir, true, 0) |
|
24
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
25
|
|
|
// has hooks |
|
26
|
|
|
var plugHooks = path.join(plugin.path, config.hooks.url) |
|
27
|
|
|
try { |
|
28
|
|
|
var directoryHook = fse.lstatSync(plugHooks) |
|
29
|
|
|
if (directoryHook.isDirectory()) { |
|
30
|
|
|
var plugHooksFile = path.join(plugHooks, 'hooks.js') |
|
31
|
|
|
var h = require(plugHooksFile) |
|
32
|
|
|
plugin.hooks = h.default |
|
33
|
|
|
}else { |
|
34
|
|
|
plugin.hooks = null |
|
35
|
|
|
} |
|
36
|
|
|
}catch(e) { |
|
37
|
|
|
plugin.hooks = null |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// has partials |
|
41
|
|
|
var plugPartials = path.join(plugin.path, config.pluginsPartials) |
|
42
|
|
|
try { |
|
43
|
|
|
var directoryPartials = fse.lstatSync(plugPartials) |
|
44
|
|
|
if (directoryPartials.isDirectory()) { |
|
45
|
|
|
plugin.partials = plugPartials |
|
46
|
|
|
}else { |
|
47
|
|
|
plugin.partials = null |
|
48
|
|
|
} |
|
49
|
|
|
}catch(e) { |
|
50
|
|
|
plugin.partials = null |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// has templates |
|
54
|
|
|
var plugTemplates = path.join(plugin.path, config.templates.url) |
|
55
|
|
|
try { |
|
56
|
|
|
var directoryTemplates = fse.lstatSync(plugTemplates) |
|
57
|
|
|
if (directoryTemplates.isDirectory()) { |
|
58
|
|
|
plugin.templates = plugTemplates |
|
59
|
|
|
}else { |
|
60
|
|
|
plugin.templates = null |
|
61
|
|
|
} |
|
62
|
|
|
}catch(e) { |
|
63
|
|
|
plugin.templates = null |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// has process |
|
67
|
|
|
var plugProcess = path.join(plugin.path, 'process') |
|
68
|
|
|
try { |
|
69
|
|
|
var directoryProcess = fse.lstatSync(plugProcess) |
|
70
|
|
|
if (directoryProcess.isDirectory()) { |
|
71
|
|
|
plugin.process = {} |
|
72
|
|
|
var processFiles = cmsData.file.getFiles(plugProcess, true, 0) |
|
73
|
|
|
Array.prototype.forEach.call(processFiles, (processFile) => { |
|
74
|
|
|
plugin.process[processFile.cleanNameNoExt] = processFile.path |
|
75
|
|
|
}) |
|
76
|
|
|
}else { |
|
77
|
|
|
plugin.process = null |
|
78
|
|
|
} |
|
79
|
|
|
}catch(e) { |
|
80
|
|
|
plugin.process = null |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// has routes |
|
84
|
|
|
var plugRoutes = path.join(plugin.path, 'routes') |
|
85
|
|
|
try { |
|
86
|
|
|
var directoryRoute = fse.lstatSync(plugRoutes) |
|
87
|
|
|
if (directoryRoute.isDirectory()) { |
|
88
|
|
|
plugin.routes = {} |
|
89
|
|
|
|
|
90
|
|
|
var gets = path.join(plugRoutes, 'get') |
|
91
|
|
|
try { |
|
92
|
|
|
var directoryGets = fse.lstatSync(gets) |
|
93
|
|
|
if (directoryGets.isDirectory()) { |
|
94
|
|
|
var routesGet = cmsData.file.getFiles(gets, true, 0) |
|
95
|
|
|
Array.prototype.forEach.call(routesGet, (route) => { |
|
96
|
|
|
route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*` |
|
97
|
|
|
}) |
|
98
|
|
|
plugin.routes.get = routesGet |
|
99
|
|
|
} |
|
100
|
|
|
}catch(e) { |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
try { |
|
104
|
|
|
var posts = path.join(plugRoutes, 'post') |
|
105
|
|
|
var directoryPosts = fse.lstatSync(gets) |
|
106
|
|
|
if (directoryPosts.isDirectory()) { |
|
107
|
|
|
var routesPost = cmsData.file.getFiles(posts, true, 0) |
|
108
|
|
|
Array.prototype.forEach.call(routesPost, (route) => { |
|
109
|
|
|
route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*` |
|
110
|
|
|
}) |
|
111
|
|
|
plugin.routes.post = routesPost |
|
112
|
|
|
} |
|
113
|
|
|
}catch(e) { |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
}else { |
|
117
|
|
|
plugin.routes = null |
|
118
|
|
|
} |
|
119
|
|
|
}catch(e) { |
|
120
|
|
|
plugin.routes = null |
|
121
|
|
|
} |
|
122
|
|
|
}) |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
} catch (e) { |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
static get instance() { |
|
131
|
|
|
if(!this[singleton]) { |
|
132
|
|
|
this[singleton] = new Plugins(singletonEnforcer) |
|
133
|
|
|
} |
|
134
|
|
|
return this[singleton] |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
getProcess(fn) { |
|
138
|
|
|
var proc = null |
|
139
|
|
|
if(typeof this._plugins !== 'undefined' && this._plugins !== null) { |
|
140
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
141
|
|
|
if(typeof plugin.process !== 'undefined' && plugin.process !== null |
|
142
|
|
|
&& typeof plugin.process[fn] !== 'undefined' && plugin.process[fn] !== null) { |
|
143
|
|
|
proc = plugin.process[fn] |
|
144
|
|
|
} |
|
145
|
|
|
}) |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return proc |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
hooks() { |
|
152
|
|
|
if(arguments.length > 0) { |
|
153
|
|
|
var args = [].slice.call(arguments) |
|
154
|
|
|
var fn = args.shift() |
|
155
|
|
|
|
|
156
|
|
|
if(this._plugins != null) { |
|
157
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
158
|
|
|
if(plugin.hooks != null&& plugin.hooks[fn] != null) { |
|
159
|
|
|
args[0] = plugin.hooks[fn].apply(this, args) |
|
160
|
|
|
} |
|
161
|
|
|
}) |
|
162
|
|
|
} |
|
163
|
|
|
} else { |
|
164
|
|
|
args = [''] |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return args[0] |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
getHooks() { |
|
171
|
|
|
return this._plugins |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
getPartials() { |
|
175
|
|
|
var partials = [] |
|
176
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
177
|
|
|
if(typeof plugin.partials !== 'undefined' && plugin.partials !== null) { |
|
178
|
|
|
partials.push(plugin.partials) |
|
179
|
|
|
} |
|
180
|
|
|
}) |
|
181
|
|
|
|
|
182
|
|
|
return partials |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
getRoutes() { |
|
186
|
|
|
var routes = [] |
|
187
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
188
|
|
|
if(typeof plugin.routes !== 'undefined' && plugin.routes !== null) { |
|
189
|
|
|
routes = routes.concat(plugin.routes) |
|
190
|
|
|
} |
|
191
|
|
|
}) |
|
192
|
|
|
|
|
193
|
|
|
return routes |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
export default Plugins |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.