Created
April 22, 2013 20:17
-
-
Save jgable/5438137 to your computer and use it in GitHub Desktop.
Building a Better Grunt Plugin
This file contains 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
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.'); | |
}); | |
}); | |
}; |
This file contains 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
/docs | |
- intro.md | |
- options.md | |
- license.md | |
/lib | |
- jsHintTask.js | |
/tasks | |
- jshint2.js | |
/tests | |
Gruntfile.js | |
package.json |
This file contains 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
var JSHintTask = require("../lib/jsHintTask"); | |
module.exports = JSHintTask.registerWithGrunt; |
This file contains 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
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; |
This file contains 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
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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A grunt config file example here would help.