Created
          September 12, 2012 02:44 
        
      - 
      
- 
        Save ctalkington/3703920 to your computer and use it in GitHub Desktop. 
    Grunt Glob to Multi Files Object
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // gruntfile dropin used to build a grunt files object with 1:1 structure | |
| function globToMultiFiles(glob, dest, options) { | |
| var path = require('path'); | |
| grunt.util = grunt.util || grunt.utils; | |
| dest = grunt.template.process(dest); | |
| options = grunt.util._.defaults(options || {}, { | |
| cwd: '', | |
| minimatch: {}, | |
| processName: false | |
| }); | |
| if (options.cwd.length > 0) { | |
| options.minimatch.cwd = options.cwd; | |
| } | |
| var result = {}; | |
| var destFile; | |
| var srcFile; | |
| var fileName; | |
| var filePath; | |
| grunt.file.expandFiles(options.minimatch, glob).forEach(function(file) { | |
| fileName = path.basename(file); | |
| filePath = path.dirname(file); | |
| srcFile = path.join(options.cwd, file); | |
| if (options.processName && grunt.util.kindOf(options.processName) === 'function') { | |
| fileName = options.processName(fileName) || fileName; | |
| } | |
| destFile = path.join(dest, filePath, fileName); | |
| result[destFile] = srcFile; | |
| }); | |
| return result; | |
| } | |
| ... | |
| // files in src to dest/src | |
| globToMultiFiles('src/*', 'dest'); | |
| // files and folders in src to dest (retaining structure) | |
| globToMultiFiles('**', 'dest', {cwd: 'src'}); | |
| // files and folders in src to dest (retaining structure) | |
| // replace ext, example use case stylus | |
| globToMultiFiles('**', 'dest', { | |
| cwd: 'src', | |
| processName: function(fileName) { | |
| fileName = fileName.replace('.styl', '.css'); | |
| return fileName; | |
| } | |
| }); | |
| ... | |
| task: { | |
| target: { | |
| files: globToMultiFiles(...) | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Note that
expandFileshas been removed from grunt, adjust as follows: