options.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var fs = require('fs');
  2. var _ = require('lodash');
  3. var file = require('file');
  4. var jsdoc = require('doctrine');
  5. var srcRoot = fs.realpathSync(__dirname + '/../src');
  6. var commentRE = /^(\s+)?(\/\*)?\*(\/)?\s?/mg;
  7. var jsdocRE = /[^\S\r\n]*\/(?:\*{2})([\W\w]+?)\*\//mg;
  8. var stripComments = function(str) {
  9. return str.replace(commentRE, '');
  10. };
  11. function options(cb, allMetadata) {
  12. var opts;
  13. file.walkSync(srcRoot, function(start, dirs, files) {
  14. opts = _.chain(files)
  15. .map(function(file) {
  16. var srcFile = fs.readFileSync(start + '/' + file, 'utf8');
  17. var docs = srcFile.match(jsdocRE);
  18. if (docs) {
  19. docs = docs
  20. .map(stripComments)
  21. .map(function(str) {
  22. return jsdoc.parse(str, {
  23. sloppy: true,
  24. tags: [
  25. 'access',
  26. 'author',
  27. 'class',
  28. 'example',
  29. 'function',
  30. 'memberOf',
  31. 'name',
  32. 'optionName',
  33. 'optionProp',
  34. 'param',
  35. 'params',
  36. 'preserve',
  37. 'private',
  38. 'returns',
  39. 'type'
  40. ]
  41. });
  42. });
  43. var option = _.chain(docs)
  44. .flatten()
  45. .filter(function(doc) {
  46. if (allMetadata) {
  47. return true;
  48. } else {
  49. return doc && _.some(doc.tags, {title: 'optionName'});
  50. }
  51. })
  52. .map(function(opt) {
  53. if (allMetadata) {
  54. return opt;
  55. } else {
  56. var tags = opt.tags.filter(function(tag) {
  57. return tag.title.indexOf('option') === 0;
  58. });
  59. return {
  60. name: _.filter(tags, {title: 'optionName'})[0].description,
  61. property: _.filter(tags, {title: 'optionProp'})[0].description
  62. };
  63. }
  64. })
  65. .value();
  66. return option;
  67. }
  68. })
  69. .filter(function(doc) {
  70. return doc && doc.length;
  71. })
  72. .flatten()
  73. .value();
  74. });
  75. if (cb) {
  76. cb(opts);
  77. }
  78. return opts;
  79. }
  80. module.exports = options;