From 12f87a1b8db9fe6e4777e29de81c250d68ab37bf Mon Sep 17 00:00:00 2001 From: Jai Pandya Date: Sun, 10 May 2015 23:04:52 +0530 Subject: [PATCH] Adding `workers` option for specifying the number of workers Set this to 1 if you want the task to be executed serially. By default it equals to twice the number of CPUs --- README.md | 9 +++++++++ package.json | 1 + tasks/haml.js | 12 ++++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b4b5a8d..b951170 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,15 @@ window.HAML['templates/example'] *Defined only `placement == 'global'`.* +#### workers +Type: ```number``` +Default: ```twice the number of CPUs``` + +Limit the number of workers when running the task + +Set this to 1 if you want the task to be executed +serially. + ### Usage examples ``` javascript diff --git a/package.json b/package.json index a3fff24..58aa6ff 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "node": "0.10.x" }, "dependencies": { + "async": "^0.9.0", "haml": "0.4.x", "haml-coffee": "1.14.x" }, diff --git a/tasks/haml.js b/tasks/haml.js index 4161a32..411cd60 100644 --- a/tasks/haml.js +++ b/tasks/haml.js @@ -9,7 +9,7 @@ module.exports = function(grunt) { 'use strict'; var path = require('path'); - var async = grunt.util.async; + var async = require('async'); var _ = grunt.util._; grunt.registerMultiTask('haml', 'Compile Haml files', function() { @@ -41,7 +41,11 @@ module.exports = function(grunt) { // Precompile templates; if false (and target == 'js'), place rendered // HTML in js variables. - precompile: true + precompile: true, + + // Limit the number of workers to this number when running the task + // Default is twice the number of CPUs + workers: require('os').cpus().length * 2 }); @@ -52,7 +56,7 @@ module.exports = function(grunt) { grunt.verbose.writeflags(options, 'Options'); // Transpile each src/dest group of files. - async.forEach(this.files, function(file, callback) { + async.eachLimit(this.files, options.workers, function(file, callback) { var opts; // Get only files that are actually there. var validFiles = file.src.filter(function(filepath) { @@ -92,7 +96,7 @@ module.exports = function(grunt) { }; // Transpile each file. - async.map(validFiles, processFile, function (err, results) { + async.mapLimit(validFiles, options.workers, processFile, function (err, results) { // Write the new file. grunt.file.write(file.dest, results.join('\n')); grunt.log.writeln('File ' + file.dest.cyan + ' created.');