|
1
|
|
|
import path from 'path' |
|
2
|
|
|
import { |
|
3
|
|
|
FileParser, |
|
4
|
|
|
config, |
|
5
|
|
|
folderUtils |
|
6
|
|
|
} from '../' |
|
7
|
|
|
|
|
8
|
|
|
let singleton = Symbol() |
|
9
|
|
|
let singletonEnforcer = Symbol() |
|
10
|
|
|
|
|
11
|
|
|
class Plugins { |
|
12
|
|
|
|
|
13
|
|
|
constructor(enforcer) { |
|
14
|
|
|
if(enforcer != singletonEnforcer) throw 'Cannot construct Plugins singleton' |
|
|
|
|
|
|
15
|
|
|
this._plugins = [] |
|
16
|
|
|
this.fn = [] |
|
17
|
|
|
var pluginsDir = path.join(config.root, config.plugins.url) |
|
18
|
|
|
if(folderUtils.isFolder(pluginsDir)) { |
|
19
|
|
|
this._plugins = FileParser.getFolders(pluginsDir, true, 0) |
|
20
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
21
|
|
|
// has hooks |
|
22
|
|
|
var plugHooks = path.join(plugin.path, config.hooks.url) |
|
23
|
|
|
if(folderUtils.isFolder(plugHooks)) { |
|
24
|
|
|
var plugHooksFile = path.join(plugHooks, 'hooks.js') |
|
25
|
|
|
var h = require(plugHooksFile) |
|
26
|
|
|
plugin.hooks = h.default |
|
27
|
|
|
}else { |
|
28
|
|
|
plugin.hooks = null |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// has partials |
|
32
|
|
|
var plugPartials = path.join(plugin.path, config.pluginsPartials) |
|
33
|
|
|
if(folderUtils.isFolder(plugPartials)) { |
|
34
|
|
|
plugin.partials = plugPartials |
|
35
|
|
|
}else { |
|
36
|
|
|
plugin.partials = null |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// has templates |
|
40
|
|
|
var plugTemplates = path.join(plugin.path, config.templates.url) |
|
41
|
|
|
if(folderUtils.isFolder(plugTemplates)) { |
|
42
|
|
|
plugin.templates = plugTemplates |
|
43
|
|
|
}else { |
|
44
|
|
|
plugin.templates = null |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// has process |
|
48
|
|
|
var plugProcess = path.join(plugin.path, 'process') |
|
49
|
|
|
if(folderUtils.isFolder(plugProcess)) { |
|
50
|
|
|
plugin.process = {} |
|
51
|
|
|
var processFiles = FileParser.getFiles(plugProcess, true, 0) |
|
52
|
|
|
Array.prototype.forEach.call(processFiles, (processFile) => { |
|
53
|
|
|
plugin.process[processFile.cleanNameNoExt] = processFile.path |
|
54
|
|
|
}) |
|
55
|
|
|
}else { |
|
56
|
|
|
plugin.process = null |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// has routes |
|
60
|
|
|
var plugRoutes = path.join(plugin.path, 'routes') |
|
61
|
|
|
if(folderUtils.isFolder(plugRoutes)) { |
|
62
|
|
|
plugin.routes = {} |
|
63
|
|
|
|
|
64
|
|
|
var gets = path.join(plugRoutes, 'get') |
|
65
|
|
|
if(folderUtils.isFolder(gets)) { |
|
66
|
|
|
var routesGet = FileParser.getFiles(gets, true, 0) |
|
67
|
|
|
Array.prototype.forEach.call(routesGet, (route) => { |
|
68
|
|
|
route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*` |
|
69
|
|
|
}) |
|
70
|
|
|
plugin.routes.get = routesGet |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
var posts = path.join(plugRoutes, 'post') |
|
74
|
|
|
if(folderUtils.isFolder(posts)) { |
|
75
|
|
|
var routesPost = FileParser.getFiles(posts, true, 0) |
|
76
|
|
|
Array.prototype.forEach.call(routesPost, (route) => { |
|
77
|
|
|
route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*` |
|
78
|
|
|
}) |
|
79
|
|
|
plugin.routes.post = routesPost |
|
80
|
|
|
} |
|
81
|
|
|
}else { |
|
82
|
|
|
plugin.routes = null |
|
83
|
|
|
} |
|
84
|
|
|
}) |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
static get instance() { |
|
89
|
|
|
if(!this[singleton]) { |
|
90
|
|
|
this[singleton] = new Plugins(singletonEnforcer) |
|
91
|
|
|
} |
|
92
|
|
|
return this[singleton] |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
getProcess(fn) { |
|
96
|
|
|
var proc = null |
|
97
|
|
|
if(typeof this._plugins !== 'undefined' && this._plugins !== null) { |
|
98
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
99
|
|
|
if(typeof plugin.process !== 'undefined' && plugin.process !== null |
|
100
|
|
|
&& typeof plugin.process[fn] !== 'undefined' && plugin.process[fn] !== null) { |
|
101
|
|
|
proc = plugin.process[fn] |
|
102
|
|
|
} |
|
103
|
|
|
}) |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return proc |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
hooks() { |
|
110
|
|
|
if(arguments.length > 0) { |
|
111
|
|
|
var args = [].slice.call(arguments) |
|
112
|
|
|
var fn = args.shift() |
|
113
|
|
|
|
|
114
|
|
|
if(typeof this._plugins !== 'undefined' && this._plugins !== null) { |
|
115
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
116
|
|
|
if(typeof plugin.hooks !== 'undefined' && plugin.hooks !== null |
|
117
|
|
|
&& typeof plugin.hooks[fn] !== 'undefined' && plugin.hooks[fn] !== null) { |
|
118
|
|
|
args[0] = plugin.hooks[fn].apply(this, args) |
|
119
|
|
|
} |
|
120
|
|
|
}) |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return args[0] |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
getHooks() { |
|
128
|
|
|
return this._plugins |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
getPartials() { |
|
132
|
|
|
var partials = [] |
|
133
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
134
|
|
|
if(typeof plugin.partials !== 'undefined' && plugin.partials !== null) { |
|
135
|
|
|
partials.push(plugin.partials) |
|
136
|
|
|
} |
|
137
|
|
|
}) |
|
138
|
|
|
|
|
139
|
|
|
return partials |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
getRoutes() { |
|
143
|
|
|
var routes = [] |
|
144
|
|
|
Array.prototype.forEach.call(this._plugins, (plugin) => { |
|
145
|
|
|
if(typeof plugin.routes !== 'undefined' && plugin.routes !== null) { |
|
146
|
|
|
routes = routes.concat(plugin.routes) |
|
147
|
|
|
} |
|
148
|
|
|
}) |
|
149
|
|
|
|
|
150
|
|
|
return routes |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
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.