Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active December 9, 2015 22:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/4338353 to your computer and use it in GitHub Desktop.
Save cowboy/4338353 to your computer and use it in GitHub Desktop.
grunt 0.4 file.expandMapping and templates
module.exports = function(grunt) {
grunt.initConfig({
stuff: {
dest: 'foo/',
ext: '.bar',
},
log_files: {
my_target: {
files: grunt.file.expandMapping('**/*.js', '<%= stuff.dest %>', {
cwd: 'test/fixtures/expand',
rename: function(destBase, destPath) {
return destBase + destPath.replace(/\.js$/, '<%= stuff.ext %>');
}
})
}
}
});
console.log('\nThe files object generated with grunt.file.expandMapping:');
console.log(grunt.config('log_files.my_target.files'));
console.log('');
grunt.registerMultiTask('log_files', 'Log src/dest pairs in a files object.', function() {
console.log('%s -> %s', this.file.src, this.file.dest);
});
grunt.registerTask('update_stuff', 'Update the stuff object', function(dest, ext) {
grunt.config.set('stuff', {dest: dest + '/', ext: '.' + ext});
});
};
# Given the source files:
# test/fixtures/expand/js/bar.js
# test/fixtures/expand/js/foo.js
$ grunt log_files update_stuff:aaa:bbb log_files
The files object generated with grunt.file.expandMapping:
{ '<%= stuff.dest %>js/bar<%= stuff.ext %>': 'test/fixtures/expand/js/bar.js',
'<%= stuff.dest %>js/foo<%= stuff.ext %>': 'test/fixtures/expand/js/foo.js' }
Running "log_files:my_target" (log_files) task
test/fixtures/expand/js/bar.js -> foo/js/bar.bar
test/fixtures/expand/js/foo.js -> foo/js/foo.bar
Running "update_stuff:aaa:bbb" (update_stuff) task
Running "log_files:my_target" (log_files) task
test/fixtures/expand/js/bar.js -> aaa/js/bar.bbb
test/fixtures/expand/js/foo.js -> aaa/js/foo.bbb
Done, without errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment