taskrun.js
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* grunt-contrib-watch
* http://gruntjs.com/
*
* Copyright (c) 2013 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
var path = require('path');
var EE = require('events').EventEmitter;
var util = require('util');
module.exports = function(grunt) {
var livereload = require('./livereload')(grunt);
// Create a TaskRun on a target
function TaskRun(target) {
this.name = target.name || 0;
this.files = target.files || [];
this.tasks = target.tasks || [];
this.options = target.options;
this.startedAt = false;
this.spawned = null;
this.changedFiles = Object.create(null);
if (typeof this.tasks === 'string') {
this.tasks = [this.tasks];
}
}
// Run it
TaskRun.prototype.run = function(done) {
var self = this;
// Dont run if already running
if (self.startedAt !== false) { return; }
// Start this task run
self.startedAt = Date.now();
// If no tasks just call done to trigger potential livereload
if (self.tasks.length < 1) { return done(); }
if (self.options.spawn === false || self.options.nospawn === true) {
grunt.task.run(self.tasks);
done();
} else {
self.spawned = grunt.util.spawn({
// Spawn with the grunt bin
grunt: true,
// Run from current working dir and inherit stdio from process
opts: {
cwd: self.options.cwd,
stdio: 'inherit',
},
// Run grunt this process uses, append the task to be run and any cli options
args: self.tasks.concat(self.options.cliArgs || []),
}, function(err, res, code) {
// Spawn is done
self.spawned = null;
done();
});
}
};
// When the task run has completed
TaskRun.prototype.complete = function() {
var time = Date.now() - this.startedAt;
this.startedAt = false;
if (this.spawned) {
this.spawned.kill('SIGINT');
this.spawned = null;
}
// Trigger livereload if set
if (this.livereload) {
this.livereload.trigger(Object.keys(this.changedFiles));
}
return time;
};
return TaskRun;
};