config-dependency.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @fileoverview `ConfigDependency` class.
  3. *
  4. * `ConfigDependency` class expresses a loaded parser or plugin.
  5. *
  6. * If the parser or plugin was loaded successfully, it has `definition` property
  7. * and `filePath` property. Otherwise, it has `error` property.
  8. *
  9. * When `JSON.stringify()` converted a `ConfigDependency` object to a JSON, it
  10. * omits `definition` property.
  11. *
  12. * `ConfigArrayFactory` creates `ConfigDependency` objects when it loads parsers
  13. * or plugins.
  14. *
  15. * @author Toru Nagashima <https://github.com/mysticatea>
  16. */
  17. import util from "util";
  18. /**
  19. * The class is to store parsers or plugins.
  20. * This class hides the loaded object from `JSON.stringify()` and `console.log`.
  21. * @template T
  22. */
  23. class ConfigDependency {
  24. /**
  25. * Initialize this instance.
  26. * @param {Object} data The dependency data.
  27. * @param {T} [data.definition] The dependency if the loading succeeded.
  28. * @param {Error} [data.error] The error object if the loading failed.
  29. * @param {string} [data.filePath] The actual path to the dependency if the loading succeeded.
  30. * @param {string} data.id The ID of this dependency.
  31. * @param {string} data.importerName The name of the config file which loads this dependency.
  32. * @param {string} data.importerPath The path to the config file which loads this dependency.
  33. */
  34. constructor({
  35. definition = null,
  36. error = null,
  37. filePath = null,
  38. id,
  39. importerName,
  40. importerPath
  41. }) {
  42. /**
  43. * The loaded dependency if the loading succeeded.
  44. * @type {T|null}
  45. */
  46. this.definition = definition;
  47. /**
  48. * The error object if the loading failed.
  49. * @type {Error|null}
  50. */
  51. this.error = error;
  52. /**
  53. * The loaded dependency if the loading succeeded.
  54. * @type {string|null}
  55. */
  56. this.filePath = filePath;
  57. /**
  58. * The ID of this dependency.
  59. * @type {string}
  60. */
  61. this.id = id;
  62. /**
  63. * The name of the config file which loads this dependency.
  64. * @type {string}
  65. */
  66. this.importerName = importerName;
  67. /**
  68. * The path to the config file which loads this dependency.
  69. * @type {string}
  70. */
  71. this.importerPath = importerPath;
  72. }
  73. // eslint-disable-next-line jsdoc/require-description
  74. /**
  75. * @returns {Object} a JSON compatible object.
  76. */
  77. toJSON() {
  78. const obj = this[util.inspect.custom]();
  79. // Display `error.message` (`Error#message` is unenumerable).
  80. if (obj.error instanceof Error) {
  81. obj.error = { ...obj.error, message: obj.error.message };
  82. }
  83. return obj;
  84. }
  85. // eslint-disable-next-line jsdoc/require-description
  86. /**
  87. * @returns {Object} an object to display by `console.log()`.
  88. */
  89. [util.inspect.custom]() {
  90. const {
  91. definition: _ignore, // eslint-disable-line no-unused-vars
  92. ...obj
  93. } = this;
  94. return obj;
  95. }
  96. }
  97. /** @typedef {ConfigDependency<import("../../shared/types").Parser>} DependentParser */
  98. /** @typedef {ConfigDependency<import("../../shared/types").Plugin>} DependentPlugin */
  99. export { ConfigDependency };