@ts-morph/bootstrap
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -33,14 +33,13 @@ 'use strict'; | ||
this.compilerOptions = compilerOptions; | ||
this.sourceFilesByFilePath = new common.KeyValueCache(); | ||
this.sourceFilesByFilePath = new Map(); | ||
this.documentRegistry = new common.DocumentRegistry(fileSystemWrapper); | ||
} | ||
containsSourceFileAtPath(filePath) { | ||
filePath = this.fileSystemWrapper.getStandardizedAbsolutePath(filePath); | ||
return this.sourceFilesByFilePath.has(filePath); | ||
} | ||
getSourceFilePaths() { | ||
return Array.from(this.sourceFilesByFilePath.getKeys()); | ||
return this.sourceFilesByFilePath.keys(); | ||
} | ||
getSourceFiles() { | ||
return this.sourceFilesByFilePath.getValuesAsArray(); | ||
return this.sourceFilesByFilePath.values(); | ||
} | ||
@@ -51,7 +50,5 @@ getSourceFileVersion(sourceFile) { | ||
getSourceFileFromCacheFromFilePath(filePath) { | ||
filePath = this.fileSystemWrapper.getStandardizedAbsolutePath(filePath); | ||
return this.sourceFilesByFilePath.get(filePath); | ||
} | ||
addOrGetSourceFileFromFilePath(filePath, options) { | ||
filePath = this.fileSystemWrapper.getStandardizedAbsolutePath(filePath); | ||
let sourceFile = this.sourceFilesByFilePath.get(filePath); | ||
@@ -73,13 +70,12 @@ if (sourceFile == null && this.fileSystemWrapper.fileExistsSync(filePath)) { | ||
setSourceFile(sourceFile) { | ||
sourceFile.fileName = this.fileSystemWrapper.getStandardizedAbsolutePath(sourceFile.fileName); | ||
this.documentRegistry.updateDocument(sourceFile.fileName, this.compilerOptions.get(), common.ts.ScriptSnapshot.fromString(sourceFile.text), this.getSourceFileVersion(sourceFile), sourceFile["scriptKind"]); | ||
this.fileSystemWrapper.queueMkdir(common.FileUtils.getDirPath(sourceFile.fileName)); | ||
this.sourceFilesByFilePath.set(sourceFile.fileName, sourceFile); | ||
const standardizedFilePath = this.fileSystemWrapper.getStandardizedAbsolutePath(sourceFile.fileName); | ||
sourceFile.fileName = standardizedFilePath; | ||
this.documentRegistry.updateDocument(standardizedFilePath, this.compilerOptions.get(), common.ts.ScriptSnapshot.fromString(sourceFile.text), this.getSourceFileVersion(sourceFile), sourceFile["scriptKind"]); | ||
this.fileSystemWrapper.queueMkdir(common.FileUtils.getDirPath(standardizedFilePath)); | ||
this.sourceFilesByFilePath.set(standardizedFilePath, sourceFile); | ||
} | ||
removeSourceFile(filePath) { | ||
filePath = this.fileSystemWrapper.getStandardizedAbsolutePath(filePath); | ||
this.sourceFilesByFilePath.removeByKey(filePath); | ||
this.sourceFilesByFilePath.delete(filePath); | ||
} | ||
containsDirectoryAtPath(dirPath) { | ||
dirPath = this.fileSystemWrapper.getStandardizedAbsolutePath(dirPath); | ||
return this.fileSystemWrapper.directoryExistsSync(dirPath); | ||
@@ -94,2 +90,3 @@ } | ||
constructor(options = {}) { | ||
verifyOptions(); | ||
this.fileSystem = getFileSystem(); | ||
@@ -99,3 +96,3 @@ this._fileSystemWrapper = new common.TransactionalFileSystem(this.fileSystem); | ||
? undefined | ||
: new common.TsConfigResolver(this._fileSystemWrapper, options.tsConfigFilePath, getEncodingFromProvidedOptions()); | ||
: new common.TsConfigResolver(this._fileSystemWrapper, this._fileSystemWrapper.getStandardizedAbsolutePath(options.tsConfigFilePath), getEncodingFromProvidedOptions()); | ||
const tsCompilerOptions = getCompilerOptions(); | ||
@@ -123,8 +120,14 @@ this.compilerOptions = new common.CompilerOptionsContainer(); | ||
} | ||
function verifyOptions() { | ||
if (options.fileSystem != null && options.useInMemoryFileSystem) | ||
throw new common.errors.InvalidOperationError("Cannot provide a file system when specifying to use an in-memory file system."); | ||
if (options.skipLoadingLibFiles && !options.useInMemoryFileSystem) { | ||
throw new common.errors.InvalidOperationError(`The ${"skipLoadingLibFiles"} option can only be true when ${"useInMemoryFileSystem"} is true.`); | ||
} | ||
} | ||
function getFileSystem() { | ||
if (options.fileSystem != null && options.useVirtualFileSystem) | ||
throw new common.errors.InvalidOperationError("Cannot provide a file system when specifying to use a virtual file system."); | ||
else if (options.useVirtualFileSystem) | ||
return new common.InMemoryFileSystemHost(); | ||
return options.fileSystem || new common.RealFileSystemHost(); | ||
var _a; | ||
if (options.useInMemoryFileSystem) | ||
return new common.InMemoryFileSystemHost({ skipLoadingLibFiles: options.skipLoadingLibFiles }); | ||
return _a = options.fileSystem, (_a !== null && _a !== void 0 ? _a : new common.RealFileSystemHost()); | ||
} | ||
@@ -147,3 +150,3 @@ function getCompilerOptions() { | ||
addSourceFileAtPathIfExists(filePath, options) { | ||
return this._sourceFileCache.addOrGetSourceFileFromFilePath(filePath, { | ||
return this._sourceFileCache.addOrGetSourceFileFromFilePath(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath), { | ||
scriptKind: options && options.scriptKind | ||
@@ -162,3 +165,3 @@ }); | ||
const sourceFiles = []; | ||
for (const filePath of this._fileSystemWrapper.glob(fileGlobs)) { | ||
for (const filePath of this._fileSystemWrapper.globSync(fileGlobs)) { | ||
const sourceFile = this.addSourceFileAtPathIfExists(filePath); | ||
@@ -171,8 +174,8 @@ if (sourceFile != null) | ||
addSourceFilesFromTsConfig(tsConfigFilePath) { | ||
tsConfigFilePath = this._fileSystemWrapper.getStandardizedAbsolutePath(tsConfigFilePath); | ||
const resolver = new common.TsConfigResolver(this._fileSystemWrapper, tsConfigFilePath, this.compilerOptions.getEncoding()); | ||
const standardizedFilePath = this._fileSystemWrapper.getStandardizedAbsolutePath(tsConfigFilePath); | ||
const resolver = new common.TsConfigResolver(this._fileSystemWrapper, standardizedFilePath, this.compilerOptions.getEncoding()); | ||
return this._addSourceFilesForTsConfigResolver(resolver, resolver.getCompilerOptions()); | ||
} | ||
createSourceFile(filePath, sourceFileText, options) { | ||
return this._sourceFileCache.createSourceFileFromText(filePath, sourceFileText || "", { scriptKind: options && options.scriptKind }); | ||
return this._sourceFileCache.createSourceFileFromText(this._fileSystemWrapper.getStandardizedAbsolutePath(filePath), sourceFileText || "", { scriptKind: options && options.scriptKind }); | ||
} | ||
@@ -200,6 +203,3 @@ updateSourceFile(filePathOrSourceFile, sourceFileText, options) { | ||
removeSourceFile(filePathOrSourceFile) { | ||
if (typeof filePathOrSourceFile === "string") | ||
this._sourceFileCache.removeSourceFile(filePathOrSourceFile); | ||
else | ||
this._sourceFileCache.removeSourceFile(filePathOrSourceFile.fileName); | ||
this._sourceFileCache.removeSourceFile(this._fileSystemWrapper.getStandardizedAbsolutePath(typeof filePathOrSourceFile === "string" ? filePathOrSourceFile : filePathOrSourceFile.fileName)); | ||
} | ||
@@ -216,3 +216,3 @@ resolveSourceFileDependencies() { | ||
const oldProgram = this._oldProgram; | ||
const program = common.ts.createProgram(Object.assign({ rootNames: this._sourceFileCache.getSourceFilePaths(), options: this.compilerOptions.get(), host: this.compilerHost, oldProgram }, options)); | ||
const program = common.ts.createProgram(Object.assign({ rootNames: Array.from(this._sourceFileCache.getSourceFilePaths()), options: this.compilerOptions.get(), host: this.compilerHost, oldProgram }, options)); | ||
this._oldProgram = program; | ||
@@ -244,6 +244,12 @@ return program; | ||
const filePathOrSearchFunction = getFilePathOrSearchFunction(this._fileSystemWrapper); | ||
if (typeof filePathOrSearchFunction === "string") { | ||
if (isStandardizedFilePath(filePathOrSearchFunction)) { | ||
return this._sourceFileCache.getSourceFileFromCacheFromFilePath(filePathOrSearchFunction); | ||
} | ||
return selectSmallestDirPathResult(this.getSourceFiles().filter(filePathOrSearchFunction)); | ||
const allSoureFilesIterable = this.getSourceFiles(); | ||
return selectSmallestDirPathResult(function* () { | ||
for (const sourceFile of allSoureFilesIterable) { | ||
if (filePathOrSearchFunction(sourceFile)) | ||
yield sourceFile; | ||
} | ||
}()); | ||
function getFilePathOrSearchFunction(fileSystemWrapper) { | ||
@@ -266,5 +272,8 @@ if (fileNameOrSearchFunction instanceof Function) | ||
} | ||
function isStandardizedFilePath(obj) { | ||
return typeof obj === "string"; | ||
} | ||
} | ||
getSourceFiles() { | ||
return this._sourceFileCache.getSourceFiles(); | ||
return Array.from(this._sourceFileCache.getSourceFiles()); | ||
} | ||
@@ -299,2 +308,8 @@ formatDiagnosticsWithColorAndContext(diagnostics, opts = {}) { | ||
}); | ||
Object.defineProperty(exports, 'InMemoryFileSystemHost', { | ||
enumerable: true, | ||
get: function () { | ||
return common.InMemoryFileSystemHost; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'SettingsContainer', { | ||
@@ -301,0 +316,0 @@ enumerable: true, |
@@ -69,3 +69,5 @@ import { ts } from "@ts-morph/common"; | ||
/** Uses pattern matching to find files or directories. */ | ||
glob(patterns: ReadonlyArray<string>): string[]; | ||
glob(patterns: ReadonlyArray<string>): Promise<string[]>; | ||
/** Synchronously uses pattern matching to find files or directories. */ | ||
globSync(patterns: ReadonlyArray<string>): string[]; | ||
} | ||
@@ -113,2 +115,63 @@ | ||
export interface InMemoryFileSystemHostOptions { | ||
/** | ||
* Set this to true to not load the /node_modules/typescript/lib files on construction. | ||
* @default false | ||
*/ | ||
skipLoadingLibFiles?: boolean; | ||
} | ||
/** An implementation of a file system that exists in memory only. */ | ||
export declare class InMemoryFileSystemHost implements FileSystemHost { | ||
/** | ||
* Constructor. | ||
* @param options - Options for creating the file system. | ||
*/ | ||
constructor(options?: InMemoryFileSystemHostOptions); | ||
/** @inheritdoc */ | ||
isCaseSensitive(): boolean; | ||
/** @inheritdoc */ | ||
delete(path: string): Promise<void>; | ||
/** @inheritdoc */ | ||
deleteSync(path: string): void; | ||
/** @inheritdoc */ | ||
readDirSync(dirPath: string): string[]; | ||
/** @inheritdoc */ | ||
readFile(filePath: string, encoding?: string): Promise<string>; | ||
/** @inheritdoc */ | ||
readFileSync(filePath: string, encoding?: string): string; | ||
/** @inheritdoc */ | ||
writeFile(filePath: string, fileText: string): Promise<void>; | ||
/** @inheritdoc */ | ||
writeFileSync(filePath: string, fileText: string): void; | ||
/** @inheritdoc */ | ||
mkdir(dirPath: string): Promise<void>; | ||
/** @inheritdoc */ | ||
mkdirSync(dirPath: string): void; | ||
/** @inheritdoc */ | ||
move(srcPath: string, destPath: string): Promise<void>; | ||
/** @inheritdoc */ | ||
moveSync(srcPath: string, destPath: string): void; | ||
/** @inheritdoc */ | ||
copy(srcPath: string, destPath: string): Promise<void>; | ||
/** @inheritdoc */ | ||
copySync(srcPath: string, destPath: string): void; | ||
/** @inheritdoc */ | ||
fileExists(filePath: string): Promise<boolean>; | ||
/** @inheritdoc */ | ||
fileExistsSync(filePath: string): boolean; | ||
/** @inheritdoc */ | ||
directoryExists(dirPath: string): Promise<boolean>; | ||
/** @inheritdoc */ | ||
directoryExistsSync(dirPath: string): boolean; | ||
/** @inheritdoc */ | ||
realpathSync(path: string): string; | ||
/** @inheritdoc */ | ||
getCurrentDirectory(): string; | ||
/** @inheritdoc */ | ||
glob(patterns: ReadonlyArray<string>): Promise<string[]>; | ||
/** @inheritdoc */ | ||
globSync(patterns: ReadonlyArray<string>): string[]; | ||
} | ||
/** Options for creating a project. */ | ||
@@ -125,6 +188,8 @@ export interface ProjectOptions { | ||
/** 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 `useInMemoryFileSystem` instead. | ||
*/ | ||
@@ -136,5 +201,3 @@ fileSystem?: FileSystemHost; | ||
/** | ||
* Project that holds source files. | ||
*/ | ||
/** Project that holds source files. */ | ||
export declare class Project { | ||
@@ -141,0 +204,0 @@ /** |
{ | ||
"name": "@ts-morph/bootstrap", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "API for getting quickly set up with the TypeScript Compiler API.", | ||
@@ -21,3 +21,3 @@ "keywords": ["typescript", "compiler", "bootstrap"], | ||
"dependencies": { | ||
"@ts-morph/common": "~0.1.0" | ||
"@ts-morph/common": "~0.2.0" | ||
}, | ||
@@ -28,4 +28,3 @@ "devDependencies": { | ||
"@types/ts-nameof": "^4.0.0", | ||
"@ts-morph/common": "~0.1.0", | ||
"@ts-morph/scripts": "~0.1.0", | ||
"@ts-morph/scripts": "~0.2.0", | ||
"chai": "^4.2.0", | ||
@@ -32,0 +31,0 @@ "cross-env": "^6.0.3", |
# @ts-morph/bootstrap | ||
[![npm version](https://badge.fury.io/js/%40ts-morph%2Fbootstrap.svg)](https://badge.fury.io/js/%40ts-morph%2Fbootstrap) | ||
[![Build Status](https://travis-ci.org/dsherret/ts-morph.svg?branch=master)](https://travis-ci.org/dsherret/ts-morph) | ||
[![Build Status](https://travis-ci.org/dsherret/ts-morph.svg?branch=latest)](https://travis-ci.org/dsherret/ts-morph) | ||
@@ -26,3 +26,3 @@ A library for quickly getting set up with the [TypeScript](https://github.com/Microsoft/TypeScript) Compiler API. | ||
"main.ts", | ||
"import { MyClass } from './MyClass.ts'" | ||
"import { MyClass } from './MyClass'" | ||
); | ||
@@ -55,3 +55,3 @@ | ||
// in memory file system | ||
const project2 = new Project({ useVirtualFileSystem: true }); | ||
const project2 = new Project({ useInMemoryFileSystem: true }); | ||
@@ -187,3 +187,3 @@ // custom file system | ||
const project = new Project({ useVirtualFileSystem: true }); | ||
const project = new Project({ useInMemoryFileSystem: true }); | ||
project.createSourceFile("test.ts", "const t: string = 5;"); | ||
@@ -190,0 +190,0 @@ |
38088
14
629
+ Added@ts-morph/common@0.2.2(transitive)
- Removed@ts-morph/common@0.1.1(transitive)
- Removed@types/glob@7.2.0(transitive)
- Removed@types/minimatch@5.1.2(transitive)
- Removed@types/node@20.14.9(transitive)
- Removeddir-glob@3.0.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedglobby@10.0.2(transitive)
- Removedignore@5.3.1(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-type@4.0.0(transitive)
- Removedslash@3.0.0(transitive)
- Removedundici-types@5.26.5(transitive)
- Removedwrappy@1.0.2(transitive)
Updated@ts-morph/common@~0.2.0