Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / tasks / lib / livereload.js
1 /*
2  * grunt-contrib-watch
3  * http://gruntjs.com/
4  *
5  * Copyright (c) 2016 "Cowboy" Ben Alman, contributors
6  * Licensed under the MIT license.
7  */
8
9 'use strict';
10
11 var tinylr = require('tiny-lr');
12 var _ = require('lodash');
13
14 // Holds the servers out of scope in case watch is reloaded
15 var servers = Object.create(null);
16
17 module.exports = function(grunt) {
18
19   var defaults = {port: 35729};
20
21   function LR(options) {
22     if (options === true) {
23       options = defaults;
24     } else if (typeof options === 'number') {
25       options = {port: options};
26     } else {
27       options = _.defaults(options, defaults);
28     }
29
30     var host = (options.host || '*') + ':' + options.port;
31
32     if (servers[host]) {
33       this.server = servers[host];
34     } else {
35       this.server = tinylr(options);
36       this.server.server.removeAllListeners('error');
37       this.server.server.on('error', function(err) {
38         if (err.code === 'EADDRINUSE') {
39           grunt.fatal('Port ' + options.port + ' is already in use by another process.');
40         } else {
41           grunt.fatal(err);
42         }
43         process.exit(1);
44       });
45       this.server.listen(options.port, options.host, function(err) {
46         if (err) {
47           return grunt.fatal(err);
48         }
49         grunt.log.verbose.writeln('Live reload server started on ' + host);
50       });
51       servers[host] = this.server;
52     }
53   }
54
55   LR.prototype.trigger = function(files) {
56     grunt.log.verbose.writeln('Live reloading ' + grunt.log.wordlist(files) + '...');
57     this.server.changed({body: {files: files}});
58   };
59
60   return function(options) {
61     return new LR(options);
62   };
63 };