// LESSr
// makes optimizations and auto-updating of less simple.
var myname  = "lessr: ";
// --
var less    = require("less");
var fs      = require("fs"); 
// --
exports.addFile = function(absLessFile, absCSSFile, autoUpdate, options, extraWatches){
  var updates = 0;
  var optimizing = false;
  var runWhenDone = false;
  options = options||{};
  var fname = options.filename||"";
  function onOptDone(){
    optimizing = false;
    if(runWhenDone){
      runWhenDone = false;
      doOptimization();
    }
  }
  function doOptimization(){
    if(optimizing){
      runWhenDone = true;
      return console.log(myname+"busy, will re-run shortly.");
    }
    optimizing = true;
    var t0 = new Date().getTime();
    fs.readFile(absLessFile, function(err, data){
      if(err){
        console.log(err);
        return onOptDone();
      }
      // --
      var dataString = (data||"").toString();
      var parser = new less.Parser(options);
      parser.parse(dataString, function(err, cssTree){
        if(err){
          console.warn(myname+"error parsing LESS:");
          less.writeError(err, options);
          return onOptDone();
        } 
        try{
          // Create the CSS from the cssTree
          var cssString = cssTree.toCSS({ 
            compress   : options.compress,
            yuicompress: options.yuicompress
          });
          if(!options.compress){
            cssString = cssString.replace(/\n\ \ /g, "\n"); 
          }
          // Write output
          fs.writeFileSync(absCSSFile, cssString, 'utf8');
          var t1 = new Date().getTime() - t0;
          console.log(myname+"-> "+fname+": created css in "+t1+"ms");
          onOptDone();
        }catch(ex){
          console.warn(myname+"error creating CSS from LESS:");
          less.writeError(ex, options);
          onOptDone();
        }
      });
      if(updates === 0 && autoUpdate){
        console.log(myname+"-> "+fname+": adding autoUpdate.");
        fs.watchFile(absLessFile, {interval: options.autoMS||5007}, function (curr, prev) {
          if(prev.mtime !== curr.mtime){
            doOptimization();
          } 
        });
        if(extraWatches){
          for(var i=0; i<extraWatches.length; i++){
             fs.watchFile(extraWatches[i], {interval: options.autoMS||5007}, function (curr, prev) {
              if(prev.mtime !== curr.mtime){
                doOptimization();
              } 
            }); 
          }
        }
      }
      updates++;
    });
  }
  doOptimization();
};


@@ -905,456 +905,280 @@
     
-var parser = new less.Parser(options);%0A      parser.parse(dataString, function(err, cssTree)%7B%0A        if(err)%7B%0A          console.warn(myname+%22error parsing LESS:%22);%0A          less.writeError(err, options);%0A          return onOptDone();%0A        %7D %0A        try%7B%0A          // Create the CSS from the cssTree%0A          var cssString = cssTree.toCSS(%7B %0A            compress   : options.compress,%0A            yuicompress: options.yuicompress%0A          %7D)
+less.render(dataString, options)%0A%09      .then(function(output) %7B%0A%09        // output.css = string of css%0A%09        // output.map = string of sourcemap%0A%09        // output.imports = array of string filenames of the imports referenced%0A%09%09%09%09%09// --%0A%09%09%09%09%09var cssString = output.css
 ;%0A  
@@ -1510,26 +1510,33 @@
 ();%0A
-  
+%09
       %7D
-catch(ex)
+,function(error) 
 %7B%0A  
@@ -1574,25 +1574,15 @@
 ror 
-creating CSS from
+parsing
  LES
@@ -1614,17 +1614,18 @@
 eError(e
-x
+rr
 , option
@@ -1630,32 +1630,39 @@
 ons);%0A          
+return 
 onOptDone();%0A   
@@ -1658,26 +1658,17 @@
 Done();%0A
-        %7D%0A
+%09
       %7D)

migrate to less.render instead of less.parse