forked from Real-Serious-Games/task-mule
-
Notifications
You must be signed in to change notification settings - Fork 1
/
task-runner.js
127 lines (96 loc) · 2.81 KB
/
task-runner.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
'use strict';
var assert = require('chai').assert;
var AsciiTable = require('ascii-table');
var E = require('linq');
//
// Responsible for finding and running tasks.
//
var TaskRunner = function (log) {
var self = this;
assert.isFunction(log.info);
//
// All tasks.
//
var tasks = [];
//
// Map of tasks for look up by name.
//
var taskMap = {};
//
// Add a task.
//
self.addTask = function (task) {
assert.isObject(task);
tasks.push(task);
taskMap[task.name()] = task;
};
//
// Get a task by name, throws exception if task doesn't exist.
//
self.getTask = function (taskName) {
assert.isString(taskName);
var task = taskMap[taskName];
if (!task) {
throw new Error("Task not found: " + taskName);
}
return task;
};
//
// Run a named task with a particular config.
//
self.runTask = function (taskName, config, configOverride) {
assert.isString(taskName);
assert.isObject(config);
assert.isObject(configOverride);
var requestedTask = taskMap[taskName];
if (!requestedTask) {
throw new Error("Failed to find task: " + taskName);
}
return self.resolveDependencies(taskName, config)
.then(function () {
var tasksValidated = {}; // Tasks that have been validated.
return requestedTask.validate(configOverride, config, tasksValidated);
})
.then(function () {
var taskInvoked = {}; // Tasks that have been invoked.
return requestedTask.invoke(configOverride, config, taskInvoked);
})
;
};
//
// List registered tasks.
//
self.listTasks = function () {
var treeOutput = "#tasks\n";
tasks.forEach(function (task) {
treeOutput += task.genTree(2);
});
var asciitree = require('ascii-tree');
console.log(asciitree.generate(treeOutput));
};
//
// Resolve dependencies for all tasks.
//
self.resolveAllDependencies = function (config) {
assert.isObject(config);
return E.from(tasks)
.aggregate(Promise.resolve(), function (prevPromise, task) {
return prevPromise.then(function () {
return task.resolveDependencies(config);
});
});
};
//
// Resolve dependencies for a particular task.
//
self.resolveDependencies = function (taskName, config) {
assert.isString(taskName);
assert.isObject(config);
var task = taskMap[taskName];
if (!task) {
throw new Error("Failed to find task: " + taskName);
}
return task.resolveDependencies(config);
};
};
module.exports = TaskRunner;