2
2
'use strict' ;
3
3
4
4
var path = require ( 'path' ) ;
5
- var http = require ( 'http' ) ;
6
5
7
- /* default reload css extensions */
6
+ // default reload css extensions
8
7
var styleExtensions = [ 'css' , 'scss' , 'sass' , 'less' , 'styl' ] ;
9
8
var reloadCssPattern = new RegExp ( '\.(' + styleExtensions . join ( '|' ) + ')$' ) ;
10
9
11
10
var noop = function ( ) { } ;
12
11
13
12
module . exports = function StylesReloader ( options ) {
14
13
var options = options ;
15
- var liveReloadEnabled = options . liveReload ;
16
14
var fsWatcher = options . watcher ;
17
15
var ui = options . ui ;
16
+ var _isRunning = false ;
17
+ var lsProxy = options . ssl ? require ( 'https' ) : require ( 'http' ) ;
18
18
19
- // Build app style pattern
19
+ // build app style pattern
20
20
var appStylePath = options . project . root + path . join ( '/app' , 'styles' , '*' ) ;
21
21
var appStylePattern = new RegExp ( '^' + appStylePath ) ;
22
22
var appStyleResource = options . project . pkg . name + '.css' ;
23
23
24
- // Livereload host/port
24
+ // livereload hostname
25
25
var liveReloadHostname = [
26
26
( options . ssl ? 'https://' :'http://' ) ,
27
27
( options . liveReloadHost || options . host ) ,
28
28
':' ,
29
29
options . liveReloadPort
30
30
] . join ( '' ) ;
31
31
32
- function getChangedStyle ( filePath ) {
33
- if ( filePath . match ( appStylePattern ) ) {
34
- return appStyleResource ;
35
- }
36
- return 'vendor.css' ;
32
+
33
+ function shouldReload ( filePath ) {
34
+ return filePath . match ( reloadCssPattern ) ;
35
+ } ;
36
+
37
+ function getReloadResource ( filePath ) {
38
+ return filePath . match ( appStylePattern ) ? appStyleResource : 'vendor.css' ;
37
39
} ;
38
40
39
41
function fileDidChange ( results ) {
40
42
var filePath = results . filePath || '' ;
41
- var liveReloadFile = getChangedStyle ( filePath ) ;
42
43
43
- if ( filePath . match ( reloadCssPattern ) ) {
44
- ui . writeLine ( 'Reloading ' + liveReloadFile + ' only' ) ;
45
- http . get ( liveReloadHostname + '/changed?files=' + liveReloadFile )
44
+ // notify livereload server if needed
45
+ if ( shouldReload ( filePath ) ) {
46
+ var reloadResource = getReloadResource ( filePath ) ;
47
+ ui . writeLine ( 'Reloading ' + reloadResource + ' only' ) ;
48
+ lsProxy . get ( liveReloadHostname + '/changed?files=' + reloadResource )
46
49
. on ( 'error' , noop ) ;
47
50
}
48
51
} ;
49
52
50
- function updateReloadFilters ( ) {
53
+ function mergeReloadFilters ( ) {
51
54
options . project . liveReloadFilterPatterns . push ( reloadCssPattern ) ;
52
55
} ;
53
56
54
57
return {
58
+
55
59
run : function ( ) {
56
- if ( liveReloadEnabled ) {
57
- ui . writeLine ( 'StylesReloader watches ' + styleExtensions . join ( '|' ) ) ;
58
- updateReloadFilters ( ) ;
60
+ if ( ! options . liveReload ) {
61
+ ui . writeLine ( 'StylesReloader is disabled' ) ;
62
+ return ;
63
+ }
64
+
65
+ if ( this . isRunning ( ) ) {
66
+ return ;
67
+ }
68
+
69
+ ui . writeLine ( 'StylesReloader watches ' + styleExtensions . join ( '|' ) ) ;
70
+ if ( fsWatcher ) {
71
+ mergeReloadFilters ( ) ;
59
72
fsWatcher . on ( 'change' , fileDidChange . bind ( this ) ) ;
73
+ _isRunning = ! _isRunning ;
60
74
}
75
+ } ,
76
+
77
+ isRunning : function ( ) {
78
+ return _isRunning ;
61
79
}
62
80
} ;
63
81
} ;
0 commit comments