spec-collection.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. var fs = require('jsdoc/fs');
  3. var path = require('jsdoc/path');
  4. var klaw = require('klaw');
  5. var specs = [];
  6. var finalSpecs = [];
  7. var createSpecObj = function(_path, root) {
  8. function relativePath() {
  9. return _path.replace(root, '').replace(/^[/\\]/, '').replace(/\\/g, '/');
  10. }
  11. return {
  12. path: function() {
  13. return _path;
  14. },
  15. relativePath: relativePath,
  16. directory: function() {
  17. return _path.replace(/[/\\][\s\w.-]*$/, '').replace(/\\/g, '/');
  18. },
  19. relativeDirectory: function() {
  20. return relativePath().replace(/[/\\][\s\w.-]*$/, '').replace(/\\/g, '/');
  21. },
  22. filename: function() {
  23. return _path.replace(/^.*[\\/]/, '');
  24. }
  25. };
  26. };
  27. var clearSpecs = exports.clearSpecs = function() {
  28. specs.splice(0, specs.length);
  29. };
  30. function addSpec(file, target) {
  31. target = target || specs;
  32. target.push( createSpecObj(file) );
  33. }
  34. function isValidSpec(file, matcher) {
  35. var result;
  36. // valid specs must...
  37. try {
  38. // ...be a file
  39. result = fs.statSync(file).isFile() &&
  40. // ...match the matcher
  41. matcher.test( path.basename(file) );
  42. }
  43. catch (e) {
  44. result = false;
  45. }
  46. return result;
  47. }
  48. function shouldLoad(file, matcher) {
  49. var result = false;
  50. // should this spec run at the end?
  51. if ( /schema\.js$/.test(file) && isValidSpec(file, matcher) ) {
  52. addSpec(file, finalSpecs);
  53. }
  54. else {
  55. result = isValidSpec(file, matcher);
  56. }
  57. return result;
  58. }
  59. exports.load = function(loadpath, matcher, clear, callback) {
  60. var wannaBeSpecs = [];
  61. if (clear === true) {
  62. clearSpecs();
  63. }
  64. klaw(loadpath)
  65. .on('data', function(spec) {
  66. wannaBeSpecs.push(spec.path);
  67. })
  68. .on('end', function() {
  69. for (var i = 0; i < wannaBeSpecs.length; i++) {
  70. var file = wannaBeSpecs[i];
  71. if ( shouldLoad(file, matcher) ) {
  72. addSpec(file);
  73. }
  74. }
  75. callback();
  76. });
  77. };
  78. exports.getSpecs = function() {
  79. return specs.concat(finalSpecs);
  80. };