forked from laravel/elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.js
122 lines (100 loc) · 2.29 KB
/
Task.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var _ = require('underscore');
var id = 0, Elixir;
/**
* Create a new Task instance.
*
* @param {string} name
* @param {Function} description
*/
var Task = function(name, description) {
this.id = id++;
this.name = name;
this.watchers = [];
if (description) {
this.describe(description);
}
};
/**
* Fetch the task(s) with the given name.
*
* @param {string} name
* @return {Task}
*/
Task.find = function(name) {
var tasks = _.where(Elixir.tasks, { name: name });
return tasks[Elixir.config.activeTasks[name]];
};
/**
* Describe the task. This is the Gulp definition.
*
* @param {Function} definition
* @return {Task}
*/
Task.prototype.describe = function(definition) {
this.definition = definition;
this.register();
return this;
};
/**
* Set the task to be called, when firing `Gulp`.
*
* @return {Task}
*/
Task.prototype.register = function() {
Elixir.tasks.push(this);
Elixir.config.activeTasks = Elixir.config.activeTasks || {};
Elixir.config.activeTasks[this.name] = 0;
return this;
};
/**
* Set a path regex to watch for changes.
*
* @param {string} regex
* @param {string|null} category
* @return {Task}
*/
Task.prototype.watch = function(regex, category) {
if (regex) {
this.watchers.push(regex);
}
this.category = category || 'default';
return this;
};
/**
* Exclude the given path from the watcher.
*
* @param {string} path
* @return {Task}
*/
Task.prototype.ignore = function(path) {
this.watchers.push(('!./' + path).replace('././', './'));
return this;
};
/**
* Execute the task definition.
*/
Task.prototype.run = function() {
return this.definition();
};
/**
* Log the task input and output.
*
* @param {string|Array} src
* @param {string|null} output
*/
Task.prototype.log = function(src, output) {
var task = this.name.substr(0,1).toUpperCase() + this.name.substr(1);
Elixir.Log
.heading("Fetching " + task + " Source Files...")
.files(src.path ? src.path : src, true);
if (output) {
Elixir.Log
.heading('Saving To...')
.files(output.path ? output.path : output);
}
};
module.exports = function(elixir) {
// Make Elixir available throughout this file.
Elixir = elixir;
return Task;
};