Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
undertaker
Advanced tools
Task registry that allows composition through series/parallel methods.
The undertaker npm package is a customizable task runner built on top of the Orchestrator library. It allows developers to define, execute, and manage tasks in a highly flexible manner. It's particularly useful for setting up build pipelines, automating repetitive tasks, and integrating with other tools and libraries in a development workflow.
Defining and executing tasks
This feature allows developers to define tasks using the `task` method and execute them in sequence or parallel using the `series` and `parallel` methods. The code sample demonstrates how to define a 'clean' task and a 'build' task that runs after the 'clean' task.
const { task, series } = require('undertaker');
task('clean', function(cb) {
// Task to clean the build directory
console.log('Cleaning build directory...');
cb();
});
task('build', series('clean', function(cb) {
// Task to build the project
console.log('Building the project...');
cb();
}));
Custom task registries
Undertaker supports custom task registries, allowing developers to define their own storage and retrieval mechanisms for tasks. This feature is useful for creating reusable task collections or integrating with other systems. The code sample shows how to create a custom registry and define a 'build' task within it.
const { Registry } = require('undertaker');
const defaultRegistry = new Registry();
defaultRegistry.set('build', function(cb) {
// Custom build task
console.log('Custom build task running...');
cb();
});
const { task } = require('undertaker')(defaultRegistry);
task('build');
Gulp is a toolkit for automating painful or time-consuming tasks in your development workflow. It's very similar to undertaker in terms of defining and running tasks, but it also provides a rich ecosystem of plugins for various tasks like minification, compilation, and more. Gulp's API is stream-based, making it efficient for handling files.
Grunt is another JavaScript task runner that offers a wide array of plugin options for automating tasks. Compared to undertaker, Grunt has a more configuration-over-code approach, where tasks and their options are defined in a Gruntfile. It's well-suited for projects that benefit from a highly configurable setup.
Task registry that allows composition through series/parallel methods.
var fs = require('fs');
var Undertaker = require('undertaker');
var taker = new Undertaker();
taker.task('task1', function(cb){
// do things
cb(); // when everything is done
});
taker.task('task2', function(){
return fs.createReadStream('./myFile.js')
.pipe(fs.createWriteStream('./myFile.copy.js'));
});
taker.task('task3', function(){
return new Promise(function(resolve, reject){
// do things
resolve(); // when everything is done
});
});
taker.task('combined', taker.series('task1', 'task2'));
taker.task('all', taker.parallel('combined', 'task3'));
Task functions can be completed in any of the ways supported by
async-done
new Undertaker([registryInstance])
The constructor is used to create a new instance of Undertaker
. Each instance of
Undertaker
gets its own instance of a registry. By default, the registry is an
instance of undertaker-registry
but it can be an instance of any other registry that follows the Custom Registries API.
To use a custom registry, pass a custom registry instance (new CustomRegistry([options])
) when
instantiating a new Undertaker
instance. This will use the custom registry instance for that Undertaker
instance.
task([taskName,] fn)
Both a getter
and setter
for tasks.
If a string (taskName
) is given as the only argument, it behaves as a getter
and returns the wrapped task (not the original function). The wrapped task has a unwrap
method that will return the original function.
If a function (fn
) and optionally a string (taskName
) is given, it behaves as
a setter
and will register the task by the taskName
. If taskName
is not
specified, the name
or displayName
property of the function is used as the taskName
.
Will throw if:
getter
: taskName
is missing or not a string.setter
: taskName
is missing and fn
is anonymous.setter
: fn
is missing or not a function.series(taskName || fn...)
Takes a variable amount of strings (taskName
) and/or functions (fn
) and
returns a function of the composed tasks or functions. Any taskNames
are
retrieved from the registry using the get
method.
When the returned function is executed, the tasks or functions will be executed in series, each waiting for the prior to finish. If an error occurs, execution will stop.
parallel(taskName || fn...)
Takes a variable amount of strings (taskName
) and/or functions (fn
) and
returns a function of the composed tasks or functions. Any taskNames
are
retrieved from the registry using the get
method.
When the returned function is executed, the tasks or functions will be executed in parallel, all being executed at the same time. If an error occurs, all execution will complete.
registry([registryInstance])
Optionally takes an instantiated registry object. If no arguments are passed, returns
the current registry object. If an instance of a registry (customRegistry
) is passed
the tasks from the current registry will be transferred to it and the current registry
will be replaced with the new registry.
The ability to assign new registries will allow you to pre-define/share tasks or add custom functionality to your registries. See Custom Registries for more information.
tree([options])
Optionally takes an options
object and returns an object representing the
tree of registered tasks. The object returned is archy
compatible. Also, each node has a type
property that can be used to determine if the node is a task
or function
.
options
options.deep
Whether or not the whole tree should be returned.
Type: Boolean
Default: false
lastRun(task, [precision])
Takes a string or function (task
) and returns a timestamp of the last time the task
was run successfully. The time will be the time the task started.
Returns undefined
if the task has not been run.
If a task errors, the result of lastRun
will be undefined because the task
should probably be re-run from scratch to get into a good state again.
The timestamp is always given in millisecond but the time resolution can be
rounded using the precision
parameter. The use case is to be able to compare a build time
to a file time attribute.
Assuming undertakerInst.lastRun('someTask')
returns 1426000001111
,
undertakerInst.lastRun('someTask', 1000)
returns 1426000001000
.
The default time resolution is 1
.
Custom registries are constructor functions allowing you to pre-define/share tasks or add custom functionality to your registries.
A registry's prototype should define:
init(taker)
: receives the undertaker instance to set pre-defined tasks using the task(taskName, fn)
method.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.You should not call these functions yourself; leave that to Undertaker, so it can keep its metadata consistent.
The easiest way to create a custom registry is to inherit from undertaker-registry:
var util = require('util');
var DefaultRegistry = require('undertaker-registry');
function MyRegistry(){
DefaultRegistry.call(this);
}
util.inherits(MyRegistry, DefaultRegistry);
module.exports = MyRegistry;
To share common tasks with all your projects, you can expose an init
method on the registry
prototype and it will receive the Undertaker
instance as the only argument. You can then use
undertaker.task(name, fn)
to register pre-defined tasks.
For example you might want to share a clean
task:
var fs = require('fs');
var util = require('util');
var DefaultRegistry = require('undertaker-registry');
var del = require('del');
function CommonRegistry(opts){
DefaultRegistry.call(this);
opts = opts || {};
this.buildDir = opts.buildDir || './build';
}
util.inherits(CommonRegistry, DefaultRegistry);
CommonRegistry.prototype.init = function(takerInst){
var buildDir = this.buildDir;
var exists = fs.existsSync(buildDir);
if(exists){
throw new Error('Cannot initialize common tasks. ' + buildDir + ' directory exists.');
}
takerInst.task('clean', function(){
return del([buildDir]);
});
}
module.exports = CommonRegistry;
Then to use it in a project:
var Undertaker = require('undertaker');
var CommonRegistry = require('myorg-common-tasks');
var taker = new Undertaker(CommonRegistry({ buildDir: '/dist' }));
taker.task('build', taker.series('clean', function build(cb) {
// do things
cb();
}));
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 that data. Be sure to return the altered task, as per the description of registry methods above:
var util = require('util');
var Undertaker = require('undertaker');
var DefaultRegistry = require('undertaker-registry');
// Some task defined somewhere else
var BuildRegistry = require('./build.js');
var ServeRegistry = require('./serve.js');
function ConfigRegistry(config){
DefaultRegistry.call(this);
this.config = config;
}
util.inherits(ConfigRegistry, DefaultRegistry);
ConfigRegistry.prototype.set = function set(name, fn) {
// The `DefaultRegistry` uses `this._tasks` for storage.
var task = this._tasks[name] = fn.bind(this.config);
return task;
};
var taker = new Undertaker();
taker.registry(new BuildRegistry());
taker.registry(new ServeRegistry());
// `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(cb) {
console.log('Server bind to ' + this.bindTo);
console.log('Serving' + this.build);
cb();
}));
MIT
2.0.0 (2024-03-22)
FAQs
Task registry that allows composition through series/parallel methods.
The npm package undertaker receives a total of 1,082,646 weekly downloads. As such, undertaker popularity was classified as popular.
We found that undertaker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.