ts-file-util
is a utility package for TypeScript projects that provides several helper functions to retrieve function information, import information, and more from TypeScript source files.
Use the package manager npm to install ts-file-util
.
npm install ts-file-util
import {
getElementList,
getFunctionText,
findImports,
findImportedByTsFiles,
findImportingTsFiles,
} from "ts-file-util";
The getElementList
function retrieves a list of all functions within a TypeScript source file, along with their child functions.
import { getElementList } from "ts-file-util";
// Assume `sourceFile` is a ts.SourceFile instance
const functionList = getElementList(sourceFile);
The getFunctionText
function retrieves the text of a specified function from a TypeScript source file.
import { getFunctionText } from "ts-file-util";
// Assume `sourceFile` is a ts.SourceFile instance
const functionText = getFunctionText("functionName", sourceFile);
The findImports
function retrieves a list of all imported modules in a TypeScript source file.
import { findImports } from "ts-file-util";
// Assume `sourceFile` is a ts.SourceFile instance
const imports = findImports(sourceFile);
The findImportedByTsFiles
function retrieves a list of all TypeScript files that import a specified file.
import { findImportedByTsFiles } from "ts-file-util";
const importingFiles = findImportedByTsFiles("targetFilePath");
The findImportingTsFiles
function retrieves a list of all TypeScript files that a specified file imports.
import { findImportingTsFiles } from "ts-file-util";
const importedFiles = findImportingTsFiles("filePath");
The ts-file-util
package provides a CLI with several commands.
The element
command retrieves a list of all functions within a TypeScript source file, along with their child functions, and prints them in a tree structure.
ts-file-util element path/to/file.ts
The function-text
command retrieves the text of a specified function from a TypeScript source file.
ts-file-util function-text functionName path/to/file.ts
The import
command retrieves a list of all TypeScript files that a specified file imports.
ts-file-util import path/to/file.ts
The imported
command retrieves a list of all TypeScript files that import a specified file.
ts-file-util imported path/to/file.ts
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.