Skip to content

Commit

Permalink
feat(interfaces): ModuleResolutionHost
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 23, 2024
1 parent 3bd4124 commit 776f17d
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ This package is fully typed with [TypeScript][].

- [`FileSystem`](./src/interfaces/file-system.mts)
- [`LoadTsconfigOptions`](./src/interfaces/options-load-tsconfig.mts)
- [`ModuleResolutionHost`](./src/interfaces/host-module-resolution.mts)
- [`ReadTsconfigOptions`](./src/interfaces/options-read-tsconfig.mts)
- [`ResolvePathOptions`](./src/interfaces/options-resolve-path.mts)
- [`ResolvedTsconfig`](./src/interfaces/options-resolve-path.mts)
Expand Down
114 changes: 114 additions & 0 deletions src/interfaces/__tests__/host-module-resolution.spec-d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* @file Type Tests - ModuleResolutionHost
* @module tsconfig-utils/interfaces/tests/unit-d/ModuleResolutionHost
*/

import type TestSubject from '#interfaces/host-module-resolution'
import type { ModuleId } from '@flex-development/mlly'
import type ts from 'typescript'

describe('unit-d:interfaces/ModuleResolutionHost', () => {
it('should match ts.ModuleResolutionHost', () => {
expectTypeOf<TestSubject>().toMatchTypeOf<ts.ModuleResolutionHost>()
})

describe('directoryExists', () => {
type Subject = TestSubject['directoryExists']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
})

describe('parameters', () => {
it('should be callable with [ModuleId]', () => {
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
})
})

describe('returns', () => {
it('should return boolean', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<boolean>()
})
})
})

describe('fileExists', () => {
type Subject = TestSubject['fileExists']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
})

describe('parameters', () => {
it('should be callable with [ModuleId]', () => {
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
})
})

describe('returns', () => {
it('should return boolean', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<boolean>()
})
})
})

describe('getDirectories', () => {
type Subject = TestSubject['getDirectories']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
})

describe('parameters', () => {
it('should be callable with [ModuleId]', () => {
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
})
})

describe('returns', () => {
it('should return string[]', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<string[]>()
})
})
})

describe('readFile', () => {
type Subject = TestSubject['readFile']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
})

describe('parameters', () => {
it('should be callable with [ModuleId]', () => {
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
})
})

describe('returns', () => {
it('should return string | undefined', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<string | undefined>()
})
})
})

describe('realpath', () => {
type Subject = TestSubject['realpath']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
})

describe('parameters', () => {
it('should be callable with [ModuleId]', () => {
expectTypeOf<Subject>().parameters.toEqualTypeOf<[ModuleId]>()
})
})

describe('returns', () => {
it('should return string', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<string>()
})
})
})
})
101 changes: 101 additions & 0 deletions src/interfaces/host-module-resolution.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @file Interfaces - ModuleResolutionHost
* @module tsconfig-utils/interfaces/ModuleResolutionHost
*/

import type { ModuleId } from '@flex-development/mlly'

/**
* Module resolution host API.
*
* The module resolution host acts a bridge between the TypeScript compiler and
* the file system.
*/
interface ModuleResolutionHost {
/**
* Check if a directory exists at `id`.
*
* @see {@linkcode ModuleId}
*
* @this {void}
*
* @param {ModuleId} id
* The module id to check
* @return {boolean}
* `true` if directory exists at `id`, `false` otherwise
*/
directoryExists(this: void, id: ModuleId): boolean

/**
* Check if a file exists at `id`.
*
* @see {@linkcode ModuleId}
*
* @this {void}
*
* @param {ModuleId} id
* The module id to check
* @return {boolean}
* `true` if file exists at `id`, `false` otherwise
*/
fileExists(this: void, id: ModuleId): boolean

/**
* Get the path to current working directory.
*
* @this {void}
*
* @return {string}
* Path to current working directory
*/
getCurrentDirectory(this: void): string

/**
* Get a list of subdirectories.
*
* @see {@linkcode ModuleId}
*
* @this {void}
*
* @param {ModuleId} id
* The directory path or URL to read
* @return {string[]}
* List of subdirectory names
*/
getDirectories(this: void, id: ModuleId): string[]

/**
* Get the contents of a file.
*
* @see {@linkcode ModuleId}
*
* @this {void}
*
* @param {ModuleId} id
* Module id of file
* @return {Buffer | string}
* File contents or `undefined` if file does not exist at `id`
*/
readFile(this: void, id: ModuleId): string | undefined

/**
* Get the resolved pathname for `id`.
*
* @see {@linkcode ModuleId}
*
* @this {void}
*
* @param {ModuleId} id
* The path or `file:` URL to handle
* @return {string}
* Resolved pathname
*/
realpath(this: void, id: ModuleId): string

/**
* Treat filenames as case-sensitive?
*/
useCaseSensitiveFileNames?: ((this: void) => boolean) | boolean | undefined
}

export type { ModuleResolutionHost as default }
3 changes: 3 additions & 0 deletions src/interfaces/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*/

export type { default as FileSystem } from '#interfaces/file-system'
export type {
default as ModuleResolutionHost
} from '#interfaces/host-module-resolution'
export type {
default as LoadTsconfigOptions
} from '#interfaces/options-load-tsconfig'
Expand Down

0 comments on commit 776f17d

Please sign in to comment.