Skip to content

Commit

Permalink
chore: switch homebrewed NodeError → official `NodeJS.ErrnoExceptio…
Browse files Browse the repository at this point in the history
…n` (#14)
  • Loading branch information
JakobJingleheimer authored Dec 22, 2024
1 parent e891923 commit edd8429
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
4 changes: 2 additions & 2 deletions recipes/correct-ts-specifiers/src/get-not-found-url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pathToFileURL } from 'node:url';

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

export const getNotFoundUrl = (err: NodeError) =>
export const getNotFoundUrl = (err: NodeJS.ErrnoException & { url?: FSAbsolutePath }) =>
pathToFileURL(err?.url ?? err.message.split("'")[1])?.href as ResolvedSpecifier;
6 changes: 0 additions & 6 deletions recipes/correct-ts-specifiers/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,3 @@ export type Specifier = URL['pathname'] | ResolvedSpecifier;
* @example 'foo/bar'
*/
export type NodeModSpecifier = string | `${string}/${string}`;

export type NodeError = Error &
Partial<{
code: string;
url: FSAbsolutePath;
}>;
3 changes: 1 addition & 2 deletions recipes/correct-ts-specifiers/src/is-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { lstat } from 'node:fs/promises';

import type {
FSAbsolutePath,
NodeError,
NodeModSpecifier,
ResolvedSpecifier,
Specifier,
Expand All @@ -14,7 +13,7 @@ export async function isDir(parentPath: FSAbsolutePath | ResolvedSpecifier, spec
try {
resolvedSpecifier = resolveSpecifier(parentPath, specifier);
} catch (err) {
if ((err as NodeError).code === 'ERR_MODULE_NOT_FOUND') return null;
if ((err as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND') return null;
}

try {
Expand Down
7 changes: 5 additions & 2 deletions recipes/correct-ts-specifiers/src/is-ignorable-specifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { extname, sep } from 'node:path';
import { pathToFileURL } from 'node:url';

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

Expand Down Expand Up @@ -34,7 +34,10 @@ export function isIgnorableSpecifier(parentPath: FSAbsolutePath, specifier: stri
pathToFileURL(parentPath).href,
) as ResolvedSpecifier; // [1]
} catch (err) {
if (!(err instanceof Error) || !IGNORABLE_RESOLVE_ERRORS.has((err as NodeError).code!))
if (
!(err instanceof Error)
|| !IGNORABLE_RESOLVE_ERRORS.has((err as NodeJS.ErrnoException).code!)
)
throw err;

resolvedSpecifier = getNotFoundUrl(err);
Expand Down
3 changes: 1 addition & 2 deletions recipes/correct-ts-specifiers/src/resolve-specifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
/* node:coverage disable */
import type {
FSAbsolutePath,
NodeError,
NodeModSpecifier,
ResolvedSpecifier,
Specifier,
Expand Down Expand Up @@ -42,7 +41,7 @@ export function resolveSpecifier(
if (!(err instanceof Error)) throw err;

if (
(err as NodeError).code === 'ERR_MODULE_NOT_FOUND' &&
(err as NodeJS.ErrnoException).code === 'ERR_MODULE_NOT_FOUND' &&
resolvesToNodeModule(getNotFoundUrl(err), parentUrl)
) {
return specifier as NodeModSpecifier;
Expand Down

0 comments on commit edd8429

Please sign in to comment.