-
Notifications
You must be signed in to change notification settings - Fork 11
/
rockout.js
457 lines (415 loc) · 11.4 KB
/
rockout.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
var roco;
var cp = require('child_process');
var coffee = require('coffee-script');
var fs = require('fs');
var vm = require('vm');
var path = require('path');
var util = require('util');
var about = {};
var beforeTask = {};
var afterTask = {};
var existsSync = fs.existsSync || path.existsSync;
/**
* Print arguments to stdout using util.puts
*/
function log() {
roco.log.apply(this, [].slice.call(arguments));
};
function print() {
roco.print.apply(this, [].slice.call(arguments));
};
/**
* Initialize module. Creates module-wide global variable called `roco`
*/
exports.init = function (env) {
return roco = new Roco(env);
};
/**
* Perform some command.
*
* @param {Array} what
* [0] {String} env - environment (optional)
* [1] {String} command
*/
exports.perform = function (cmd) {
var env = roco.env;
log('Running in', $(env).bold, 'mode');
if (roco[cmd]) {
roco[cmd]();
} else if (roco[cmd + ':default']) {
roco[cmd + ':default']();
} else {
roco.abort('Unknown command ' + cmd);
}
};
exports.list = function listTasks(how, noDescriptions) {
Object.keys(about).forEach(function (ns) {
console.log('\n' + ns + '\n');
about[ns].forEach(function (cmd) {
if (noDescriptions || !cmd.description) {
console.log(' roco', ns + ':' + cmd.name);
} else {
console.log(' roco', ns + ':' + cmd.name, '-', cmd.description);
}
});
});
};
/**
* Roco API object, this is context-object for running rocofiles
*
* Every rocofile changes context of this object.
*/
function Roco(env) {
roco = this;
roco.env = env;
roco.roco = this;
this.init();
}
/**
* Initialize roco object with some settings:
*
* - ./package.json
* - /etc/roco.coffee
* - ~/.roco.coffee
* - ./Roco.coffee
* - ./config/Roco.coffee
*
* Package descriptor `./package.json` can provide information about:
*
* - application name (`application` var set up from package.name)
* - git url (`repository` set from package.repository.url)
*/
Roco.prototype.init = function () {
var cwd = process.cwd();
var packageFile = path.resolve(cwd, 'package.json');
var configFiles = [
'/etc/roco.coffee',
path.resolve(process.env.HOME, '.roco.coffee'),
path.resolve(cwd, 'Roco.coffee'),
path.resolve(cwd, 'config/Roco.coffee')
];
var cockOutDir = path.resolve(__dirname, '../cockout');
this.require = require;
this.console = console;
this.process = process;
if (process.env.HOSTS) {
roco.hosts = process.env.HOSTS.split(',');
}
if (existsSync(packageFile)) {
var package = require(packageFile);
this.application = package.name;
if (package.repository) {
roco.scm = package.repository.type;
roco.repository = package.repository.url;
}
}
if (process.env.APP) {
roco.application = process.env.APP;
}
fs.readdirSync(cockOutDir).forEach(function (file) {
if (file.match(/\.coffee$/)) {
roco.load(path.resolve(cockOutDir, file));
}
});
configFiles.forEach(function (configFile) {
if (existsSync(path.resolve(configFile))) {
roco.load(configFile);
}
});
};
Roco.prototype.log = function () {
util.puts([].join.call(arguments, ' '));
};
Roco.prototype.print = function () {
util.print([].join.call(arguments, ' '));
};
/**
* Run command on all remote hosts listed in `hosts` var.
*
* Each host can be 'hostname' or 'hostname:portnumber'. Example:
*
* HOSTS jsdoc.info:222,node-js.ru:2222 roco i:free
*/
Roco.prototype.run = function (cmd, callback) {
if (typeof roco.hosts === 'string') {
roco.hosts = [roco.hosts];
}
log('Executing ' + $(cmd).yellow + ' on ' + $(roco.hosts.join(', ')).blue);
var wait = 0;
data = [];
if (roco.hosts.length > 1) {
roco.parallelRunning = true;
}
roco.hosts.forEach(function (host) {
wait += 1;
var options = [];
if (host.match(/:\d+$/)) {
var h = host.split(':');
options.push(h[0]);
options.push('-p' + h[1]);
} else {
options.push(host);
}
if (cmd[0] === '@') {
options.ignoreError = true;
cmd = cmd.substr(1);
}
options.push(cmd);
spawnProcess('ssh', options, function (err, out) {
if (!err) {
data.push({
host: host,
out: out
});
}
done(err);
});
});
var error;
function done(err) {
error = error || err;
if (--wait === 0) {
roco.parallelRunning = false;
if (error) {
roco.abort('FAILED TO RUN, return code: ' + error);
} else if (callback) {
callback(data);
}
}
}
};
/**
* Run command locally
*/
Roco.prototype.localRun = function (cmd, callback) {
log('Executing ' + $(cmd).green + ' locally');
spawnProcess('sh', [ '-c', cmd ], function (err, data) {
if (err) {
roco.abort('FAILED TO RUN, return code: ' + err);
} else {
if (callback) callback(data);
}
});
};
/**
* Spawn process with `command` and `options`, call `callback` when process
* finishes. Callback called with (code, output). Code 0 means ok, output contain
* both `stderr` and `stdout`.
*/
function spawnProcess(command, options, callback) {
var child = cp.spawn(command, options), waiting = true;
var prefix = roco.parallelRunning && command === 'ssh' ? '[' + options[0] + '] ' : '';
prefix = $(prefix).grey;
child.stderr.on('data', function (chunk) {
print(addBeauty(chunk));
});
var res = [];
child.stdout.on('data', function (chunk) {
res.push(chunk.toString());
print(addBeauty(chunk));
});
function addBeauty(buf) {
return prefix + buf
.toString()
//.replace(/\s+$/, ' ')
.replace(/\n/g, '\n' + prefix);
}
if (options.ignoreError) {
child.stdout.on('end', function() {
if (waiting) {
callback(null, res.join('\n'));
waiting = false;
}
});
setTimeout(function() {
if (waiting) {
waiting = false;
callback(null, res.join('\n'));
}
}, 5000);
} else {
child.on('exit', function (code) {
if (callback) {
callback(code === 0 ? null : code, res && res.join('\n'));
}
});
}
}
/**
* Define `key` only if it is not defined yet
*
* @param {String} key
* @param {Mixed} def
*/
Roco.prototype.ensure = function (key, def) {
if (roco.hasOwnProperty(key)) return;
roco.set(key, def);
};
/**
* Define `key` on current roco object. When def is function, it called each time
* when roco[key] getter called. This is odd befavior. It should be called only
* once and then return cached value.
*
* TODO: only call `def` once
*
* @param {String} key
* @param {Mixed} def
*/
Roco.prototype.set = function (key, def) {
if (typeof def === 'function') {
roco.__defineGetter__(key, def);
} else {
roco.__defineGetter__(key, function () {
return def;
});
}
};
/**
* Load rocofile `file`
*
* @param {String} file - path to rocofile
*/
Roco.prototype.load = function (file) {
if (!file) throw new Error('File not specified');
if (!existsSync(file)) return;
// console.log('loading', file);
var code = coffee.compile(fs.readFileSync(file).toString());
var dir = path.dirname(file);
var fn = new Function('roco', '__dirname', 'with(roco){(function(){ ' + code + ' })();}');
fn(this, dir);
};
/**
* Exit with status 1 and error message `msg`
*
* @param {String} msg
*/
Roco.prototype.abort = function (msg) {
log($(msg).red);
process.exit(1);
};
/**
* Define namespace. No namespace nesting!
*/
Roco.prototype.namespace = function (name, callback) {
if (roco.ns) {
throw new Error('Nested namespaces is not supported at the mo');
}
roco.ns = name;
callback();
roco.ns = '';
};
/**
* Run tasks listed as arguments sequentially
*/
Roco.prototype.sequence = function () {
var args = arguments;
var ns = args.callee.caller.ns;
roco.asyncLoop([].slice.call(args), function (arg, next) {
if (typeof arg === 'function') {
arg.call(roco, next);
} else {
roco[ns ? ns + ':' + arg : arg].call(roco, next);
}
});
};
/**
* Loop asyncronouslythrough `collection` calling `iteration` for each item
* and then call `complete` (when done)
*/
Roco.prototype.asyncLoop = function asyncLoop(collection, iteration, complete) {
var self = this;
var item = collection.shift();
if (item) {
iteration.call(self, item, function next() {
asyncLoop.call(self, collection, iteration, complete);
});
} else if (typeof complete === 'function') {
complete.call(self);
}
};
var description = '';
Roco.prototype.desc = function (text) {
description = text;
};
/**
* Describe task
*
* @param {String} name
* @param {Function} action
*/
Roco.prototype.task = function (name, action) {
var ns = roco.ns;
var fullname = ns + ':' + name;
roco[fullname] = function task(done) {
var displayName = name === 'default' ? ns : fullname;
log('Executing', displayName);
var queue = [];
if (beforeTask[fullname]) {
queue = queue.concat(beforeTask[fullname]);
}
queue.push(function (next) {
var time = Date.now();
action(function () {
if (next) next();
});
});
if (afterTask[fullname]) {
queue = queue.concat(afterTask[fullname]);
}
if (done) queue.push(done);
roco.sequence.apply(roco, queue);
};
action.ns = ns;
action.task = name;
about[ns] = about[ns] || [];
about[ns].push({
name: name,
description: description
});
description = '';
};
Roco.prototype.before = function (name, action) {
beforeTask[name] = beforeTask[name] || [];
beforeTask[name].push(action);
};
Roco.prototype.after = function (name, action) {
afterTask[name] = afterTask[name] || [];
afterTask[name].push(action);
};
/**
* Stylize a string
*/
function stylize(str, style) {
var styles = {
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'cyan' : [96, 39],
'blue' : [34, 39],
'yellow' : [33, 39],
'green' : [32, 39],
'red' : [31, 39],
'grey' : [90, 39],
'green-hi' : [92, 32],
};
return '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm';
};
/**
* Stylize string chainable helper, allows to call stylize like that:
*
* $('some string').bold.yellow
*
*/
function $(str) {
str = new(String)(str);
['bold', 'grey', 'yellow', 'red', 'green', 'cyan', 'blue', 'italic', 'underline'].forEach(function (style) {
Object.defineProperty(str, style, {
get: function () {
return $(stylize(this, style));
}
});
});
return str;
};
stylize.$ = $;