#!/usr/bin/env node
"use strict";

/** @module script bin/jester-batch */

var loadConfig = require("../lib/loadConfig"),
    lintFile = require("../lib/lintFile"),
    clearDir = require("../lib/clearDir"),
    rebuildProject = require("../lib/rebuildProject"),
    launchKarma = require("../lib/launchKarma"),
    createTestFile = require("../lib/createTestFile"),
    glob = require("glob"),
    when = require("when"),
    rebuildDocumentation = require("../lib/rebuildDocumentation");

var config = loadConfig("./jester.json");

/** */
function startTests() {
    var testsSucceeded = true;
    var deferred = when.defer();

    var createTestFiles = function () {
        console.log("Creating test files for karma");
        
        clearDir(config.karmaPath, function directoryCleared() {
            glob(config.srcPath + "/**/*.test.js", function (err, testfiles) {
                createTestFile(testfiles, config.karmaPath, runTests)
            });
        });
    }

    var runTests = function() {
        console.log("running the tests.");
        
        new launchKarma(false, config.karmaPath, config.karmaOptions, function (exitCode) {
           testsSucceeded = testsSucceeded && exitCode === 0;
           console.log(exitCode | !testsSucceeded)
           deferred.resolve(exitCode | !testsSucceeded);
        });
    }

    console.log("linting all files.");
    
    glob(config.srcPath + "/**/*.js", function (err, jsFiles) {
        var filesToGo = jsFiles.length;
        if (jsFiles.length === 0) {
            console.log("No JS files found! My work here is done.")
        }
        jsFiles.forEach(function (file) {
            lintFile(file, config.eslintRules, function onLintReady(lintSucceeded) {
                testsSucceeded = testsSucceeded && lintSucceeded;
                filesToGo -= 1;
                if (filesToGo === 0) {
                    createTestFiles();
                }
            });
        })
    });

    return deferred.promise;
}

console.log("Rebuilding the project artifacts.");

rebuildProject(config.fullEntryGlob, config.artifactPath);

when.join(
    startTests(), 
    rebuildDocumentation(config.srcPath, config.apiDocPath, config.jsdocConf, config.readme)
).then(function(exitCodes) {
    // first non-zero exit code or 0
    return exitCodes.reduce(function(a,b) { return a | b; }, 0);
}).then(function(exitCode) {
    //karma doesn't seem to end properly. This is a bit of a sledge hammer.
    process.exit(exitCode);
});


@@ -35,50 +35,8 @@
 %0D%0A%0D%0A
-/** @module script bin/jester-batch */%0D%0A%0D%0A
 var 
@@ -506,16 +506,8 @@
 %0D%0A%0D%0A
-/** */%0D%0A
 func
@@ -591,24 +591,347 @@
 defer();%0D%0A%0D%0A
+    var runTests = function() %7B%0D%0A        console.log(%22running the tests.%22);%0D%0A        %0D%0A        new launchKarma(false, config.karmaPath, config.karmaOptions, function (exitCode) %7B%0D%0A           testsSucceeded = testsSucceeded && exitCode === 0;%0D%0A           deferred.resolve(exitCode %7C !testsSucceeded);%0D%0A        %7D);%0D%0A    %7D%0D%0A%0D%0A
     var crea
@@ -1282,383 +1282,12 @@
  %7D%0D%0A
-%0D%0A
     
-var runTests = function() %7B%0D%0A        console.log(%22running the tests.%22);%0D%0A        %0D%0A        new launchKarma(false, config.karmaPath, config.karmaOptions, function (exitCode) %7B%0D%0A           testsSucceeded = testsSucceeded && exitCode === 0;%0D%0A           console.log(exitCode %7C !testsSucceeded)%0D%0A           deferred.resolve(exitCode %7C !testsSucceeded);%0D%0A        %7D);%0D%0A    %7D%0D%0A
 %0D%0A  
@@ -1540,16 +1540,51 @@
  done.%22)
+;%0D%0A            deferred.resolve(0);
 %0D%0A      
@@ -1580,32 +1580,52 @@
 0);%0D%0A        %7D%0D%0A
+        else %7B%0D%0A    
         jsFiles.
@@ -1659,24 +1659,28 @@
             
+  
+  
 lintFile(fil
@@ -1749,32 +1749,36 @@
                 
+    
 testsSucceeded =
@@ -1828,16 +1828,20 @@
         
+ 
+   
 filesToG
@@ -1849,16 +1849,20 @@
  -= 1;%0D%0A
+    
         
@@ -1905,32 +1905,36 @@
                 
+    
 createTestFiles(
@@ -1945,33 +1945,58 @@
                 
-%7D
+    %7D%0D%0A                %7D);
 %0D%0A            %7D)
@@ -2007,17 +2007,16 @@
        %7D
-)
 %0D%0A    %7D)

resolve jester-batch plus some stylistic changes