Skip to content

Commit edd8429

Browse files
chore: switch homebrewed NodeError → official NodeJS.ErrnoException (#14)
1 parent e891923 commit edd8429

File tree

5 files changed

+9
-14
lines changed

5 files changed

+9
-14
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { pathToFileURL } from 'node:url';
22

3-
import type { NodeError, ResolvedSpecifier } from './index.d.ts';
3+
import type { FSAbsolutePath, ResolvedSpecifier } from './index.d.ts';
44

5-
export const getNotFoundUrl = (err: NodeError) =>
5+
export const getNotFoundUrl = (err: NodeJS.ErrnoException & { url?: FSAbsolutePath }) =>
66
pathToFileURL(err?.url ?? err.message.split("'")[1])?.href as ResolvedSpecifier;

recipes/correct-ts-specifiers/src/index.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,3 @@ export type Specifier = URL['pathname'] | ResolvedSpecifier;
2020
* @example 'foo/bar'
2121
*/
2222
export type NodeModSpecifier = string | `${string}/${string}`;
23-
24-
export type NodeError = Error &
25-
Partial<{
26-
code: string;
27-
url: FSAbsolutePath;
28-
}>;

recipes/correct-ts-specifiers/src/is-dir.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { lstat } from 'node:fs/promises';
22

33
import type {
44
FSAbsolutePath,
5-
NodeError,
65
NodeModSpecifier,
76
ResolvedSpecifier,
87
Specifier,
@@ -14,7 +13,7 @@ export async function isDir(parentPath: FSAbsolutePath | ResolvedSpecifier, spec
1413
try {
1514
resolvedSpecifier = resolveSpecifier(parentPath, specifier);
1615
} catch (err) {
17-
if ((err as NodeError).code === 'ERR_MODULE_NOT_FOUND') return null;
16+
if ((err as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND') return null;
1817
}
1918

2019
try {

recipes/correct-ts-specifiers/src/is-ignorable-specifier.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { extname, sep } from 'node:path';
33
import { pathToFileURL } from 'node:url';
44

55
import { tsExts } from './exts.ts';
6-
import type { FSAbsolutePath, NodeError, ResolvedSpecifier, Specifier } from './index.d.ts';
6+
import type { FSAbsolutePath, ResolvedSpecifier, Specifier } from './index.d.ts';
77
import { resolvesToNodeModule } from './resolves-to-node-module.ts';
88
import { getNotFoundUrl } from './get-not-found-url.ts';
99

@@ -34,7 +34,10 @@ export function isIgnorableSpecifier(parentPath: FSAbsolutePath, specifier: stri
3434
pathToFileURL(parentPath).href,
3535
) as ResolvedSpecifier; // [1]
3636
} catch (err) {
37-
if (!(err instanceof Error) || !IGNORABLE_RESOLVE_ERRORS.has((err as NodeError).code!))
37+
if (
38+
!(err instanceof Error)
39+
|| !IGNORABLE_RESOLVE_ERRORS.has((err as NodeJS.ErrnoException).code!)
40+
)
3841
throw err;
3942

4043
resolvedSpecifier = getNotFoundUrl(err);

recipes/correct-ts-specifiers/src/resolve-specifier.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
44
/* node:coverage disable */
55
import type {
66
FSAbsolutePath,
7-
NodeError,
87
NodeModSpecifier,
98
ResolvedSpecifier,
109
Specifier,
@@ -42,7 +41,7 @@ export function resolveSpecifier(
4241
if (!(err instanceof Error)) throw err;
4342

4443
if (
45-
(err as NodeError).code === 'ERR_MODULE_NOT_FOUND' &&
44+
(err as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND' &&
4645
resolvesToNodeModule(getNotFoundUrl(err), parentUrl)
4746
) {
4847
return specifier as NodeModSpecifier;

0 commit comments

Comments
 (0)