Module

x/ts_morph/mod.ts>Project

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
Very Popular
Go to Latest
class Project
import { Project } from "https://dotland.deno.dev/x/ts_morph@21.0.0/mod.ts";

Project that holds source files.

Constructors

new
Project(options?: ProjectOptions)

Initializes a new instance.

Properties

readonly
compilerOptions: CompilerOptionsContainer

Gets the compiler options for modification.

readonly
manipulationSettings: ManipulationSettingsContainer

Gets the manipulation settings.

Methods

addDirectoryAtPath(dirPath: string, options?: DirectoryAddOptions): Directory

Adds an existing directory from the path or throws if it doesn't exist.

Will return the directory if it was already added.

addDirectoryAtPathIfExists(dirPath: string, options?: DirectoryAddOptions): Directory | undefined

Adds an existing directory from the path or returns undefined if it doesn't exist.

Will return the directory if it was already added.

addSourceFileAtPath(filePath: string): SourceFile

Adds an existing source file from a file path or throws if it doesn't exist.

Will return the source file if it was already added.

addSourceFileAtPathIfExists(filePath: string): SourceFile | undefined

Adds a source file from a file path if it exists or returns undefined.

Will return the source file if it was already added.

addSourceFilesAtPaths(fileGlobs: string | ReadonlyArray<string>): SourceFile[]

Adds source files based on file globs.

addSourceFilesFromTsConfig(tsConfigFilePath: string): SourceFile[]

Adds all the source files from the specified tsconfig.json.

Note that this is done by default when specifying a tsconfig file in the constructor and not explicitly setting the skipAddingFilesFromTsConfig option to true.

createDirectory(dirPath: string): Directory

Creates a directory at the specified path.

createSourceFile(
filePath: string,
sourceFileText?: string | OptionalKind<SourceFileStructure> | WriterFunction,
): SourceFile

Creates a source file at the specified file path with the specified text.

Note: The file will not be created and saved to the file system until .save() is called on the source file.

Creates a writer with the current manipulation settings.

emit(emitOptions?: EmitOptions): Promise<EmitResult>

Asynchronously emits all the source files to the file system as JavaScript files.

Synchronously emits all the source files to the file system as JavaScript files.

Emits all the source files to memory.

enableLogging(enabled?: boolean): void

Enables logging to the console.

forgetNodesCreatedInBlock<T = void>(block: (remember: (...node: Node[]) => void) => T): T

Forgets the nodes created in the scope of the passed in block.

This is an advanced method that can be used to easily "forget" all the nodes created within the scope of the block.

forgetNodesCreatedInBlock<T = void>(block: (remember: (...node: Node[]) => void) => Promise<T>): Promise<T>

Forgets the nodes created in the scope of the passed in block asynchronously.

This is an advanced method that can be used to easily "forget" all the nodes created within the scope of the block.

formatDiagnosticsWithColorAndContext(diagnostics: ReadonlyArray<Diagnostic>, opts?: { newLineChar?: "\n" | "\r\n"; }): string

Formats an array of diagnostics with their color and context into a string.

getAmbientModule(moduleName: string): Symbol | undefined

Gets the specified ambient module symbol or returns undefined if not found.

getAmbientModuleOrThrow(moduleName: string, message?: string | (() => string)): Symbol

Gets the specified ambient module symbol or throws if not found.

Gets the ambient module symbols (ex. modules in the

Gets the compiler options.

Gets the diagnostics found when parsing the tsconfig.json file provided in the project's constructor.

Gets all the directories.

getDirectory(dirPath: string): Directory | undefined

Gets a directory by the specified path or returns undefined if it doesn't exist.

getDirectoryOrThrow(dirPath: string, message?: string | (() => string)): Directory

Gets a directory by the specified path or throws if it doesn't exist.

Gets the file system.

Gets the language service.

getModuleResolutionHost(): ts.ModuleResolutionHost

Gets a ts.ModuleResolutionHost for the project.

Gets the pre-emit diagnostics.

Gets the program.

Gets the directories without a parent.

getSourceFile(fileNameOrPath: string): SourceFile | undefined

Gets a source file by a file name or file path. Returns undefined if none exists.

getSourceFile(searchFunction: (file: SourceFile) => boolean): SourceFile | undefined

Gets a source file by a search function. Returns undefined if none exists.

getSourceFileOrThrow(fileNameOrPath: string): SourceFile

Gets a source file by a file name or file path. Throws an error if it doesn't exist.

getSourceFileOrThrow(searchFunction: (file: SourceFile) => boolean): SourceFile

Gets a source file by a search function. Throws an error if it doesn't exist.

Gets all the source files added to the project.

getSourceFiles(globPattern: string): SourceFile[]

Gets all the source files added to the project that match a pattern.

getSourceFiles(globPatterns: ReadonlyArray<string>): SourceFile[]

Gets all the source files added to the project that match the passed in patterns.

Gets the type checker.

removeSourceFile(sourceFile: SourceFile): boolean

Removes a source file from the project.

Adds the source files the project's source files depend on to the project.

save(): Promise<void>

Saves all the unsaved source files to the file system and deletes all deleted files.

saveSync(): void

Synchronously saves all the unsaved source files to the file system and deletes all deleted files.

Remarks: This might be very slow compared to the asynchronous version if there are a lot of files.