package/package.json000644 0000003064 3560116604 011551 0ustar00000000 000000 { "name": "undertaker", "version": "1.2.1", "description": "Task registry that allows composition through series/parallel methods.", "author": "Gulp Team (http://gulpjs.com/)", "contributors": [ "Blaine Bublitz ", "Damien Lebrun " ], "repository": "gulpjs/undertaker", "license": "MIT", "engines": { "node": ">= 0.10" }, "main": "index.js", "files": [ "LICENSE", "index.js", "lib" ], "scripts": { "lint": "eslint . && jscs index.js lib/ test/", "pretest": "npm run lint", "test": "mocha --async-only", "cover": "istanbul cover _mocha --report lcovonly", "coveralls": "npm run cover && istanbul-coveralls" }, "dependencies": { "arr-flatten": "^1.0.1", "arr-map": "^2.0.0", "bach": "^1.0.0", "collection-map": "^1.0.0", "es6-weak-map": "^2.0.1", "last-run": "^1.1.0", "object.defaults": "^1.0.0", "object.reduce": "^1.0.0", "undertaker-registry": "^1.0.0" }, "devDependencies": { "async-once": "^1.0.0", "del": "^2.0.2", "eslint": "^1.7.3", "eslint-config-gulp": "^2.0.0", "expect": "^1.19.0", "gulp-jshint": "^1.8.4", "istanbul": "^0.4.3", "istanbul-coveralls": "^1.0.3", "jscs": "^2.3.5", "jscs-preset-gulp": "^1.0.0", "mocha": "^2.4.5", "once": "^1.3.1", "through2": "^2.0.0", "undertaker-common-tasks": "^1.0.0", "undertaker-task-metadata": "^1.0.0", "vinyl-fs": "^2.2.0" }, "keywords": [ "registry", "runner", "task" ] } package/index.js000644 0000002120 3560116604 010720 0ustar00000000 000000 'use strict'; var inherits = require('util').inherits; var EventEmitter = require('events').EventEmitter; var DefaultRegistry = require('undertaker-registry'); var tree = require('./lib/tree'); var task = require('./lib/task'); var series = require('./lib/series'); var lastRun = require('./lib/last-run'); var parallel = require('./lib/parallel'); var registry = require('./lib/registry'); var _getTask = require('./lib/get-task'); var _setTask = require('./lib/set-task'); function Undertaker(customRegistry) { EventEmitter.call(this); this._registry = new DefaultRegistry(); if (customRegistry) { this.registry(customRegistry); } this._settle = (process.env.UNDERTAKER_SETTLE === 'true'); } inherits(Undertaker, EventEmitter); Undertaker.prototype.tree = tree; Undertaker.prototype.task = task; Undertaker.prototype.series = series; Undertaker.prototype.lastRun = lastRun; Undertaker.prototype.parallel = parallel; Undertaker.prototype.registry = registry; Undertaker.prototype._getTask = _getTask; Undertaker.prototype._setTask = _setTask; module.exports = Undertaker; package/LICENSE000644 0000002142 3560116604 010264 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2014 Blaine Bublitz, Eric Schoffstall and other contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/README.md000644 0000026002 3560116604 010537 0ustar00000000 000000

