Completed
Push — master ( a35fd3...1710d3 )
by Thomas
31s
created

Autoreload.js ➔ matchFile   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 16
rs 8.8571
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
const matchFile = function (options, path) {
21
  if (typeof options.paths === 'string') {
22
    options.paths = [options.paths]
23
  }
24
25
  for (var i in options.paths) {
26
    if (!options.paths.hasOwnProperty(i)) {
27
      continue
28
    }
29
    if (minimatch(path, options.paths[i])) {
30
      return true
31
    }
32
  }
33
34
  return false
35
}
36
37
module.exports = function (containerToken, path, event) {
38
  Module.getManifest(function (manifest) {
39
    var options = manifest.getDevOption('autoreload')
40
    if (!options || !('paths' in options)) {
41
      return
42
    }
43
44
    if (!matchFile(options, path)) {
45
      return
46
    }
47
48
    reload(containerToken, path, event)
49
  })
50
}
51