forked from Real-Serious-Games/task-mule
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
287 lines (224 loc) · 7.01 KB
/
index.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict';
var argv = require('yargs').argv;
var conf = require('confucious');
var path = require('path');
var fs = require('fs-extra');
var globby = require('globby');
var chalk = require('chalk');
var validate = require('./validate');
var S = require('string');
var AsciiTable = require('ascii-table');
var assert = require('chai').assert;
var loadTasks = require('./task-loader')
var JobRunner = require('./job-runner');
var consoleLog = require('./log')(argv.verbose, argv.nocolors);
//
// task-mule init
//
var commandInit = function (config) {
if (fs.existsSync(config.buildFilePath)) {
consoleLog.error("Can't overwrite existing 'mule.js'.");
process.exit(1);
}
// Auto create mule.js.
var defaultBuildJs = path.join(__dirname, 'template', 'mule.js');
fs.copySync(defaultBuildJs, config.buildFilePath);
consoleLog.info("Created new 'mule.js' at " + config.buildFilePath);
process.exit(0);
};
//
// task-mule create-task <task-name>
//
var commandCreateTask = function (config) {
var newTaskName = argv._[1];
if (!newTaskName) {
consoleLog.error("Task name not specified.");
process.exit(1);
}
if (!S(newTaskName.toLowerCase()).endsWith(".js")) {
if (newTaskName[newTaskName.length-1] === '.') {
// Trim final period.
newTaskName = newTaskName.substring(0, newTaskName.length-1);
}
// Auto add extension.
newTaskName += ".js";
}
var newTaskFilePath = path.join(config.tasksDir, newTaskName);
if (fs.existsSync(newTaskFilePath)) {
consoleLog.error("Can't create task, file already exists: " + newTaskFilePath);
process.exit(1);
}
var defaultTaskFile = path.join(__dirname, 'template', 'task.js');
fs.copySync(defaultTaskFile, newTaskFilePath);
consoleLog.info("Created new task file at " + newTaskFilePath);
};
//
// Init config prior to running or listing tasks.
//
var initConfig = function (config, buildConfig, log) {
assert.isObject(config);
assert.isObject(buildConfig);
assert.isFunction(log.error);
assert.isFunction(log.info);
assert.isFunction(log.warn);
assert.isFunction(log.verbose);
var defaultConfigFilePath = path.join(config.workingDirectory, 'config.json');
if (fs.existsSync(defaultConfigFilePath)) {
log.verbose("Loading config from file: " + defaultConfigFilePath);
conf.pushJsonFile(defaultConfigFilePath);
}
conf.pushEnv();
if (config.defaultConfig) {
conf.push(config.defaultConfig)
}
if (buildConfig.initConfig) {
buildConfig.initConfig();
}
conf.pushArgv();
buildConfig.init();
return buildConfig;
};
//
// task-mule schedule
//
var commandSchedule = function (config, buildConfig, log) {
assert.isObject(config);
assert.isObject(buildConfig);
assert.isFunction(log.error);
assert.isFunction(log.info);
assert.isFunction(log.warn);
if (!fs.existsSync('schedule.json')) {
log.error('Expected schedule.json to specify the schedule of tasks.');
process.exit(1);
}
initConfig(config, buildConfig, log);
var taskRunner = loadTasks(config, log, validate, conf);
var jobRunner = new JobRunner(taskRunner, log, buildConfig);
var schedule = JSON.parse(fs.readFileSync('schedule.json', 'utf8'));
var TaskScheduler = require('./task-scheduler');
var taskScheduler = new TaskScheduler(jobRunner, config, log);
taskScheduler.start(schedule, buildConfig);
};
//
// task-mule <task-name>
//
var commandRunTask = function (config, buildConfig, log, requestedTaskName) {
assert.isObject(config);
assert.isObject(buildConfig);
assert.isFunction(log.error);
assert.isFunction(log.info);
assert.isFunction(log.warn);
initConfig(config, buildConfig, log);
var taskRunner = loadTasks(config, log, validate, conf);
var jobRunner = new JobRunner(taskRunner, log, buildConfig);
if (requestedTaskName) {
return jobRunner.runTask(requestedTaskName, conf, {})
.catch(function (err) {
log.error('Build failed.');
if (err.message) {
log.warn(err.message);
}
if (err.stack) {
log.warn(err.stack);
}
else {
log.warn('no stack');
}
if (!config.noExit) {
process.exit(1);
}
runDoneCallback(buildConfig);
throw err;
})
.then(function () {
runDoneCallback(buildConfig);
});
}
else if (argv.tasks) {
return taskRunner.resolveAllDependencies(conf)
.then(function () {
taskRunner.listTasks();
process.exit(1);
});
}
else {
throw new Error("Unexpected usage of task-mule.");
}
};
//
// Run config done callback.
//
var runDoneCallback = function (buildConfig) {
if (buildConfig.done) {
assert.isFunction(buildConfig.done);
buildConfig.done();
}
}
//
// Display usage and help.
//
var displayHelp = function (buildConfig, log) {
log.info("Usage: task-mule <task-name> [options]\n");
var optionsTable = new AsciiTable('Options');
optionsTable
.setHeading('Options', 'Description');
buildConfig.options.forEach(function (option) {
optionsTable.addRow(option[0], option[1]);
});
console.log(chalk.bold.green(optionsTable.toString()));
var examplesTable = new AsciiTable('Examples');
examplesTable.setHeading('What?', 'Command Line');
buildConfig.examples.forEach(function (example) {
examplesTable.addRow(example[0], example[1]);
});
console.log(chalk.bold.green(examplesTable.toString()));
};
module.exports = function (config) {
config = config || {};
if (!config.workingDirectory) {
config.workingDirectory = process.cwd();
}
config.buildFilePath = path.join(config.workingDirectory, "mule.js");
if (!config.tasksDir) {
config.tasksDir = path.join(config.workingDirectory, 'tasks');
}
var requestedTaskName = config.requestedTaskName || argv._[0];
if (requestedTaskName === 'init') {
commandInit(config);
process.exit(0);
}
else if (requestedTaskName === 'create-task') {
commandCreateTask(config);
process.exit(0);
}
else {
if (!fs.existsSync(config.buildFilePath)) {
consoleLog.error("'mule.js' not found, please run task-mule in a directory that has this file.");
consoleLog.info("Run 'task-mule init' to create a default 'mule.js'.")
process.exit(1);
}
if (!fs.existsSync(config.tasksDir)) {
consoleLog.error("'tasks' directory doesn't exist.");
consoleLog.info("Run 'task-mule create-task <task-name> to create your first task.");
process.exit(1);
}
var buildConfig = require(config.buildFilePath)(conf, validate);
var log = consoleLog;
if (buildConfig.initLog) {
log = buildConfig.initLog();
}
global.runCmd = require('./run-cmd')(log);
if (!requestedTaskName && !argv.tasks) {
console.log(chalk.bold.red("Expected parameter: task-mule <task-name>"));
console.log(chalk.bold.yellow("To list tasks: task-mule --tasks"));
console.log();
displayHelp(buildConfig, log);
process.exit(1);
}
if (requestedTaskName === 'schedule') {
commandSchedule(config, buildConfig, log);
return;
}
return commandRunTask(config, buildConfig, log, requestedTaskName);
}
};