diff --git a/.gitignore b/.gitignore index c2658d7..d35bbf7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +.idea/ \ No newline at end of file diff --git a/index.js b/index.js index 9a8cc45..1d1d773 100644 --- a/index.js +++ b/index.js @@ -2,33 +2,58 @@ var spawn = require('child_process').spawn; -var gulpMultiProcess = function(tasks, cb) { - var completed = 0; +var gulpMultiProcess = function(tasks, cb, cpusRespective) { var code = 0; - - tasks.forEach(function(taskName) { - var args = [process.argv[1], taskName]; - - process.argv.forEach(function (val) { - if(val[0] === '-' && val !== '--gulpfile') { - args.push(val); - } - }); - - var worker = spawn(process.execPath, args , { stdio: 'inherit' }); - - worker.on('exit', function (workerCode) { + var completed; + var each; + var cpusNumber; + var q; + var createWorker = function(onExit, taskName) { + var args = [process.argv[1], taskName]; + var worker; + process.argv.forEach(function (val) { + if(val[0] === '-' && val !== '--gulpfile') { + args.push(val); + } + }); + worker = spawn(process.execPath, args , { stdio: 'inherit' }); + worker.on('exit', onExit); + }; + + if (!cpusRespective) { + completed = 0; + each = createWorker.bind(this, function (workerCode) { if(workerCode !== 0) { code = workerCode; } - completed++; - if(completed === tasks.length) { cb(code); } }); - }); + tasks.forEach(each); + } else { + cpusNumber = require('os').cpus().length; + q = require('async.queue'); + q = q(function (taskName, callback) { + createWorker( + function (workerCode) { + if(workerCode !== 0) { + code = workerCode; + } + callback(); + }, + taskName + ); + }, cpusNumber); + tasks.forEach(function (task) { + q.push(task) + }); + + q.drain = function () { + cb(code); + } + } }; module.exports = gulpMultiProcess; diff --git a/package.json b/package.json index bb3477c..8526430 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-multi-process", - "version": "1.2.0", + "version": "1.3.0", "homepage": "http://github.com/juanfran/gulp-multi-process", "repository": "git://github.com/juanfran/gulp-multi-process.git", "author": { @@ -17,12 +17,19 @@ "performance", "child-process", "multi-process", - "threads" + "threads", + "spawn", + "fork", + "gulp", + "parallel" ], "license": "MIT", "devDependencies": { "chai": "^3.4.1", "gulp": "^3.9.0", "mocha": "^2.3.4" + }, + "dependencies": { + "async.queue": "^0.5.2" } } diff --git a/test/gulpfile.js b/test/gulpfile.js index 9e7dbb9..40f3615 100644 --- a/test/gulpfile.js +++ b/test/gulpfile.js @@ -1,3 +1,4 @@ +'use strict'; var gulpMultiProcess = require('../index.js'); var gulp = require('gulp'); @@ -22,3 +23,7 @@ gulp.task('task3', function(cb) { gulp.task('multi', function(cb) { return gulpMultiProcess(['task1', 'task2', 'task3'], cb); }); + +gulp.task('multi-cpus', function (cb) { + return gulpMultiProcess(['task1', 'task2', 'task3'], cb, true); +}); diff --git a/test/main.js b/test/main.js index 03fedd3..49f424d 100644 --- a/test/main.js +++ b/test/main.js @@ -1,3 +1,4 @@ +'use strict'; var exec = require('child_process').exec; var path = require('path'); var chai = require('chai'); @@ -10,8 +11,17 @@ describe('gulp-multi-process', function() { expect(out).to.be.match(/Finished 'task2'/); expect(out).to.be.match(/Finished 'task3'/); expect(out).to.be.match(/Finished 'multi'/); - done(); }); }); + + it('takes into account cpus number', function (done) { + exec('gulp multi-cpus --gulpfile ' + path.join(__dirname, 'gulpfile.js'), function (err, out) { + expect(out).to.be.match(/Finished 'task1'/); + expect(out).to.be.match(/Finished 'task2'/); + expect(out).to.be.match(/Finished 'task3'/); + expect(out).to.be.match(/Finished 'multi-cpus'/); + done(); + }); + }) });