-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr-invalid-package-target.ts
163 lines (151 loc) · 4.32 KB
/
err-invalid-package-target.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @file Errors - ERR_INVALID_PACKAGE_TARGET
* @module errnode/errors/ERR_INVALID_PACKAGE_TARGET
* @see https://github.com/nodejs/node/blob/v22.7.0/lib/internal/errors.js#L1488-L1503
*/
import E from '#e'
import { codes } from '#src/enums'
import type {
NodeError,
NodeErrorConstructor,
Stringifiable
} from '#src/interfaces'
import { DOT } from '@flex-development/tutils'
import { ok } from 'devlop'
/**
* `ERR_INVALID_PACKAGE_TARGET` schema.
*
* @see {@linkcode NodeError}
* @see https://nodejs.org/api/errors.html#err_invalid_package_target
*
* @extends {NodeError<codes.ERR_INVALID_PACKAGE_TARGET>}
*/
interface ErrInvalidPackageTarget
extends NodeError<codes.ERR_INVALID_PACKAGE_TARGET> {}
/**
* `ERR_INVALID_PACKAGE_TARGET` message arguments.
*
* @see {@linkcode Stringifiable}
*/
type Args = [
dir: Stringifiable,
subpath: string,
target: unknown,
isImports?: boolean | null | undefined,
base?: Stringifiable | null | undefined
]
/**
* `ERR_INVALID_PACKAGE_TARGET` constructor.
*
* @see {@linkcode Args}
* @see {@linkcode ErrInvalidPackageTarget}
* @see {@linkcode NodeErrorConstructor}
*
* @extends {NodeErrorConstructor<ErrInvalidPackageTarget,Args>}
*/
interface ErrInvalidPackageTargetConstructor
extends NodeErrorConstructor<ErrInvalidPackageTarget, Args> {
/**
* Create a new `ERR_INVALID_PACKAGE_TARGET` error.
*
* @see {@linkcode ErrInvalidPackageTarget}
* @see {@linkcode Stringifiable}
*
* @param {Stringifiable} dir
* Package directory module id
* @param {string} subpath
* Package subpath
* @param {unknown} target
* Invalid package target
* @param {boolean | null | undefined} [isImports]
* Internal `subpath`?
* @param {Stringifiable | null | undefined} [base]
* Parent module id
* @return {ErrInvalidPackageTarget}
*/
new (
dir: Stringifiable,
subpath: string,
target: unknown,
isImports?: boolean | null | undefined,
base?: Stringifiable | null | undefined
): ErrInvalidPackageTarget
}
/**
* `ERR_INVALID_PACKAGE_TARGET` model.
*
* Thrown when a `package.json` [`"exports"`][1] or [`"imports"`][2] field
* contains an invalid target mapping value for the attempted module resolution.
*
* [1]: https://nodejs.org/api/packages.html#exports
* [2]: https://nodejs.org/api/packages.html#imports
*
* @see {@linkcode ErrInvalidPackageTargetConstructor}
*
* @type {ErrInvalidPackageTargetConstructor}
*
* @class
*/
const ERR_INVALID_PACKAGE_TARGET: ErrInvalidPackageTargetConstructor = E(
codes.ERR_INVALID_PACKAGE_TARGET,
Error,
/**
* @param {Stringifiable} dir
* Package directory module id
* @param {string} subpath
* Package subpath
* @param {unknown} target
* Invalid package target
* @param {boolean | null | undefined} [isImports]
* Internal `subpath`?
* @param {Stringifiable | null | undefined} [base]
* Parent module id
* @return {string}
* Error message
*/
function message(
dir: Stringifiable,
subpath: string,
target: unknown,
isImports: boolean | null | undefined = false,
base: Stringifiable | null | undefined = null
): string {
ok(
String(dir).endsWith('/'),
'expected `dir` to end with trailing slash ("/")'
)
/**
* Relative error?
*
* @const {boolean} relativeError
*/
const relativeError: boolean = typeof target === 'string' &&
isImports !== true &&
target.length > 0 &&
!target.startsWith(`${DOT}/`)
/**
* Error message.
*
* @var {string} message
*/
let message: string = ''
if (subpath === DOT) {
ok(isImports !== true, 'expected `internal` to not be `true`')
message += `Invalid "exports" main target ${JSON.stringify(target)}` +
` defined in the package config ${String(dir)}package.json`
} else {
message = `Invalid "${isImports ? 'imports' : 'exports'}" target` +
` ${JSON.stringify(target)} defined for '${subpath}' in the package` +
` config ${String(dir)}package.json`
}
if (base !== null) message += ` imported from ${String(base)}`
if (relativeError) message += `; targets must start with "${DOT}/"`
return message
}
)
export {
ERR_INVALID_PACKAGE_TARGET as default,
type ErrInvalidPackageTarget,
type Args as ErrInvalidPackageTargetArgs,
type ErrInvalidPackageTargetConstructor
}