1 |
|
2 | (function() {
|
3 | var CoffeeScript, cakefileDirectory, existsSync, fatalError, fs, helpers, missingTask, oparse, options, optparse, path, printTasks, switches, tasks;
|
4 |
|
5 | fs = require('fs');
|
6 |
|
7 | path = require('path');
|
8 |
|
9 | helpers = require('./helpers');
|
10 |
|
11 | optparse = require('./optparse');
|
12 |
|
13 | CoffeeScript = require('./coffee-script');
|
14 |
|
15 | existsSync = fs.existsSync || path.existsSync;
|
16 |
|
17 | tasks = {};
|
18 |
|
19 | options = {};
|
20 |
|
21 | switches = [];
|
22 |
|
23 | oparse = null;
|
24 |
|
25 | helpers.extend(global, {
|
26 | task: function(name, description, action) {
|
27 | var _ref;
|
28 | if (!action) {
|
29 | _ref = [description, action], action = _ref[0], description = _ref[1];
|
30 | }
|
31 | return tasks[name] = {
|
32 | name: name,
|
33 | description: description,
|
34 | action: action
|
35 | };
|
36 | },
|
37 | option: function(letter, flag, description) {
|
38 | return switches.push([letter, flag, description]);
|
39 | },
|
40 | invoke: function(name) {
|
41 | if (!tasks[name]) {
|
42 | missingTask(name);
|
43 | }
|
44 | return tasks[name].action(options);
|
45 | }
|
46 | });
|
47 |
|
48 | exports.run = function() {
|
49 | var arg, args, _i, _len, _ref, _results;
|
50 | global.__originalDirname = fs.realpathSync('.');
|
51 | process.chdir(cakefileDirectory(__originalDirname));
|
52 | args = process.argv.slice(2);
|
53 | CoffeeScript.run(fs.readFileSync('Cakefile').toString(), {
|
54 | filename: 'Cakefile'
|
55 | });
|
56 | oparse = new optparse.OptionParser(switches);
|
57 | if (!args.length) {
|
58 | return printTasks();
|
59 | }
|
60 | try {
|
61 | options = oparse.parse(args);
|
62 | } catch (e) {
|
63 | return fatalError("" + e);
|
64 | }
|
65 | _ref = options["arguments"];
|
66 | _results = [];
|
67 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
68 | arg = _ref[_i];
|
69 | _results.push(invoke(arg));
|
70 | }
|
71 | return _results;
|
72 | };
|
73 |
|
74 | printTasks = function() {
|
75 | var cakefilePath, desc, name, relative, spaces, task;
|
76 | relative = path.relative || path.resolve;
|
77 | cakefilePath = path.join(relative(__originalDirname, process.cwd()), 'Cakefile');
|
78 | console.log("" + cakefilePath + " defines the following tasks:\n");
|
79 | for (name in tasks) {
|
80 | task = tasks[name];
|
81 | spaces = 20 - name.length;
|
82 | spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
|
83 | desc = task.description ? "# " + task.description : '';
|
84 | console.log("cake " + name + spaces + " " + desc);
|
85 | }
|
86 | if (switches.length) {
|
87 | return console.log(oparse.help());
|
88 | }
|
89 | };
|
90 |
|
91 | fatalError = function(message) {
|
92 | console.error(message + '\n');
|
93 | console.log('To see a list of all tasks/options, run "cake"');
|
94 | return process.exit(1);
|
95 | };
|
96 |
|
97 | missingTask = function(task) {
|
98 | return fatalError("No such task: " + task);
|
99 | };
|
100 |
|
101 | cakefileDirectory = function(dir) {
|
102 | var parent;
|
103 | if (existsSync(path.join(dir, 'Cakefile'))) {
|
104 | return dir;
|
105 | }
|
106 | parent = path.normalize(path.join(dir, '..'));
|
107 | if (parent !== dir) {
|
108 | return cakefileDirectory(parent);
|
109 | }
|
110 | throw new Error("Cakefile not found in " + (process.cwd()));
|
111 | };
|
112 |
|
113 | }).call(this);
|