diff --git a/lib/coffee-script/cake.js b/lib/coffee-script/cake.js index f781bbe2..4b19da6f 100644 --- a/lib/coffee-script/cake.js +++ b/lib/coffee-script/cake.js @@ -1,5 +1,5 @@ (function() { - var CoffeeScript, fs, helpers, missingTask, oparse, options, optparse, path, printTasks, switches, tasks; + var CoffeeScript, findCakefilePathSync, fs, helpers, missingTask, oparse, options, optparse, path, printTasks, switches, tasks; fs = require('fs'); path = require('path'); helpers = require('./helpers'); @@ -30,24 +30,22 @@ } }); exports.run = function() { - return path.exists('Cakefile', function(exists) { - var arg, args, _i, _len, _ref, _results; - if (!exists) throw new Error("Cakefile not found in " + (process.cwd())); - args = process.argv.slice(2); - CoffeeScript.run(fs.readFileSync('Cakefile').toString(), { - filename: 'Cakefile' - }); - oparse = new optparse.OptionParser(switches); - if (!args.length) return printTasks(); - options = oparse.parse(args); - _ref = options.arguments; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - arg = _ref[_i]; - _results.push(invoke(arg)); - } - return _results; + var arg, args, _i, _len, _ref, _results; + process.chdir(findCakefilePathSync(fs.realpathSync('.'))); + args = process.argv.slice(2); + CoffeeScript.run(fs.readFileSync('Cakefile').toString(), { + filename: 'Cakefile' }); + oparse = new optparse.OptionParser(switches); + if (!args.length) return printTasks(); + options = oparse.parse(args); + _ref = options.arguments; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + arg = _ref[_i]; + _results.push(invoke(arg)); + } + return _results; }; printTasks = function() { var desc, name, spaces, task; @@ -65,4 +63,11 @@ console.log("No such task: \"" + task + "\""); return process.exit(1); }; + findCakefilePathSync = function(curPath) { + var parent; + if (path.existsSync(path.join(curPath, 'Cakefile'))) return curPath; + parent = path.normalize(path.join(curPath, '..')); + if (parent !== curPath) return findCakefilePathSync(parent); + throw new Error("Cakefile not found in " + (process.cwd())); + }; }).call(this); diff --git a/src/cake.coffee b/src/cake.coffee index 34c1b168..9fc31b86 100644 --- a/src/cake.coffee +++ b/src/cake.coffee @@ -44,7 +44,7 @@ helpers.extend global, # asynchrony may cause tasks to execute in a different order than you'd expect. # If no tasks are passed, print the help screen. exports.run = -> - process.chdir findCakefilePathSync(fs.realpathSync '.') + process.chdir cakefileDirectory fs.realpathSync '.' args = process.argv.slice 2 CoffeeScript.run fs.readFileSync('Cakefile').toString(), filename: 'Cakefile' oparse = new optparse.OptionParser switches @@ -67,10 +67,10 @@ missingTask = (task) -> console.log "No such task: \"#{task}\"" process.exit 1 -# Search in current and parent directories for Cakefile -findCakefilePathSync = (curPath) -> - return curPath if path.existsSync path.join(curPath, 'Cakefile') - parent = path.normalize path.join(curPath, '..') - return findCakefilePathSync parent unless parent == curPath - # None found - throw new Error("Cakefile not found in #{process.cwd()}") +# When `cake` is invoked, search in the current and all parent directories +# to find the relevant Cakefile. +cakefileDirectory = (dir) -> + return dir if path.existsSync path.join dir, 'Cakefile' + parent = path.normalize path.join dir, '..' + return cakefileDirectory parent unless parent is dir + throw new Error "Cakefile not found in #{process.cwd()}"