UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

192 lines (191 loc) 7.6 kB
import { SourceFile } from "./../compiler"; import { SourceFileStructure } from "./../structures"; import { DirectoryEmitResult } from "./DirectoryEmitResult"; export declare class Directory { private readonly _path; private _global; private readonly _pathParts; private _parent; private _directories; private _sourceFiles; /** * Checks if this directory is an ancestor of the provided directory. * @param possibleDescendant - Directory or source file that's a possible descendant. */ isAncestorOf(possibleDescendant: Directory | SourceFile): boolean; /** * Checks if this directory is a descendant of the provided directory. * @param possibleAncestor - Directory or source file that's a possible ancestor. */ isDescendantOf(possibleAncestor: Directory): boolean; /** * Gets the path to the directory. */ getPath(): string; /** * Gets the directory path's base name. */ getBaseName(): string; /** * Gets the parent directory or throws if it doesn't exist or was never added to the AST. */ getParentOrThrow(): Directory; /** * Gets the parent directory if it exists and was added to the AST. */ getParent(): Directory | undefined; /** * Gets a child directory with the specified name or throws if not found. * @param dirName - Directory name. */ getDirectoryOrThrow(dirName: string): Directory; /** * Gets a child directory by the specified condition or throws if not found. * @param condition - Condition to check the directory with. */ getDirectoryOrThrow(condition: (directory: Directory) => boolean): Directory; /** * Gets a child directory with the specified name or undefined if not found. * @param dirName - Directory name. */ getDirectory(dirName: string): Directory | undefined; /** * Gets a child directory by the specified condition or undefined if not found. * @param condition - Condition to check the directory with. */ getDirectory(condition: (directory: Directory) => boolean): Directory | undefined; /** * Gets a child source file with the specified name or throws if not found. * @param fileName - File name. */ getSourceFileOrThrow(fileName: string): SourceFile; /** * Gets a child source file by the specified condition or throws if not found. * @param condition - Condition to check the source file with. */ getSourceFileOrThrow(condition: (sourceFile: SourceFile) => boolean): SourceFile; /** * Gets a child source file with the specified name or undefined if not found. * @param fileName - File name. */ getSourceFile(fileName: string): SourceFile | undefined; /** * Gets a child source file by the specified condition or undefined if not found. * @param condition - Condition to check the source file with. */ getSourceFile(condition: (sourceFile: SourceFile) => boolean): SourceFile | undefined; /** * Gets the child directories. */ getDirectories(): Directory[]; /** * Gets the source files within this directory. */ getSourceFiles(): SourceFile[]; /** * Gets the source files in the current directory and all the descendant directories. */ getDescendantSourceFiles(): SourceFile[]; /** * Adds an existing directory to the AST from the relative path or directory name. * * Will return the directory if it was already added. * @param path - Directory name or path to the directory that should be added. */ addExistingDirectory(path: string): Directory; /** * Creates a directory if it doesn't exist. * @param path - Directory name or path to the directory that should be created. */ createDirectory(path: string): Directory; /** * Creates a source file in the AST, relative to this directory. * * Note: The file will not be created and saved to the file system until .save() is called on the source file. * @param relativeFilePath - Relative file path of the source file to create. * @throws - InvalidOperationError if a source file already exists at the provided file name. */ createSourceFile(relativeFilePath: string): SourceFile; /** * Creates a source file in the AST, relative to this directory. * * Note: The file will not be created and saved to the file system until .save() is called on the source file. * @param relativeFilePath - Relative file path of the source file to create. * @param sourceFileText - Text of the source file. * @throws - InvalidOperationError if a source file already exists at the provided file name. */ createSourceFile(relativeFilePath: string, sourceFileText: string): SourceFile; /** * Creates a source file in the AST, relative to this directory. * * Note: The file will not be created and saved to the file system until .save() is called on the source file. * @param relativeFilePath - Relative file path of the source file to create. * @param structure - Structure that represents the source file. * @throws - InvalidOperationError if a source file already exists at the provided file name. */ createSourceFile(relativeFilePath: string, structure: SourceFileStructure): SourceFile; /** * Adds an existing source file to the AST, relative to this directory. * * Will return the source file if it was already added. * @param relativeFilePath - Relative file path to add. */ addExistingSourceFile(relativeFilePath: string): SourceFile; /** * Emits the files in the directory. * @param options - Options for emitting. */ emit(options?: { emitOnlyDtsFiles?: boolean; outDir?: string; declarationDir?: string; }): Promise<DirectoryEmitResult>; /** * Emits the files in the directory synchronously. * * Remarks: This might be very slow compared to the asynchronous version if there are a lot of files. * @param options - Options for emitting. */ emitSync(options?: { emitOnlyDtsFiles?: boolean; outDir?: string; declarationDir?: string; }): DirectoryEmitResult; private _emitInternal(options?); /** * Copies a directory to a new directory. * @param relativeOrAbsolutePath - The relative or absolute path to the new directory. * @returns The directory the copy was made to. */ copy(relativeOrAbsolutePath: string): Directory; /** * Asyncronously deletes the directory and all its descendants. * * WARNING: This will delete the directory from the file system. */ delete(): Promise<void>; /** * Synchronously deletes the directory and all its descendants. * * WARNING: This will delete the directory from the file system. */ deleteSync(): void; /** * Removes the directory and all its descendants from the AST. * * Note: Does not delete the directory from the file system. */ remove(): void; /** * Saves all the unsaved descendant source files. */ saveUnsavedSourceFiles(): Promise<void[]>; /** * Saves all the unsaved descendant source files synchronously. * * Remarks: This might be very slow compared to the asynchronous version if there are a lot of files. */ saveUnsavedSourceFilesSync(): void; private throwIfDeletedOrRemoved(); private _getUnsavedSourceFiles(); }