1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/cake.js

75 lines
2.2 KiB
JavaScript
Raw Normal View History

(function() {
var CoffeeScript, fs, helpers, missingTask, oparse, options, optparse, path, printTasks, switches, tasks;
fs = require('fs');
path = require('path');
2010-09-24 20:29:44 -04:00
helpers = require('./helpers');
optparse = require('./optparse');
CoffeeScript = require('./coffee-script');
tasks = {};
options = {};
switches = [];
oparse = null;
helpers.extend(global, {
task: function(name, description, action) {
var _ref;
2010-10-07 07:05:22 -04:00
if (!action) {
_ref = [description, action], action = _ref[0], description = _ref[1];
}
return tasks[name] = {
name: name,
description: description,
action: action
};
},
option: function(letter, flag, description) {
return switches.push([letter, flag, description]);
},
invoke: function(name) {
2010-10-07 07:05:22 -04:00
if (!tasks[name]) {
missingTask(name);
}
return tasks[name].action(options);
}
});
exports.run = function() {
return path.exists('Cakefile', function(exists) {
var _i, _len, _ref, _result, arg, args;
2010-10-07 07:05:22 -04:00
if (!exists) {
throw new Error("Cakefile not found in " + (process.cwd()));
}
2010-10-12 16:47:45 -04:00
args = process.argv.slice(2);
CoffeeScript.run(fs.readFileSync('Cakefile').toString(), {
fileName: 'Cakefile'
});
oparse = new optparse.OptionParser(switches);
2010-10-07 07:05:22 -04:00
if (!args.length) {
return printTasks();
}
options = oparse.parse(args);
_ref = options.arguments;
_result = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
arg = _ref[_i];
_result.push(invoke(arg));
}
return _result;
});
};
printTasks = function() {
var _ref, desc, name, spaces, task;
console.log('');
for (name in _ref = tasks) {
task = _ref[name];
spaces = 20 - name.length;
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
desc = task.description ? "# " + task.description : '';
console.log("cake " + name + spaces + " " + desc);
}
return switches.length ? console.log(oparse.help()) : void 0;
2010-02-25 01:17:43 -05:00
};
missingTask = function(task) {
console.log("No such task: \"" + task + "\"");
2010-02-25 01:17:43 -05:00
return process.exit(1);
};
}).call(this);