Project
Project that holds source files.
Constructors
new Project(options)
new Project(
options
?):Project
Initializes a new instance.
Parameters
• options?: ProjectOptions
Optional options.
Returns
Source
node_modules/ts-morph/lib/ts-morph.d.ts:504
Accessors
compilerOptions
get
compilerOptions():CompilerOptionsContainer
Gets the compiler options for modification.
Returns
CompilerOptionsContainer
Source
node_modules/ts-morph/lib/ts-morph.d.ts:508
manipulationSettings
get
manipulationSettings():ManipulationSettingsContainer
Gets the manipulation settings.
Returns
ManipulationSettingsContainer
Source
node_modules/ts-morph/lib/ts-morph.d.ts:506
Methods
addDirectoryAtPath()
addDirectoryAtPath(
dirPath
,options
?):Directory
Adds an existing directory from the path or throws if it doesn’t exist.
Will return the directory if it was already added.
Parameters
• dirPath: string
Path to add the directory at.
• options?: DirectoryAddOptions
Options.
Returns
Directory
Throws
DirectoryNotFoundError when the directory does not exist.
Source
node_modules/ts-morph/lib/ts-morph.d.ts:534
addDirectoryAtPathIfExists()
addDirectoryAtPathIfExists(
dirPath
,options
?):undefined
|Directory
Adds an existing directory from the path or returns undefined if it doesn’t exist.
Will return the directory if it was already added.
Parameters
• dirPath: string
Path to add the directory at.
• options?: DirectoryAddOptions
Options.
Returns
undefined
| Directory
Source
node_modules/ts-morph/lib/ts-morph.d.ts:525
addSourceFileAtPath()
addSourceFileAtPath(
filePath
):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.
Parameters
• filePath: string
File path to get the file from.
Returns
SourceFile
Throws
FileNotFoundError when the file is not found.
Source
node_modules/ts-morph/lib/ts-morph.d.ts:574
addSourceFileAtPathIfExists()
addSourceFileAtPathIfExists(
filePath
):undefined
|SourceFile
Adds a source file from a file path if it exists or returns undefined.
Will return the source file if it was already added.
Parameters
• filePath: string
File path to get the file from.
Returns
undefined
| SourceFile
Source
node_modules/ts-morph/lib/ts-morph.d.ts:566
addSourceFilesAtPaths()
addSourceFilesAtPaths(
fileGlobs
):SourceFile
[]
Adds source files based on file globs.
Parameters
• fileGlobs: string
| readonly string
[]
File glob or globs to add files based on.
Returns
SourceFile
[]
The matched source files.
Source
node_modules/ts-morph/lib/ts-morph.d.ts:559
addSourceFilesFromTsConfig()
addSourceFilesFromTsConfig(
tsConfigFilePath
):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
.
Parameters
• tsConfigFilePath: string
File path to the tsconfig.json file.
Returns
SourceFile
[]
Source
node_modules/ts-morph/lib/ts-morph.d.ts:582
createDirectory()
createDirectory(
dirPath
):Directory
Creates a directory at the specified path.
Parameters
• dirPath: string
Path to create the directory at.
Returns
Directory
Source
node_modules/ts-morph/lib/ts-morph.d.ts:539
createSourceFile()
createSourceFile(
filePath
,sourceFileText
?,options
?):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.
Parameters
• filePath: string
File path of the source file.
• sourceFileText?: string
| WriterFunction
| OptionalKind
<SourceFileStructure
>
Text, structure, or writer function for the source file text.
• options?: SourceFileCreateOptions
Options.
Returns
SourceFile
Throws
- InvalidOperationError if a source file already exists at the provided file path.
Source
node_modules/ts-morph/lib/ts-morph.d.ts:592
createWriter()
createWriter():
CodeBlockWriter
Creates a writer with the current manipulation settings.
Returns
CodeBlockWriter
Remarks
Generally it’s best to use a provided writer, but this may be useful in some scenarios.
Source
node_modules/ts-morph/lib/ts-morph.d.ts:692
emit()
emit(
emitOptions
?):Promise
<EmitResult
>
Asynchronously emits all the source files to the file system as JavaScript files.
Parameters
• emitOptions?: EmitOptions
Optional emit options.
Returns
Promise
<EmitResult
>
Source
node_modules/ts-morph/lib/ts-morph.d.ts:673
emitSync()
emitSync(
emitOptions
?):EmitResult
Synchronously emits all the source files to the file system as JavaScript files.
Parameters
• emitOptions?: EmitOptions
Optional emit options.
Returns
EmitResult
Source
node_modules/ts-morph/lib/ts-morph.d.ts:678
emitToMemory()
emitToMemory(
emitOptions
?):MemoryEmitResult
Emits all the source files to memory.
Parameters
• emitOptions?: EmitOptions
Optional emit options.
Returns
MemoryEmitResult
Source
node_modules/ts-morph/lib/ts-morph.d.ts:683
enableLogging()
enableLogging(
enabled
?):void
Enables logging to the console.
Parameters
• enabled?: boolean
Enabled.
Returns
void
Source
node_modules/ts-morph/lib/ts-morph.d.ts:658
forgetNodesCreatedInBlock()
forgetNodesCreatedInBlock(block)
forgetNodesCreatedInBlock<
T
>(block
):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.
Type parameters
• T = void
Parameters
• block
Block of code to run. Use the remember
callback or return a node to remember it.
Returns
T
Source
node_modules/ts-morph/lib/ts-morph.d.ts:699
forgetNodesCreatedInBlock(block)
forgetNodesCreatedInBlock<
T
>(block
):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.
Type parameters
• T = void
Parameters
• block
Block of code to run. Use the remember
callback or return a node to remember it.
Returns
Promise
<T
>
Source
node_modules/ts-morph/lib/ts-morph.d.ts:706
formatDiagnosticsWithColorAndContext()
formatDiagnosticsWithColorAndContext(
diagnostics
,opts
?):string
Formats an array of diagnostics with their color and context into a string.
Parameters
• diagnostics: readonly Diagnostic
<Diagnostic
>[]
Diagnostics to get a string of.
• opts?
• opts.newLineChar?: "\n"
| "\r\n"
Returns
string
Source
node_modules/ts-morph/lib/ts-morph.d.ts:712
getAmbientModule()
getAmbientModule(
moduleName
):undefined
|Symbol
Gets the specified ambient module symbol or returns undefined if not found.
Parameters
• moduleName: string
The ambient module name with or without quotes.
Returns
undefined
| Symbol
Source
node_modules/ts-morph/lib/ts-morph.d.ts:635
getAmbientModuleOrThrow()
getAmbientModuleOrThrow(
moduleName
,message
?):Symbol
Gets the specified ambient module symbol or throws if not found.
Parameters
• moduleName: string
The ambient module name with or without quotes.
• message?: string
| () => string
Returns
Symbol
Source
node_modules/ts-morph/lib/ts-morph.d.ts:640
getAmbientModules()
getAmbientModules():
Symbol
[]
Gets the ambient module symbols (ex. modules in the
Returns
Symbol
[]
Types
folder or node_modules).
Source
node_modules/ts-morph/lib/ts-morph.d.ts:645
getCompilerOptions()
getCompilerOptions():
CompilerOptions
Gets the compiler options.
Returns
CompilerOptions
Source
node_modules/ts-morph/lib/ts-morph.d.ts:685
getConfigFileParsingDiagnostics()
getConfigFileParsingDiagnostics():
Diagnostic
<Diagnostic
>[]
Gets the diagnostics found when parsing the tsconfig.json file provided in the project’s constructor.
Returns
Diagnostic
<Diagnostic
>[]
Source
node_modules/ts-morph/lib/ts-morph.d.ts:687
getDirectories()
getDirectories():
Directory
[]
Gets all the directories.
Returns
Directory
[]
Source
node_modules/ts-morph/lib/ts-morph.d.ts:551
getDirectory()
getDirectory(
dirPath
):undefined
|Directory
Gets a directory by the specified path or returns undefined if it doesn’t exist.
Parameters
• dirPath: string
Directory path.
Returns
undefined
| Directory
Source
node_modules/ts-morph/lib/ts-morph.d.ts:549
getDirectoryOrThrow()
getDirectoryOrThrow(
dirPath
,message
?):Directory
Gets a directory by the specified path or throws if it doesn’t exist.
Parameters
• dirPath: string
Path to create the directory at.
• message?: string
| () => string
Returns
Directory
Source
node_modules/ts-morph/lib/ts-morph.d.ts:544
getFileSystem()
getFileSystem():
FileSystemHost
Gets the file system.
Returns
FileSystemHost
Source
node_modules/ts-morph/lib/ts-morph.d.ts:668
getLanguageService()
getLanguageService():
LanguageService
Gets the language service.
Returns
LanguageService
Source
node_modules/ts-morph/lib/ts-morph.d.ts:662
getModuleResolutionHost()
getModuleResolutionHost():
ModuleResolutionHost
Gets a ts.ModuleResolutionHost for the project.
Returns
ModuleResolutionHost
Source
node_modules/ts-morph/lib/ts-morph.d.ts:716
getPreEmitDiagnostics()
getPreEmitDiagnostics():
Diagnostic
<Diagnostic
>[]
Gets the pre-emit diagnostics.
Returns
Diagnostic
<Diagnostic
>[]
Source
node_modules/ts-morph/lib/ts-morph.d.ts:660
getProgram()
getProgram():
Program
Gets the program.
Returns
Program
Source
node_modules/ts-morph/lib/ts-morph.d.ts:664
getRootDirectories()
getRootDirectories():
Directory
[]
Gets the directories without a parent.
Returns
Directory
[]
Source
node_modules/ts-morph/lib/ts-morph.d.ts:553
getSourceFile()
getSourceFile(fileNameOrPath)
getSourceFile(
fileNameOrPath
):undefined
|SourceFile
Gets a source file by a file name or file path. Returns undefined if none exists.
Parameters
• fileNameOrPath: string
File name or path that the path could end with or equal.
Returns
undefined
| SourceFile
Source
node_modules/ts-morph/lib/ts-morph.d.ts:613
getSourceFile(searchFunction)
getSourceFile(
searchFunction
):undefined
|SourceFile
Gets a source file by a search function. Returns undefined if none exists.
Parameters
• searchFunction
Search function.
Returns
undefined
| SourceFile
Source
node_modules/ts-morph/lib/ts-morph.d.ts:618
getSourceFileOrThrow()
getSourceFileOrThrow(fileNameOrPath)
getSourceFileOrThrow(
fileNameOrPath
):SourceFile
Gets a source file by a file name or file path. Throws an error if it doesn’t exist.
Parameters
• fileNameOrPath: string
File name or path that the path could end with or equal.
Returns
SourceFile
Source
node_modules/ts-morph/lib/ts-morph.d.ts:603
getSourceFileOrThrow(searchFunction)
getSourceFileOrThrow(
searchFunction
):SourceFile
Gets a source file by a search function. Throws an error if it doesn’t exist.
Parameters
• searchFunction
Search function.
Returns
SourceFile
Source
node_modules/ts-morph/lib/ts-morph.d.ts:608
getSourceFiles()
getSourceFiles()
getSourceFiles():
SourceFile
[]
Gets all the source files added to the project.
Returns
SourceFile
[]
Source
node_modules/ts-morph/lib/ts-morph.d.ts:620
getSourceFiles(globPattern)
getSourceFiles(
globPattern
):SourceFile
[]
Gets all the source files added to the project that match a pattern.
Parameters
• globPattern: string
Glob pattern for filtering out the source files.
Returns
SourceFile
[]
Source
node_modules/ts-morph/lib/ts-morph.d.ts:625
getSourceFiles(globPatterns)
getSourceFiles(
globPatterns
):SourceFile
[]
Gets all the source files added to the project that match the passed in patterns.
Parameters
• globPatterns: readonly string
[]
Glob patterns for filtering out the source files.
Returns
SourceFile
[]
Source
node_modules/ts-morph/lib/ts-morph.d.ts:630
getTypeChecker()
getTypeChecker():
TypeChecker
Gets the type checker.
Returns
TypeChecker
Source
node_modules/ts-morph/lib/ts-morph.d.ts:666
removeSourceFile()
removeSourceFile(
sourceFile
):boolean
Removes a source file from the project.
Parameters
• sourceFile: SourceFile
Source file to remove.
Returns
boolean
True if removed.
Source
node_modules/ts-morph/lib/ts-morph.d.ts:598
resolveSourceFileDependencies()
resolveSourceFileDependencies():
SourceFile
[]
Adds the source files the project’s source files depend on to the project.
Returns
SourceFile
[]
The added source files.
Remarks
- This should be done after source files are added to the project, preferably once to avoid doing more work than necessary.
- This is done by default when creating a Project and providing a tsconfig.json and not specifying to not add the source files.
Source
node_modules/ts-morph/lib/ts-morph.d.ts:517
save()
save():
Promise
<void
>
Saves all the unsaved source files to the file system and deletes all deleted files.
Returns
Promise
<void
>
Source
node_modules/ts-morph/lib/ts-morph.d.ts:647
saveSync()
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.
Returns
void
Source
node_modules/ts-morph/lib/ts-morph.d.ts:653