Gathering detailed insights and metrics for undertaker
Gathering detailed insights and metrics for undertaker
Gathering detailed insights and metrics for undertaker
Gathering detailed insights and metrics for undertaker
Task registry that allows composition through series/parallel methods.
npm install undertaker
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
201 Stars
111 Commits
31 Forks
13 Watching
4 Branches
19 Contributors
Updated on 22 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-4.7%
289,906
Compared to previous day
Last week
4.6%
1,564,250
Compared to previous week
Last month
15.6%
6,179,854
Compared to previous month
Last year
7.4%
64,583,974
Compared to previous year
Task registry that allows composition through series/parallel methods.
1var fs = require('fs'); 2var Undertaker = require('undertaker'); 3 4var taker = new Undertaker(); 5 6taker.task('task1', function(cb){ 7 // do things 8 9 cb(); // when everything is done 10}); 11 12taker.task('task2', function(){ 13 return fs.createReadStream('./myFile.js') 14 .pipe(fs.createWriteStream('./myFile.copy.js')); 15}); 16 17taker.task('task3', function(){ 18 return new Promise(function(resolve, reject){ 19 // do things 20 21 resolve(); // when everything is done 22 }); 23}); 24 25taker.task('combined', taker.series('task1', 'task2')); 26 27taker.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:
1var util = require('util'); 2 3var DefaultRegistry = require('undertaker-registry'); 4 5function MyRegistry(){ 6 DefaultRegistry.call(this); 7} 8 9util.inherits(MyRegistry, DefaultRegistry); 10 11module.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:
1var fs = require('fs'); 2var util = require('util'); 3 4var DefaultRegistry = require('undertaker-registry'); 5var del = require('del'); 6 7function CommonRegistry(opts){ 8 DefaultRegistry.call(this); 9 10 opts = opts || {}; 11 12 this.buildDir = opts.buildDir || './build'; 13} 14 15util.inherits(CommonRegistry, DefaultRegistry); 16 17CommonRegistry.prototype.init = function(takerInst){ 18 var buildDir = this.buildDir; 19 var exists = fs.existsSync(buildDir); 20 21 if(exists){ 22 throw new Error('Cannot initialize common tasks. ' + buildDir + ' directory exists.'); 23 } 24 25 takerInst.task('clean', function(){ 26 return del([buildDir]); 27 }); 28} 29 30module.exports = CommonRegistry;
Then to use it in a project:
1var Undertaker = require('undertaker'); 2var CommonRegistry = require('myorg-common-tasks'); 3 4var taker = new Undertaker(CommonRegistry({ buildDir: '/dist' })); 5 6taker.task('build', taker.series('clean', function build(cb) { 7 // do things 8 cb(); 9}));
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:
1var util = require('util'); 2 3var Undertaker = require('undertaker'); 4var DefaultRegistry = require('undertaker-registry'); 5 6// Some task defined somewhere else 7var BuildRegistry = require('./build.js'); 8var ServeRegistry = require('./serve.js'); 9 10function ConfigRegistry(config){ 11 DefaultRegistry.call(this); 12 this.config = config; 13} 14 15util.inherits(ConfigRegistry, DefaultRegistry); 16 17ConfigRegistry.prototype.set = function set(name, fn) { 18 // The `DefaultRegistry` uses `this._tasks` for storage. 19 var task = this._tasks[name] = fn.bind(this.config); 20 return task; 21}; 22 23var taker = new Undertaker(); 24 25taker.registry(new BuildRegistry()); 26taker.registry(new ServeRegistry()); 27 28// `taker.registry` will reset each task in the registry with 29// `ConfigRegistry.prototype.set` which will bind them to the config object. 30taker.registry(new ConfigRegistry({ 31 src: './src', 32 build: './build', 33 bindTo: '0.0.0.0:8888' 34})); 35 36taker.task('default', taker.series('clean', 'build', 'serve', function(cb) { 37 console.log('Server bind to ' + this.bindTo); 38 console.log('Serving' + this.build); 39 cb(); 40}));
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 9/29 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More