# undertaker [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url] Task registry that allows composition through `series`/`parallel` methods. ## Usage ```js 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')); ``` ## API __Task functions can be completed in any of the ways supported by [`async-done`][async-resolution]__ ### `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`][undertaker-registry] but it can be an instance of any other registry that follows the [Custom Registries API][custom-registries]. 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: * As a `getter`: `taskName` is missing or not a string. * As a `setter`: `taskName` is missing and `fn` is anonymous. * As a `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][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`][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. On node v0.10 or with file system like HFS or FAT, `fs.stat` time attributes like `mtime` precision is one second. Assuming `undertakerInst.lastRun('someTask')` returns `1426000001111`, `undertakerInst.lastRun('someTask', 1000)` returns `1426000001000`. The default time resolution is `1000` on node v0.10, `0` on node 0.11+ but it can be overwritten using `UNDERTAKER_TIME_RESOLUTION` environment variable. ## Custom Registries 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]: ```js 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 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: ```js 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: ```js 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(); })); ``` ### 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 that data. Be sure to return the altered task, as per the description of registry methods above: ```js 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(); })); ``` ### In the wild * [undertaker-registry] - Custom registries probably want to inherit from this. * [undertaker-forward-reference] - Custom registry supporting forward referenced tasks (similar to gulp 3.x). * [undertaker-task-metadata] - Proof-of-concept custom registry that attaches metadata to each task. * [undertaker-common-tasks] - Proof-of-concept custom registry that pre-defines some tasks. * [alchemist-gulp] - A default set of tasks for building alchemist plugins. * [gulp-hub] - Custom registry to run tasks in multiple gulpfiles. (In a branch as of this writing) * [gulp-pipeline] - [RailsRegistry][rails-registry] is an ES2015 class that provides a gulp pipeline replacement for rails applications ## License MIT [custom-registries]: #custom-registries [async-resolution]: https://github.com/phated/async-done#completion-and-error-resolution [archy]: https://www.npmjs.org/package/archy [undertaker-registry]: https://github.com/gulpjs/undertaker-registry [undertaker-forward-reference]: https://github.com/gulpjs/undertaker-forward-reference [undertaker-task-metadata]: https://github.com/gulpjs/undertaker-task-metadata [undertaker-common-tasks]: https://github.com/gulpjs/undertaker-common-tasks [alchemist-gulp]: https://github.com/webdesserts/alchemist-gulp [gulp-hub]: https://github.com/frankwallis/gulp-hub/tree/registry-init [gulp-pipeline]: https://github.com/alienfast/gulp-pipeline [rails-registry]: https://github.com/alienfast/gulp-pipeline/blob/master/src/registry/railsRegistry.js [downloads-image]: http://img.shields.io/npm/dm/undertaker.svg [npm-url]: https://www.npmjs.com/package/undertaker [npm-image]: http://img.shields.io/npm/v/undertaker.svg [travis-url]: https://travis-ci.org/gulpjs/undertaker [travis-image]: http://img.shields.io/travis/gulpjs/undertaker.svg?label=travis-ci [appveyor-url]: https://ci.appveyor.com/project/gulpjs/undertaker [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/undertaker.svg?label=appveyor [coveralls-url]: https://coveralls.io/r/gulpjs/undertaker [coveralls-image]: http://img.shields.io/coveralls/gulpjs/undertaker/master.svg [gitter-url]: https://gitter.im/gulpjs/gulp [gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg package/lib/get-task.js000644 0000000140 3560116604 012076 0ustar00000000 000000 'use strict'; function get(name) { return this._registry.get(name); } module.exports = get; package/lib/helpers/buildTree.js000644 0000001007 3560116604 013743 0ustar00000000 000000 'use strict'; var map = require('collection-map'); var metadata = require('./metadata'); function buildTree(tasks) { return map(tasks, function(task) { var meta = metadata.get(task); if (meta) { return meta.tree; } var name = task.displayName || task.name || ''; meta = { name: name, tree: { label: name, type: 'function', nodes: [], }, }; metadata.set(task, meta); return meta.tree; }); } module.exports = buildTree; package/lib/helpers/createExtensions.js000644 0000003201 3560116604 015345 0ustar00000000 000000 'use strict'; var captureLastRun = require('last-run').capture; var releaseLastRun = require('last-run').release; var metadata = require('./metadata'); var uid = 0; function Storage(fn) { var meta = metadata.get(fn); this.fn = meta.orig || fn; this.uid = uid++; this.name = meta.name; this.branch = meta.branch || false; this.captureTime = Date.now(); this.startHr = []; } Storage.prototype.capture = function() { captureLastRun(this.fn, this.captureTime); }; Storage.prototype.release = function() { releaseLastRun(this.fn); }; function createExtensions(ee) { return { create: function(fn) { return new Storage(fn); }, before: function(storage) { storage.startHr = process.hrtime(); ee.emit('start', { uid: storage.uid, name: storage.name, branch: storage.branch, time: Date.now(), }); }, after: function(result, storage) { if (result && result.state === 'error') { return this.error(result.value, storage); } storage.capture(); ee.emit('stop', { uid: storage.uid, name: storage.name, branch: storage.branch, duration: process.hrtime(storage.startHr), time: Date.now(), }); }, error: function(error, storage) { if (Array.isArray(error)) { error = error[0]; } storage.release(); ee.emit('error', { uid: storage.uid, name: storage.name, branch: storage.branch, error: error, duration: process.hrtime(storage.startHr), time: Date.now(), }); }, }; } module.exports = createExtensions; package/lib/helpers/metadata.js000644 0000000206 3560116604 013604 0ustar00000000 000000 'use strict'; // WeakMap for storing metadata var WM = require('es6-weak-map'); var metadata = new WM(); module.exports = metadata; package/lib/helpers/normalizeArgs.js000644 0000001057 3560116604 014646 0ustar00000000 000000 'use strict'; var assert = require('assert'); var map = require('arr-map'); var flatten = require('arr-flatten'); function normalizeArgs(registry, args) { function getFunction(task) { if (typeof task === 'function') { return task; } var fn = registry.get(task); assert(fn, 'Task never defined: ' + task); return fn; } var flattenArgs = flatten(args); assert(flattenArgs.length, 'One or more tasks should be combined using series or parallel'); return map(flattenArgs, getFunction); } module.exports = normalizeArgs; package/lib/helpers/validateRegistry.js000644 0000002224 3560116604 015350 0ustar00000000 000000 'use strict'; var assert = require('assert'); function isFunction(fn) { return typeof fn === 'function'; } function isConstructor(registry) { if (!(registry && registry.prototype)) { return false; } var hasProtoGet = isFunction(registry.prototype.get); var hasProtoSet = isFunction(registry.prototype.set); var hasProtoInit = isFunction(registry.prototype.init); var hasProtoTasks = isFunction(registry.prototype.tasks); if (hasProtoGet || hasProtoSet || hasProtoInit || hasProtoTasks) { return true; } return false; } function validateRegistry(registry) { try { assert(isFunction(registry.get), 'Custom registry must have `get` function'); assert(isFunction(registry.set), 'Custom registry must have `set` function'); assert(isFunction(registry.init), 'Custom registry must have `init` function'); assert(isFunction(registry.tasks), 'Custom registry must have `tasks` function'); } catch (err) { if (isConstructor(registry)) { assert(false, 'Custom registries must be instantiated, but it looks like you passed a constructor'); } else { throw err; } } } module.exports = validateRegistry; package/lib/last-run.js000644 0000000743 3560116604 012135 0ustar00000000 000000 'use strict'; var retrieveLastRun = require('last-run'); var metadata = require('./helpers/metadata'); function lastRun(task, timeResolution) { if (timeResolution == null) { timeResolution = process.env.UNDERTAKER_TIME_RESOLUTION; } var fn = task; if (typeof task === 'string') { fn = this._getTask(task); } var meta = metadata.get(fn); if (meta) { fn = meta.orig || fn; } return retrieveLastRun(fn, timeResolution); } module.exports = lastRun; package/lib/parallel.js000644 0000001326 3560116604 012162 0ustar00000000 000000 'use strict'; var bach = require('bach'); var metadata = require('./helpers/metadata'); var buildTree = require('./helpers/buildTree'); var normalizeArgs = require('./helpers/normalizeArgs'); var createExtensions = require('./helpers/createExtensions'); function parallel() { var create = this._settle ? bach.settleParallel : bach.parallel; var args = normalizeArgs(this._registry, arguments); var extensions = createExtensions(this); var fn = create(args, extensions); var name = ''; metadata.set(fn, { name: name, branch: true, tree: { label: name, type: 'function', branch: true, nodes: buildTree(args), }, }); return fn; } module.exports = parallel; package/lib/registry.js000644 0000000732 3560116604 012236 0ustar00000000 000000 'use strict'; var reduce = require('object.reduce'); var validateRegistry = require('./helpers/validateRegistry'); function setTasks(inst, task, name) { inst.set(name, task); return inst; } function registry(newRegistry) { if (!newRegistry) { return this._registry; } validateRegistry(newRegistry); var tasks = this._registry.tasks(); this._registry = reduce(tasks, setTasks, newRegistry); this._registry.init(this); } module.exports = registry; package/lib/series.js000644 0000001314 3560116604 011655 0ustar00000000 000000 'use strict'; var bach = require('bach'); var metadata = require('./helpers/metadata'); var buildTree = require('./helpers/buildTree'); var normalizeArgs = require('./helpers/normalizeArgs'); var createExtensions = require('./helpers/createExtensions'); function series() { var create = this._settle ? bach.settleSeries : bach.series; var args = normalizeArgs(this._registry, arguments); var extensions = createExtensions(this); var fn = create(args, extensions); var name = ''; metadata.set(fn, { name: name, branch: true, tree: { label: name, type: 'function', branch: true, nodes: buildTree(args), }, }); return fn; } module.exports = series; package/lib/set-task.js000644 0000001465 3560116604 012125 0ustar00000000 000000 'use strict'; var assert = require('assert'); var metadata = require('./helpers/metadata'); function set(name, fn) { assert(name, 'Task name must be specified'); assert(typeof name === 'string', 'Task name must be a string'); assert(typeof fn === 'function', 'Task function must be specified'); function taskWrapper() { return fn.apply(this, arguments); } function unwrap() { return fn; } taskWrapper.unwrap = unwrap; taskWrapper.displayName = name; var meta = metadata.get(fn) || {}; var nodes = []; if (meta.branch) { nodes.push(meta.tree); } var task = this._registry.set(name, taskWrapper) || taskWrapper; metadata.set(task, { name: name, orig: fn, tree: { label: name, type: 'task', nodes: nodes, }, }); } module.exports = set; package/lib/task.js000644 0000000356 3560116604 011332 0ustar00000000 000000 'use strict'; function task(name, fn) { if (typeof name === 'function') { fn = name; name = fn.displayName || fn.name; } if (!fn) { return this._getTask(name); } this._setTask(name, fn); } module.exports = task; package/lib/tree.js000644 0000000770 3560116604 011327 0ustar00000000 000000 'use strict'; var defaults = require('object.defaults'); var map = require('collection-map'); var metadata = require('./helpers/metadata'); function tree(opts) { opts = defaults(opts || {}, { deep: false, }); var tasks = this._registry.tasks(); var nodes = map(tasks, function(task) { var meta = metadata.get(task); if (opts.deep) { return meta.tree; } return meta.tree.label; }); return { label: 'Tasks', nodes: nodes, }; } module.exports = tree;