I'm using ts-morph
in a Node.js environment, but its synchronous functions are blocking the main thread, which causes issues with a terminal spinner (since the spinner also runs on the main thread in the terminal).
import { Project, StructureKind } from "ts-morph";
// Initialize
const project = new Project({...});
const myEnumFile = project.createSourceFile("src/MyEnum.ts", {
statements: [{
kind: StructureKind.Enum,
name: "MyEnum",
isExported: true,
members: [{ name: "member" }],
}],
});
// Get information
const myClass = myClassFile.getClassOrThrow("MyClass");
myClass.getName(); // returns: "MyClass"
myClass.hasExportKeyword(); // returns: true
myClass.isDefaultExport(); // returns: false
How can I wrap all ts-morph
functions in promises to avoid blocking the main thread, preferably without manually wrapping each one?
For example:
const myClass = myClassFile.getClassOrThrow("MyClass");
to:
const myClass = myClassFile.getClassOrThrowAsync("MyClass"); // returns a Promise
This should be done without losing the typing of the function.