Completed
Push — master ( e0892a...db4e0d )
by
unknown
01:51
created

src/cli/extend/abe-plugins.js   A

Size

Lines of Code 195

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
nc 1
dl 0
loc 195
rs 10
noi 5

1 Function

Rating   Name   Duplication   Size   Complexity  
B abe-plugins.js ➔ ??? 0 115 4
1
import path from 'path'
2
import fse from 'fs-extra'
3
import {
4
  FileParser,
5
  config
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'
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

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 (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
15
    this._plugins = []
16
    this.fn = []
17
    var pluginsDir = path.join(config.root, config.plugins.url)
18
    try {
19
      var directoryPlugins = fse.lstatSync(pluginsDir);
20
      if (directoryPlugins.isDirectory()) {
21
        
22
        this._plugins = FileParser.getFolders(pluginsDir, true, 0)
23
        Array.prototype.forEach.call(this._plugins, (plugin) => {
24
          // has hooks
25
          var plugHooks = path.join(plugin.path, config.hooks.url)
26
          try {
27
            var directoryHook = fse.lstatSync(plugHooks);
28
            if (directoryHook.isDirectory()) {
29
              var plugHooksFile = path.join(plugHooks, 'hooks.js')
30
              var h = require(plugHooksFile)
31
              plugin.hooks = h.default
32
            }else {
33
              plugin.hooks = null
34
            }
35
          }catch(e) {
36
            plugin.hooks = null
37
          }
38
39
          // has partials
40
          var plugPartials = path.join(plugin.path, config.pluginsPartials)
41
          try {
42
            var directoryPartials = fse.lstatSync(plugPartials);
43
            if (directoryPartials.isDirectory()) {
44
              plugin.partials = plugPartials
45
            }else {
46
              plugin.partials = null
47
            }
48
          }catch(e) {
49
            plugin.partials = null
50
          }
51
52
          // has templates
53
          var plugTemplates = path.join(plugin.path, config.templates.url)
54
          try {
55
            var directoryTemplates = fse.lstatSync(plugTemplates);
56
            if (directoryTemplates.isDirectory()) {
57
              plugin.templates = plugTemplates
58
            }else {
59
              plugin.templates = null
60
            }
61
          }catch(e) {
62
            plugin.templates = null
63
          }
64
65
          // has process
66
          var plugProcess = path.join(plugin.path, 'process')
67
          try {
68
            var directoryProcess = fse.lstatSync(plugProcess);
69
            if (directoryProcess.isDirectory()) {
70
              plugin.process = {}
71
              var processFiles = FileParser.getFiles(plugProcess, true, 0)
72
              Array.prototype.forEach.call(processFiles, (processFile) => {
73
                plugin.process[processFile.cleanNameNoExt] = processFile.path
74
              })
75
            }else {
76
              plugin.process = null
77
            }
78
          }catch(e) {
79
            plugin.process = null
80
          }
81
82
          // has routes
83
          var plugRoutes = path.join(plugin.path, 'routes')
84
          try {
85
            var directoryRoute = fse.lstatSync(plugRoutes);
86
            if (directoryRoute.isDirectory()) {
87
              plugin.routes = {}
88
89
              var gets = path.join(plugRoutes, 'get')
90
              try {
91
                var directoryGets = fse.lstatSync(gets);
92
                if (directoryGets.isDirectory()) {
93
                  var routesGet = FileParser.getFiles(gets, true, 0)
94
                  Array.prototype.forEach.call(routesGet, (route) => {
95
                    route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*`
96
                  })
97
                  plugin.routes.get = routesGet
98
                }
99
              }catch(e) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
100
101
              }
102
              try {
103
                var posts = path.join(plugRoutes, 'post')
104
                var directoryPosts = fse.lstatSync(gets);
105
                if (directoryPosts.isDirectory()) {
106
                  var routesPost = FileParser.getFiles(posts, true, 0)
107
                  Array.prototype.forEach.call(routesPost, (route) => {
108
                    route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*`
109
                  })
110
                  plugin.routes.post = routesPost
111
                }
112
              }catch(e) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
113
114
              }
115
            }else {
116
              plugin.routes = null
117
            }
118
          }catch(e) {
119
            plugin.routes = null
120
          }
121
        })
122
      
123
      }
124
    } catch (e) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
125
      
126
    }
127
  }
128
129
  static get instance() {
130
    if(!this[singleton]) {
131
      this[singleton] = new Plugins(singletonEnforcer)
132
    }
133
    return this[singleton]
134
  }
135
136
  getProcess(fn) {
137
    var proc = null
138
    if(typeof this._plugins !== 'undefined' && this._plugins !== null) {
139
      Array.prototype.forEach.call(this._plugins, (plugin) => {
140
        if(typeof plugin.process !== 'undefined' && plugin.process !== null
141
          && typeof plugin.process[fn] !== 'undefined' && plugin.process[fn] !== null) {
142
          proc = plugin.process[fn]
143
        }
144
      })
145
    }
146
147
    return proc
148
  }
149
150
  hooks() {
151
    if(arguments.length > 0) {
152
      var args = [].slice.call(arguments)
153
      var fn = args.shift()
154
155
      if(typeof this._plugins !== 'undefined' && this._plugins !== null) {
156
        Array.prototype.forEach.call(this._plugins, (plugin) => {
157
          if(typeof plugin.hooks !== 'undefined' && plugin.hooks !== null
158
            && typeof plugin.hooks[fn] !== 'undefined' && plugin.hooks[fn] !== null) {
159
            args[0] = plugin.hooks[fn].apply(this, args)
160
          }
161
        })
162
      }
163
    }
164
165
    return args[0]
0 ignored issues
show
Bug introduced by
The variable args does not seem to be initialized in case arguments.length > 0 on line 151 is false. Are you sure this can never be the case?
Loading history...
166
  }
167
168
  getHooks() {
169
    return this._plugins
170
  }
171
172
  getPartials() {
173
    var partials = []
174
    Array.prototype.forEach.call(this._plugins, (plugin) => {
175
      if(typeof plugin.partials !== 'undefined' && plugin.partials !== null) {
176
        partials.push(plugin.partials)
177
      }
178
    })
179
180
    return partials
181
  }
182
183
  getRoutes() {
184
    var routes = []
185
    Array.prototype.forEach.call(this._plugins, (plugin) => {
186
      if(typeof plugin.routes !== 'undefined' && plugin.routes !== null) {
187
        routes = routes.concat(plugin.routes)
188
      }
189
    })
190
191
    return routes
192
  }
193
}
194
195
export default Plugins