|
| 1 | +/** |
| 2 | + * @file Error Models - ERR_INVALID_PACKAGE_TARGET |
| 3 | + * @module errnode/models/ERR_INVALID_PACKAGE_TARGET |
| 4 | + * @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1331-L1346 |
| 5 | + */ |
| 6 | + |
| 7 | +import { ErrorCode } from '#src/enums' |
| 8 | +import type { MessageFn, NodeError, NodeErrorConstructor } from '#src/types' |
| 9 | +import { createNodeError } from '#src/utils' |
| 10 | +import type pkg from '@flex-development/pkg-types' |
| 11 | + |
| 12 | +/** |
| 13 | + * `ERR_INVALID_PACKAGE_TARGET` model. |
| 14 | + * |
| 15 | + * Thrown when a `package.json` [`"exports"`][1] or [`"imports"`][2] field |
| 16 | + * contains an invalid target mapping value for the attempted module resolution. |
| 17 | + * |
| 18 | + * [1]: https://nodejs.org/api/packages.html#exports |
| 19 | + * [2]: https://nodejs.org/api/packages.html#imports |
| 20 | + * |
| 21 | + * @see https://nodejs.org/api/errors.html#err_invalid_package_target |
| 22 | + * |
| 23 | + * @class |
| 24 | + * |
| 25 | + * @param {string} dir - Directory containing `package.json` file |
| 26 | + * @param {string} key - `exports` or `imports` key |
| 27 | + * @param {pkg.Exports} target - Invalid package target |
| 28 | + * @param {boolean?} [internal=false] - `target` is package [`imports`][2]? |
| 29 | + * @param {string?} [base=''] - Id of module package was imported from |
| 30 | + * @return {NodeError} `Error` instance |
| 31 | + */ |
| 32 | +const ERR_INVALID_PACKAGE_TARGET: NodeErrorConstructor< |
| 33 | + ErrorConstructor, |
| 34 | + MessageFn<[string, string, pkg.Exports, boolean?, string?]> |
| 35 | +> = createNodeError( |
| 36 | + ErrorCode.ERR_INVALID_PACKAGE_TARGET, |
| 37 | + Error, |
| 38 | + /** |
| 39 | + * Creates a [`ERR_INVALID_PACKAGE_TARGET`][1] message. |
| 40 | + * |
| 41 | + * [1]: https://nodejs.org/api/errors.html#err_invalid_package_target |
| 42 | + * [2]: https://nodejs.org/api/packages.html#imports |
| 43 | + * |
| 44 | + * @param {string} dir - Directory containing `package.json` file |
| 45 | + * @param {string} key - `exports` or `imports` key |
| 46 | + * @param {pkg.Exports} target - Invalid package target |
| 47 | + * @param {boolean?} [internal=false] - `target` is package [`imports`][2]? |
| 48 | + * @param {string?} [base=''] - Id of module package was imported from |
| 49 | + * @return {string} Error message |
| 50 | + */ |
| 51 | + ( |
| 52 | + dir: string, |
| 53 | + key: string, |
| 54 | + target: pkg.Exports, |
| 55 | + internal: boolean = false, |
| 56 | + base: string = '' |
| 57 | + ): string => { |
| 58 | + /** |
| 59 | + * [`"exports"`][1] main target check. |
| 60 | + * |
| 61 | + * [1]: https://nodejs.org/api/packages.html#exports |
| 62 | + * |
| 63 | + * @see https://nodejs.org/api/packages.html#subpath-exports |
| 64 | + * |
| 65 | + * @const {boolean} main |
| 66 | + */ |
| 67 | + const main: boolean = !internal && key === '.' |
| 68 | + |
| 69 | + /** |
| 70 | + * Error message. |
| 71 | + * |
| 72 | + * @var {string} ret |
| 73 | + */ |
| 74 | + let ret: string = `Invalid "${internal ? 'imports' : 'exports'}"` |
| 75 | + |
| 76 | + // include if package target is main entry point |
| 77 | + if (main) ret += ' main' |
| 78 | + |
| 79 | + // add invalid package target |
| 80 | + ret += ` target ${JSON.stringify(target)} defined` |
| 81 | + |
| 82 | + // add key of invalid package target if target is not main entry point |
| 83 | + if (!main) ret += ` for '${key}'` |
| 84 | + |
| 85 | + // add package.json location |
| 86 | + ret += ` in the package config ${dir}package.json` |
| 87 | + |
| 88 | + // add import location |
| 89 | + if (base) ret += ` imported from ${base}` |
| 90 | + |
| 91 | + // add reason package target is invalid |
| 92 | + ret += |
| 93 | + typeof target === 'string' && !internal && !target.startsWith('./') |
| 94 | + ? '; targets must start with "./"' |
| 95 | + : '' |
| 96 | + |
| 97 | + return ret |
| 98 | + } |
| 99 | +) |
| 100 | + |
| 101 | +export default ERR_INVALID_PACKAGE_TARGET |
0 commit comments