Skip to content

Instantly share code, notes, and snippets.

@AlexGeb
Created January 28, 2026 10:31
Show Gist options
  • Select an option

  • Save AlexGeb/e0b4e7d0d528cd2736d0ce77ad7988f3 to your computer and use it in GitHub Desktop.

Select an option

Save AlexGeb/e0b4e7d0d528cd2736d0ce77ad7988f3 to your computer and use it in GitHub Desktop.
Organize imports using ts-morph
import path from "path";
import { Project } from "ts-morph";
const project = new Project({
tsConfigFilePath: path.resolve("tsconfig.json"), // root tsconfig with references
skipAddingFilesFromTsConfig: true,
});
project.addSourceFilesFromTsConfig(
path.resolve("packages/my-package/tsconfig.json"),
);
const sourceFiles = project.getSourceFiles(
"packages/my-package/src/**/*{.ts,.tsx}",
);
console.log("Organizing imports in", sourceFiles.length, "files...");
for (const file of sourceFiles) {
file.organizeImports();
file.saveSync();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment