Completed
Push — master ( 652c21...99e276 )
by greg
10:52 queued 08:59
created

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

Size

Lines of Code 56

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A abe-hooks.js ➔ ??? 0 11 3
1
import extend from 'extend'
2
import path from 'path'
3
4
import hooksDefault from '../../hooks/hooks'
5
6
import {
7
  config
8
  ,fileUtils
9
  ,Plugins
10
} from '../'
11
12
import * as abe from '../'
13
14
let singleton = Symbol()
15
let singletonEnforcer = Symbol()
16
17
class Hooks {
18
19
  constructor(enforcer) {
20
    if(enforcer != singletonEnforcer) throw 'Cannot construct Json 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...
21
22
    if(fileUtils.isFile(path.join(config.root, config.hooks.url, 'hooks.js'))){
23
      var h = require(path.join(config.root, config.hooks.url, 'hooks.js'))
24
      this.fn = extend(true, hooksDefault, h.default)
25
    }
26
    else{
27
      this.fn = hooksDefault
28
    }
29
  }
30
31
  trigger() {
32
    if(arguments.length > 0) {
33
      var args = [].slice.call(arguments)
34
      var fn = args.shift()
35
      args.push(abe)
36
      
37
      if(typeof this.fn !== 'undefined' && this.fn !== null
38
        && typeof this.fn[fn] !== 'undefined' && this.fn[fn] !== null) {
39
        args[0] = this.fn[fn].apply(this, args)
40
      }
41
42
      args[0] = Plugins.instance.hooks.apply(Plugins.instance, [fn].concat(args))
43
    }
44
45
    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 32 is false. Are you sure this can never be the case?
Loading history...
46
  }
47
48
  static get instance() {
49
    if(!this[singleton]) {
50
      this[singleton] = new Hooks(singletonEnforcer)
51
    }
52
    return this[singleton]
53
  }
54
}
55
56
export default Hooks