#!/usr/bin/env node

/* Gear task runner. Executes Gearfile using a tasks workflow.
 * See http://gearjs.org/#tasks.tasks
 *
 * Usage:
 * > gear
 */ 
var gear = require('../index'),
    vm = require('vm'),
    path = require('path'),
    fs = require('fs'),
    filename = 'Gearfile',
    existsSync = fs.existsSync || path.existsSync; // 0.6 compat

if (!existsSync(filename)) {
    notify(filename + ' not found');
    return 1;
}

try {
    var tasks = vm.runInNewContext('var tasks = ' + fs.readFileSync(filename) + '; tasks;', {
        require: require,
        process: process,
        console: console,
        gear: gear
    }, filename, true);
} catch(e) {
    notify(e);
    return 1;
}

if (tasks) {
    new gear.Queue({registry: new gear.Registry({module: 'gear-lib'})})
        .tasks(tasks)
        .run(function(err, res) {
            if (err) {
                notify(err);
            }
        });
}

function notify(msg) {
    console.error('gear: ', msg);
}

@@ -12,16 +12,30 @@
 env node
+%0A'use strict';
 %0A%0A/* Gea
@@ -155,22 +155,156 @@
 gear
-%0A */ %0Avar gear
+ %5Boptions%5D%0A *%0A * Available options:%0A * --cwd %3Cdir%3E        change working directory%0A * --Gearfile %3Cpath%3E  execute a specific gearfile%0A */%0Avar Liftoff
  = r
@@ -315,16 +315,15 @@
 re('
-../index
+liftoff
 '),%0A
@@ -495,91 +495,496 @@
 at%0A%0A
-if (!existsSy
+var GearCLI = new Liftoff(%7B%0A    name: 'Gear',%0A    configName: filename,%0A    extensions: %7B%0A        '': null,%0A        '.js': null%0A    %7D%0A%7D);%0A%0AGearCLI.lau
 nc
+h
 (f
-ilename)) %7B%0A    notify(filename + ' not found');%0A    return 1;%0A%7D%0A%0A
+unction(env) %7B%0A    // Loads a local install of gear. Falls back to the global install.%0A    var gear = require(env.modulePath %7C%7C '../index');%0A    if(process.cwd !== env.cwd) %7B%0A        process.chdir(env.cwd);%0A    %7D%0A%0A    if (!env.configPath) %7B%0A        notify(filename + ' not found');%0A        process.exit(1);%0A    %7D%0A%0A    var tasks;%0A    
 try 
@@ -985,27 +985,27 @@
   try %7B%0A    
-var
+   
  tasks = vm.
@@ -1087,16 +1087,20 @@
         
+ 
+   
 require:
@@ -1113,24 +1113,28 @@
 re,%0A        
+   
+ 
 process: pro
@@ -1139,16 +1139,20 @@
 rocess,%0A
+    
         
@@ -1177,16 +1177,20 @@
         
+    
 gear: ge
@@ -1200,19 +1200,29 @@
     
-%7D, filename
+    %7D, env.configPath
 , tr
@@ -1226,16 +1226,20 @@
  true);%0A
+    
 %7D catch(
@@ -1239,24 +1239,28 @@
  catch(e) %7B%0A
+    
     notify(e
@@ -1270,21 +1270,40 @@
     
-return 1;%0A%7D%0A%0A
+    process.exit(1);%0A    %7D%0A%0A    
 if (
@@ -1307,24 +1307,28 @@
 f (tasks) %7B%0A
+    
     new gear
@@ -1395,16 +1395,20 @@
         
+ 
+   
 .tasks(t
@@ -1413,16 +1413,20 @@
 (tasks)%0A
+    
         
@@ -1447,24 +1447,28 @@
 err, res) %7B%0A
+    
             
@@ -1486,32 +1486,36 @@
                 
+    
 notify(err);%0A   
@@ -1527,18 +1527,25 @@
     
-%7D%0A
+    %7D%0A   
         
 %7D);%0A
@@ -1544,13 +1544,23 @@
     
+ 
 %7D);%0A
-%7D
+    %7D%0A%7D);%0A
 %0A%0Afu

Use node-liftoff to improve CLI mode.