Skip to content

Commit

Permalink
remove rootPath option
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Nov 14, 2024
1 parent 3f3ab2b commit 5b64b34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
9 changes: 9 additions & 0 deletions packages/renoun/src/file-system/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { minimatch } from 'minimatch'
import type { DirectoryEntry } from './types.js'

interface FileSystemOptions {
/** Base path to use when reading files. */
basePath?: string

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

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

constructor(options: FileSystemOptions = {}) {
this.#basePath = options.basePath || '.'
this.#tsConfigPath = options.tsConfigPath || 'tsconfig.json'
}

Expand All @@ -21,6 +26,10 @@ export abstract class FileSystem {
abstract readDirectory(path?: string): Promise<DirectoryEntry[]>
abstract isFilePathGitIgnored(filePath: string): boolean

getPath() {
return this.#basePath
}

#getTsConfig() {
try {
const tsConfigContents = this.readFileSync(this.#tsConfigPath)
Expand Down
41 changes: 15 additions & 26 deletions packages/renoun/src/file-system/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export class File {
return this.#path
}

/** Get the path of the file relative to another path. */
getPathRelativeTo(path: string) {
return relative(path, this.#path)
}

/** Get the absolute path of the file. */
getAbsolutePath() {
return this.#absolutePath
Expand Down Expand Up @@ -91,17 +96,6 @@ export class JavaScriptFileExport<
getName() {
return this.#name
}

getRelativePath() {
return relative(
this.#file.getDirectory().getRootPath(),
this.#file.getPath()
)
}

getAbsolutePath() {
return this.#file.getAbsolutePath()
}
}

interface JavaScriptFileOptions extends FileOptions {
Expand Down Expand Up @@ -225,12 +219,15 @@ export class JavaScriptFileExportWithRuntime<
*/
async getRuntimeValue(): Promise<Exports[ExportName]> {
const exportName = this.getName()
const fileModule = await this.#getModule(this.getRelativePath())
const fileSystem = await this.#file.getDirectory().getFileSystem()
const fileModule = await this.#getModule(
this.#file.getPathRelativeTo(fileSystem.getPath())
)
const fileModuleExport = fileModule[this.getName()]

if (fileModuleExport === undefined) {
throw new Error(
`[renoun] JavaScript file export "${String(this.getName())}" not found in ${this.getAbsolutePath()}`
`[renoun] JavaScript file export "${String(this.getName())}" not found in ${this.#file.getAbsolutePath()}`
)
}

Expand Down Expand Up @@ -312,7 +309,6 @@ interface ExtensionSchemas {

interface DirectoryOptions<Types extends ExtensionTypes = ExtensionTypes> {
path?: string
rootPath?: string
fileSystem?: FileSystem
directory?: Directory<any, any>
schema?: {
Expand All @@ -333,7 +329,7 @@ export class Directory<
#fileSystem: FileSystem | undefined
#directory?: Directory<any, any>
#path: string
#rootPath: string

#schema?: DirectoryOptions<Types>['schema']
#getModule?: (path: string) => Promise<any>

Expand All @@ -343,19 +339,18 @@ export class Directory<
? options.path
: join('.', options.path)
: '.'
this.#rootPath = options.rootPath ?? this.#path
this.#fileSystem = options.fileSystem
this.#directory = options.directory
this.#schema = options.schema
this.#getModule = options.getModule
}

async #getFileSystem() {
async getFileSystem() {
if (this.#fileSystem) {
return this.#fileSystem
}
const { NodeFileSystem } = await import('./NodeFileSystem.js')
this.#fileSystem = new NodeFileSystem()
this.#fileSystem = new NodeFileSystem({ basePath: this.#path })
return this.#fileSystem
}

Expand All @@ -374,7 +369,7 @@ export class Directory<
| undefined
> {
const normalizedPath = Array.isArray(path) ? join(...path) : path
const filePath = join(this.#rootPath, normalizedPath)
const filePath = join(this.#path, normalizedPath)
const allFiles = await this.getEntries()
const fileExtensions = Array.isArray(extension) ? extension : [extension]

Expand Down Expand Up @@ -447,7 +442,7 @@ export class Directory<
* from the tsconfig file if configured.
*/
async getEntries(): Promise<FileSystemEntry<any>[]> {
const fileSystem = await this.#getFileSystem()
const fileSystem = await this.getFileSystem()
const directoryEntries = await fileSystem.readDirectory(this.#path)
const entries: FileSystemEntry<any>[] = []

Expand All @@ -465,7 +460,6 @@ export class Directory<
fileSystem,
directory: this,
path: entry.path,
rootPath: this.#rootPath,
schema: this.#schema,
getModule: this.#getModule,
})
Expand Down Expand Up @@ -543,11 +537,6 @@ export class Directory<
getAbsolutePath() {
return this.#path
}

/** Get the path of the root directory. */
getRootPath() {
return this.#rootPath
}
}

/** Determines if a `FileSystemEntry` is a `File`. */
Expand Down

0 comments on commit 5b64b34

Please sign in to comment.