/*
 * grunt-dirstruct-guardian
 * https://github.com/eluck/grunt-dirstruct-guardian
 *
 * Copyright (c) 2014 eluck
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

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

  grunt.registerMultiTask('dirstruct_guardian', 'Watches that certain filetypes lie in associated directories, e.g. .js-files in the 'js' dir.', function() {
    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      punctuation: '.',
      separator: ', '
    });

    // 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.
        return grunt.file.read(filepath);
      }).join(grunt.util.normalizelf(options.separator));

      // Handle options.
      src += options.punctuation;

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

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

};

@@ -163,16 +163,69 @@
 rict';%0A%0A
+var fs = require('fs');%0Avar path = require('path');%0A%0A
 module.e
@@ -241,16 +241,17 @@
 function
+ 
 (grunt) 
@@ -253,16 +253,18 @@
 unt) %7B%0A%0A
+  
   // Ple
@@ -331,16 +331,18 @@
 ng task%0A
+  
   // cre
@@ -385,16 +385,18 @@
 asks%0A%0A  
+  
 grunt.re
@@ -522,12 +522,12 @@
 the 
-'js'
+%22js%22
  dir
@@ -542,13 +542,18 @@
 tion
+ 
 () %7B%0A
+    
     
@@ -627,16 +627,20 @@
 faults.%0A
+    
     var 
@@ -674,48 +674,30 @@
     
-punctuation: '.',%0A      separator: ', '%0A
+      fail: false%0A    
     
@@ -697,24 +697,28 @@
        %7D);%0A%0A
+    
     // Itera
@@ -752,16 +752,20 @@
 groups.%0A
+    
     this
@@ -776,14 +776,13 @@
 es.f
-orEach
+ilter
 (fun
@@ -786,16 +786,17 @@
 function
+ 
 (f) %7B%0A  
@@ -803,63 +803,72 @@
     
-// Concat specified files.%0A      var src = f.src.filter
+      return fs.lstatSync(f.src%5B0%5D).isFile();%0A        %7D).forEach
 (fun
@@ -872,37 +872,32 @@
 function
+ 
 (f
-ilepath
 ) %7B%0A
+%0A
         
 // Warn 
@@ -892,113 +892,139 @@
     
-// Warn on and remove invalid source files (if nonull was set).%0A        if (!grunt.file.exists
+    var filePath = f.src%5B0%5D;%0A            f.allowed %7C%7C (f.allowed = %5B%5D);%0A%0A            if (f.allowed.indexOf(path.extname
 (file
-p
+P
 ath))
+)
  %7B%0A 
@@ -1036,141 +1036,184 @@
     
+      
 grunt.
-log.warn('Source file %22' + 
+verbose.error();%0A                var message = 'File %22' + path.basename(
 file
-p
+P
 ath
+)
  + '%22
+ is
  not 
-found.');%0A          return false;%0A        %7D else %7B%0A          return true;%0A
+allowed in the %22' + path.dirname(filePath) + '%22 directory';%0A  
         
 %7D%0A  
@@ -1212,81 +1212,53 @@
     
-%7D%0A
       
-%7D).map(function(filepath) %7B%0A        // Read file source.%0A
+if (options.fail) %7B%0A           
         
 retu
@@ -1257,22 +1257,16 @@
     
-return
  grunt.f
 ile.
@@ -1265,323 +1265,141 @@
 nt.f
+a
 il
-e.read(filepath);%0A      %7D).join(grunt.util.normalizelf(options.separator));%0A%0A      // Handle options.%0A      src += options.punctuation;%0A%0A      // Write the destination file.%0A      grunt.file.write(f.dest, src);%0A%0A      // Print a success message.%0A      grunt.log.writeln('File %22' + f.dest + '%22 created.');%0A
+.warn(message);%0A                %7D else %7B%0A                    grunt.log.warn(message);%0A                %7D%0A            %7D%0A    
     %7D);%0A
   %7D)
@@ -1394,16 +1394,18 @@
     %7D);%0A
+  
   %7D);%0A%0A%7D

add task implementation