From 47730eab7e66509fc945e8a653e0feb93c9eeda1 Mon Sep 17 00:00:00 2001 From: Travis Arnold Date: Sun, 17 Nov 2024 12:25:06 -0800 Subject: [PATCH] clean up file extension utilities --- packages/renoun/src/file-system/index.test.ts | 1 + packages/renoun/src/file-system/index.tsx | 54 +++++++------------ 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/packages/renoun/src/file-system/index.test.ts b/packages/renoun/src/file-system/index.test.ts index ba2ef902..0f6a15e2 100644 --- a/packages/renoun/src/file-system/index.test.ts +++ b/packages/renoun/src/file-system/index.test.ts @@ -1,4 +1,5 @@ import { describe, test, expect, expectTypeOf } from 'vitest' +import type { MDXContent } from '@renoun/mdx' import { runInNewContext } from 'node:vm' import { VirtualFileSystem } from './VirtualFileSystem' diff --git a/packages/renoun/src/file-system/index.tsx b/packages/renoun/src/file-system/index.tsx index 32e686e5..da8fa500 100644 --- a/packages/renoun/src/file-system/index.tsx +++ b/packages/renoun/src/file-system/index.tsx @@ -50,17 +50,7 @@ export class File { /** Narrow the file type based on its extension. */ hasExtension( extension: Extension - ): this is Extension extends string - ? IsJavaScriptLikeExtension extends true - ? JavaScriptFile - : File - : Extension extends string[] - ? HasJavaScriptLikeExtensions extends true - ? JavaScriptFile< - Types[Extract] - > - : File - : File { + ): this is FileWithExtension { const fileExtension = this.getExtension() if (extension instanceof Array) { @@ -840,26 +830,23 @@ export function isDirectory(entry: FileSystemEntry): entry is Directory { /** Determines if a `FileSystemEntry` is a `File`. */ export function isFile( - entry: FileSystemEntry + entry: FileSystemEntry ): entry is File { return entry instanceof File } /** Determines if a `FileSystemEntry` is a `JavaScriptFile`. */ -export function isJavaScriptFile( +export function isJavaScriptFile( entry: FileSystemEntry -): entry is JavaScriptFile { +): entry is JavaScriptFile { return entry instanceof JavaScriptFile } -/** Determines if a `FileSystemEntry` is a `File` with a specific extension. */ -export function isFileWithExtension< +/** Determines the type of a `FileSystemEntry` based on its extension. */ +export type FileWithExtension< Types extends ExtensionTypes, - const Extension extends string | string[], ->( - entry: FileSystemEntry, - extension: Extension -): entry is Extension extends string + Extension extends keyof Types | (keyof Types)[], +> = Extension extends string ? IsJavaScriptLikeExtension extends true ? JavaScriptFile : File @@ -869,19 +856,18 @@ export function isFileWithExtension< Types[Extract] > : File - : File { - if (!isFile(entry)) { - return false - } + : File - if (extension instanceof Array) { - for (const possibleExtension of extension) { - if (entry.hasExtension(possibleExtension)) { - return true - } - } - return false +/** Determines if a `FileSystemEntry` is a `File` with a specific extension. */ +export function isFileWithExtension< + Types extends ExtensionTypes, + const Extension extends string | string[], +>( + entry: FileSystemEntry, + extension: Extension +): entry is FileWithExtension { + if (isFile(entry)) { + return (entry as File).hasExtension(extension) } - - return entry.hasExtension(extension) + return false }