Skip to content

Commit

Permalink
Add each(), update test specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Mar 19, 2016
1 parent 07a5104 commit f4ae11a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
50 changes: 50 additions & 0 deletions runner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
var ast = require('mkast')
, out = require('mkout')
, MAIN = 'main';

/**
* Get a task runner.
*
Expand All @@ -19,8 +23,13 @@ function runner(opts) {
* @option {Object} task collection of tasks.
*/
function Runner(task) {
this.task = task;

this.tasks = task.tasks;
this.scope = task.scope || {};

this.scope.ast = this.scope.ast || ast;
this.scope.out = this.scope.out || out;
}

/**
Expand Down Expand Up @@ -164,9 +173,50 @@ function exec(id, cb) {
return task;
}

/**
* Execute a list of tasks to by string identifiers.
*
* @function each
* @member Runner
*
* @param {Array} [names] list of task names.
* @param {Function} cb callback function.
*/
function each(names, cb) {
if(names instanceof Function) {
cb = names;
names = null;
}

if(!names) {
names = this.tasks.map(function(task) {
return task.id;
})
}

var scope = this;
names = names.slice();

function next(err) {
if(err) {
return cb(err);
}
var id = names.shift();
if(!id) {
return cb();
}
scope.exec(id, next);
}

next();
}

Runner.prototype.get = get;
Runner.prototype.exec = exec;
Runner.prototype.series = series;
Runner.prototype.parallel = parallel;
Runner.prototype.each = each;

Runner.prototype.MAIN = MAIN;

module.exports = runner;
12 changes: 12 additions & 0 deletions test/fixtures/pipeline-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var mk = require('../../index');

function readme(cb) {
doc('doc/readme.md')
//.pipe(this.pi())
//.pipe(this.msg())
//.pipe(this.ref())
.pipe(mk.out('target/pipeline-task.js'))
.once('finish', cb);
}

mk.task(readme);
2 changes: 2 additions & 0 deletions test/spec/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ describe('mktask:', function() {

function readme(cb) {
called++;
expect(this.ast).to.be.an('object');
expect(this.out).to.be.a('function');
cb();
}

Expand Down

0 comments on commit f4ae11a

Please sign in to comment.