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.

76 lines
1.9 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 path = require('path');
  10. var EE = require('events').EventEmitter;
  11. var util = require('util');
  12. module.exports = function(grunt) {
  13. var livereload = require('./livereload')(grunt);
  14. // Create a TaskRun on a target
  15. function TaskRun(target, defaults) {
  16. this.name = target.name || 0;
  17. this.files = target.files || [];
  18. this.tasks = target.tasks || [];
  19. this.options = target.options;
  20. this.startedAt = false;
  21. this.spawned = null;
  22. this.changedFiles = Object.create(null);
  23. }
  24. // Run it
  25. TaskRun.prototype.run = function(done) {
  26. var self = this;
  27. // Dont run if already running
  28. if (self.startedAt !== false) { return; }
  29. self.startedAt = Date.now();
  30. if (self.options.nospawn === true) {
  31. grunt.task.run(self.tasks);
  32. done();
  33. } else {
  34. self.spawned = grunt.util.spawn({
  35. // Spawn with the grunt bin
  36. grunt: true,
  37. // Run from current working dir and inherit stdio from process
  38. opts: {
  39. cwd: self.options.cwd,
  40. stdio: 'inherit',
  41. },
  42. // Run grunt this process uses, append the task to be run and any cli options
  43. args: self.tasks.concat(self.options.cliArgs || [])
  44. }, function(err, res, code) {
  45. // Spawn is done
  46. self.spawned = null;
  47. done();
  48. });
  49. }
  50. };
  51. // When the task run has completed
  52. TaskRun.prototype.complete = function() {
  53. var time = Date.now() - this.startedAt;
  54. this.startedAt = false;
  55. if (this.spawned) {
  56. this.spawned.kill('SIGINT');
  57. this.spawned = null;
  58. }
  59. // Trigger livereload if set
  60. if (this.livereload) {
  61. this.livereload.trigger(Object.keys(this.changedFiles));
  62. }
  63. return time;
  64. };
  65. return TaskRun;
  66. };