/*
 * grunt-run-node
 * https://github.com/jamesdbloom/grunt-run-node
 *
 * Copyright (c) 2014 James Bloom
 * Licensed under the MIT license.
 */

'use strict';

var processList;

module.exports = function (grunt) {

    processList = (function (grunt) {
        var spawn = require('child_process').spawn,
            processes = [];

        return {
            add: function (command, args, options) {
                processes.unshift(spawn(command, args, options));
                grunt.log.ok("Started " + command + " " + args.join(" "));
            },
            stop_all: function () {
                for (var i = 0; i < processes.length; i++) {
                    processes[i].kill();
                }
                grunt.log.ok('Stopped %s launched node processes', processes.length);
            }
        };
    })(grunt);

    grunt.registerMultiTask('run_node', 'Start node asynchronously from your grunt build.', function () {
        // mark task as asynchronous
        var done = this.async();

        this.files.forEach(function (file) {
            file.src.forEach(function (filepath, index, array) {
                if (!grunt.file.exists(filepath)) {
                    grunt.log.warn('Source file "' + filepath + '" not found.');
                    done(false);
                } else {
                    processList.add('node', [filepath], {
                        cwd: grunt.option('cwd') || process.cwd(),
                        stdio: grunt.option('stdio') || [ 'ignore', (grunt.option('verbose') ? process.stdout : 'ignore'), process.stderr ],
                        env: grunt.option('env'),
                        detached: grunt.option('detached')
                    });
                }
                // last element
                if (index === array.length - 1) {
                    done(true);
                }
            });
        });
    });

    grunt.registerMultiTask('stop_node', 'Stops node instances previously started from your grunt build.', function () {
        processList.stop_all();
    });

    // stop all started node processes on exit
    process.on('exit', function(code) {
        processList.stop_all();
    });
};

@@ -155,16 +155,54 @@
 rict';%0A%0A
+var extend = require('util')._extend;%0A
 var proc
@@ -1051,16 +1051,312 @@
 async();
+%0A        var options = this.options(%7B%0A            cwd: process.cwd,%0A            stdio: %5B 'ignore', (grunt.option('verbose') ? process.stdout : 'ignore'), process.stderr %5D,%0A            env: process.env,%0A            detached: true%0A        %7D);%0A        options.env = extend(process.env, options.env);
 %0A%0A      
@@ -1710,348 +1710,15 @@
 h%5D, 
-%7B%0A                        cwd: grunt.option('cwd') %7C%7C process.cwd(),%0A                        stdio: grunt.option('stdio') %7C%7C %5B 'ignore', (grunt.option('verbose') ? process.stdout : 'ignore'), process.stderr %5D,%0A                        env: grunt.option('env'),%0A                        detached: grunt.option('detached')%0A                    %7D
+options
 );%0A 

Set options the right way with this.options()