-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtasks.js
169 lines (153 loc) · 3.46 KB
/
tasks.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Buildify tasks
*
* @param {{run: String}} [options]
* @constructor Tasks
*/
function Tasks (options) {
var me = this;
// properties
this.tasks = {};
this.options = {
run: 'auto' // can be 'auto' or 'manual'
};
this.executed = false;
// set options
if (options) {
this.setOptions(options);
}
// execute a function on next process tick which will execute the tasks
// (when configured to be executed automatically)
if (process && process.nextTick) {
process.nextTick(function () {
if (me.options.run == 'auto') {
me.run();
}
});
}
}
/**
* Add a task
* @param {{
* name: String,
* (depends: String | String[]),
* (desc: String),
* (task: Function)
* }} options
*/
Tasks.prototype.task = function task (options) {
// test requirements
if (!options) {
throw new SyntaxError('Task configuration expected');
}
if (!options.name) {
throw new SyntaxError('Parameter "name" missing');
}
else if (this.tasks[options.name]) {
throw new SyntaxError('Task already existing');
}
// create task
var task = {
name: options.name,
desc: options.desc,
task: options.task,
executed: false
};
// write dependencies
if (options.depends instanceof Array) {
task.depends = options.depends;
}
else if (options.depends) {
task.depends = [
options.depends
];
}
else {
task.depends = [];
}
// append to list with tasks
this.tasks[task.name] = task;
};
/**
* Configure tasks
* @param {{run: String}} options
*/
Tasks.prototype.setOptions = function setOptions (options) {
for (var prop in options) {
if (options.hasOwnProperty(prop)) {
this.options[prop] = options[prop];
}
}
};
/**
* Run the provided task. Depending tasks will be executed first
* @param {{
* name: String,
* (depends: String | String[]),
* (desc: String),
* (task: Function)
* }} task
* @private
*/
Tasks.prototype._runTask = function _runTask (task) {
var me = this;
if (task.executed) {
throw new Error('Cannot run task: task is already executed');
}
task.executed = true;
// execute dependencies
if (task.depends) {
// TODO: give an error on circular dependencies
task.depends.forEach(function (name) {
var dep = me.tasks[name];
if (!dep.executed) {
me._runTask(dep);
}
});
}
// execute the task itself
if (task.task) {
task.task();
}
};
/**
* Run the tasks.
* If there are command line arguments with task names only these tasks
* will be executed. Else, all tasks will be executed.
*/
Tasks.prototype.run = function run () {
if (this.executed) {
throw new Error('Tasks are already executed');
}
this.executed = true;
// build list with tasks to execute
var tasks = this.tasks;
var names;
if (process.argv.length > 2) {
// specific task names from command line arguments
names = process.argv.slice(2);
}
else {
// execute all tasks
names = Object.keys(tasks);
}
// run the selected tasks
var me = this;
names.forEach(function (name) {
var task = tasks[name];
if (!task) {
throw new Error('Task "' + name + '" not found');
}
if (!task.executed) {
me._runTask(task);
}
});
};
/**
* Factory method which creates a new Tasks
*
* @param {Object} [options] Constructor options
*/
module.exports = function(options) {
return new Tasks(options);
};