Skip to content

Instantly share code, notes, and snippets.

@knu2xs
Created August 10, 2014 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knu2xs/589941f6e8ef50a8b135 to your computer and use it in GitHub Desktop.
Save knu2xs/589941f6e8ef50a8b135 to your computer and use it in GitHub Desktop.
Streamline the process of working through the Getting Started with Grunt book and constantly creating a new directory with a new Gruntfile in the directory for each exercise.
// purpose: Streamline the process of working through the Getting Started with Grunt book and constantly creating a
// new directory with a new Gruntfile in the directory for each exercise.
//
// use: First create a directory for working through the exercises. Next, get your dependencies all taken care of in
// this directory by using '$ npm init' to create a package.json file. Now, get grunt in this directory using
// '$ npm install --save-dev grunt'. Although you will need more dependencies through the course of the book,
// this is enough to get started. Now, getting started on a new exercise is as simple as using
// '$ grunt newEx:<chapter number>:<exercise number>'. For instance, if starting on chapter three, exercise
// four, just enter '$ grunt newEx:03:04'. This grunt will create ./04/0403/Gruntfile.js. Now start editing
// the Gruntfile to do the exercise.
// import modules
path = require('path');
fs = require('fs');
module.exports = function(grunt){
// create new exercise task with two parameters, the chapter number and the exercise number
grunt.registerTask('newEx', function(){
// make sure the process does not exit early
var done = this.async();
// give access within the function to variables
var params = this.args;
// create full path to where the new exercise will reside
var exGrunt = path.join(__dirname, params[0], params[0] + params[1], 'Gruntfile.js');
// see if the exercise already exists
fs.exists(exGrunt, function(exists){
// if it exists, warn the user and exit
if (exists) {
grunt.fail.warn('Chapter ' + params[0] + ', exercise ' + params[1] + ' already exists.');
// if it does not exist, create
} else {
// create and place a new Gruntfile in the directory
var comment = '// chapter ' + params[0] + ' exercise ' + params[1];
grunt.file.write(exGrunt, comment);
// let grunt know it is done
done();
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment