[d565449] | 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 |
|
---|
| 18 | import util from "util";
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * The class is to store parsers or plugins.
|
---|
| 22 | * This class hides the loaded object from `JSON.stringify()` and `console.log`.
|
---|
| 23 | * @template T
|
---|
| 24 | */
|
---|
| 25 | class ConfigDependency {
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Initialize this instance.
|
---|
| 29 | * @param {Object} data The dependency data.
|
---|
| 30 | * @param {T} [data.definition] The dependency if the loading succeeded.
|
---|
| 31 | * @param {T} [data.original] The original, non-normalized dependency if the loading succeeded.
|
---|
| 32 | * @param {Error} [data.error] The error object if the loading failed.
|
---|
| 33 | * @param {string} [data.filePath] The actual path to the dependency if the loading succeeded.
|
---|
| 34 | * @param {string} data.id The ID of this dependency.
|
---|
| 35 | * @param {string} data.importerName The name of the config file which loads this dependency.
|
---|
| 36 | * @param {string} data.importerPath The path to the config file which loads this dependency.
|
---|
| 37 | */
|
---|
| 38 | constructor({
|
---|
| 39 | definition = null,
|
---|
| 40 | original = null,
|
---|
| 41 | error = null,
|
---|
| 42 | filePath = null,
|
---|
| 43 | id,
|
---|
| 44 | importerName,
|
---|
| 45 | importerPath
|
---|
| 46 | }) {
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * The loaded dependency if the loading succeeded.
|
---|
| 50 | * @type {T|null}
|
---|
| 51 | */
|
---|
| 52 | this.definition = definition;
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * The original dependency as loaded directly from disk if the loading succeeded.
|
---|
| 56 | * @type {T|null}
|
---|
| 57 | */
|
---|
| 58 | this.original = original;
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * The error object if the loading failed.
|
---|
| 62 | * @type {Error|null}
|
---|
| 63 | */
|
---|
| 64 | this.error = error;
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * The loaded dependency if the loading succeeded.
|
---|
| 68 | * @type {string|null}
|
---|
| 69 | */
|
---|
| 70 | this.filePath = filePath;
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * The ID of this dependency.
|
---|
| 74 | * @type {string}
|
---|
| 75 | */
|
---|
| 76 | this.id = id;
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * The name of the config file which loads this dependency.
|
---|
| 80 | * @type {string}
|
---|
| 81 | */
|
---|
| 82 | this.importerName = importerName;
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * The path to the config file which loads this dependency.
|
---|
| 86 | * @type {string}
|
---|
| 87 | */
|
---|
| 88 | this.importerPath = importerPath;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | // eslint-disable-next-line jsdoc/require-description
|
---|
| 92 | /**
|
---|
| 93 | * @returns {Object} a JSON compatible object.
|
---|
| 94 | */
|
---|
| 95 | toJSON() {
|
---|
| 96 | const obj = this[util.inspect.custom]();
|
---|
| 97 |
|
---|
| 98 | // Display `error.message` (`Error#message` is unenumerable).
|
---|
| 99 | if (obj.error instanceof Error) {
|
---|
| 100 | obj.error = { ...obj.error, message: obj.error.message };
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | return obj;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | // eslint-disable-next-line jsdoc/require-description
|
---|
| 107 | /**
|
---|
| 108 | * @returns {Object} an object to display by `console.log()`.
|
---|
| 109 | */
|
---|
| 110 | [util.inspect.custom]() {
|
---|
| 111 | const {
|
---|
| 112 | definition: _ignore1, // eslint-disable-line no-unused-vars
|
---|
| 113 | original: _ignore2, // eslint-disable-line no-unused-vars
|
---|
| 114 | ...obj
|
---|
| 115 | } = this;
|
---|
| 116 |
|
---|
| 117 | return obj;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /** @typedef {ConfigDependency<import("../../shared/types").Parser>} DependentParser */
|
---|
| 122 | /** @typedef {ConfigDependency<import("../../shared/types").Plugin>} DependentPlugin */
|
---|
| 123 |
|
---|
| 124 | export { ConfigDependency };
|
---|