This document describes how to create, register and run updaters.
- TODO
- What is an updater?
- Creating updaters
- Registering updaters
- Running updaters
- Resolving updaters
- Discovering updaters
- Default updater
- document updater args
- explain how the
base
instance works - document
env
Updaters are plugins that are registered by name. If you're not familiar with plugins yet, it might be a good idea to review the plugins docs first.
The primary difference between "updaters" and "plugins" is how they're registered, but there are a few other minor differences:
Plugin | Updater | |
---|---|---|
Registered with | .use method | .register method or .updater method |
Instance | Loaded onto "current" Update instance |
A new Update() instance is created for every updater registered |
Invoked | Immediately | .register deferred (lazy), .updater immediately |
Run using | .run: all plugins are run at once | .update : only specified plugin(s) are run |
An updater function takes an instance of Update
as the first argument.
Example
function updater(app) {
// do updater stuff
}
Updaters may be registered using either of the following methods:
.register
: if the plugin should not be invoked until it's called by.update
(stays lazy while it's cached, this is preferred).updater
: if the plugin needs to be invoked immediately when registered
Register an updater function with the given name
using the .register
method.
Example
var update = require('update');
var app = update();
function updater(app) {
// do updater stuff when the updater is run with the `.update` method.
console.log('foo is being run');
}
// register as an updater with the `.register` method
app.register('foo', updater);
// run the `foo` updater with the `.update` method
app.update('foo', function(err) {
if (err) return console.log(err);
});
//=> "foo is being run"
Register an updater function with the given name
using the .updater
method.
Example
var update = require('update');
var app = update();
function updater(app) {
// do updater stuff when the updater is registered
console.log('foo is being registered');
}
// register as an updater using `.updater`
app.updater('foo', updater);
//=> "foo is being registered"
Should I use .updater
or .register
?
In general, it's recommended that you use the .register
method. In most cases update is smart enough to figure out when to invoke updater functions.
However, there are always exceptions. If you create custom code and notice that update can't find the information it needs. Try using the .updater
method to invoke the function when the updater is registered.
Updaters and their tasks can be run by command line or API.
Command line
To run globally or locally installed updater-foo
, or an updater named foo
in updatefile.js
, run:
$ update foo
API
var update = require('update');
var app = update();
function fn() {
// do updater stuff
}
// the `.register` method does not invoke the updater
app.register('foo', fn);
// the `.updater` method invokes the updater immediately
app.updater('bar', fn);
// run updaters foo and bar in series (both updaters will be invoked)
app.update(['foo', 'bar'], function(err) {
if (err) return console.log(err);
});
Updaters can be published to npm and installed globally or locally. But there is no requirement that updaters must be published. You can also create custom updaters and register using the .register or .updater methods.
This provides a great deal of flexibility, but it also means that we need a strategy for finding updaters when update
is run from the command line.
- When both a task and an updater have the same name on the same instance, Update will always try to run the task first (this is unlikely to happen unless you intend for it to - there are reasons to do this)
Since the .build method only runs tasks, you can use this to your advantage by aliasing sub-generators with tasks.
Don't do this
module.exports = function(app) {
app.register('foo', function(foo) {
foo.task('default', function(cb) {
// do task stuff
cb();
});
});
// `.build` doesn't run updaters
app.build('foo', function(err) {
if (err) return console.log(err);
});
};
Do this
module.exports = function(app) {
app.register('foo', function(foo) {
foo.task('default', function(cb) {
// do task stuff
cb();
});
});
// `.update` will run updater `foo`
app.update('foo', function(err) {
if (err) return console.log(err);
});
};
Or this
module.exports = function(app) {
app.register('foo', function(foo) {
foo.task('default', function(cb) {
// do task stuff
cb();
});
});
app.task('foo', function(cb) {
app.update('foo', cb);
});
// `.build` will run task `foo`, which runs updater `foo`
app.build('foo', function(err) {
if (err) return console.log(err);
});
};
When the command line is used, Update's CLI resolves updaters in the following order:
- default updater: attempts to match given names to updaters and tasks registered on the
default
updater - built-in updaters: attempts to match given names to Update's built-in updaters
- locally installed updaters
- globally installed updaters
todo
If an updater is registered with the name default
it will receive special treatment from Update and Update's CLI. More specifically, when Update's CLI looks for updaters or tasks to run, it will search for them on the default
updater first.
There is a catch...
Registering the "default" updater
The only way to register a default
updater is by creating an updatefile.js in the current working directory.
When used by command line, Update's CLI will then use node's require()
system to get the function exported by updatefile.js
and use it as the default
updater.
Docs