Completed
Push — master ( a2efde...24c14a )
by Thomas
49s
created

lib/plugins/Autoreload.js   A

Complexity

Total Complexity 10
Complexity/F 3.33

Size

Lines of Code 47
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 2
dl 0
loc 47
rs 10
wmc 10
mnd 2
bc 9
fnc 3
bpm 3
cpm 3.3333
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B module.exports 0 28 1
A _.debounce 0 11 1
1
'use strict'
2
3
const minimatch = require('minimatch')
4
const _ = require('lodash')
5
const request = require('../util').request
6
const Module = require('../containers/Module')
7
8
const reload = _.debounce(function (containerToken, path, event) {
9
  request(containerToken + '/ws', {
10
    type: 'autoreload',
11
    payload: {
12
      path: path,
13
      event: event
14
    }
15
  }, {
16
    'Content-Type': 'multipart/form-data'
17
  }).end()
18
}, 120)
19
20
module.exports = function (containerToken, path, event) {
21
  Module.getManifest(function (manifest) {
22
    var options = manifest.getDevOption('autoreload')
23
    if (!options || !('paths' in options)) {
24
      return
25
    }
26
    if (typeof options.paths === 'string') {
27
      options.paths = [options.paths]
28
    }
29
30
    var matched = false
31
    for (var i in options.paths) {
32
      if (!options.paths.hasOwnProperty(i)) {
33
        continue
34
      }
35
      if (minimatch(path, options.paths[i])) {
36
        matched = true
37
        break
38
      }
39
    }
40
41
    if (!matched) {
42
      return
43
    }
44
45
    reload(containerToken, path, event)
46
  })
47
}
48