/*
 * grunt-commonjs-tamer
 * https://github.com/freezedev/grunt-commonjs-tamer
 *
 */

'use strict';

var path = require('path');


module.exports = function(grunt) {

  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks

  grunt.registerMultiTask('commonjs_tamer', 'Tame your CommonJS modules', function() {
    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      register: 'require.register',
      separator: grunt.util.linefeed,
      indent: '\t',
      normalizeIndexFile: true,
      base: null,
      namespace: null,
      sourcemap: true,
      processName: function(name) {
        return name;
      },
      process: function() {

      },
      blacklist: []
    });

    // Iterate over all specified file groups.
    this.files.forEach(function(f) {
      
      // Concat specified files.
      var src = f.src.filter(function(filepath) {
        // Warn on and remove invalid source files (if nonull was set).
        if (!grunt.file.exists(filepath)) {
          grunt.log.warn('Source file "' + filepath + '" not found.');
          return false;
        } else {
          return true;
        }
      }).map(function(filepath) {
        // Read file source.
        var source = grunt.file.read(filepath);
        var extension = path.extname(filepath);
        var isCoffeeScript = (extension === '.coffee');
        
        var moduleName = filepath.split(extension)[0];
        if (options.base && moduleName.indexOf(options.base) === 0) {
          moduleName = moduleName.split(options.base)[1];
        }
        
        if (options.normalizeIndexFile && moduleName.indexOf('/index') > 0) {
          moduleName = moduleName.replace('/index', '');
        }
        
        if (options.namespace) {
          // If index.js is in the root folder and a namespace has been defined
          // use the namespace as the module name
          if (options.normalizeIndexFile && moduleName === 'index') {
            moduleName = options.namespace;
          } else {
            moduleName = options.namespace + '/' + moduleName;            
          }
        }
        
        moduleName = options.processName(moduleName, path.basename(moduleName));

        
        if (isCoffeeScript) {
          // Do coffee magic here
        }

        /*source = source.solit('\n').map(function(line) {
          return options.indent + line;
        });*/

        source = options.register + '(\'' + moduleName + '\', function(require, module, exports) {'
        + options.separator + source + options.separator + '});';

        return source;
      }).join(grunt.util.normalizelf(options.separator));
      
      /*var modules = options.modules;
      
      var moduleKeys = Object.keys(modules);
      
      for (var i = 0, j = moduleKeys.length; i < j; i++) {
        (function(key, value) {
          src += 'define(' + quotes + key + quotes + ', ' + value.toString() + ');';
        })(moduleKeys[i], modules[moduleKeys[i]]);
      }*/

      // Write the destination file.
      grunt.file.write(f.dest, src);

      // Print a success message.
      grunt.log.writeln('File "' + f.dest + '" created.');
    });
  });

};

@@ -124,16 +124,66 @@
 path');%0A
+var beautify = require('js-beautify').js_beautify;
 %0A%0Amodule
@@ -621,20 +621,22 @@
     
-indent: '%5Ct'
+beautify: true
 ,%0A  
@@ -1460,64 +1460,8 @@
 h);%0A
-        var isCoffeeScript = (extension === '.coffee');%0A
     
@@ -2304,25 +2304,24 @@
 Name));%0A
-%0A
         
 %0A       
@@ -2316,196 +2316,8 @@
     
-%0A        if (isCoffeeScript) %7B%0A          // Do coffee magic here%0A        %7D%0A%0A        /*source = source.solit('%5Cn').map(function(line) %7B%0A          return options.indent + line;%0A        %7D);*/
 %0A%0A  
@@ -2413,24 +2413,26 @@
 orts) %7B'
+ +
 %0A       
  + optio
@@ -2423,18 +2423,16 @@
 %0A       
- +
  options
@@ -2484,142 +2484,92 @@
 ;';%0A
-%0A
+ 
        
- return source;%0A      %7D).join(grunt.util.normalizelf(options.separator));
+%0A        if (options.beautify) %7B
 %0A      
-%0A  
     
-/*var modules = options.modules
+source = beautify(source)
 ;%0A      
 %0A   
@@ -2568,297 +2568,93 @@
     
-%0A
   
+%7D%0A%0A
     
-var moduleKeys = Object.keys(modules);%0A      %0A      for (var i = 0, j = moduleKeys.length; i %3C j; i++) %7B%0A        (function(key, value) %7B%0A          src += 'define(' + quotes + key + quotes + ', ' + value.toString() + ');';%0A        %7D)(moduleKeys%5Bi%5D, modules%5BmoduleKeys%5Bi%5D%5D);%0A      %7D*/
+    return source;%0A      %7D).join(grunt.util.normalizelf(options.separator));
 %0A%0A  

Add beautifier