Skip to content

Commit

Permalink
feat: Rename useVirtualFileSystem to useInMemoryFileSystem
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `ProjectOptions#useVirtualFileSystem` is now `useInMemoryFileSystem`. This is a more accurate name.
  • Loading branch information
dsherret committed Nov 25, 2019
1 parent 48229e9 commit b69750c
Show file tree
Hide file tree
Showing 36 changed files with 247 additions and 218 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ before_script:
- travis_wait yarn build
- lerna run ensure-no-project-compile-errors
script:
- travis_wait lerna run test:ci # running coveralls might take a bit of time
- lerna run test:ci
- lerna run test:ts-versions
after_script:
- lerna run code-verification
1 change: 1 addition & 0 deletions docs/setup/ast-viewers.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Features:
* View the type and symbol of the selected node.
* Toggle the tree between `node.forEachChild(...)` and `node.getChildren()`.
* Change compiler API versions.
* Use some compiler objects in the browser console.

[![TypeScript AST Viewer](images/ts-ast-viewer.png)](http://ts-ast-viewer.com)

Expand Down
18 changes: 8 additions & 10 deletions docs/setup/file-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ title: File System

## File System

By default, the library will use the local file system based on the current working directory. In most scenarios, you won't have to bother with what's outlined here, but it may
by useful in some scenarios (for example, using a virtual file system is useful for mocking the file system for testing purposes).
By default, the library will use the local file system based on the current working directory. In most scenarios, you won't have to bother with what's outlined here, but it may be useful in some scenarios (for example, using an in-memory file system is useful for mocking the file system for testing purposes).

### Current File System Object

Expand All @@ -19,35 +18,34 @@ const fs = project.getFileSystem(); // returns: FileSystemHost
This file system object can be used to interact with the current file system. The methods available on it are very obvious and not worth explaining
here (ex. `writeFile(filePath: string, fileText: string): Promise<void>`, `readFile(filePath: string): Promise<string>`, `readFileSync(filePath: string): string`, etc..).

### Virtual File System
### In-Memory File System

If you want to use a virtual file system that is stored in memory, specify that when creating an `Ast` object:
If you want to use a file system that is stored in memory, specify that when creating a `Project` instance:

```ts
import { Project } from "ts-morph";

const project = new Project({ useVirtualFileSystem: true });
const project = new Project({ useInMemoryFileSystem: true });
const fs = project.getFileSystem();

// note that it's ok to use synchronous commands when using a virtual file system
const sourceFile = project.createSourceFile("file.ts", "console.log(5);");
sourceFile.saveSync();
fs.readFileSync("file.ts"); // returns: "console.log(5);"
console.log(fs.readFileSync("file.ts")); // outputs: "console.log(5);"
```

The current working directory on this file system will be `/`.

#### `lib.d.ts` files

Since ts-morph 6.0, the virtual file system will have the [`lib.d.ts` files](https://github.com/Microsoft/TypeScript/tree/master/lib) loaded into the `/node_modules/typescript/lib` folder by default.
Since ts-morph 6.0, the in memory file system will have the [`lib.d.ts` files](https://github.com/Microsoft/TypeScript/tree/master/lib) loaded into the `/node_modules/typescript/lib` folder by default.

If you want the old behaviour, you can specify to skip loading them by providing a `skipLoadingLibFiles` option:

```ts ignore-error: 1109
import { Project, FileSystemHost } from "ts-morph";

const project = new Project({
useVirtualFileSystem: true,
inMemoryFileSystem: true,
skipLoadingLibFiles: true
});

Expand All @@ -58,7 +56,7 @@ When using a non-default file system, the library will search for these files in

### Custom File System

It's possible to use your own custom file system by implementing the `FileSystemHost` interface then passing in an instance of this when creating a new `Ast` object:
It's possible to use your own custom file system by implementing the `FileSystemHost` interface then passing in an instance of this when creating a new `Project` instance:

```ts ignore-error: 2420, 2345
import { Project, FileSystemHost } from "ts-morph";
Expand Down
10 changes: 5 additions & 5 deletions packages/bootstrap/lib/ts-morph-bootstrap.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ export interface ProjectOptions {
/** Skip resolving file dependencies when providing a ts config file path and adding the files from tsconfig. */
skipFileDependencyResolution?: boolean;
/** Whether to use an in-memory file system. */
useVirtualFileSystem?: boolean;
useInMemoryFileSystem?: boolean;
/** Skip loading the lib files when using an in-memory file system. @default false */
skipLoadingLibFiles?: boolean;
/**
* Optional file system host. Useful for mocking access to the file system.
* @remarks Consider using `useVirtualFileSystem` instead.
* @remarks Consider using `inMemoryFileSystem` instead.
*/
fileSystem?: FileSystemHost;
/** Creates a resolution host for specifying custom module and/or type reference directive resolution. */
resolutionHost?: ResolutionHostFactory;
}

/**
* Project that holds source files.
*/
/** Project that holds source files. */
export declare class Project {
/**
* Initializes a new instance.
Expand Down
4 changes: 2 additions & 2 deletions packages/bootstrap/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const project = new Project({ tsConfigFilePath: "tsconfig.json" });
const project = new Project();

// in memory file system
const project2 = new Project({ useVirtualFileSystem: true });
const project2 = new Project({ inMemoryFileSystem: true });

// custom file system
const fileSystem: FileSystemHost = { ...etc... };
Expand Down Expand Up @@ -182,7 +182,7 @@ project.removeSourceFile(sourceFile);
```ts
import { Project, ts } from "@ts-morph/bootstrap";

const project = new Project({ useVirtualFileSystem: true });
const project = new Project({ inMemoryFileSystem: true });
project.createSourceFile("test.ts", "const t: string = 5;");

const program = project.createProgram();
Expand Down
33 changes: 21 additions & 12 deletions packages/bootstrap/src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ export interface ProjectOptions {
/** Skip resolving file dependencies when providing a ts config file path and adding the files from tsconfig. */
skipFileDependencyResolution?: boolean;
/** Whether to use an in-memory file system. */
useVirtualFileSystem?: boolean;
useInMemoryFileSystem?: boolean;
/** Skip loading the lib files when using an in-memory file system. @default false */
skipLoadingLibFiles?: boolean;
/**
* Optional file system host. Useful for mocking access to the file system.
* @remarks Consider using `useVirtualFileSystem` instead.
* @remarks Consider using `inMemoryFileSystem` instead.
*/
fileSystem?: FileSystemHost;
/** Creates a resolution host for specifying custom module and/or type reference directive resolution. */
resolutionHost?: ResolutionHostFactory;
}

/**
* Project that holds source files.
*/
/** Project that holds source files. */
export class Project {
/** @internal */
private readonly _sourceFileCache: SourceFileCache;
Expand All @@ -41,6 +41,8 @@ export class Project {
* @param options - Optional options.
*/
constructor(options: ProjectOptions = {}) {
verifyOptions();

this.fileSystem = getFileSystem();
this._fileSystemWrapper = new TransactionalFileSystem(this.fileSystem);

Expand Down Expand Up @@ -86,13 +88,20 @@ export class Project {
this.resolveSourceFileDependencies();
}

function getFileSystem(): FileSystemHost {
// setup file system
if (options.fileSystem != null && options.useVirtualFileSystem)
throw new errors.InvalidOperationError("Cannot provide a file system when specifying to use a virtual file system.");
else if (options.useVirtualFileSystem)
return new InMemoryFileSystemHost();
return options.fileSystem || new RealFileSystemHost();
function verifyOptions() {
if (options.fileSystem != null && options.useInMemoryFileSystem)
throw new errors.InvalidOperationError("Cannot provide a file system when specifying to use an in-memory file system.");
if (options.skipLoadingLibFiles && !options.useInMemoryFileSystem) {
throw new errors.InvalidOperationError(
`The ${nameof(options.skipLoadingLibFiles)} option can only be true when ${nameof(options.useInMemoryFileSystem)} is true.`
);
}
}

function getFileSystem() {
if (options.useInMemoryFileSystem)
return new InMemoryFileSystemHost({ skipLoadingLibFiles: options.skipLoadingLibFiles });
return options.fileSystem ?? new RealFileSystemHost();
}

function getCompilerOptions(): ts.CompilerOptions {
Expand Down

0 comments on commit b69750c

Please sign in to comment.