index.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*jshint node:true */
  2. "use strict";
  3. var sequence = function (tasks, names, results, nest) {
  4. var i, name, node, e, j;
  5. nest = nest || [];
  6. for (i = 0; i < names.length; i++) {
  7. name = names[i];
  8. // de-dup results
  9. if (results.indexOf(name) === -1) {
  10. node = tasks[name];
  11. if (!node) {
  12. e = new Error('task "'+name+'" is not defined');
  13. e.missingTask = name;
  14. e.taskList = [];
  15. for (j in tasks) {
  16. if (tasks.hasOwnProperty(j)) {
  17. e.taskList.push(tasks[j].name);
  18. }
  19. }
  20. throw e;
  21. }
  22. if (nest.indexOf(name) > -1) {
  23. nest.push(name);
  24. e = new Error('Recursive dependencies detected: '+nest.join(' -> '));
  25. e.recursiveTasks = nest;
  26. e.taskList = [];
  27. for (j in tasks) {
  28. if (tasks.hasOwnProperty(j)) {
  29. e.taskList.push(tasks[j].name);
  30. }
  31. }
  32. throw e;
  33. }
  34. if (node.dep.length) {
  35. nest.push(name);
  36. sequence(tasks, node.dep, results, nest); // recurse
  37. nest.pop(name);
  38. }
  39. results.push(name);
  40. }
  41. }
  42. };
  43. module.exports = sequence;