1 | /* |
---|
2 | * grunt-contrib-watch |
---|
3 | * http://gruntjs.com/ |
---|
4 | * |
---|
5 | * Copyright (c) 2014 "Cowboy" Ben Alman, contributors |
---|
6 | * Licensed under the MIT license. |
---|
7 | */ |
---|
8 | |
---|
9 | 'use strict'; |
---|
10 | |
---|
11 | var path = require('path'); |
---|
12 | var EE = require('events').EventEmitter; |
---|
13 | var util = require('util'); |
---|
14 | |
---|
15 | module.exports = function(grunt) { |
---|
16 | |
---|
17 | var livereload = require('./livereload')(grunt); |
---|
18 | |
---|
19 | // Create a TaskRun on a target |
---|
20 | function TaskRun(target) { |
---|
21 | this.name = target.name || 0; |
---|
22 | this.files = target.files || []; |
---|
23 | this._getConfig = target._getConfig; |
---|
24 | this.options = target.options; |
---|
25 | this.startedAt = false; |
---|
26 | this.spawned = null; |
---|
27 | this.changedFiles = Object.create(null); |
---|
28 | this.spawnTaskFailure = false; |
---|
29 | this.livereloadOnError = true; |
---|
30 | if (typeof this.options.livereloadOnError !== 'undefined') { |
---|
31 | this.livereloadOnError = this.options.livereloadOnError; |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | var getErrorCount = function(){ |
---|
36 | if (typeof grunt.fail.forever_warncount !== 'undefined') { |
---|
37 | return grunt.fail.forever_warncount + grunt.fail.forever_errorcount; |
---|
38 | } else { |
---|
39 | return grunt.fail.warncount + grunt.fail.errorcount; |
---|
40 | } |
---|
41 | }; |
---|
42 | |
---|
43 | // Run it |
---|
44 | TaskRun.prototype.run = function(done) { |
---|
45 | var self = this; |
---|
46 | |
---|
47 | // Dont run if already running |
---|
48 | if (self.startedAt !== false) { return; } |
---|
49 | |
---|
50 | // Start this task run |
---|
51 | self.startedAt = Date.now(); |
---|
52 | |
---|
53 | // reset before each run |
---|
54 | self.spawnTaskFailure = false; |
---|
55 | self.errorsAndWarningsCount = getErrorCount(); |
---|
56 | |
---|
57 | // pull the tasks here in case they were changed by a watch event listener |
---|
58 | self.tasks = self._getConfig('tasks') || []; |
---|
59 | if (typeof self.tasks === 'string') { |
---|
60 | self.tasks = [self.tasks]; |
---|
61 | } |
---|
62 | |
---|
63 | // If no tasks just call done to trigger potential livereload |
---|
64 | if (self.tasks.length < 1) { return done(); } |
---|
65 | |
---|
66 | if (self.options.spawn === false || self.options.nospawn === true) { |
---|
67 | grunt.task.run(self.tasks); |
---|
68 | done(); |
---|
69 | } else { |
---|
70 | self.spawned = grunt.util.spawn({ |
---|
71 | // Spawn with the grunt bin |
---|
72 | grunt: true, |
---|
73 | // Run from current working dir and inherit stdio from process |
---|
74 | opts: { |
---|
75 | cwd: self.options.cwd.spawn, |
---|
76 | stdio: 'inherit', |
---|
77 | }, |
---|
78 | // Run grunt this process uses, append the task to be run and any cli options |
---|
79 | args: self.tasks.concat(self.options.cliArgs || []), |
---|
80 | }, function(err, res, code) { |
---|
81 | self.spawnTaskFailure = (code !== 0); |
---|
82 | if (self.options.interrupt !== true || (code !== 130 && code !== 1)) { |
---|
83 | // Spawn is done |
---|
84 | self.spawned = null; |
---|
85 | done(); |
---|
86 | } |
---|
87 | }); |
---|
88 | } |
---|
89 | }; |
---|
90 | |
---|
91 | // When the task run has completed |
---|
92 | TaskRun.prototype.complete = function() { |
---|
93 | var time = Date.now() - this.startedAt; |
---|
94 | this.startedAt = false; |
---|
95 | if (this.spawned) { |
---|
96 | this.spawned.kill('SIGINT'); |
---|
97 | this.spawned = null; |
---|
98 | } |
---|
99 | |
---|
100 | var taskFailed = this.spawnTaskFailure || (getErrorCount() > this.errorsAndWarningsCount); |
---|
101 | this.errorsAndWarningsCount = getErrorCount(); |
---|
102 | |
---|
103 | // Trigger livereload if necessary |
---|
104 | if (this.livereload && (this.livereloadOnError || !taskFailed)) { |
---|
105 | this.livereload.trigger(Object.keys(this.changedFiles)); |
---|
106 | this.changedFiles = Object.create(null); |
---|
107 | } |
---|
108 | return time; |
---|
109 | }; |
---|
110 | |
---|
111 | return TaskRun; |
---|
112 | }; |
---|