Commits

Kazunori Ninomiya  committed f63c032

Support JavaScript object as root.

  • Participants
  • Parent commits 4e1183b

Comments (0)

Files changed (1)

File tasks/ect.js

 
     this.files.forEach(function(f) {
       try {
-        var validFiles = removeInvalidFiles(getSrc(f, options));
+        var validFiles = getSrc(f, options);
         writeFile(f.dest, concatOutput(validFiles, options));
       }
       catch (e) {
 
   function getSrc(file, options)
   {
-    var result
-      , src = file.src
+    var result, re, length
+      , src  = file.src
+      , orig = file.orig
+      , root = options.root
       ;
 
-    if (typeof file.cwd === 'undefined'
-      && typeof options.root !== 'undefined'
-    ) {
-      file.cwd = options.root;
-      result = grunt.file.expandMapping(file.orig.src, null, {
-        cwd: file.cwd,
-        flatten: file.flatten,
-        ext: file.ext,
-      });
-
-      if (result.length > 0) {
-        src = result[0].src;
-      }
+    switch (grunt.util.kindOf(root)) {
+      case 'object':
+        re = orig.src.map(function(v) {
+          return new RegExp(v.replace('*', '.*'));
+        });
+        length = re.length;
+        src = Object.keys(root).filter(function(key) {
+          var i = length;
+          while (i--) {
+            if (re[i].test(key)) {
+              return true;
+            }
+          }
+          return false;
+        });
+        break;
+
+      case 'string':
+        if (typeof file.cwd === 'undefined') {
+          file.cwd = root;
+          result = grunt.file.expandMapping(orig.src, null, {
+            cwd: file.cwd,
+            flatten: file.flatten,
+            ext: file.ext,
+          });
+
+          if (result.length > 0) {
+            src = result[0].src;
+          }
+        }
+
+      default: /* FALLTHROUGH */
+        src = removeInvalidFiles(src);
+        break;
     }
 
     return src;
 
   function concatOutput(files, options)
   {
-    return files.map(function(filepath) {
-      return render(filepath, options);
+    return files.map(function(src) {
+      return render(src, options);
     }).join(grunt.util.normalizelf(options.separator));
   }
 
-  function render(filepath, options)
+  function render(src, options)
   {
     var renderer, root;
 
     options = _.clone(options);
-    if (typeof options.root === 'undefined') {
-      options.root = path.dirname(filepath);
-      filepath = path.basename(filepath);
-    }
-    else {
-      root = path.join(options.root, path.sep);
-      filepath = filepath.replace(new RegExp('^' + quote(root), 'i'), '');
+    switch (grunt.util.kindOf(options.root)) {
+      case 'object':
+        break;
+
+      case 'string':
+        root = path.join(options.root, path.sep);
+        src  = src.replace(new RegExp('^' + quote(root), 'i'), '');
+        break;
+
+      default:
+        options.root = path.dirname(src);
+        src = path.basename(src);
+        break;
     }
 
     renderer = new ECT(options);
-    return renderer.render(filepath, options.variables);
+    return renderer.render(src, options.variables);
   }
 
   function warnOnEmptyFile(path)
 
   function quote(str)
   {
-    return (str + '').replace(/([.?*+\^$\[\]\\(){}|\-])/g, "\\$1");
+    return (str + '').replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
   }
 };