diff --git a/README.md b/README.md index d9788b7..8c1e630 100644 --- a/README.md +++ b/README.md @@ -18,16 +18,26 @@ For the command line interface install [mkdoc][] globally (`npm i -g mkdoc`). ## API -### task +### mk ```javascript -task() +mk() ``` Creates a task collection. Returns a Task. +### #task + +```javascript +static task() +``` + +Adds a task to the default task collection. + +Returns a Task. + ### Task ```javascript @@ -47,14 +57,37 @@ Adds task function(s) to the list of known tasks. ### .run ```javascript -Task.prototype.run([opts]) +Task.prototype.run() ``` Gets a task runner for this collection of tasks. Returns a task Runner. -* `opts` processing options. +### #src + +```javascript +static src() +``` + +Parses a markdown string into a stream. + +Returns the output stream. + +### #dest + +```javascript +static dest([file]) +``` + +Get a destination output stream. + +If the file option is not given a destination stream that prints to +stdout is returned. + +Returns an output stream. + +* `file` String path to the output file. ### runner @@ -80,8 +113,7 @@ Execute task functions. #### Options -* `list` Array of tasks. -* `scope` Object task execution scope. +* `task` Object collection of tasks. ### .get @@ -95,6 +127,29 @@ Returns a task or undefined. * `id` Function|String task identifier. +### .series + +```javascript +Runner.prototype.series(list, cb) +``` + +Execute task functions in series. + +* `list` Array of task functions. +* `cb` Function callback function. + +### .parallel + +```javascript +Runner.prototype.parallel(list[, concurrent], cb) +``` + +Execute task functions in parallel. + +* `list` Array of task functions. +* `concurrent` Number number of concurrent calls. +* `cb` Function callback function. + ### .exec ```javascript @@ -103,11 +158,24 @@ Runner.prototype.exec(id, cb) Execute a task by name identifier. +Dependencies are run in parallel before task execution. + Returns a task or undefined. * `id` Function|String task identifier. * `cb` Function callback function. +### .each + +```javascript +Runner.prototype.each([names], cb) +``` + +Execute a list of tasks to by string identifiers. + +* `names` Array list of task names. +* `cb` Function callback function. + ## License MIT diff --git a/index.js b/index.js index 85535c5..032957f 100644 --- a/index.js +++ b/index.js @@ -79,7 +79,7 @@ function add() { } if(!args.length) { - throw new TypeError('task function(s) expected'); + throw new TypeError('not enough arguments for task()'); } function gather(args) { @@ -126,10 +126,13 @@ Task.prototype.run = run; mk.src = ast.src; /** - * Get a file write output stream. + * Get a destination output stream. + * + * If the file option is not given a destination stream that prints to + * stdout is returned. * * @static {function} dest - * @param {String} file path to the output file. + * @param {String} [file] path to the output file. * * @returns an output stream. */ diff --git a/runner.js b/runner.js index ee0fdaa..088aa17 100644 --- a/runner.js +++ b/runner.js @@ -114,10 +114,12 @@ function parallel(list, concurrent, cb) { concurrent = concurrent || items.length; function run() { + if(complete) { + items = items.slice(concurrent); + } for(var i = 0;i < concurrent;i++) { items[i].call(scope, next); } - items = items.slice(concurrent); } function next(err) { diff --git a/test/spec/defaults.js b/test/spec/defaults.js new file mode 100644 index 0000000..9c83141 --- /dev/null +++ b/test/spec/defaults.js @@ -0,0 +1,13 @@ +var expect = require('chai').expect + , mktask = require('../../index'); + +describe('mktask:', function() { + + it('should get default task collection', function(done) { + // static task() method + var res = mktask.task(); + expect(res).to.be.an('object'); + done(); + }); + +}); diff --git a/test/spec/dest.js b/test/spec/dest.js new file mode 100644 index 0000000..6480d4f --- /dev/null +++ b/test/spec/dest.js @@ -0,0 +1,18 @@ +var expect = require('chai').expect + , mktask = require('../../index'); + +describe('mktask:', function() { + + it('should get file write destination stream', function(done) { + var res = mktask.dest('target/foo.txt'); + expect(res).to.be.an('object'); + done(); + }); + + it('should get default destination stream', function(done) { + var res = mktask.dest(); + expect(res).to.be.an('object'); + done(); + }); + +}); diff --git a/test/spec/each.js b/test/spec/each.js new file mode 100644 index 0000000..d95ab87 --- /dev/null +++ b/test/spec/each.js @@ -0,0 +1,48 @@ +var expect = require('chai').expect + , mktask = require('../../index'); + +describe('mktask:', function() { + + it('should iterate task function (each)', function(done) { + var mk = mktask() + , called = 0; + + function readme(cb) { + called++; + cb(); + } + + mk.task(readme); + + var runner = mk.run(); + runner.each([readme.name], function() { + expect(called).to.eql(1); + done(); + }); + }); + + it('should iterate all task functions (each)', function(done) { + var mk = mktask() + , called = 0; + + function api(cb) { + called++; + cb(); + } + + function readme(cb) { + called++; + cb(); + } + + mk.task(api); + mk.task(readme); + + var runner = mk.run(); + runner.each(function() { + expect(called).to.eql(2); + done(); + }); + }); + +}); diff --git a/test/spec/run.js b/test/spec/exec.js similarity index 61% rename from test/spec/run.js rename to test/spec/exec.js index 1a9370c..b8b3ce6 100644 --- a/test/spec/run.js +++ b/test/spec/exec.js @@ -3,23 +3,6 @@ var expect = require('chai').expect describe('mktask:', function() { - it('should get task from runner', function(done) { - var mk = mktask() - , readme = function readme(){} - , res = mk.task(readme) - , runner = mk.run(); - - expect(res).to.be.an('object'); - expect(runner).to.be.an('object'); - - expect(runner.get(readme)).to.eql(res); - expect(runner.get(readme.name)).to.eql(res); - - expect(runner.get('foo')).to.eql(undefined); - - done(); - }); - it('should return task from runner (exec)', function(done) { var mk = mktask() , readme = function readme(){} @@ -87,43 +70,6 @@ describe('mktask:', function() { }); }); - it('should exec task functions dependencies in parallel', function(done) { - var mk = mktask() - , called = 0 - , docsCalled - , apiCalled - , readmeCalled; - - function docs(cb) { - called++; - docsCalled = 1; - cb(); - } - - function api(cb) { - called++; - apiCalled = 1; - cb(); - } - - function readme(cb) { - called++; - readmeCalled = 1; - cb(); - } - - mk.task([docs, api], readme); - - var runner = mk.run(); - runner.exec(readme, function() { - expect(called).to.eql(3); - expect(docsCalled).to.eql(1); - expect(apiCalled).to.eql(1); - expect(readmeCalled).to.eql(1); - done(); - }); - }); - it('should callback with error on task function exec', function(done) { var mk = mktask() diff --git a/test/spec/parallel.js b/test/spec/parallel.js new file mode 100644 index 0000000..d707147 --- /dev/null +++ b/test/spec/parallel.js @@ -0,0 +1,84 @@ +var expect = require('chai').expect + , mktask = require('../../index'); + +describe('mktask:', function() { + + //it('should exec task functions dependencies in parallel', + //function(done) { + //var mk = mktask() + //, called = 0 + //, docsCalled + //, apiCalled + //, readmeCalled; + + //function docs(cb) { + //called++; + //docsCalled = 1; + //cb(); + //} + + //function api(cb) { + //called++; + //apiCalled = 1; + //cb(); + //} + + //function readme(cb) { + //called++; + //readmeCalled = 1; + //cb(); + //} + + //mk.task([docs, api], readme); + + //var runner = mk.run(); + //runner.exec(readme, function() { + //expect(called).to.eql(3); + //expect(docsCalled).to.eql(1); + //expect(apiCalled).to.eql(1); + //expect(readmeCalled).to.eql(1); + //done(); + //}); + //} + //); + + it('should exec task functions dependencies in parallel w/ concurrent', + function(done) { + var mk = mktask() + , called = 0 + , docsCalled + , apiCalled + , readmeCalled; + + function docs(cb) { + called++; + docsCalled = 1; + cb(); + } + + function api(cb) { + called++; + apiCalled = 1; + cb(); + } + + function readme(cb) { + called++; + readmeCalled = 1; + cb(); + } + + mk.task([docs, api], readme); + + var runner = mk.run(); + runner.parallel([docs, api, readme], 1, function() { + expect(called).to.eql(3); + expect(docsCalled).to.eql(1); + expect(apiCalled).to.eql(1); + expect(readmeCalled).to.eql(1); + done(); + }); + } + ); + +}); diff --git a/test/spec/runner.js b/test/spec/runner.js new file mode 100644 index 0000000..4a32352 --- /dev/null +++ b/test/spec/runner.js @@ -0,0 +1,23 @@ +var expect = require('chai').expect + , mktask = require('../../index'); + +describe('mktask:', function() { + + it('should get task from runner', function(done) { + var mk = mktask() + , readme = function readme(){} + , res = mk.task(readme) + , runner = mk.run(); + + expect(res).to.be.an('object'); + expect(runner).to.be.an('object'); + + expect(runner.get(readme)).to.eql(res); + expect(runner.get(readme.name)).to.eql(res); + + expect(runner.get('foo')).to.eql(undefined); + + done(); + }); + +}); diff --git a/test/spec/task.js b/test/spec/task.js index 27b04a6..f0e0451 100644 --- a/test/spec/task.js +++ b/test/spec/task.js @@ -95,5 +95,4 @@ describe('mktask:', function() { done(); }); - });