Skip to content

Instantly share code, notes, and snippets.

@lacolaco
Created September 11, 2020 01:27
Show Gist options
  • Save lacolaco/b3e259eb38d33167dd732288286b0492 to your computer and use it in GitHub Desktop.
Save lacolaco/b3e259eb38d33167dd732288286b0492 to your computer and use it in GitHub Desktop.
Angular Schematics Virtual FS for ts-morph
import { normalize, resolve } from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import { FileSystemHost } from 'ts-morph';
import * as ts from 'typescript';
export class VirtualFileSystem implements FileSystemHost {
constructor(private readonly tree: Tree, private readonly rootDir: string) {}
private resolvePath(filePath: string) {
return normalize(resolve(normalize(this.rootDir), normalize(filePath)));
}
isCaseSensitive(): boolean {
return ts.sys.useCaseSensitiveFileNames;
}
async delete(path: string): Promise<void> {
return this.deleteSync(path);
}
deleteSync(path: string): void {
return this.tree.delete(path);
}
readDirSync(dirPath: string): string[] {
return this.tree.getDir(dirPath).subfiles.map((path) => this.readFileSync(path));
}
async readFile(filePath: string, encoding?: string | undefined): Promise<string> {
return this.readFileSync(filePath, encoding);
}
readFileSync(filePath: string, encoding?: string | undefined): string {
// tslint:disable-next-line: no-non-null-assertion
return this.tree.get(this.resolvePath(filePath))!.content.toString(encoding);
}
async writeFile(filePath: string, fileText: string): Promise<void> {
return this.writeFileSync(filePath, fileText);
}
writeFileSync(filePath: string, fileText: string): void {
return this.tree.overwrite(filePath, fileText);
}
async mkdir(dirPath: string): Promise<void> {
return this.mkdirSync(dirPath);
}
mkdirSync(dirPath: string): void {
return this.tree.create(`${dirPath}/.gitkeep`, '');
}
async move(srcPath: string, destPath: string): Promise<void> {
return this.moveSync(srcPath, destPath);
}
moveSync(srcPath: string, destPath: string): void {
return this.tree.rename(srcPath, destPath);
}
async copy(srcPath: string, destPath: string): Promise<void> {
return this.copySync(srcPath, destPath);
}
copySync(srcPath: string, destPath: string): void {
throw new Error('Method not implemented.');
}
async fileExists(filePath: string): Promise<boolean> {
return this.fileExists(filePath);
}
fileExistsSync(filePath: string): boolean {
return this.tree.exists(filePath);
}
async directoryExists(dirPath: string): Promise<boolean> {
return this.directoryExistsSync(dirPath);
}
directoryExistsSync(dirPath: string): boolean {
return this.tree.exists(dirPath);
}
realpathSync(path: string): string {
throw new Error('Method not implemented.');
}
getCurrentDirectory(): string {
return this.tree.root.path.toString();
}
glob(patterns: readonly string[]): Promise<string[]> {
throw new Error('Method not implemented.');
}
globSync(patterns: readonly string[]): string[] {
throw new Error('Method not implemented.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment