Skip to content

Commit 9c111c1

Browse files
committed
added unit tests
1 parent a31b0a3 commit 9c111c1

File tree

3 files changed

+96
-66
lines changed

3 files changed

+96
-66
lines changed

index.js

-46
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,9 @@
11
// /* jshint node: true */
22
// 'use strict';
33

4-
// /* default reload css extensions */
5-
// var styleExtensions = ['css', 'scss', 'sass', 'less', 'styl'];
6-
// var reloadCssPattern = new RegExp('.(' + styleExtensions.join('|') + ')$');
7-
8-
// function LiveStyleReloader(options){
9-
10-
// var noop = function(){},
11-
// options = options,
12-
// appCssPattern = new RegExp('^' + options.project.root + '/app/styles/*'),
13-
// ui = options.ui,
14-
// http = null,
15-
// liveReloadHost = options.liveReloadHost || options.host
16-
// liveReloadHostname = [(options.ssl ? 'https://' :'http://'), liveReloadHost, ':', options.liveReloadPort].join(''),
17-
// reloadCssPattern = options.reloadCssPattern;
18-
19-
// var getDirtyAsset = function(changedFilePath){
20-
// if (changedFilePath.match(appCssPattern)){
21-
// return options.project.pkg.name + '.css';
22-
// }
23-
// return 'vendor.css';
24-
// };
25-
26-
// var fileDidChange = function(results){
27-
// var filePath = results.filePath || '';
28-
// var dirtyAsset = getDirtyAsset(filePath);
29-
30-
// if (filePath.match(reloadCssPattern)){
31-
// ui.writeLine("Reloading " + dirtyAsset + " only");
32-
// http.get(liveReloadHostname + '/changed?files=' + dirtyAsset)
33-
// .on('error', noop);
34-
// }
35-
// };
36-
37-
// this.startObserving = function(watcher){
38-
// if (options.liveReload){
39-
// ui.writeLine('StyleReloader watches ' + styleExtensions.join('|'));
40-
// http = require('http');
41-
// watcher.on('change', fileDidChange.bind(this));
42-
// }
43-
// };
44-
// };
45-
464
module.exports = {
475
name: 'ember-cli-styles-reloader',
486

49-
isDevelopingAddon: function() {
50-
return true;
51-
},
52-
537
serverMiddleware: function(config){
548
var lsReloader = require('./lib/styles-reloader')(config.options);
559
lsReloader.run();

lib/styles-reloader.js

+36-18
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,80 @@
22
'use strict';
33

44
var path = require('path');
5-
var http = require('http');
65

7-
/* default reload css extensions */
6+
// default reload css extensions
87
var styleExtensions = ['css', 'scss', 'sass', 'less', 'styl'];
98
var reloadCssPattern = new RegExp('\.(' + styleExtensions.join('|') + ')$');
109

1110
var noop = function(){};
1211

1312
module.exports = function StylesReloader(options){
1413
var options = options;
15-
var liveReloadEnabled = options.liveReload;
1614
var fsWatcher = options.watcher;
1715
var ui = options.ui;
16+
var _isRunning = false;
17+
var lsProxy = options.ssl ? require('https') : require('http');
1818

19-
// Build app style pattern
19+
// build app style pattern
2020
var appStylePath = options.project.root + path.join('/app', 'styles', '*');
2121
var appStylePattern = new RegExp('^' + appStylePath);
2222
var appStyleResource = options.project.pkg.name + '.css';
2323

24-
// Livereload host/port
24+
// livereload hostname
2525
var liveReloadHostname = [
2626
(options.ssl ? 'https://' :'http://'),
2727
(options.liveReloadHost || options.host),
2828
':',
2929
options.liveReloadPort
3030
].join('');
3131

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';
3739
};
3840

3941
function fileDidChange(results){
4042
var filePath = results.filePath || '';
41-
var liveReloadFile = getChangedStyle(filePath);
4243

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)
4649
.on('error', noop);
4750
}
4851
};
4952

50-
function updateReloadFilters(){
53+
function mergeReloadFilters(){
5154
options.project.liveReloadFilterPatterns.push(reloadCssPattern);
5255
};
5356

5457
return {
58+
5559
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();
5972
fsWatcher.on('change', fileDidChange.bind(this));
73+
_isRunning = !_isRunning;
6074
}
75+
},
76+
77+
isRunning: function(){
78+
return _isRunning;
6179
}
6280
};
6381
};

tests/reloader-test.js

+60-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,70 @@
11
var sReloader = require('../lib/styles-reloader');
2+
var expect = require('chai').expect;
3+
var should = require('chai').should();
4+
25

36
describe("styles reloader", function(){
7+
var watcher = {
8+
_handlers: [],
9+
10+
on: function(event, handler){
11+
this._handlers[event] = handler;
12+
},
13+
14+
trigger: function(event, args){
15+
this._handlers[event].call(this, args);
16+
}
17+
};
18+
var opts = {
19+
uiMsg: [],
20+
liveReload: true,
21+
ui: { writeLine: function(msg){ opts.uiMsg.push(msg); }},
22+
project: { root: 'app', pkg: {name:'app'}, liveReloadFilterPatterns:[]},
23+
watcher: watcher
24+
};
25+
26+
var resetOptions = function(){
27+
watcher._handlers.length = 0;
28+
opts.uiMsg.length = 0;
29+
opts.liveReload = true;
30+
opts.project.liveReloadFilterPatterns.length = 0;
31+
opts.watcher = watcher;
32+
};
33+
34+
beforeEach(function(){
35+
resetOptions();
36+
});
37+
38+
it('does not run when liveReload is disabled. Must inform the user', function(done){
39+
opts.liveReload = false;
40+
var rl = sReloader(opts);
41+
rl.run();
42+
rl.isRunning().should.equal(false);
43+
opts.uiMsg.length.should.equal(1);
44+
opts.uiMsg[0].should.equal('StylesReloader is disabled');
45+
done();
46+
});
47+
48+
it('does not run if watcher is not defined.', function(done){
49+
delete opts.watcher;
50+
var rl = sReloader(opts);
51+
rl.run();
52+
rl.isRunning().should.equal(false);
53+
done();
54+
});
455

5-
it('this must work', function(done){
56+
it('attach default live reload style pattern', function(done){
57+
opts.project.liveReloadFilterPatterns.should.be.empty;
58+
sReloader(opts).run();
59+
opts.project.liveReloadFilterPatterns.should.deep.equal([/.(css|scss|sass|less|styl)$/]);
660
done();
761
});
862

9-
it('worked again', function(done){
63+
it('runs as expected', function(done){
64+
var rl = sReloader(opts);
65+
rl.run();
66+
rl.isRunning().should.equal(true);
67+
opts.uiMsg[0].should.have.string('StylesReloader watches');
1068
done();
1169
});
1270

0 commit comments

Comments
 (0)