Last active
June 26, 2023 19:10
-
-
Save madbonez/7b3e0fa11b0d3b16c709a0e7d7860c1e to your computer and use it in GitHub Desktop.
external-folder-dependensies
This file contains 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 { Node, Project, SourceFile, SyntaxKind, ImportDeclaration, ImportSpecifier } from 'ts-morph'; | |
import * as path from 'path'; | |
const SRC_ROOT_DIR = process.argv[2];; | |
const project = new Project({ | |
tsConfigFilePath: path.resolve(SRC_ROOT_DIR, 'src', 'tsconfig.app.json'), | |
skipLoadingLibFiles: true, | |
skipFileDependencyResolution: true, | |
skipAddingFilesFromTsConfig: true, | |
}); | |
project.addSourceFileAtPath(path.resolve(SRC_ROOT_DIR, 'src', 'app', 'app.module.ts')); | |
project.resolveSourceFileDependencies(); | |
export const allTsSources: SourceFile[] = project.getSourceFiles(); | |
console.log(`Total source files equal ${allTsSources.length}`); | |
export function getAllTsImportsPaths(source: SourceFile): string[] { | |
const result: string[] = []; | |
source.forEachChild((node: Node) => { | |
if (!node.isKind(SyntaxKind.ImportDeclaration)) { | |
return; | |
} | |
if (node.getNamedImports()?.length) { | |
node.getNamedImports()?.forEach((node: ImportSpecifier) => { | |
const path = getImportedDeclarationPath(node); | |
if (path) { | |
result.push(path); | |
} | |
}); | |
} else { | |
const path = getImportedDeclarationPath(node); | |
if (path) { | |
result.push(path); | |
} | |
} | |
}) | |
return result.filter((val, index, arr) => { | |
return arr.indexOf(val) === index && val !== source.getFilePath(); | |
}); | |
} | |
function getImportedDeclarationPath(node: ImportDeclaration | ImportSpecifier): string { | |
const languageService = project.getLanguageService(); | |
const referencedSymbols = languageService.findReferences(node); | |
for (const referencedSymbol of referencedSymbols) { | |
for (const reference of referencedSymbol.getReferences()) { | |
const parentNodeKind = reference.getNode().getParentOrThrow().getKind(); | |
if ( | |
parentNodeKind === SyntaxKind.PropertyDeclaration || | |
parentNodeKind === SyntaxKind.MethodDeclaration || | |
parentNodeKind === SyntaxKind.ClassStaticBlockDeclaration || | |
parentNodeKind === SyntaxKind.VariableDeclaration || | |
parentNodeKind === SyntaxKind.VariableDeclarationList || | |
parentNodeKind === SyntaxKind.FunctionDeclaration || | |
parentNodeKind === SyntaxKind.ClassDeclaration || | |
parentNodeKind === SyntaxKind.InterfaceDeclaration || | |
parentNodeKind === SyntaxKind.TypeAliasDeclaration || | |
parentNodeKind === SyntaxKind.EnumDeclaration || | |
parentNodeKind === SyntaxKind.ModuleDeclaration || | |
parentNodeKind === SyntaxKind.NamespaceExportDeclaration || | |
parentNodeKind === SyntaxKind.ImportEqualsDeclaration || | |
parentNodeKind === SyntaxKind.ImportDeclaration || | |
parentNodeKind === SyntaxKind.ExportDeclaration || | |
parentNodeKind === SyntaxKind.MergeDeclarationMarker || | |
parentNodeKind === SyntaxKind.EndOfDeclarationMarker | |
) { | |
const path = reference.getSourceFile().getFilePath(); | |
if (path.indexOf('node_modules') === -1) { | |
return reference.getSourceFile().getFilePath(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment