You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <p align="center">
  2. <a href="http://gulpjs.com">
  3. <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
  4. </a>
  5. </p>
  6. # undertaker
  7. [![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]
  8. Task registry that allows composition through `series`/`parallel` methods.
  9. ## Usage
  10. ```js
  11. var fs = require('fs');
  12. var Undertaker = require('undertaker');
  13. var taker = new Undertaker();
  14. taker.task('task1', function(cb){
  15. // do things
  16. cb(); // when everything is done
  17. });
  18. taker.task('task2', function(){
  19. return fs.createReadStream('./myFile.js')
  20. .pipe(fs.createWriteStream('./myFile.copy.js'));
  21. });
  22. taker.task('task3', function(){
  23. return new Promise(function(resolve, reject){
  24. // do things
  25. resolve(); // when everything is done
  26. });
  27. });
  28. taker.task('combined', taker.series('task1', 'task2'));
  29. taker.task('all', taker.parallel('combined', 'task3'));
  30. ```
  31. ## API
  32. __Task functions can be completed in any of the ways supported by
  33. [`async-done`][async-resolution]__
  34. ### `new Undertaker([registryInstance])`
  35. The constructor is used to create a new instance of `Undertaker`. Each instance of
  36. `Undertaker` gets its own instance of a registry. By default, the registry is an
  37. instance of [`undertaker-registry`][undertaker-registry]
  38. but it can be an instance of any other registry that follows the [Custom Registries API][custom-registries].
  39. To use a custom registry, pass a custom registry instance (`new CustomRegistry([options])`) when
  40. instantiating a new `Undertaker` instance. This will use the custom registry instance for that `Undertaker` instance.
  41. ### `task([taskName,] fn)`
  42. Both a `getter` and `setter` for tasks.
  43. If a string (`taskName`) is given as the only argument, it behaves as a `getter`
  44. and returns the wrapped task (not the original function). The wrapped task has a `unwrap`
  45. method that will return the original function.
  46. If a function (`fn`) and optionally a string (`taskName`) is given, it behaves as
  47. a `setter` and will register the task by the `taskName`. If `taskName` is not
  48. specified, the `name` or `displayName` property of the function is used as the `taskName`.
  49. Will throw if:
  50. * As a `getter`: `taskName` is missing or not a string.
  51. * As a `setter`: `taskName` is missing and `fn` is anonymous.
  52. * As a `setter`: `fn` is missing or not a function.
  53. ### `series(taskName || fn...)`
  54. Takes a variable amount of strings (`taskName`) and/or functions (`fn`) and
  55. returns a function of the composed tasks or functions. Any `taskNames` are
  56. retrieved from the registry using the `get` method.
  57. When the returned function is executed, the tasks or functions will be executed
  58. in series, each waiting for the prior to finish. If an error occurs, execution
  59. will stop.
  60. ### `parallel(taskName || fn...)`
  61. Takes a variable amount of strings (`taskName`) and/or functions (`fn`) and
  62. returns a function of the composed tasks or functions. Any `taskNames` are
  63. retrieved from the registry using the `get` method.
  64. When the returned function is executed, the tasks or functions will be executed
  65. in parallel, all being executed at the same time. If an error occurs, all execution
  66. will complete.
  67. ### `registry([registryInstance])`
  68. Optionally takes an instantiated registry object. If no arguments are passed, returns
  69. the current registry object. If an instance of a registry (`customRegistry`) is passed
  70. the tasks from the current registry will be transferred to it and the current registry
  71. will be replaced with the new registry.
  72. The ability to assign new registries will allow you to pre-define/share tasks or add
  73. custom functionality to your registries. See [Custom Registries][custom-registries]
  74. for more information.
  75. ### `tree([options])`
  76. Optionally takes an `options` object and returns an object representing the
  77. tree of registered tasks. The object returned is [`archy`][archy]
  78. compatible. Also, each node has a `type` property that can be used to determine if the node is a `task` or `function`.
  79. #### `options`
  80. ##### `options.deep`
  81. Whether or not the whole tree should be returned.
  82. Type: `Boolean`
  83. Default: `false`
  84. ### `lastRun(task, [precision])`
  85. Takes a string or function (`task`) and returns a timestamp of the last time the task
  86. was run successfully. The time will be the time the task started.
  87. Returns `undefined` if the task has not been run.
  88. If a task errors, the result of `lastRun` will be undefined because the task
  89. should probably be re-run from scratch to get into a good state again.
  90. The timestamp is always given in millisecond but the time resolution can be
  91. rounded using the `precision` parameter. The use case is to be able to compare a build time
  92. to a file time attribute. On node v0.10 or with file system like HFS or FAT,
  93. `fs.stat` time attributes like `mtime` precision is one second.
  94. Assuming `undertakerInst.lastRun('someTask')` returns `1426000001111`,
  95. `undertakerInst.lastRun('someTask', 1000)` returns `1426000001000`.
  96. The default time resolution is `1000` on node v0.10, `0` on node 0.11+ but
  97. it can be overwritten using `UNDERTAKER_TIME_RESOLUTION` environment variable.
  98. ## Custom Registries
  99. Custom registries are constructor functions allowing you to pre-define/share tasks
  100. or add custom functionality to your registries.
  101. A registry's prototype should define:
  102. - `init(taker)`: receives the undertaker instance to set pre-defined tasks using the `task(taskName, fn)` method.
  103. - `get(taskName)`: returns the task with that name
  104. or `undefined` if no task is registered with that name.
  105. - `set(taskName, fn)`: add task to the registry. If `set` modifies a task, it should return the new task.
  106. - `tasks()`: returns an object listing all tasks in the registry.
  107. You should not call these functions yourself; leave that to Undertaker, so it can
  108. keep its metadata consistent.
  109. The easiest way to create a custom registry is to inherit from [undertaker-registry]:
  110. ```js
  111. var util = require('util');
  112. var DefaultRegistry = require('undertaker-registry');
  113. function MyRegistry(){
  114. DefaultRegistry.call(this);
  115. }
  116. util.inherits(MyRegistry, DefaultRegistry);
  117. module.exports = MyRegistry;
  118. ```
  119. ### Sharing tasks
  120. To share common tasks with all your projects, you can expose an `init` method on the registry
  121. prototype and it will receive the `Undertaker` instance as the only argument. You can then use
  122. `undertaker.task(name, fn)` to register pre-defined tasks.
  123. For example you might want to share a `clean` task:
  124. ```js
  125. var fs = require('fs');
  126. var util = require('util');
  127. var DefaultRegistry = require('undertaker-registry');
  128. var del = require('del');
  129. function CommonRegistry(opts){
  130. DefaultRegistry.call(this);
  131. opts = opts || {};
  132. this.buildDir = opts.buildDir || './build';
  133. }
  134. util.inherits(CommonRegistry, DefaultRegistry);
  135. CommonRegistry.prototype.init = function(takerInst){
  136. var buildDir = this.buildDir;
  137. var exists = fs.existsSync(buildDir);
  138. if(exists){
  139. throw new Error('Cannot initialize common tasks. ' + buildDir + ' directory exists.');
  140. }
  141. takerInst.task('clean', function(){
  142. return del([buildDir]);
  143. });
  144. }
  145. module.exports = CommonRegistry;
  146. ```
  147. Then to use it in a project:
  148. ```js
  149. var Undertaker = require('undertaker');
  150. var CommonRegistry = require('myorg-common-tasks');
  151. var taker = new Undertaker(CommonRegistry({ buildDir: '/dist' }));
  152. taker.task('build', taker.series('clean', function build(cb) {
  153. // do things
  154. cb();
  155. }));
  156. ```
  157. ### Sharing Functionalities
  158. By controlling how tasks are added to the registry, you can decorate them.
  159. For example if you wanted all tasks to share some data, you can use a custom registry
  160. to bind them to that data. Be sure to return the altered task, as per the description
  161. of registry methods above:
  162. ```js
  163. var util = require('util');
  164. var Undertaker = require('undertaker');
  165. var DefaultRegistry = require('undertaker-registry');
  166. // Some task defined somewhere else
  167. var BuildRegistry = require('./build.js');
  168. var ServeRegistry = require('./serve.js');
  169. function ConfigRegistry(config){
  170. DefaultRegistry.call(this);
  171. this.config = config;
  172. }
  173. util.inherits(ConfigRegistry, DefaultRegistry);
  174. ConfigRegistry.prototype.set = function set(name, fn) {
  175. // The `DefaultRegistry` uses `this._tasks` for storage.
  176. var task = this._tasks[name] = fn.bind(this.config);
  177. return task;
  178. };
  179. var taker = new Undertaker();
  180. taker.registry(new BuildRegistry());
  181. taker.registry(new ServeRegistry());
  182. // `taker.registry` will reset each task in the registry with
  183. // `ConfigRegistry.prototype.set` which will bind them to the config object.
  184. taker.registry(new ConfigRegistry({
  185. src: './src',
  186. build: './build',
  187. bindTo: '0.0.0.0:8888'
  188. }));
  189. taker.task('default', taker.series('clean', 'build', 'serve', function(cb) {
  190. console.log('Server bind to ' + this.bindTo);
  191. console.log('Serving' + this.build);
  192. cb();
  193. }));
  194. ```
  195. ### In the wild
  196. * [undertaker-registry] - Custom registries probably want to inherit from this.
  197. * [undertaker-forward-reference] - Custom registry supporting forward referenced tasks (similar to gulp 3.x).
  198. * [undertaker-task-metadata] - Proof-of-concept custom registry that attaches metadata to each task.
  199. * [undertaker-common-tasks] - Proof-of-concept custom registry that pre-defines some tasks.
  200. * [alchemist-gulp] - A default set of tasks for building alchemist plugins.
  201. * [gulp-hub] - Custom registry to run tasks in multiple gulpfiles. (In a branch as of this writing)
  202. * [gulp-pipeline] - [RailsRegistry][rails-registry] is an ES2015 class that provides a gulp pipeline replacement for rails applications
  203. ## License
  204. MIT
  205. [custom-registries]: #custom-registries
  206. [async-resolution]: https://github.com/phated/async-done#completion-and-error-resolution
  207. [archy]: https://www.npmjs.org/package/archy
  208. [undertaker-registry]: https://github.com/gulpjs/undertaker-registry
  209. [undertaker-forward-reference]: https://github.com/gulpjs/undertaker-forward-reference
  210. [undertaker-task-metadata]: https://github.com/gulpjs/undertaker-task-metadata
  211. [undertaker-common-tasks]: https://github.com/gulpjs/undertaker-common-tasks
  212. [alchemist-gulp]: https://github.com/webdesserts/alchemist-gulp
  213. [gulp-hub]: https://github.com/frankwallis/gulp-hub/tree/registry-init
  214. [gulp-pipeline]: https://github.com/alienfast/gulp-pipeline
  215. [rails-registry]: https://github.com/alienfast/gulp-pipeline/blob/master/src/registry/railsRegistry.js
  216. [downloads-image]: http://img.shields.io/npm/dm/undertaker.svg
  217. [npm-url]: https://www.npmjs.com/package/undertaker
  218. [npm-image]: http://img.shields.io/npm/v/undertaker.svg
  219. [travis-url]: https://travis-ci.org/gulpjs/undertaker
  220. [travis-image]: http://img.shields.io/travis/gulpjs/undertaker.svg?label=travis-ci
  221. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/undertaker
  222. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/undertaker.svg?label=appveyor
  223. [coveralls-url]: https://coveralls.io/r/gulpjs/undertaker
  224. [coveralls-image]: http://img.shields.io/coveralls/gulpjs/undertaker/master.svg
  225. [gitter-url]: https://gitter.im/gulpjs/gulp
  226. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg