includable /
cli
| 1 | View Code Duplication | 'use strict' |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 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 |