Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc for registry #12

Merged
merged 1 commit into from
Feb 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 122 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,128 @@ property that can be used to determine if the node is a `task` or `function`.

## Custom Registries

Coming Soon...
Custom registries are constructor allowing you to pre-define/share tasks
or add custom functionality to your registries.

A registry's prototype should define:

- `get(taskName)`: returns the task with that name
or `undefined` if no task is registered with that name.
- `set(taskName, fn)`: add task to the registry. if `set` modifies a task, it should return the new task.
- `tasks()`: returns an object listing all tasks in the registry.

The easiest way to create a custom registry is to inherits from
[undertaker-registry](https://www.npmjs.com/package/undertaker-registry):

```javascript
var util = require('util');

var DefaultRegistry = require('undertaker-registry');

function MyRegistry(){
DefaultRegistry.call(this);
}

util.inherits(MyRegistry, DefaultRegistry);

module.exports = MyRegistry;
```

### Sharing tasks

To share common tasks with all your projects, you can set a registry with those
task pre-defined. For example you might want to share a `clean` task:

```javascript
var fs = require('fs');
var util = require('util');

var DefaultRegistry = require('undertaker-registry');
var del = require('del');

function CommonRegistry(){
DefaultRegistry.call(this);

var buildDir = './build';
var exists = fs.existsSync(buildDir);

if(exists){
throw new Error('Cannot initialize common tasks. `build/` directory exists.');
}

this.set('clean', function(cb){
del([buildDir], cb);
});
}

util.inherits(CommonRegistry, DefaultRegistry);

module.exports = CommonRegistry;
```

Then to use it in any of a project:
```javascript
var Undertaker = require('undertaker');
var CommonRegistry = require('myorg-common-tasks');

var taker = new Undertaker(CommonRegistry);

taker.task('build', taker.series('clean', function build(cb) {
// do things
cb();
}));
```


### Sharing Functionalities

By controlling how tasks are added to the registry, you can decorate them.

For example if you wanted all tasks to share some data, you can use a custom registry
to bind them to those data:

```javascript
var util = require('util');

var Undertaker = require('undertaker');
var DefaultRegistry = require('undertaker-registry');

// Some task defined somewhere else
var BuildRegistery = require('./build.js');
var ServeRegistery = require('./serve.js');


function ConfigRegistry(config){
DefaultRegistry.call(this);
this.config = config;
}

util.inherits(ConfigRegistry, DefaultRegistry);

ConfigRegistry.prototype.set = function set(name, fn) {
var task = this._tasks[name] = fn.bind(this.config);
return task;
};

var taker = new Undertaker();

taker.registry(new BuildRegistery());
taker.registry(new ServeRegistery());

// `taker.registry` will reset each task in the registry with
// `ConfigRegistry.prototype.set` which will bind them to the config object.
taker.registry(new ConfigRegistry({
src: './src',
build: './build',
bindTo: '0.0.0.0:8888'
}));

taker.task('default', taker.series('clean', 'build', 'serve', function default(cb) {
console.log('Server bind to ' + this.bindTo);
console.log('Serving' + this.build);
cb();
}))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phated is that example works? I am not sure how the tasks get run. It seems Undertaker.prototype.get returns the original function, not the wrapped one.

[...]
taker.task('default', function(cb) {
  console.log('Server bind to ' + this.bindTo);
  console.log('Serving' + this.build);
  cb();
});

var noop = function(){};
tasker.get('default')(noop);
// Server bind to undefined
// Serving undefined
tasker._registry.get('default')(noop);
// Server bind to 0.0.0.0:8888
// Serving './build'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nerver mind

tasker.parallel(['default])(noop);
// Server bind to 0.0.0.0:8888
// Serving ./build

```

## License

Expand Down