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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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-registry
  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. Default registry in gulp 4.
  9. ## Usage
  10. ```js
  11. var gulp = require('gulp');
  12. var UndertakerRegistry = require('undertaker-registry');
  13. var registry = new UndertakerRegistry();
  14. gulp.registry(registry);
  15. ```
  16. ## API
  17. ### new UndertakerRegistry([options])
  18. Constructor for the default registry. Inherit from this constructor to build custom registries.
  19. ### init(taker)
  20. No-op method that receives the undertaker instance. Useful to set pre-defined tasks using the
  21. `undertaker.task(taskName, fn)` method. Custom registries can override this method when inheriting
  22. from this default registry.
  23. ### get(taskName) => Function
  24. Returns the task with that name or undefined if no task is registered with that name. Useful for custom
  25. task storage. Custom registries can override this method when inheriting from this default registry.
  26. ### set(taskName, fn) => [Function]
  27. Adds a task to the registry. If `set` modifies a task, it should return the new task so Undertaker can
  28. properly maintain metadata for the task. Useful for adding custom behavior to every task as it is
  29. registered in the system. Custom registries can override this method when inheriting from this default
  30. registry.
  31. ### tasks() => Object
  32. Returns an object listing all tasks in the registry. Necessary to override if the `get` method is overridden
  33. for custom task storage. Custom registries can override this when when inheriting from this default
  34. registry.
  35. ## Custom Registries
  36. Custom registries are constructor functions allowing you to pre-define/share tasks
  37. or add custom functionality to your registries.
  38. A registry's prototype should define:
  39. - `init(taker)`: receives the undertaker instance to set pre-defined tasks using the `task(taskName, fn)` method.
  40. - `get(taskName)`: returns the task with that name
  41. or `undefined` if no task is registered with that name.
  42. - `set(taskName, fn)`: add task to the registry. If `set` modifies a task, it should return the new task.
  43. - `tasks()`: returns an object listing all tasks in the registry.
  44. You should not call these functions yourself; leave that to Undertaker, so it can
  45. keep its metadata consistent.
  46. The easiest way to create a custom registry is to inherit from
  47. [undertaker-registry](https://www.npmjs.com/package/undertaker-registry):
  48. ```javascript
  49. var util = require('util');
  50. var DefaultRegistry = require('undertaker-registry');
  51. function MyRegistry(){
  52. DefaultRegistry.call(this);
  53. }
  54. util.inherits(MyRegistry, DefaultRegistry);
  55. module.exports = MyRegistry;
  56. ```
  57. ### Sharing tasks
  58. To share common tasks with all your projects, you can expose an `init` method on the registry
  59. prototype and it will receive the Undertaker instance as the only argument. You can then use
  60. `undertaker.task(name, fn)` to register pre-defined tasks.
  61. For example you might want to share a `clean` task:
  62. ```javascript
  63. var fs = require('fs');
  64. var util = require('util');
  65. var DefaultRegistry = require('undertaker-registry');
  66. var del = require('del');
  67. function CommonRegistry(opts){
  68. DefaultRegistry.call(this);
  69. opts = opts || {};
  70. this.buildDir = opts.buildDir || './build';
  71. }
  72. util.inherits(CommonRegistry, DefaultRegistry);
  73. CommonRegistry.prototype.init = function(takerInst){
  74. var buildDir = this.buildDir;
  75. var exists = fs.existsSync(buildDir);
  76. if(exists){
  77. throw new Error('Cannot initialize common tasks. ' + buildDir + ' directory exists.');
  78. }
  79. takerInst.task('clean', function(){
  80. return del([buildDir]);
  81. });
  82. }
  83. module.exports = CommonRegistry;
  84. ```
  85. Then to use it in a project:
  86. ```javascript
  87. var Undertaker = require('undertaker');
  88. var CommonRegistry = require('myorg-common-tasks');
  89. var taker = new Undertaker(CommonRegistry({ buildDir: '/dist' }));
  90. taker.task('build', taker.series('clean', function build(cb) {
  91. // do things
  92. cb();
  93. }));
  94. ```
  95. ### Sharing Functionalities
  96. By controlling how tasks are added to the registry, you can decorate them.
  97. For example if you wanted all tasks to share some data, you can use a custom registry
  98. to bind them to that data. Be sure to return the altered task, as per the description
  99. of registry methods above:
  100. ```javascript
  101. var util = require('util');
  102. var Undertaker = require('undertaker');
  103. var DefaultRegistry = require('undertaker-registry');
  104. // Some task defined somewhere else
  105. var BuildRegistry = require('./build.js');
  106. var ServeRegistry = require('./serve.js');
  107. function ConfigRegistry(config){
  108. DefaultRegistry.call(this);
  109. this.config = config;
  110. }
  111. util.inherits(ConfigRegistry, DefaultRegistry);
  112. ConfigRegistry.prototype.set = function set(name, fn) {
  113. // The `DefaultRegistry` uses `this._tasks` for storage.
  114. var task = this._tasks[name] = fn.bind(this.config);
  115. return task;
  116. };
  117. var taker = new Undertaker();
  118. taker.registry(new BuildRegistry());
  119. taker.registry(new ServeRegistry());
  120. // `taker.registry` will reset each task in the registry with
  121. // `ConfigRegistry.prototype.set` which will bind them to the config object.
  122. taker.registry(new ConfigRegistry({
  123. src: './src',
  124. build: './build',
  125. bindTo: '0.0.0.0:8888'
  126. }));
  127. taker.task('default', taker.series('clean', 'build', 'serve', function(cb) {
  128. console.log('Server bind to ' + this.bindTo);
  129. console.log('Serving' + this.build);
  130. cb();
  131. }));
  132. ```
  133. ## License
  134. MIT
  135. [downloads-image]: http://img.shields.io/npm/dm/undertaker-registry.svg
  136. [npm-url]: https://npmjs.org/package/undertaker-registry
  137. [npm-image]: http://img.shields.io/npm/v/undertaker-registry.svg
  138. [travis-url]: https://travis-ci.org/gulpjs/undertaker-registry
  139. [travis-image]: http://img.shields.io/travis/gulpjs/undertaker-registry.svg
  140. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/undertaker-registry
  141. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/undertaker-registry.svg?label=appveyor
  142. [coveralls-url]: https://coveralls.io/r/gulpjs/undertaker-registry
  143. [coveralls-image]: http://img.shields.io/coveralls/gulpjs/undertaker-registry/master.svg
  144. [gitter-url]: https://gitter.im/gulpjs/gulp
  145. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg