package_info.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var _ = require('lodash');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var app = {};
  5. function PackageInfo(_app) {
  6. // global variables
  7. app = _app;
  8. }
  9. /**
  10. * Exports
  11. */
  12. module.exports = PackageInfo;
  13. /**
  14. * Read apidoc.json / package.json data
  15. */
  16. PackageInfo.prototype.get = function() {
  17. var result = {};
  18. // Read package.json
  19. var packageJson = this._readPackageData('package.json');
  20. if (packageJson.apidoc)
  21. result = packageJson.apidoc;
  22. result = _.defaults({}, result, {
  23. name : packageJson.name || '',
  24. version : packageJson.version || '0.0.0',
  25. description: packageJson.description || '',
  26. });
  27. // read apidoc.json (and overwrite package.json information)
  28. var apidocJson = this._readPackageData('apidoc.json');
  29. // apidoc.json has higher priority
  30. _.extend(result, apidocJson);
  31. // options.packageInfo overwrites packageInfo
  32. _.extend(result, app.options.packageInfo);
  33. // replace header footer with file contents
  34. _.extend(result, this._getHeaderFooter(result));
  35. if (Object.keys(apidocJson).length === 0 && ! packageJson.apidoc)
  36. app.log.warn('Please create an apidoc.json configuration file.');
  37. return result;
  38. };
  39. /**
  40. * Read json data from source dir, or if it not exists from current dir.
  41. * Return the data merged with the default values.
  42. *
  43. * @param {String} filename
  44. * @param {Object} defaults
  45. * @returns {Object}
  46. */
  47. PackageInfo.prototype._readPackageData = function(filename) {
  48. var result = {};
  49. var dir = this._resolveSrcPath();
  50. var jsonFilename = path.join(dir, filename);
  51. // Read from source dir
  52. if ( ! fs.existsSync(jsonFilename)) {
  53. // Read from config dir (default './')
  54. jsonFilename = path.join(app.options.config, filename);
  55. }
  56. if ( ! fs.existsSync(jsonFilename)) {
  57. app.log.debug(jsonFilename + ' not found!');
  58. } else {
  59. try {
  60. result = JSON.parse( fs.readFileSync(jsonFilename, 'utf8') );
  61. app.log.debug('read: ' + jsonFilename);
  62. } catch (e) {
  63. throw new Error('Can not read: ' + filename + ', please check the format (e.g. missing comma).');
  64. }
  65. }
  66. return result;
  67. };
  68. /**
  69. * Get json.header / json.footer title and markdown content (from file)
  70. *
  71. * @param {Object} json
  72. * @returns {Object}
  73. */
  74. PackageInfo.prototype._getHeaderFooter = function(json) {
  75. var result = {};
  76. var self = this;
  77. ['header', 'footer'].forEach(function(key) {
  78. if (json[key] && json[key].filename) {
  79. // var filename = path.join(app.options.src, json[key].filename);
  80. var dir = self._resolveSrcPath();
  81. var filename = path.join(dir, json[key].filename);
  82. if ( ! fs.existsSync(filename))
  83. filename = path.join('./', json[key].filename);
  84. try {
  85. app.log.debug('read header file: ' + filename);
  86. var content = fs.readFileSync(filename, 'utf8');
  87. result[key] = {
  88. title : json[key].title,
  89. content: app.markdownParser ? app.markdownParser.render(content) : content
  90. };
  91. } catch (e) {
  92. throw new Error('Can not read: ' + filename + '.');
  93. }
  94. }
  95. });
  96. return result;
  97. };
  98. /**
  99. * Resolve source path.
  100. *
  101. * If multiple input dirs are given, the current workdir './' will be returned.
  102. * On one input dir, the current workdir will be the input dir.
  103. *
  104. * @returns {string}
  105. * @private
  106. */
  107. PackageInfo.prototype._resolveSrcPath = function() {
  108. var dir = './';
  109. if (app.options.src instanceof Array) {
  110. if (app.options.src.length === 1) {
  111. dir = app.options.src[0];
  112. }
  113. } else {
  114. if (app.options.src) {
  115. dir = app.options.src;
  116. }
  117. }
  118. return dir;
  119. };