Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
@ts-morph/bootstrap
Advanced tools
A library for quickly getting set up with the TypeScript Compiler API.
This library is separate from ts-morph, but uses some of its underlying infrastructure.
import { Project, ts } from "@ts-morph/bootstrap";
const project = new Project();
// these are typed as ts.SourceFile
const myClassFile = project.createSourceFile("MyClass.ts", "export class MyClass { prop: string; }");
const mainFile = project.createSourceFile("main.ts", "import { MyClass } from './MyClass.ts'");
const program = project.createProgram(); // ts.Program
const typeChecker = program.getTypeChecker(); // ts.TypeChecker
const languageService = project.getLanguageService(); // ts.LanguageService
const moduleResolutionHost = project.getModuleResolutionHost(); // ts.ModuleResolutionHost
Generally:
const project = new Project({ tsConfigFilePath: "tsconfig.json" });
// will use a real file system
const project = new Project();
// in memory file system
const project2 = new Project({ useVirtualFileSystem: true });
// custom file system
const fileSystem: FileSystemHost = { ...etc... };
const project = new Project({ fileSystem });
To access the file system after creating a project, you can use the fileSystem
property:
project.fileSystem.writeFileSync("MyClass.ts", "class MyClass {}");
const project = new Project({
compilerOptions: {
target: ts.ScriptTarget.ES3
}
});
If you would like to manually specify the path to a tsconfig.json file then specify that:
const project = new Project({
tsConfigFilePath: "packages/my-library/tsconfig.json"
});
// output all the source files that were added
console.log(project.getSourceFiles().map(s => s.fileName));
Note: You can override any tsconfig.json options by also providing a compilerOptions
object.
For your convenience, this will automatically add all the associated source files from the tsconfig.json. If you don't wish to do that, then you will need to explicitly set addFilesFromTsConfig
to false
:
const project = new Project({
tsConfigFilePath: "path/to/tsconfig.json",
addFilesFromTsConfig: false
});
Custom module resolution can be specified by providing a resolution host factory function. This also supports providing custom type reference directive resolution.
For example:
import { Project, ts } from "ts-morph";
// this is deno style module resolution (ex. `import { MyClass } from "./MyClass.ts"`)
const project = new Project({
resolutionHost: (moduleResolutionHost, getCompilerOptions) => {
return {
resolveModuleNames: (moduleNames, containingFile) => {
const compilerOptions = getCompilerOptions();
const resolvedModules: ts.ResolvedModule[] = [];
for (const moduleName of moduleNames.map(removeTsExtension)) {
const result = ts.resolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost);
if (result.resolvedModule)
resolvedModules.push(result.resolvedModule);
}
return resolvedModules;
}
};
function removeTsExtension(moduleName: string) {
if (moduleName.slice(-3).toLowerCase() === ".ts")
return moduleName.slice(0, -3);
return moduleName;
}
}
});
Use the following methods:
const sourceFiles = project.addSourceFiles("**/*.ts");
or provide an array of file globs.const sourceFile = project.addSourceFileAtPath("src/my-file.ts");
or use addSourceFileAtPathIfExists(filePath)
const sourceFiles = project.addSourceFilesFromTsConfig("path/to/tsconfig.json")
Use the Project#createSourceFile
method:
const sourceFile = project.createSourceFile("MyClass.ts", "class MyClass {}");
Use the Project#updateSourceFile
method. This can be provided a file path and string for the text or a new ts.SourceFile
object:
const newSourceFile = project.updateSourceFile("MyClass.ts", "class MyClass {}");
// or
project.updateSourceFile(newSourceFileObj);
Use the Project#removeSourceFile
method:
project.removeSourceFile("MyClass.ts");
// or
project.removeSourceFile(sourceFile);
import { Project, ts } from "@ts-morph/bootstrap";
const project = new Project({ useVirtualFileSystem: true });
project.createSourceFile("test.ts", "const t: string = 5;");
const program = project.createProgram();
const diagnostics = ts.getPreEmitDiagnostics(project.createProgram());
console.log(project.formatDiagnosticsWithColorAndContext(diagnostics));
FAQs
API for getting quickly set up with the TypeScript Compiler API.
The npm package @ts-morph/bootstrap receives a total of 98,162 weekly downloads. As such, @ts-morph/bootstrap popularity was classified as popular.
We found that @ts-morph/bootstrap demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.