config.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @module jsdoc/config
  3. */
  4. const stripBom = require('jsdoc/util/stripbom');
  5. const stripJsonComments = require('strip-json-comments');
  6. function mergeRecurse(target, source) {
  7. Object.keys(source).forEach(p => {
  8. if ( source[p].constructor === Object ) {
  9. if ( !target[p] ) {
  10. target[p] = {};
  11. }
  12. mergeRecurse(target[p], source[p]);
  13. }
  14. else {
  15. target[p] = source[p];
  16. }
  17. });
  18. return target;
  19. }
  20. // required config values, override these defaults in your config.json if necessary
  21. const defaults = {
  22. plugins: [],
  23. recurseDepth: 10,
  24. source: {
  25. includePattern: '.+\\.js(doc|x)?$',
  26. excludePattern: ''
  27. },
  28. sourceType: 'module',
  29. tags: {
  30. allowUnknownTags: true,
  31. dictionaries: ['jsdoc', 'closure']
  32. },
  33. templates: {
  34. monospaceLinks: false,
  35. cleverLinks: false
  36. }
  37. };
  38. /**
  39. * Represents a JSDoc application configuration.
  40. */
  41. class Config {
  42. /**
  43. * @param {(string|object)} [jsonOrObject] - The contents of config.json, or a JavaScript object
  44. * exported from a .js config file.
  45. */
  46. constructor(jsonOrObject) {
  47. if (typeof jsonOrObject === 'undefined') {
  48. jsonOrObject = {};
  49. }
  50. if (typeof jsonOrObject === 'string') {
  51. jsonOrObject = JSON.parse( (stripJsonComments(stripBom.strip(jsonOrObject)) || '{}') );
  52. }
  53. if (typeof jsonOrObject !== 'object') {
  54. jsonOrObject = {};
  55. }
  56. this._config = mergeRecurse(defaults, jsonOrObject);
  57. }
  58. /**
  59. * Get the merged configuration values.
  60. */
  61. get() {
  62. return this._config;
  63. }
  64. }
  65. module.exports = Config;