Skip to content

Instantly share code, notes, and snippets.

@jgable
Created April 22, 2013 20:17
Show Gist options
  • Save jgable/5438137 to your computer and use it in GitHub Desktop.
Save jgable/5438137 to your computer and use it in GitHub Desktop.
Building a Better Grunt Plugin
module.exports = function(grunt) {
grunt.registerMultiTask('{%= short_name %}', 'Your task description goes here.', 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) {
// Do something to some files...
// Print a success message.
grunt.log.writeln('File "' + f.dest + '" created.');
});
});
};
/docs
- intro.md
- options.md
- license.md
/lib
- jsHintTask.js
/tasks
- jshint2.js
/tests
Gruntfile.js
package.json
var JSHintTask = require("../lib/jsHintTask");
module.exports = JSHintTask.registerWithGrunt;
function JSHintTask(task) {
this.origTask = task;
this.options = task.options(JSHintTask.Defaults);
}
JSHintTask.prototype = {
// Get the party started.
run: function() {
}
};
// A static attribute holding our defaults so we can test against them.
JSHintTask.Defaults = {
something: true
};
// Some static task information
JSHintTask.taskName = "jshint2";
JSHintTask.taskDescription = "A better jshint task";
// A static helper method for registering with Grunt
JSHintTask.registerWithGrunt = function(grunt) {
grunt.registerMultiTask(JSHintTask.taskName, JSHintTask.taskDescription, function() {
var task = new JSHintTask(this);
task.run();
});
};
module.exports = JSHintTask;
var grunt = require("grunt"),
should = require("should"),
_ = grunt.util._;
var JSHintTask = require("../lib/jsHintTask");
describe("JSHintTask", function() {
var makeMockTask = function(done) {
return {
_taskOptions: { bad: "option" },
filesSrc: grunt.file.expand("test/res/good*.js"),
options: function(defs) { return _.defaults(this._taskOptions, defs); },
async: function() {
return done;
}
};
};
it("registers itself with grunt", function() {
should.exist(JSHintTask.registerWithGrunt);
JSHintTask.registerWithGrunt(grunt);
// Check that it registered
should.exist(grunt.task._tasks[JSHintTask.taskName]);
grunt.task._tasks[JSHintTask.taskName].info.should.equal(JSHintTask.taskDescription);
});
it("loads options from a task", function() {
var task = new JSHintTask(makeMockTask()),
actual = task.options;
should.exist(actual);
actual.something.should.equal(JSHintTask.Defaults.something);
});
});
@joseph-jja
Copy link

A grunt config file example here would help.

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