Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Created October 18, 2024 19:44
Show Gist options
  • Save aleclarson/14d4a3d3634917dcacb3ed608c7e7a1d to your computer and use it in GitHub Desktop.
Save aleclarson/14d4a3d3634917dcacb3ed608c7e7a1d to your computer and use it in GitHub Desktop.
import { FileSystemHost, RuntimeDirEntry } from '@ts-morph/common'
class MemfsFileSystemHost implements FileSystemHost {
constructor(private readonly root: string) {}
isCaseSensitive(): boolean {
return true
}
readDirSync(dirPath: string): RuntimeDirEntry[] {
console.log('readDirSync:', dirPath)
return vfs
.readdirSync(path.resolve(this.root, dirPath), {
withFileTypes: true,
})
.map(file => ({
name: file.name,
get isFile() {
return file.isFile()
},
get isDirectory() {
return file.isDirectory()
},
get isSymlink() {
return file.isSymbolicLink()
},
}))
}
async readFile(filePath: string, encoding = 'utf-8') {
return this.readFileSync(filePath, encoding)
}
readFileSync(filePath: string, encoding = 'utf-8') {
console.log('readFileSync:', filePath)
return vfs.readFileSync(
path.resolve(this.root, filePath),
encoding as BufferEncoding
)
}
async fileExists(filePath: string) {
return this.fileExistsSync(path.resolve(this.root, filePath))
}
fileExistsSync(filePath: string) {
console.log('fileExistsSync:', filePath)
const stat = vfs.statSync(path.resolve(this.root, filePath), {
throwIfNoEntry: false,
})
return stat !== undefined && stat.isFile()
}
async directoryExists(dirPath: string) {
return this.directoryExistsSync(path.resolve(this.root, dirPath))
}
directoryExistsSync(dirPath: string): boolean {
// console.log('directoryExistsSync:', dirPath)
const stat = vfs.statSync(path.resolve(this.root, dirPath), {
throwIfNoEntry: false,
})
return stat !== undefined && stat.isDirectory()
}
realpathSync(path: string) {
return path
}
getCurrentDirectory() {
return this.root
}
async glob(patterns: ReadonlyArray<string>) {
return this.globSync(patterns)
}
globSync(patterns: ReadonlyArray<string>): string[] {
console.log('globSync:', patterns)
return globSync(patterns as string[], { cwd: this.root })
}
async delete(path: string) {
notImplemented('delete')
}
deleteSync(path: string): void {
notImplemented('deleteSync')
}
async writeFile(filePath: string, fileText: string) {
notImplemented('writeFile')
}
writeFileSync(filePath: string, fileText: string) {
notImplemented('writeFileSync')
}
async mkdir(dirPath: string) {
notImplemented('mkdir')
}
mkdirSync(dirPath: string) {
notImplemented('mkdirSync')
}
async move(srcPath: string, destPath: string) {
notImplemented('move')
}
moveSync(srcPath: string, destPath: string) {
notImplemented('moveSync')
}
async copy(srcPath: string, destPath: string) {
notImplemented('copy')
}
copySync(srcPath: string, destPath: string) {
notImplemented('copySync')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment