Skip to content

Commit

Permalink
Improved testing. Added docs on subtasks and deferred tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonmoeller committed Jun 6, 2016
1 parent ceff167 commit 3d107b8
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 95 deletions.
71 changes: 66 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function test() {
// test something
}

ygor.task('default', bundle)
ygor
.task('default', bundle)
.task('test', test);
```

Expand Down Expand Up @@ -69,11 +70,12 @@ async function cover() {
// report coverage
}

ygor.task('cover', cover)
ygor
.task('cover', cover)
.task('test', test);
```

Then run it with [esprev](https://github.com/shannonmoeller/esprev) or [babel-node](http://babeljs.io/docs/usage/cli/#babel-node) ([plugin](transform-async-to-generator) required).
Then run it with [esprev](https://github.com/shannonmoeller/esprev) or [babel-node](http://babeljs.io/docs/usage/cli/#babel-node) ([presets](http://babeljs.io/docs/plugins/#official-presets) and [plugin](transform-async-to-generator) required).

$ es make cover
$ babel-node make cover
Expand All @@ -87,9 +89,9 @@ Command-line arguments as parsed by [minimist](http://npm.im/minimist).
### `ygor.task(name, callback) : ygor`

- `name` `{String}` Unique task identifier.
- `callback` `{Function}` Function to run when the task is invoked.
- `callback` `{Function(cli, ygor)}` Function to run when the task is invoked.

Registers a task with Ygor.
Registers a task with Ygor. The callback provided will be executed with `ygor.cli` as the first argument and `ygor` as the second.

### `ygor.error(err) : ygor`

Expand Down Expand Up @@ -133,6 +135,65 @@ ygor.task('bar', function () {
});
```

## Deferred Tasks and Subtasks

If you need to define tasks asynchronously, you may call `ygor()` as a function at a later time.

```js
TaskAPI
.getTasks()
.then(tasks => {
return ygor()
.task('foo', tasks.foo)
.task('bar', tasks.bar);
});
```

You may also call `ygor()` within a task callback to create subtasks.

```js
function childA1() {
console.log('hi from child A1');
}

function childA2() {
console.log('hi from child A2');
}

function childB1() {
console.log('hi from child B1');
}

function childB2() {
console.log('hi from child B2');
}

function parentA() {
// Subtasks
return ygor()
.task('1', childA1)
.task('2', childA2);
}

function parentB() {
// Subtasks
return ygor()
.task('1', childB1)
.task('2', childB2);
}

ygor
.task('a', parentA)
.task('b', parentB);
```

Then execute subtasks by passing the parent task name as the first argument and the child task name as the second.

$ node make a 2
hello from child A2
$ node make b 1
hello from child B1

## That's It?

Ygor doesn't know how to find, edit, or watch files. NPM is his plugin system. He requests that you select the right tools for him. These look good:
Expand Down
62 changes: 34 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
'use strict';

var path = require('path');
var assign = require('object-assign');
var chalk = require('chalk');
var columns = require('cli-columns');
var minimist = require('minimist');

var script = path.basename(process.argv[1]);
var cli = minimist(process.argv.slice(2));

function noop() {}
function noop() {
// no operation
}

function ygor(options) {
options = options || cli;

var promise;
var tasks = Object.create(null);
var isVerbose = options.v || options.verbose;
var localOptions = options || cli;
var isVerbose = localOptions.v || localOptions.verbose;

function sub(options) {
return ygor(options);
function sub(opts) {
return ygor(opts);
}

function error(err) {
Expand All @@ -27,13 +30,13 @@ function ygor(options) {
}

function time(name) {
name = '[' + chalk.green(script) + '] ' + chalk.magenta(name);
var localName = '[' + chalk.green(script) + '] ' + chalk.magenta(name);

console.log(name + '...');
console.time(name);
console.log(localName + '...');
console.time(localName);

return function timeEnd(val) {
console.timeEnd(name);
console.timeEnd(localName);

return val;
};
Expand All @@ -54,47 +57,50 @@ function ygor(options) {
}

function run(name) {
var timeEnd;
var localName = name;
var keys = Object.keys(tasks);

if (!keys.length) {
return;
return null;
}

if (!arguments.length) {
name = options._.shift();
localName = localOptions._.shift();
}

name = name || 'default';
localName = localName || 'default';

if (!(name in tasks)) {
if (!(localName in tasks)) {
console.log(columns(keys));
return;

return null;
}

var timer = isVerbose ? time(name) : noop;
timeEnd = isVerbose ? time(localName) : noop;

return Promise
.resolve(tasks[name](options, sub))
.resolve(tasks[localName](localOptions, sub))
.catch(error)
.then(timer);
.then(timeEnd);
}

sub.cli = cli;
sub.error = error;
sub.time = time;
sub.task = task;
sub.run = run;

var promise = new Promise(function (resolve) {
promise = new Promise(function (resolve) {
process.nextTick(function () {
resolve(run());
});
});

sub.then = promise.then.bind(promise);
sub.catch = promise.catch.bind(promise);
return assign(sub, {
cli: cli,
error: error,
time: time,
task: task,
run: run,

return sub;
then: promise.then.bind(promise),
catch: promise.catch.bind(promise),
});
}

module.exports = ygor();
27 changes: 16 additions & 11 deletions make.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,39 @@

var ygor = require('./index');

function foo() {
function dflt() {
console.log('should run default task');
}

function bar() {
function tst() {
console.log('should run named task');
}

function baz() {
function thrw() {
throw new Error('should throw');
}

function qux() {
function errr() {
ygor.error(new Error('should not throw'));
}

function parent() {
function child() {
function childA() {
console.log('should run sub task');
}

function childB() {
throw new Error('child b should not run');
}

return ygor()
.task('child', child);
.task('childA', childA)
.task('childB', childB);
}

ygor
.task('default', foo)
.task('test', bar)
.task('parent', parent)
.task('throw', baz)
.task('error', qux);
.task('default', dflt)
.task('test', tst)
.task('throw', thrw)
.task('error', errr)
.task('parent', parent);
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
"dependencies": {
"chalk": "^1.1.3",
"cli-columns": "^1.0.6",
"minimist": "^1.2.0"
"minimist": "^1.2.0",
"object-assign": "^4.1.0"
},
"devDependencies": {
"whim": "^3.0.6"
"whim": "^3.0.9"
},
"scripts": {
"coveralls": "whim test report",
Expand Down
Loading

0 comments on commit 3d107b8

Please sign in to comment.