Created
June 17, 2024 08:33
-
-
Save kamiljozwik/98e0c90aa58f117335ab5f3694139e27 to your computer and use it in GitHub Desktop.
List named exports and it's imports
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 { Project } from 'ts-morph'; | |
import * as path from 'path'; | |
import * as fs from 'fs'; | |
// Initialize a new ts-morph project | |
const project = new Project(); | |
// Specify the directory of the ui-components package | |
const uiComponentsDir = path.resolve(__dirname, 'libs/ui-components/src'); | |
const otherPackagesDir = path.resolve(__dirname, 'apps'); | |
const otherPackagesDir2 = path.resolve(__dirname, 'libs'); | |
// Add all TypeScript files in the ui-components directory to the project | |
project.addSourceFilesAtPaths(`${uiComponentsDir}/**/*.ts`); | |
project.addSourceFilesAtPaths(`${uiComponentsDir}/**/*.tsx`); | |
project.addSourceFilesAtPaths(`${otherPackagesDir}/**/*.ts`); | |
project.addSourceFilesAtPaths(`${otherPackagesDir}/**/*.tsx`); | |
project.addSourceFilesAtPaths(`${otherPackagesDir2}/**/*.ts`); | |
project.addSourceFilesAtPaths(`${otherPackagesDir2}/**/*.tsx`); | |
// Collect exported components | |
const exportedComponents: string[] = []; | |
project.getSourceFiles().forEach((sourceFile) => { | |
sourceFile.getExportDeclarations().forEach((exportDecl) => { | |
exportDecl.getNamedExports().forEach((namedExport) => { | |
const name = namedExport.getName(); | |
exportedComponents.push(name); | |
}); | |
}); | |
}); | |
// console.log('Exported components from ui-components package:'); | |
// console.log(exportedComponents); | |
// Initialize a map to track component usage | |
const usageMap: { [key: string]: number } = {}; | |
exportedComponents.forEach((component) => { | |
usageMap[component] = 0; | |
}); | |
// Scan other packages for imports from @portal/ui-components | |
project.getSourceFiles().forEach((sourceFile) => { | |
sourceFile.getImportDeclarations().forEach((importDecl) => { | |
const moduleSpecifier = importDecl.getModuleSpecifierValue(); | |
if (moduleSpecifier === '@portal/ui-components') { | |
importDecl.getNamedImports().forEach((namedImport) => { | |
const name = namedImport.getName(); | |
if (usageMap.hasOwnProperty(name)) { | |
usageMap[name] += 1; | |
} | |
}); | |
} | |
}); | |
}); | |
// Sort the components by usage | |
const sortedComponents = Object.entries(usageMap) | |
.sort((a, b) => b[1] - a[1]) | |
.map(([component, usage]) => ({ component, usage })); | |
// Create object with component name and usage | |
const componentUsage = sortedComponents.reduce( | |
(obj, { component, usage }) => ({ | |
...obj, | |
[component]: usage, | |
}), | |
{} | |
); | |
console.log('Usage of components from ui-components package:'); | |
console.log(componentUsage); | |
// Optionally, write the result to a file | |
fs.writeFileSync( | |
'component-usage.json', | |
JSON.stringify(componentUsage, null, 2) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yarn ts-node list-exports.ts
npx ts-node list-exports.ts