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