/*
**  grunt-traceur-simple -- Grunt Task for ECMAScript 6 to ECMAScript 5 Transpiling
**  Copyright (c) 2014 Ralf S. Engelschall <rse@engelschall.com>
**
**  Permission is hereby granted, free of charge, to any person obtaining
**  a copy of this software and associated documentation files (the
**  "Software"), to deal in the Software without restriction, including
**  without limitation the rights to use, copy, modify, merge, publish,
**  distribute, sublicense, and/or sell copies of the Software, and to
**  permit persons to whom the Software is furnished to do so, subject to
**  the following conditions:
**
**  The above copyright notice and this permission notice shall be included
**  in all copies or substantial portions of the Software.
**
**  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
**  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
**  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
**  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
**  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
**  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
**  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* global require: false */
/* global module:  false */
/* global process: false */

/*  Grunt plugin information  */
var NAME = "traceur";
var DESC = "Transpiles ECMAScript 6 to ECMAScript 5 with Traceur";

/*  external requirements  */
var exec  = require("child_process").exec;
var path  = require("path");
var chalk = require("chalk");

/*  external paths to Traceur  */
var traceurCommandPath = path.resolve(path.join(__dirname, "../node_modules/traceur/src/node/command.js"));
var traceurRuntimePath = path.resolve(path.join(__dirname, "../node_modules/traceur/bin/traceur-runtime.js"));

/*  export the Grunt task  */
module.exports = function (grunt) {
    grunt.registerMultiTask(NAME, DESC, function () {
        /*  provide default options  */
        var options = this.options({
            traceurRuntime: traceurRuntimePath,
            traceurCommand: traceurCommandPath,
            traceurOptions: "",
            includeRuntime: false
        });
        grunt.verbose.writeflags(options, "Options");

        /*  iterate over all src-dest file pairs  */
        var rc = true;
        var done = this.async();
        var tasksCur = 0;
        var tasksMax = this.files.length;
        if (options.includeRuntime && tasksMax !== 1)
            grunt.fail.fatal("including the runtime into more than one output file rejected");
        this.files.forEach(function (f) {

            /*  assemble the Traceur shell command  */
            var cmd = "";
            if (options.traceurCommand.match(/\.js$/))
                cmd = process.execPath + " " + options.traceurCommand;
            else
                cmd = options.traceurCommand;
            if (options.traceurOptions !== "")
                cmd += " " + options.traceurOptions;
            cmd += " --out " + f.dest;
            cmd += " " + f.src.join(" ");

            /*  execute the Traceur shell command  */
            exec(cmd, function (error, stdout, stderr) {
                if (error) {
                    /*  error reporting  */
                    grunt.log.writeln("transpiling: " + chalk.red(f.dest) + " <- " + chalk.red(f.src.join(" ")));
                    grunt.log.error(error);
                    grunt.log.error(stderr);
                    rc = false;
                }
                else {
                    /*  success reporting  */
                    grunt.log.writeln("transpiling: " + chalk.green(f.dest) + " <- " + chalk.green(f.src.join(" ")));
                }

                /*  optional runtime inclusion  */
                if (options.includeRuntime) {
                    grunt.log.writeln("injecting:   " + chalk.green(f.dest) + " <- " + chalk.green(options.traceurRuntime));
                    var rt = grunt.file.read(options.traceurRuntime, { encoding: "utf8" });
                    var txt = grunt.file.read(f.dest, { encoding: "utf8" });
                    txt = rt + "\n" + txt;
                    grunt.file.write(f.dest, txt, { encoding: "utf8" });
                }

                /*  determine end of asynchronous transpiling  */
                tasksCur++;
                if (tasksCur === tasksMax)
                    done(rc);

            });
        });
    });
};


@@ -1261,16 +1261,18 @@
 require:
+  
  false *
@@ -1292,16 +1292,18 @@
 odule:  
+ 
+ 
 false */
@@ -1321,16 +1321,48 @@
 process:
+   false */%0A/* global __dirname:
  false *
@@ -1618,16 +1618,58 @@
 chalk%22);
+%0Avar quote = require(%22shell-quote%22).quote;
 %0A%0A/*  ex
@@ -2792,16 +2792,80 @@
 d = %22%22;%0A
+            var sq = function (txt) %7B return quote(%5B txt %5D); %7D;%0A
         
@@ -2933,16 +2933,19 @@
   cmd = 
+sq(
 process.
@@ -2952,16 +2952,17 @@
 execPath
+)
  + %22 %22 +
@@ -2958,24 +2958,27 @@
 th) + %22 %22 + 
+sq(
 options.trac
@@ -2979,32 +2979,33 @@
 s.traceurCommand
+)
 ;%0A            el
@@ -3029,16 +3029,19 @@
   cmd = 
+sq(
 options.
@@ -3054,16 +3054,17 @@
 rCommand
+)
 ;%0A      
@@ -3188,22 +3188,26 @@
 out %22 + 
+sq(
 f.dest
+)
 ;%0A      
@@ -3227,24 +3227,66 @@
 %22 %22 + f.src.
+map(function (name) %7B return sq(name); %7D).
 join(%22 %22);%0A%0A

fix linting