Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with Grunt: The JavaScript Task Runner

You're reading from   Getting Started with Grunt: The JavaScript Task Runner If you know JavaScript you ought to know Grunt – the Task Runner for managing sophisticated web applications. From a basic understanding to constructing your own advanced Grunt tasks, this tutorial has it all covered.

Arrow left icon
Product type Paperback
Published in Feb 2014
Publisher
ISBN-13 9781783980628
Length 132 pages
Edition Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Jaime Pillora Jaime Pillora
Author Profile Icon Jaime Pillora
Jaime Pillora
Bocoup LLC Bocoup LLC
Author Profile Icon Bocoup LLC
Bocoup LLC
Arrow right icon
View More author details
Toc

Creating your own tasks


In this section, we shall explore the creation of Grunt tasks in more detail, specifically normal tasks and multitasks, and asynchronous tasks.

Tasks

As exemplified previously, creating tasks is extremely simple. We provide a name and a function to grunt.registerTask and we're ready to execute. Tasks (as opposed to multitasks) are best suited to build processes that will only be performed once in a given build. A real world example of such a process might be to update a deployment log file, which we could run whenever we deploy, providing a simple history of deployments for future reference. This task might look like:

//Code example 01-deploy-log-task
var fs = require('fs');
module.exports = function(grunt) {

  grunt.registerTask('log-deploy', function() {
    var message = 'Deployment on ' + new Date();
    fs.appendFileSync('deploy.log', message + '\n');
    grunt.log.writeln('Appended "' + message + '"');
  });

};

Note

See the Node.js API documentation for more information...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime