You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.2 KiB

  1. /*
  2. * grunt-contrib-watch
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2013 "Cowboy" Ben Alman, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. var tinylr = require('tiny-lr');
  10. // Holds the servers out of scope in case watch is reloaded
  11. var servers = Object.create(null);
  12. module.exports = function(grunt) {
  13. var defaults = { port: 35729 };
  14. function LR(options) {
  15. if (options === true) {
  16. options = defaults;
  17. } else if (typeof options === 'number') {
  18. options = {port: options};
  19. } else {
  20. options = grunt.util._.defaults(options, defaults);
  21. }
  22. if (servers[options.port]) {
  23. this.server = servers[options.port];
  24. } else {
  25. this.server = tinylr();
  26. this.server.listen(options.port, function(err) {
  27. if (err) { return grunt.fatal(err); }
  28. grunt.log.verbose.writeln('Live reload server started on port: ' + options.port);
  29. });
  30. servers[options.port] = this.server;
  31. }
  32. }
  33. LR.prototype.trigger = function(files) {
  34. grunt.log.verbose.writeln('Live reloading ' + grunt.log.wordlist(files) + '...');
  35. this.server.changed({body:{files:files}});
  36. };
  37. return function(options) {
  38. return new LR(options);
  39. };
  40. };