Skip to content

Commit

Permalink
remove tsConfigPath option from Directory
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Nov 14, 2024
1 parent b9794c8 commit 3f3ab2b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
34 changes: 24 additions & 10 deletions packages/renoun/src/file-system/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,45 @@ import { minimatch } from 'minimatch'

import type { DirectoryEntry } from './types.js'

interface FileSystemOptions {
/**
* Path to the tsconfig.json file to use when analyzing types and determining if a file is excluded. */
tsConfigPath?: string
}

export abstract class FileSystem {
#tsConfigPath: string
#tsConfig?: any

constructor(tsConfigFilePath?: string) {
if (tsConfigFilePath) {
this.#tsConfig = this.#getTsConfig(tsConfigFilePath)
}
constructor(options: FileSystemOptions = {}) {
this.#tsConfigPath = options.tsConfigPath || 'tsconfig.json'
}

abstract readFileSync(path: string): string
abstract readFile(path: string): Promise<string>
abstract readDirectory(path?: string): Promise<DirectoryEntry[]>
abstract isFilePathGitIgnored(filePath: string): boolean

#getTsConfig = async (tsConfigFilePath: string) => {
const tsConfigContents = await this.readFile(tsConfigFilePath)
#getTsConfig() {
try {
const parsedTsConfig = JSON.parse(tsConfigContents)
return parsedTsConfig
const tsConfigContents = this.readFileSync(this.#tsConfigPath)
try {
const parsedTsConfig = JSON.parse(tsConfigContents)
return parsedTsConfig
} catch (error) {
throw new Error('Failed to parse tsconfig.json', { cause: error })
}
} catch (error) {
throw new Error(`Failed to parse tsconfig.json`, { cause: error })
return null
}
}

isFilePathExcludedFromTsConfig(filePath: string) {
if (!this.#tsConfig) {
if (this.#tsConfig === undefined) {
this.#tsConfig = this.#getTsConfig()
}

if (this.#tsConfig === null) {
return false
}

Expand Down
4 changes: 4 additions & 0 deletions packages/renoun/src/file-system/NodeFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class NodeFileSystem extends FileSystem {
})
}

readFileSync(path: string): string {
return readFileSync(path, 'utf-8')
}

async readFile(path: string): Promise<string> {
return readFile(path, 'utf-8')
}
Expand Down
1 change: 0 additions & 1 deletion packages/renoun/src/file-system/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ describe('file system', () => {
test('getRuntimeValue resolves export runtime value from getModule', async () => {
const FileSystemDirectory = new Directory({
path: 'src/file-system',
tsConfigPath: 'tsconfig.json',
getModule: (path) => import(`./${path}`),
})
const file = await FileSystemDirectory.getFileOrThrow('path', 'ts')
Expand Down
8 changes: 1 addition & 7 deletions packages/renoun/src/file-system/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ interface ExtensionSchemas {
interface DirectoryOptions<Types extends ExtensionTypes = ExtensionTypes> {
path?: string
rootPath?: string
tsConfigPath?: string
fileSystem?: FileSystem
directory?: Directory<any, any>
schema?: {
Expand All @@ -335,7 +334,6 @@ export class Directory<
#directory?: Directory<any, any>
#path: string
#rootPath: string
#tsConfigPath?: string
#schema?: DirectoryOptions<Types>['schema']
#getModule?: (path: string) => Promise<any>

Expand All @@ -346,7 +344,6 @@ export class Directory<
: join('.', options.path)
: '.'
this.#rootPath = options.rootPath ?? this.#path
this.#tsConfigPath = options.tsConfigPath
this.#fileSystem = options.fileSystem
this.#directory = options.directory
this.#schema = options.schema
Expand All @@ -358,7 +355,7 @@ export class Directory<
return this.#fileSystem
}
const { NodeFileSystem } = await import('./NodeFileSystem.js')
this.#fileSystem = new NodeFileSystem(this.#tsConfigPath)
this.#fileSystem = new NodeFileSystem()
return this.#fileSystem
}

Expand Down Expand Up @@ -470,7 +467,6 @@ export class Directory<
path: entry.path,
rootPath: this.#rootPath,
schema: this.#schema,
tsConfigPath: this.#tsConfigPath,
getModule: this.#getModule,
})
)
Expand All @@ -486,7 +482,6 @@ export class Directory<
absolutePath: entry.absolutePath,
getModule: this.#getModule,
schema: this.#schema,
tsConfigFilePath: this.#tsConfigPath,
isVirtualFileSystem: fileSystem instanceof VirtualFileSystem,
})
)
Expand All @@ -497,7 +492,6 @@ export class Directory<
path: entry.path,
absolutePath: entry.absolutePath,
schema: this.#schema,
tsConfigFilePath: this.#tsConfigPath,
isVirtualFileSystem: fileSystem instanceof VirtualFileSystem,
})
)
Expand Down

0 comments on commit 3f3ab2b

Please sign in to comment.