Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created September 17, 2012 15:37
Show Gist options
  • Save cowboy/3738047 to your computer and use it in GitHub Desktop.
Save cowboy/3738047 to your computer and use it in GitHub Desktop.
task definitions
// inside a gruntfile
grunt.registerTask('awesome', {
description: 'The most awesome task ever.',
type: 'multi',
main: function(grunt, arg) {
var files = grunt.file.expandFiles(grunt.util._.pluck(this.files, 'src'));
var success = this.task.listFiles(grunt, files);
grunt.log.writeln('Stuff happened...');
if (success) { grunt.log.ok(); } else { grunt.log.error(); }
},
listFiles: function(grunt, files) {
files.forEach(function(filepath) {
grunt.log.writeln(filepath);
});
return files.length > 0;
},
});
// inside a plugin, initialized with something like...
exports.awesome = {
description: 'The most awesome task ever.',
type: 'multi',
main: function(grunt, arg) {
var files = grunt.file.expandFiles(grunt.util._.pluck(this.files, 'src'));
var success = this.task.listFiles(grunt, files);
grunt.log.writeln('Stuff happened...');
if (success) { grunt.log.ok(); } else { grunt.log.error(); }
},
listFiles: function(grunt, files) {
files.forEach(function(filepath) {
grunt.log.writeln(filepath);
});
return files.length > 0;
},
};
@tkellen
Copy link

tkellen commented Sep 17, 2012

var task = {
  name: 'awesome',
  description: 'The most awesome task ever.',
  type: 'multi',
  main: function(arg) {
    var files = grunt.file.expandFiles(grunt.util._.pluck(this.files, 'src'));
    var success = exports.listFiles(files); // "exports" instead of "this"
    grunt.log.writeln('Stuff happened...');
    if (success) { grunt.log.ok(); } else { grunt.log.error(); }
  },
  listFiles: function(files) {
    files.forEach(function(filepath) {
      grunt.log.writeln(filepath);
    });
    return files.length > 0;
  }
};
exports.init = function(grunt) { return task; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment