Using GruntJS for building JavaScript Projects

I stumbled into Grunt a while ago but only recently I got a chance to play around with it and integrate it into a NodeJS project.

Grunt is described by its developers as "The JavaScript Task Runner". I like to refer to it as Ant for JavaScript. It follows a similar "Task" oriented approach with a rich library of configurable tasks. It has quite a vast verity of predefined tasks ("plugins") – some are provided by the core team and lots are contributed by open source developers. There are plugins for linting, uglifying, compressing, running tests and more. The full lest of tasks can be found here.

Writing custom task is also pretty simple and straight forward, Especially with the grunt utilities. Here is an example of a custom plugin that performs Git commits:

 


module.exports = function (grunt) {
    grunt.registerMultiTask('git', 'Execute git commands.', function () {
        // Merge task-specific and/or target-specific options with these defaults.
        var options = this.options({
            command: 'commit',
            message: 'Commit'
        });
 
 
        if (options.command === 'commit') {
            var done = this.async();
 
            var addFile = function (file, cb) {
                grunt.util.spawn({
                    cmd: "git",
                    args: ["add", file.src]
                }, cb);
            };
 
            grunt.util.async.forEach(this.files, addFile, function (err) {
                grunt.util.spawn({
                    cmd: "git",
                    args: ["commit", "-m", options.message]
                }, function (err) {
                    done(!err);
                });
            });
        } else {
            grunt.log.error('No or unknown command specified: ' + options.command);
        }
    });
 
};

Full plugin can be found here
* Notice how simple it is to spawn command line (external) tasks.

Another point in favor of Grunt is the good documentation & tutorial that have become quite rare in the JS Jungle.

To conclude this short overview – I will defiantly use Grunt in future client JS and Node JS  projects.

Javascript Architect

Frontend Group
Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com