set-task.js 821 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var assert = require('assert');
  3. var metadata = require('./helpers/metadata');
  4. function set(name, fn) {
  5. assert(name, 'Task name must be specified');
  6. assert(typeof name === 'string', 'Task name must be a string');
  7. assert(typeof fn === 'function', 'Task function must be specified');
  8. function taskWrapper() {
  9. return fn.apply(this, arguments);
  10. }
  11. function unwrap() {
  12. return fn;
  13. }
  14. taskWrapper.unwrap = unwrap;
  15. taskWrapper.displayName = name;
  16. var meta = metadata.get(fn) || {};
  17. var nodes = [];
  18. if (meta.branch) {
  19. nodes.push(meta.tree);
  20. }
  21. var task = this._registry.set(name, taskWrapper) || taskWrapper;
  22. metadata.set(task, {
  23. name: name,
  24. orig: fn,
  25. tree: {
  26. label: name,
  27. type: 'task',
  28. nodes: nodes,
  29. },
  30. });
  31. }
  32. module.exports = set;