Doc.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var fs = require('fs');
  2. var Buffer = require('buffer').Buffer;
  3. function Doc() {
  4. }
  5. Doc.prototype.getClassName = function(filePath) {
  6. fs.open(filePath, 'r', function(err, fd) {
  7. var contents = "";
  8. var raw = new Buffer(1024);
  9. fs.read(fd, raw, 0, raw.length, null, function(err, bytesRead, buffer) {
  10. var buf = buffer.slice(0, bytesRead);
  11. contents += buf.toString().split('module.exports = ')[1].split(';')[0];
  12. fs.close(fd, function() {
  13. console.log(contents);
  14. })
  15. })
  16. })
  17. }
  18. Doc.prototype.getClassInfos = function(path) {
  19. var fileNames = fs.readdirSync(path);
  20. var classInfos = {};
  21. var that = this;
  22. for(var i in fileNames) {
  23. var className = fileNames[i].split('Controller.js')[0];
  24. classInfos[className] = {
  25. author : "",
  26. desc : "",
  27. funcs : that.getFunc(className)
  28. }
  29. }
  30. return classInfos;
  31. }
  32. /**
  33. * 获取类的信息
  34. * @param className 类名
  35. */
  36. Doc.prototype.getClassInfo = function(className) {
  37. return;
  38. }
  39. /**
  40. * 获取接口信息
  41. * @param className 类名
  42. * @param funcName 接口名
  43. */
  44. Doc.prototype.getFuncInfo = function(className, funcName) {
  45. var controllerName = className.charAt(0).toUpperCase() + className.slice(1);
  46. var Controller;
  47. try{
  48. //Controller = require('../controllers/' + controllerName + 'Controller.js');
  49. //var objFunc = Controller.prototype[funcName];
  50. //return
  51. } catch(e) {
  52. console.error(e);
  53. }
  54. }
  55. Doc.prototype.getFunc = function(className) {
  56. var controllerName = className.charAt(0).toUpperCase() + className.slice(1);
  57. var Controller;
  58. try{
  59. Controller = require('../controllers/' + controllerName + 'Controller.js');
  60. var funcList = {};
  61. for(var i in Controller.prototype) {
  62. if(i.match('action')){
  63. funcList[i] = {
  64. author : "",
  65. desc : "",
  66. param : []
  67. }
  68. }
  69. }
  70. return funcList;
  71. } catch(e) {
  72. console.error(e);
  73. return null;
  74. }
  75. }
  76. module.exports = Doc;