Merge pull request #389 from timothykang/add-hostname-option

Expose hostname parameter of tiny-lr
This commit is contained in:
Kyle Robinson Young 2015-02-25 17:03:31 -08:00
commit 0396f8fc0f
4 changed files with 20 additions and 15 deletions

View File

@ -171,7 +171,7 @@ watch: {
}, },
``` ```
It's possible to get livereload working over https connections. To do this, pass an object to `livereload` with a `key` and `cert` paths specified. Passing an object to `livereload` allows listening on a specific port and hostname/IP or over https connections (by specifying `key` and `cert` paths).
Example: Example:
```js ```js
@ -181,6 +181,7 @@ watch: {
tasks: ['sass'], tasks: ['sass'],
options: { options: {
livereload: { livereload: {
host: 'localhost',
port: 9000, port: 9000,
key: grunt.file.read('path/to/ssl.key'), key: grunt.file.read('path/to/ssl.key'),
cert: grunt.file.read('path/to/ssl.crt') cert: grunt.file.read('path/to/ssl.crt')

View File

@ -26,8 +26,11 @@ module.exports = function(grunt) {
} else { } else {
options = _.defaults(options, defaults); options = _.defaults(options, defaults);
} }
if (servers[options.port]) {
this.server = servers[options.port]; var host = (options.host || '*') + ':' + options.port;
if (servers[host]) {
this.server = servers[host];
} else { } else {
this.server = tinylr(options); this.server = tinylr(options);
this.server.server.removeAllListeners('error'); this.server.server.removeAllListeners('error');
@ -39,11 +42,11 @@ module.exports = function(grunt) {
} }
process.exit(1); process.exit(1);
}); });
this.server.listen(options.port, function(err) { this.server.listen(options.port, options.host, function(err) {
if (err) { return grunt.fatal(err); } if (err) { return grunt.fatal(err); }
grunt.log.verbose.writeln('Live reload server started on port: ' + options.port); grunt.log.verbose.writeln('Live reload server started on ' + host);
}); });
servers[options.port] = this.server; servers[host] = this.server;
} }
} }

View File

@ -12,11 +12,12 @@ module.exports = function(grunt) {
files: ['lib/*.js'], files: ['lib/*.js'],
tasks: ['before'], tasks: ['before'],
}, },
customport: { customhost: {
files: ['lib/*.js'], files: ['lib/*.js'],
tasks: ['before'], tasks: ['before'],
options: { options: {
livereload: { livereload: {
host: 'localhost',
port: 8675, port: 8675,
}, },
}, },

View File

@ -56,7 +56,7 @@ exports.livereload = {
result = helper.unixify(result); result = helper.unixify(result);
helper.verboseLog(result); helper.verboseLog(result);
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
test.ok(result.indexOf('Live reload server started on port: 35729') !== -1, test.ok(result.indexOf('Live reload server started on *:35729') !== -1,
'live reload server should have been started on port 35729.'); 'live reload server should have been started on port 35729.');
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
resultData = JSON.parse(resultData); resultData = JSON.parse(resultData);
@ -64,11 +64,11 @@ exports.livereload = {
test.done(); test.done();
}); });
}, },
customport: function(test) { customhost: function(test) {
test.expect(4); test.expect(4);
var resultData = ''; var resultData = '';
var cwd = path.resolve(fixtures, 'livereload'); var cwd = path.resolve(fixtures, 'livereload');
var assertWatch = helper.assertTask(['watch:customport', '-v'], {cwd: cwd}); var assertWatch = helper.assertTask(['watch:customhost', '-v'], {cwd: cwd});
assertWatch([function() { assertWatch([function() {
request(8675, function(data) { request(8675, function(data) {
resultData += data; resultData += data;
@ -78,8 +78,8 @@ exports.livereload = {
result = helper.unixify(result); result = helper.unixify(result);
helper.verboseLog(result); helper.verboseLog(result);
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
test.ok(result.indexOf('Live reload server started on port: 8675') !== -1, test.ok(result.indexOf('Live reload server started on localhost:8675') !== -1,
'live reload server should have been started on port 35729.'); 'live reload server should have been started on localhost:8675.');
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
resultData = JSON.parse(resultData); resultData = JSON.parse(resultData);
test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.'); test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.');
@ -119,7 +119,7 @@ exports.livereload = {
result = helper.unixify(result); result = helper.unixify(result);
helper.verboseLog(result); helper.verboseLog(result);
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
test.ok(result.indexOf('Live reload server started on port: 9876') !== -1, test.ok(result.indexOf('Live reload server started on *:9876') !== -1,
'live reload server should have been started on port 9876.'); 'live reload server should have been started on port 9876.');
test.ok(/Live reloading (lib\/one\.js, lib\/two.js|lib\/two.js, lib\/one.js)\.\.\./.test(result), test.ok(/Live reloading (lib\/one\.js, lib\/two.js|lib\/two.js, lib\/one.js)\.\.\./.test(result),
'live reload should have triggered on lib/one.js and lib/two.js'); 'live reload should have triggered on lib/one.js and lib/two.js');
@ -142,7 +142,7 @@ exports.livereload = {
result = helper.unixify(result); result = helper.unixify(result);
helper.verboseLog(result); helper.verboseLog(result);
test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.');
test.ok(result.indexOf('Live reload server started on port: 1337') !== -1, test.ok(result.indexOf('Live reload server started on *:1337') !== -1,
'live reload server should have been started on port 1337.'); 'live reload server should have been started on port 1337.');
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
resultData = JSON.parse(resultData); resultData = JSON.parse(resultData);
@ -163,7 +163,7 @@ exports.livereload = {
}], function(result) { }], function(result) {
result = helper.unixify(result); result = helper.unixify(result);
helper.verboseLog(result); helper.verboseLog(result);
test.ok(result.indexOf('Live reload server started on port: 35729') !== -1, test.ok(result.indexOf('Live reload server started on *:35729') !== -1,
'live reload server should have been started on port 35729.'); 'live reload server should have been started on port 35729.');
test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js');
resultData = JSON.parse(resultData); resultData = JSON.parse(resultData);