Created
January 28, 2026 10:31
-
-
Save AlexGeb/e0b4e7d0d528cd2736d0ce77ad7988f3 to your computer and use it in GitHub Desktop.
Organize imports using ts-morph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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