react-prune 1.1.1 โ 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -63
- package/dist/cli.js +2 -1
- package/dist/cli.js.map +1 -1
- package/package.json +7 -5
- package/dist/cli.d.mts +0 -1
- package/dist/cli.mjs +0 -3
- package/dist/cli.mjs.map +0 -1
- package/dist/index.d.mts +0 -11
- package/dist/index.d.ts +0 -11
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -4,120 +4,155 @@
|
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
[](https://www.npmjs.com/package/react-prune)
|
|
6
6
|
|
|
7
|
-
> **
|
|
7
|
+
> **Static analysis for identifying unused files and package usage in React-based codebases.**
|
|
8
8
|
|
|
9
|
-
`react-prune` is a
|
|
9
|
+
`react-prune` is a lightweight CLI tool that analyzes your React, Next.js, and React Native projects to surface **unused local files** and **package import usage**, helping you reduce dead code and dependency bloat.
|
|
10
|
+
|
|
11
|
+
---
|
|
10
12
|
|
|
11
13
|
## ๐ Features
|
|
12
14
|
|
|
13
|
-
- **๐ฆ Package Analysis
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
-
|
|
17
|
-
|
|
15
|
+
- **๐ฆ Package Usage Analysis**
|
|
16
|
+
Counts how often each external npm package is imported across your codebase.
|
|
17
|
+
|
|
18
|
+
- **โ๏ธ Optional Package Size Estimation**
|
|
19
|
+
Estimates package sizes from `node_modules` to highlight heavy dependencies.
|
|
20
|
+
|
|
21
|
+
- **๐งน Unused File Detection**
|
|
22
|
+
Identifies local source files that are never imported anywhere in the project.
|
|
23
|
+
|
|
24
|
+
- **โ๏ธ React Ecosystem Support**
|
|
25
|
+
Works with React, Next.js (Pages & App Router), and React Native projects.
|
|
26
|
+
|
|
27
|
+
- **๐ CLI-Friendly Output**
|
|
28
|
+
Displays results in readable tables or as structured JSON for automation.
|
|
29
|
+
|
|
30
|
+
---
|
|
18
31
|
|
|
19
32
|
## ๐ฆ Installation
|
|
20
33
|
|
|
21
|
-
|
|
34
|
+
Install as a dev dependency (recommended):
|
|
22
35
|
|
|
23
|
-
###
|
|
36
|
+
### npm
|
|
24
37
|
|
|
25
38
|
```bash
|
|
26
39
|
npm install -D react-prune
|
|
27
40
|
```
|
|
28
41
|
|
|
29
|
-
###
|
|
42
|
+
### yarn
|
|
30
43
|
|
|
31
44
|
```bash
|
|
32
45
|
yarn add -D react-prune
|
|
33
46
|
```
|
|
34
47
|
|
|
35
|
-
###
|
|
48
|
+
### pnpm
|
|
36
49
|
|
|
37
50
|
```bash
|
|
38
51
|
pnpm add -D react-prune
|
|
39
52
|
```
|
|
40
53
|
|
|
41
|
-
|
|
54
|
+
Or run once via `npx`:
|
|
42
55
|
|
|
43
56
|
```bash
|
|
44
|
-
npx react-prune
|
|
57
|
+
npx react-prune
|
|
45
58
|
```
|
|
46
59
|
|
|
60
|
+
---
|
|
61
|
+
|
|
47
62
|
## ๐ Usage
|
|
48
63
|
|
|
49
|
-
|
|
64
|
+
Run from the **root of your project**:
|
|
50
65
|
|
|
51
66
|
```bash
|
|
52
|
-
react-prune
|
|
67
|
+
react-prune
|
|
53
68
|
```
|
|
54
69
|
|
|
55
|
-
|
|
70
|
+
### Options
|
|
71
|
+
|
|
72
|
+
| Option | Description |
|
|
73
|
+
| ------------- | ------------------------------------------ |
|
|
74
|
+
| `--json` | Output the report as JSON |
|
|
75
|
+
| `--no-size` | Skip package size calculation |
|
|
76
|
+
| `--limit <n>` | Limit displayed package rows (default: 50) |
|
|
56
77
|
|
|
57
|
-
### Example
|
|
78
|
+
### Example
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
react-prune --limit 20
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## ๐ Example Output
|
|
58
87
|
|
|
59
88
|
```text
|
|
60
89
|
โญโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
|
|
61
|
-
โ โ
|
|
62
90
|
โ ๐ฆ Package Usage โ
|
|
63
|
-
โ Report โ
|
|
64
|
-
โ โ
|
|
65
91
|
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
|
|
66
92
|
|
|
67
|
-
|
|
68
|
-
โ Package
|
|
69
|
-
|
|
70
|
-
โ react
|
|
71
|
-
|
|
72
|
-
โ
|
|
73
|
-
|
|
74
|
-
โ framer-motion โ 23 โ 1.1 MB โ
|
|
75
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
|
|
93
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโโ
|
|
94
|
+
โ Package โ Count โ Size โ
|
|
95
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโโค
|
|
96
|
+
โ react โ 142 โ 312 KB โ
|
|
97
|
+
โ lodash โ 5 โ 4.2 MB โ
|
|
98
|
+
โ framer-motion โ 23 โ 1.1 MB โ
|
|
99
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโ
|
|
76
100
|
|
|
77
101
|
โญโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
|
|
78
|
-
โ
|
|
79
|
-
โ โ ๏ธ Potential โ
|
|
80
|
-
โ Unused Files โ
|
|
81
|
-
โ โ
|
|
102
|
+
โ โ ๏ธ Unused Files (2) โ
|
|
82
103
|
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
|
|
83
104
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
87
|
-
โ src/components/OldButton.tsx โ
|
|
88
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
|
89
|
-
โ src/utils/deprecated-helper.ts โ
|
|
90
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
105
|
+
src/components/OldButton.tsx
|
|
106
|
+
src/utils/deprecated-helper.ts
|
|
91
107
|
```
|
|
92
108
|
|
|
93
|
-
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## โ๏ธ How It Works
|
|
112
|
+
|
|
113
|
+
1. **File Discovery**
|
|
114
|
+
Recursively scans `.js`, `.jsx`, `.ts`, and `.tsx` files (excluding `node_modules`, `.next`, `dist`, etc.).
|
|
94
115
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
116
|
+
2. **AST Parsing**
|
|
117
|
+
Uses `ts-morph` to parse TypeScript/JavaScript ASTs for accurate import analysis.
|
|
118
|
+
|
|
119
|
+
3. **Dependency Resolution**
|
|
120
|
+
Differentiates between local file imports and external package imports.
|
|
121
|
+
|
|
122
|
+
4. **Static Usage Mapping**
|
|
123
|
+
Tracks which files and packages are actually referenced in the project.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## โ ๏ธ Limitations (Important)
|
|
128
|
+
|
|
129
|
+
- This is **static analysis** โ dynamic imports and runtime usage may not be detected.
|
|
130
|
+
- Files referenced only via tooling configuration (e.g. Storybook, tests, build scripts) may appear unused.
|
|
131
|
+
- Package size estimates are approximate and based on disk size, not bundle size.
|
|
132
|
+
|
|
133
|
+
---
|
|
99
134
|
|
|
100
135
|
## ๐ค Contributing
|
|
101
136
|
|
|
102
|
-
Contributions are welcome
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
node dist/cli.js analyze
|
|
119
|
-
```
|
|
137
|
+
Contributions are welcome.
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
git clone https://github.com/danieljohnson18/react-prune.git
|
|
141
|
+
cd react-prune
|
|
142
|
+
npm install
|
|
143
|
+
npm run dev
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Test locally:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
node dist/cli.js
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
120
153
|
|
|
121
154
|
## ๐ License
|
|
122
155
|
|
|
123
156
|
MIT ยฉ [Daniel Arikawe](https://github.com/danieljohnson18)
|
|
157
|
+
|
|
158
|
+
---
|
package/dist/cli.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
'use strict';var commander=require('commander'),
|
|
2
|
+
'use strict';var commander=require('commander'),j=require('picocolors'),U=require('cli-table3'),F=require('boxen'),tsMorph=require('ts-morph'),M=require('fast-glob'),h=require('path'),k=require('fs');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var j__default=/*#__PURE__*/_interopDefault(j);var U__default=/*#__PURE__*/_interopDefault(U);var F__default=/*#__PURE__*/_interopDefault(F);var M__default=/*#__PURE__*/_interopDefault(M);var h__default=/*#__PURE__*/_interopDefault(h);var k__default=/*#__PURE__*/_interopDefault(k);var O=(o,t)=>()=>(t||o((t={exports:{}}).exports,t),t.exports);var T=O((X,K)=>{K.exports={name:"react-prune",version:"1.2.1",main:"dist/index.js",bin:{"react-prune":"./dist/cli.js"},scripts:{build:"tsup src/cli.ts --format cjs --dts",dev:"tsup src/cli.ts --watch",lint:"eslint src/**",format:"prettier --write .",prepublishOnly:"npm run build",release:"changeset publish",test:"vitest"},publishConfig:{access:"public"},keywords:["react","react-native","nextjs","analysis","dead-code","imports","dependency-analysis","bundle-size","prune","typescript","developer-tools","cli"],author:"Daniel Arikawe",license:"MIT",files:["dist"],repository:{type:"git",url:"git+https://github.com/danieljohnson18/react-prune.git"},engines:{node:">=18"},bugs:{url:"https://github.com/danieljohnson18/react-prune/issues"},homepage:"https://github.com/danieljohnson18/react-prune#readme",description:"A powerful CLI tool to monitor package usage, analyze component imports, and detect dead code across React, Next.js, and React Native applications.",dependencies:{boxen:"^8.0.1","cli-table3":"^0.6.5",commander:"^14.0.2","fast-glob":"^3.3.3",picocolors:"^1.1.1","ts-morph":"^27.0.2"},devDependencies:{"@changesets/cli":"^2.29.8","@types/glob":"^8.1.0","@types/node":"^25.0.10",eslint:"^9.39.2",prettier:"^3.8.1",tsup:"^8.5.1",typescript:"^5.9.3",vitest:"^4.0.18"}};});function C(o){let t=0;try{let i=k__default.default.readdirSync(o);for(let n of i){let a=h__default.default.join(o,n),r=k__default.default.statSync(a);r.isDirectory()?t+=C(a):t+=r.size;}}catch{return 0}return t}function $(o,t=2){if(o===0)return "0 Bytes";let i=1024,n=t<0?0:t,a=["Bytes","KB","MB","GB","TB"],r=Math.floor(Math.log(o)/Math.log(i));return parseFloat((o/Math.pow(i,r)).toFixed(n))+" "+a[r]}function I(o,t){let i=h__default.default.join(o,"node_modules",t);if(k__default.default.existsSync(i)){let n=C(i);return $(n)}return "N/A"}async function D(o){let{rootPath:t,includeSizes:i=true,analyzeExports:n=true}=o;console.log(j__default.default.green(`Analyzing project at ${t}`));let a=await M__default.default("**/*.{js,jsx,ts,tsx,ios.tsx,android.tsx,native.tsx}",{cwd:t,ignore:["**/node_modules/**","**/dist/**","**/build/**","**/.next/**","**/coverage/**","**/*.config.{js,ts,cjs,mjs}","**/*.d.ts"],absolute:true});console.log(j__default.default.blue(`Found ${a.length} files to analyze.`));let r=h__default.default.join(t,"tsconfig.json"),c={skipAddingFilesFromTsConfig:true};k__default.default.existsSync(r)&&(c.tsConfigFilePath=r);let d=new tsMorph.Project(c);a.forEach(e=>{try{d.addSourceFileAtPath(e);}catch(s){console.warn(j__default.default.yellow(`Skipping file ${e}:`),s);}});let m={},y={};a.forEach(e=>{let s=h__default.default.relative(t,e).replace(/\\/g,"/");y[s]=0;});for(let e of d.getSourceFiles()){let s=e.getImportDeclarations();for(let x of s){let l=x.getModuleSpecifierValue();if(l.startsWith(".")||l.startsWith("/")){let u=h__default.default.dirname(e.getFilePath()),g=h__default.default.resolve(u,l),p=["",".ts",".tsx",".js",".jsx","/index.ts","/index.tsx","/index.js","/index.jsx"];for(let f of p){let S=g+f,z=h__default.default.relative(t,S).replace(/\\/g,"/");if(y.hasOwnProperty(z)){y[z]++;break}}}else {let u=l.startsWith("@")?l.split("/").slice(0,2).join("/"):l.split("/")[0];m[u]=(m[u]||0)+1;}}let P=e.getDescendantsOfKind(tsMorph.SyntaxKind.CallExpression);for(let x of P)if(x.getExpression().getText()==="require"){let u=x.getArguments();if(u.length&&u[0].getKind()===tsMorph.SyntaxKind.StringLiteral){let g=u[0].getText().replace(/['"`]/g,"");if(!g.startsWith(".")&&!g.startsWith("/")){let p=g.startsWith("@")?g.split("/").slice(0,2).join("/"):g.split("/")[0];m[p]=(m[p]||0)+1;}}}}let E={};for(let[e,s]of Object.entries(m))E[e]={count:s,size:i?I(t,e):"\u2014"};let W=Object.entries(y).filter(([e,s])=>e.includes("pages/")||e.includes("app/")||e.endsWith("main.tsx")||e.endsWith("index.tsx")||e.endsWith("index.js")||e.endsWith("App.tsx")||e.endsWith("App.js")||!e.includes(h__default.default.sep)&&!e.endsWith(".ts")&&!e.endsWith(".tsx")&&!e.endsWith(".js")&&!e.endsWith(".jsx")?false:s===0).map(([e])=>e),A={};if(n)for(let e of d.getSourceFiles()){let s=e.getFilePath(),P=h__default.default.relative(t,s).replace(/\\/g,"/");if(s.includes("pages/")||s.includes("app/")||s.endsWith("main.tsx")||s.endsWith("index.tsx")||s.endsWith("index.js")||s.endsWith("App.tsx")||s.endsWith("App.js")||W.includes(s))continue;let x=e.getExportedDeclarations(),l=[];for(let[u,g]of x){let p=false;for(let f of g){if(tsMorph.Node.isFunctionDeclaration(f)||tsMorph.Node.isClassDeclaration(f)||tsMorph.Node.isVariableDeclaration(f)||tsMorph.Node.isEnumDeclaration(f)||tsMorph.Node.isInterfaceDeclaration(f)||tsMorph.Node.isTypeAliasDeclaration(f))try{let S=f.findReferences();for(let z of S){for(let N of z.getReferences())if(N.getSourceFile().getFilePath()!==s){p=!0;break}if(p)break}}catch{p=true;}else p=true;if(p)break}p||l.push(u);}l.length&&(A[P]=l);}return {packages:E,components:y,unusedFiles:W,unusedExports:A}}var v=new commander.Command;v.name("react-prune").description("Analyze React/Next/Vite/React Native codebases for unused files, exports, and packages").version(T().version).option("--no-size","Skip calculating package sizes").option("--no-exports","Skip analyzing unused exports").option("--limit <n>","Limit output rows","50");v.action(async o=>{let t=process.cwd(),i=Number(o.limit),n=await D({rootPath:t,includeSizes:o.size,analyzeExports:o.exports});console.log(F__default.default(j__default.default.bold("\u{1F4E6} Package Usage"),{padding:1,borderColor:"green",borderStyle:"round"}));let a=new U__default.default({head:["Package","Count","Size"]});Object.entries(n.packages).sort((c,d)=>d[1].count-c[1].count).slice(0,i).forEach(([c,d])=>a.push([c,d.count,d.size])),console.log(a.toString()),n.unusedFiles.length?(console.log(F__default.default(j__default.default.bold(`\u26A0\uFE0F Unused Files (${n.unusedFiles.length})`),{padding:1,borderColor:"yellow"})),n.unusedFiles.forEach(c=>console.log(j__default.default.yellow(c)))):console.log(F__default.default(j__default.default.green("\u2705 No unused files detected!"),{padding:1,borderColor:"green"}));let r=Object.entries(n.unusedExports);if(r.length){console.log(F__default.default(j__default.default.bold(`\u26A0\uFE0F Potential Unused Exports (${r.length})`),{padding:1,borderColor:"yellow"}));let c=new U__default.default({head:["File","Unused Exports"],wordWrap:true});r.slice(0,i).forEach(([d,m])=>{c.push([d,m.join(", ")]);}),console.log(c.toString());}});v.parse();
|
|
3
|
+
//# sourceMappingURL=cli.js.map
|
|
3
4
|
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/analyzer.ts","../src/cli.ts"],"names":["getFolderSize","dirPath","size","files","fs","file","filePath","path","stats","formatBytes","bytes","decimals","k","dm","sizes","i","getPackageSize","rootPath","packageName","pkgPath","analyzeProject","pc","glob","tsConfigPath","projectConfig","project","Project","e","packageUsage","localUsage","f","relative","sourceFile","imports","imp","moduleSpecifier","sourceFilePath","sourceDir","resolvedPath","extensions","ext","tryPath","relativeTry","resolvedLocalFile","resolvedSourceFile","possibleMatches","match","normalizedMatch","parts","callExpressions","SyntaxKind","call","args","rawArg","pkg","reportPackages","count","unused","program","Command","directory","targetDir","report","packageTable","Table","sortedPackages","a","b","data","boxen","unusedTable","error"],"mappings":";miBAaA,SAASA,EAAcC,CAAAA,CAAyB,CAC9C,IAAIC,CAAAA,CAAO,CAAA,CACX,GAAI,CACF,IAAMC,CAAAA,CAAQC,kBAAAA,CAAG,WAAA,CAAYH,CAAO,CAAA,CACpC,IAAA,IAAWI,CAAAA,IAAQF,CAAAA,CAAO,CACxB,IAAMG,CAAAA,CAAWC,kBAAAA,CAAK,IAAA,CAAKN,CAAAA,CAASI,CAAI,CAAA,CAClCG,CAAAA,CAAQJ,kBAAAA,CAAG,QAAA,CAASE,CAAQ,CAAA,CAC9BE,CAAAA,CAAM,aAAY,CACpBN,CAAAA,EAAQF,CAAAA,CAAcM,CAAQ,CAAA,CAE9BJ,CAAAA,EAAQM,CAAAA,CAAM,KAElB,CACF,CAAA,KAAY,CACV,OAAO,CACT,CACA,OAAON,CACT,CAEA,SAASO,CAAAA,CAAYC,CAAAA,CAAeC,CAAAA,CAAW,CAAA,CAAG,CAChD,GAAID,CAAAA,GAAU,CAAA,CAAG,OAAO,SAAA,CACxB,IAAME,CAAAA,CAAI,KACJC,CAAAA,CAAKF,CAAAA,CAAW,CAAA,CAAI,CAAA,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AAAA,CAAG,IAAA,CACD,CAAA,sCAAA,EAA+ByC,CAAAA,CAAO,WAAA,CAAY,MAAM,CAAA,CAAA,CAC1D,CAAA,CACA,CACE,OAAA,CAAS,CAAA,CACT,MAAA,CAAQ,CAAA,CACR,WAAA,CAAa,OAAA,CACb,WAAA,CAAa,QACf,CACF,CACF,CAAA,CAEAA,CAAAA,CAAO,WAAA,CAAY,OAAA,CAASzD,CAAAA,EAASiE,CAAAA,CAAY,IAAA,CAAK,CAACjE,CAAI,CAAC,CAAC,CAAA,CAC7D,OAAA,CAAQ,GAAA,CAAIiE,CAAAA,CAAY,QAAA,EAAU,EACpC,CAAA,KACE,OAAA,CAAQ,GAAA,CACND,kBAAAA,CAAMhD,kBAAAA,CAAG,IAAA,CAAK,kCAA6B,CAAA,CAAG,CAC5C,OAAA,CAAS,CAAA,CACT,MAAA,CAAQ,EACR,WAAA,CAAa,OAAA,CACb,WAAA,CAAa,OACf,CAAC,CACH,EAEJ,CAAA,MAASkD,CAAAA,CAAO,CACd,OAAA,CAAQ,KAAA,CAAMlD,kBAAAA,CAAG,GAAA,CAAI,kBAAkB,CAAA,CAAGkD,CAAK,CAAA,CAC/C,OAAA,CAAQ,IAAA,CAAK,CAAC,EAChB,CACF,CAAC,CAAA,CAEHb,CAAAA,CAAQ,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA","file":"cli.js","sourcesContent":["import { Project, SyntaxKind } from \"ts-morph\";\nimport pc from \"picocolors\";\nimport glob from \"fast-glob\";\nimport path from \"path\";\nimport fs from \"fs\";\n\nexport interface UsageReport {\n packages: Record<string, { count: number; size: string }>;\n components: Record<string, number>;\n unusedFiles: string[];\n}\n\n// Helper to calculate folder size recursively\nfunction getFolderSize(dirPath: string): number {\n let size = 0;\n try {\n const files = fs.readdirSync(dirPath);\n for (const file of files) {\n const filePath = path.join(dirPath, file);\n const stats = fs.statSync(filePath);\n if (stats.isDirectory()) {\n size += getFolderSize(filePath);\n } else {\n size += stats.size;\n }\n }\n } catch (e) {\n return 0;\n }\n return size;\n}\n\nfunction formatBytes(bytes: number, decimals = 2) {\n if (bytes === 0) return \"0 Bytes\";\n const k = 1024;\n const dm = decimals < 0 ? 0 : decimals;\n const sizes = [\"Bytes\", \"KB\", \"MB\", \"GB\", \"TB\"];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + \" \" + sizes[i];\n}\n\nfunction getPackageSize(rootPath: string, packageName: string): string {\n // Try to find module in node_modules\n // Search in local node_modules first, then maybe recursive?\n // For now simple check in root/node_modules\n const pkgPath = path.join(rootPath, \"node_modules\", packageName);\n if (fs.existsSync(pkgPath)) {\n const size = getFolderSize(pkgPath);\n return formatBytes(size);\n }\n return \"N/A\";\n}\n\nexport async function analyzeProject(rootPath: string): Promise<UsageReport> {\n console.log(pc.green(`Analyzing project at ${rootPath}`));\n\n // 1. Find all files\n const files = await glob(\"**/*.{js,jsx,ts,tsx}\", {\n cwd: rootPath,\n ignore: [\n \"**/node_modules/**\",\n \"**/dist/**\",\n \"**/build/**\",\n \"**/.next/**\",\n \"**/coverage/**\",\n \"**/*.config.{js,ts,cjs,mjs}\", // Ignore config files\n \"**/.d.ts\" // Ignore definition files\n ],\n absolute: true\n });\n\n console.log(pc.blue(`Found ${files.length} files to analyze.`));\n\n // 2. Initialize ts-morph project\n const tsConfigPath = path.join(rootPath, \"tsconfig.json\");\n const projectConfig: any = {\n skipAddingFilesFromTsConfig: true\n };\n\n if (fs.existsSync(tsConfigPath)) {\n projectConfig.tsConfigFilePath = tsConfigPath;\n }\n\n const project = new Project(projectConfig);\n\n // Add files to project\n files.forEach((file) => {\n try {\n project.addSourceFileAtPath(file);\n } catch (e) {\n console.warn(pc.yellow(`Skipping file ${file} due to load error:`), e);\n }\n });\n\n const packageUsage: Record<string, number> = {};\n const localUsage: Record<string, number> = {};\n\n // Initialize local usage with 0 for all files to track unused ones\n files.forEach((f) => {\n // Normalize path to be relative and standard for comparison\n const relative = path.relative(rootPath, f);\n localUsage[relative] = 0;\n });\n\n // 3. Analyze Imports\n for (const sourceFile of project.getSourceFiles()) {\n const imports = sourceFile.getImportDeclarations();\n\n for (const imp of imports) {\n const moduleSpecifier = imp.getModuleSpecifierValue();\n\n if (moduleSpecifier.startsWith(\".\") || moduleSpecifier.startsWith(\"/\")) {\n // Local Import\n try {\n // Resolve the import to a file on disk\n const sourceFilePath = sourceFile.getFilePath();\n const sourceDir = path.dirname(sourceFilePath);\n\n const resolvedPath = path.resolve(sourceDir, moduleSpecifier);\n // We need to try extensions\n const extensions = [\n \"\",\n \".ts\",\n \".tsx\",\n \".js\",\n \".jsx\",\n \"/index.ts\",\n \"/index.tsx\",\n \"/index.js\",\n \"/index.jsx\"\n ];\n\n for (const ext of extensions) {\n const tryPath = resolvedPath + ext;\n const relativeTry = path.relative(rootPath, tryPath);\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n break;\n }\n }\n } catch (e) {\n // ignore resolution errors\n }\n } else {\n // Check if it's a path alias or non-relative local import\n let resolvedLocalFile: string | undefined;\n\n // Try ts-morph resolution first (if tsconfig loaded)\n try {\n const resolvedSourceFile = imp.getModuleSpecifierSourceFile();\n if (resolvedSourceFile) {\n resolvedLocalFile = resolvedSourceFile.getFilePath();\n }\n } catch (e) {}\n\n // Fallback: Check if the module specifier matches a known local file\n // relative to baseUrl (src) or just fuzzy match\n if (!resolvedLocalFile) {\n const possibleMatches = files.filter((f) =>\n f.includes(moduleSpecifier)\n );\n if (possibleMatches.length > 0) {\n for (const match of possibleMatches) {\n const normalizedMatch = match.replace(/\\\\/g, \"/\");\n if (\n normalizedMatch.endsWith(`${moduleSpecifier}.tsx`) ||\n normalizedMatch.endsWith(`${moduleSpecifier}.ts`) ||\n normalizedMatch.endsWith(`${moduleSpecifier}/index.tsx`)\n ) {\n resolvedLocalFile = match;\n break;\n }\n }\n }\n }\n\n if (resolvedLocalFile) {\n const relativeTry = path.relative(rootPath, resolvedLocalFile);\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n continue; // Skip package counting\n }\n }\n\n // Package Import\n let packageName = moduleSpecifier;\n if (moduleSpecifier.startsWith(\"@\")) {\n const parts = moduleSpecifier.split(\"/\");\n if (parts.length >= 2) {\n packageName = `${parts[0]}/${parts[1]}`;\n }\n } else {\n const parts = moduleSpecifier.split(\"/\");\n if (parts.length >= 1) {\n packageName = parts[0];\n }\n }\n\n packageUsage[packageName] = (packageUsage[packageName] || 0) + 1;\n }\n }\n\n // Check for require() calls (CommonJS)\n const callExpressions = sourceFile.getDescendantsOfKind(\n SyntaxKind.CallExpression\n );\n for (const call of callExpressions) {\n const expression = call.getExpression();\n if (expression.getText() === \"require\") {\n const args = call.getArguments();\n if (args.length > 0 && args[0].getKind() === SyntaxKind.StringLiteral) {\n const rawArg = args[0].getText().replace(/['\"`]/g, \"\");\n // Simple duplicate logic for MVP (should refactor)\n if (!rawArg.startsWith(\".\") && !rawArg.startsWith(\"/\")) {\n let pkg = rawArg.startsWith(\"@\")\n ? rawArg.split(\"/\").slice(0, 2).join(\"/\")\n : rawArg.split(\"/\")[0];\n packageUsage[pkg] = (packageUsage[pkg] || 0) + 1;\n }\n }\n }\n }\n }\n\n // 4. Construct Report Data\n const reportPackages: Record<string, { count: number; size: string }> = {};\n\n for (const [pkg, count] of Object.entries(packageUsage)) {\n const size = getPackageSize(rootPath, pkg);\n reportPackages[pkg] = { count, size };\n }\n\n const unused = Object.entries(localUsage)\n .filter(([file, count]) => {\n // Known Entry Points & Framework specifics\n if (\n file.includes(\"pages/\") ||\n file.includes(\"app/\") ||\n file.endsWith(\"main.tsx\") ||\n file.endsWith(\"index.tsx\") ||\n file.endsWith(\"index.js\") ||\n file.endsWith(\"App.tsx\") ||\n file.endsWith(\"App.js\")\n )\n return false;\n\n // Ignore files in the project root (usually configs, scripts, etc.)\n // We check if the relative path contains a separator. If not, it's in the root.\n const relative = path.relative(rootPath, file);\n if (!relative.includes(path.sep)) {\n return false;\n }\n return count === 0;\n })\n .map(([file]) => file);\n\n return {\n packages: reportPackages,\n components: localUsage,\n unusedFiles: unused\n };\n}\n","#!/usr/bin/env node\nimport { Command } from \"commander\";\nimport pc from \"picocolors\";\nimport path from \"path\";\nimport { analyzeProject } from \"./analyzer\";\n// @ts-ignore\nimport Table from \"cli-table3\";\n// @ts-ignore\nimport boxen from \"boxen\";\n\nconst program = new Command();\n\nprogram\n .name(\"react-prune\")\n .description(\n \"Monitor usage of packages and component imports across your React/Next.js/React Native app\"\n )\n .version(\"1.0.0\");\n\nprogram\n .command(\"analyze [directory]\")\n .description(\"Analyze the current project for package and component usage\")\n .action(async (directory) => {\n console.log(pc.blue(\"Starting analysis...\"));\n try {\n const targetDir = directory ? path.resolve(directory) : process.cwd();\n const report = await analyzeProject(targetDir);\n\n // Package Usage Table\n const packageTable = new Table({\n head: [\n pc.cyan(\"Package Name\"),\n pc.cyan(\"Usage Count\"),\n pc.cyan(\"Est. Size\")\n ],\n colWidths: [40, 15, 15]\n });\n\n const sortedPackages = Object.entries(report.packages).sort(\n (a, b) => b[1].count - a[1].count\n );\n\n sortedPackages.slice(0, 50).forEach(([pkg, data]) => {\n packageTable.push([pkg, data.count, data.size]);\n });\n\n console.log(\n boxen(pc.bold(\"๐ฆ Package Usage Report\"), {\n padding: 1,\n margin: 1,\n borderStyle: \"round\",\n borderColor: \"green\"\n })\n );\n console.log(packageTable.toString());\n if (sortedPackages.length > 50) {\n console.log(\n pc.gray(`...and ${sortedPackages.length - 50} more packages.`)\n );\n }\n\n // Unused Files\n if (report.unusedFiles.length > 0) {\n const unusedTable = new Table({\n head: [pc.yellow(\"File Path\")],\n colWidths: [80]\n });\n\n console.log(\n boxen(\n pc.bold(\n `โ ๏ธ Potential Unused Files (${report.unusedFiles.length})`\n ),\n {\n padding: 1,\n margin: 1,\n borderStyle: \"round\",\n borderColor: \"yellow\"\n }\n )\n );\n\n report.unusedFiles.forEach((file) => unusedTable.push([file]));\n console.log(unusedTable.toString());\n } else {\n console.log(\n boxen(pc.bold(\"โ
No unused files detected!\"), {\n padding: 1,\n margin: 1,\n borderStyle: \"round\",\n borderColor: \"green\"\n })\n );\n }\n } catch (error) {\n console.error(pc.red(\"Analysis failed:\"), error);\n process.exit(1);\n }\n });\n\nprogram.parse(process.argv);\n"]}
|
|
1
|
+
{"version":3,"sources":["../package.json","../src/analyzer/index.ts","../src/cli.ts"],"names":["require_package","__commonJSMin","exports","module","getFolderSize","dirPath","size","files","fs","file","filePath","path","stats","formatBytes","bytes","decimals","k","dm","sizes","i","getPackageSize","rootPath","packageName","pkgPath","analyzeProject","options","includeSizes","analyzeExports","pc","glob","tsConfigPath","projectConfig","project","Project","e","packageUsage","localUsage","f","relative","sourceFile","imports","imp","moduleSpecifier","sourceDir","resolvedPath","extensions","ext","tryPath","relativeTry","callExpressions","SyntaxKind","call","args","rawArg","pkg","reportPackages","count","unused","unusedExports","relativePath","exportedDeclarations","fileUnusedExports","name","declarations","isUsed","decl","Node","refs","ref","entry","program","Command","opts","limit","report","boxen","packageTable","Table","a","b","data","unusedExportsEntries","exportsTable"],"mappings":";miBAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,KAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,OAAA,CAAA,EAAA,CAAA,EAAA,OAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,OAAA,CAAA,CAAA,IAAAA,CAAAA,CAAAC,CAAAA,CAAA,CAAAC,CAAAA,CAAAC,IAAA,CAAAA,CAAAA,CAAA,OAAA,CAAA,CACE,IAAA,CAAQ,aAAA,CACR,OAAA,CAAW,OAAA,CACX,IAAA,CAAQ,gBACR,GAAA,CAAO,CACL,aAAA,CAAe,eACjB,CAAA,CACA,OAAA,CAAW,CACT,KAAA,CAAS,qCACT,GAAA,CAAO,yBAAA,CACP,IAAA,CAAQ,eAAA,CACR,MAAA,CAAU,oBAAA,CACV,cAAA,CAAkB,eAAA,CACl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AAA,CAAE,QAAQ,CAAC,CAAChE,CAAAA,CAAMP,CAAO,CAAA,GAAM,CAChEgF,CAAAA,CAAa,IAAA,CAAK,CAACzE,CAAAA,CAAMP,CAAAA,CAAQ,IAAA,CAAK,IAAI,CAAC,CAAC,EAC9C,CAAC,EACD,OAAA,CAAQ,GAAA,CAAIgF,CAAAA,CAAa,QAAA,EAAU,EACrC,CACF,CAAC,CAAA,CAEDZ,EAAQ,KAAA,EAAM","file":"cli.js","sourcesContent":["{\n \"name\": \"react-prune\",\n \"version\": \"1.2.1\",\n \"main\": \"dist/index.js\",\n \"bin\": {\n \"react-prune\": \"./dist/cli.js\"\n },\n \"scripts\": {\n \"build\": \"tsup src/cli.ts --format cjs --dts\",\n \"dev\": \"tsup src/cli.ts --watch\",\n \"lint\": \"eslint src/**\",\n \"format\": \"prettier --write .\",\n \"prepublishOnly\": \"npm run build\",\n \"release\": \"changeset publish\",\n \"test\": \"vitest\"\n },\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"keywords\": [\n \"react\",\n \"react-native\",\n \"nextjs\",\n \"analysis\",\n \"dead-code\",\n \"imports\",\n \"dependency-analysis\",\n \"bundle-size\",\n \"prune\",\n \"typescript\",\n \"developer-tools\",\n \"cli\"\n ],\n \"author\": \"Daniel Arikawe\",\n \"license\": \"MIT\",\n \"files\": [\n \"dist\"\n ],\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/danieljohnson18/react-prune.git\"\n },\n \"engines\": {\n \"node\": \">=18\"\n },\n \"bugs\": {\n \"url\": \"https://github.com/danieljohnson18/react-prune/issues\"\n },\n \"homepage\": \"https://github.com/danieljohnson18/react-prune#readme\",\n \"description\": \"A powerful CLI tool to monitor package usage, analyze component imports, and detect dead code across React, Next.js, and React Native applications.\",\n \"dependencies\": {\n \"boxen\": \"^8.0.1\",\n \"cli-table3\": \"^0.6.5\",\n \"commander\": \"^14.0.2\",\n \"fast-glob\": \"^3.3.3\",\n \"picocolors\": \"^1.1.1\",\n \"ts-morph\": \"^27.0.2\"\n },\n \"devDependencies\": {\n \"@changesets/cli\": \"^2.29.8\",\n \"@types/glob\": \"^8.1.0\",\n \"@types/node\": \"^25.0.10\",\n \"eslint\": \"^9.39.2\",\n \"prettier\": \"^3.8.1\",\n \"tsup\": \"^8.5.1\",\n \"typescript\": \"^5.9.3\",\n \"vitest\": \"^4.0.18\"\n }\n}\n","import { Project, SyntaxKind, Node } from \"ts-morph\";\nimport glob from \"fast-glob\";\nimport path from \"path\";\nimport fs from \"fs\";\nimport pc from \"picocolors\";\n\nexport interface UsageReport {\n packages: Record<string, { count: number; size: string }>;\n components: Record<string, number>;\n unusedFiles: string[];\n unusedExports: Record<string, string[]>;\n}\n\nexport interface AnalyzerOptions {\n rootPath: string;\n includeSizes?: boolean;\n analyzeExports?: boolean;\n}\n\n// --- Helper: calculate folder size recursively\nfunction getFolderSize(dirPath: string): number {\n let size = 0;\n try {\n const files = fs.readdirSync(dirPath);\n for (const file of files) {\n const filePath = path.join(dirPath, file);\n const stats = fs.statSync(filePath);\n if (stats.isDirectory()) size += getFolderSize(filePath);\n else size += stats.size;\n }\n } catch {\n return 0;\n }\n return size;\n}\n\nfunction formatBytes(bytes: number, decimals = 2) {\n if (bytes === 0) return \"0 Bytes\";\n const k = 1024;\n const dm = decimals < 0 ? 0 : decimals;\n const sizes = [\"Bytes\", \"KB\", \"MB\", \"GB\", \"TB\"];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + \" \" + sizes[i];\n}\n\nfunction getPackageSize(rootPath: string, packageName: string): string {\n const pkgPath = path.join(rootPath, \"node_modules\", packageName);\n if (fs.existsSync(pkgPath)) {\n const size = getFolderSize(pkgPath);\n return formatBytes(size);\n }\n return \"N/A\";\n}\n\n// --- Analyzer function\nexport async function analyzeProject(\n options: AnalyzerOptions\n): Promise<UsageReport> {\n const { rootPath, includeSizes = true, analyzeExports = true } = options;\n\n console.log(pc.green(`Analyzing project at ${rootPath}`));\n\n // --- 1. Find all JS/TS files\n const files = await glob(\n \"**/*.{js,jsx,ts,tsx,ios.tsx,android.tsx,native.tsx}\",\n {\n cwd: rootPath,\n ignore: [\n \"**/node_modules/**\",\n \"**/dist/**\",\n \"**/build/**\",\n \"**/.next/**\",\n \"**/coverage/**\",\n \"**/*.config.{js,ts,cjs,mjs}\",\n \"**/*.d.ts\"\n ],\n absolute: true\n }\n );\n\n console.log(pc.blue(`Found ${files.length} files to analyze.`));\n\n // --- 2. Initialize ts-morph project\n const tsConfigPath = path.join(rootPath, \"tsconfig.json\");\n const projectConfig: any = { skipAddingFilesFromTsConfig: true };\n if (fs.existsSync(tsConfigPath))\n projectConfig.tsConfigFilePath = tsConfigPath;\n\n const project = new Project(projectConfig);\n\n files.forEach((file) => {\n try {\n project.addSourceFileAtPath(file);\n } catch (e) {\n console.warn(pc.yellow(`Skipping file ${file}:`), e);\n }\n });\n\n const packageUsage: Record<string, number> = {};\n const localUsage: Record<string, number> = {};\n\n files.forEach((f) => {\n const relative = path.relative(rootPath, f).replace(/\\\\/g, \"/\");\n localUsage[relative] = 0;\n });\n\n // --- 3. Analyze imports\n for (const sourceFile of project.getSourceFiles()) {\n const imports = sourceFile.getImportDeclarations();\n\n for (const imp of imports) {\n const moduleSpecifier = imp.getModuleSpecifierValue();\n\n if (moduleSpecifier.startsWith(\".\") || moduleSpecifier.startsWith(\"/\")) {\n // Local import\n const sourceDir = path.dirname(sourceFile.getFilePath());\n const resolvedPath = path.resolve(sourceDir, moduleSpecifier);\n const extensions = [\n \"\",\n \".ts\",\n \".tsx\",\n \".js\",\n \".jsx\",\n \"/index.ts\",\n \"/index.tsx\",\n \"/index.js\",\n \"/index.jsx\"\n ];\n\n for (const ext of extensions) {\n const tryPath = resolvedPath + ext;\n const relativeTry = path\n .relative(rootPath, tryPath)\n .replace(/\\\\/g, \"/\");\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n break;\n }\n }\n } else {\n // Package import\n const packageName = moduleSpecifier.startsWith(\"@\")\n ? moduleSpecifier.split(\"/\").slice(0, 2).join(\"/\")\n : moduleSpecifier.split(\"/\")[0];\n packageUsage[packageName] = (packageUsage[packageName] || 0) + 1;\n }\n }\n\n // --- Require() CommonJS\n const callExpressions = sourceFile.getDescendantsOfKind(\n SyntaxKind.CallExpression\n );\n for (const call of callExpressions) {\n const expression = call.getExpression();\n if (expression.getText() === \"require\") {\n const args = call.getArguments();\n if (args.length && args[0].getKind() === SyntaxKind.StringLiteral) {\n const rawArg = args[0].getText().replace(/['\"`]/g, \"\");\n if (!rawArg.startsWith(\".\") && !rawArg.startsWith(\"/\")) {\n const pkg = rawArg.startsWith(\"@\")\n ? rawArg.split(\"/\").slice(0, 2).join(\"/\")\n : rawArg.split(\"/\")[0];\n packageUsage[pkg] = (packageUsage[pkg] || 0) + 1;\n }\n }\n }\n }\n }\n\n // --- 4. Packages\n const reportPackages: Record<string, { count: number; size: string }> = {};\n for (const [pkg, count] of Object.entries(packageUsage)) {\n reportPackages[pkg] = {\n count,\n size: includeSizes ? getPackageSize(rootPath, pkg) : \"โ\"\n };\n }\n\n // --- 5. Unused files\n const unused = Object.entries(localUsage)\n .filter(([file, count]) => {\n if (\n file.includes(\"pages/\") ||\n file.includes(\"app/\") ||\n file.endsWith(\"main.tsx\") ||\n file.endsWith(\"index.tsx\") ||\n file.endsWith(\"index.js\") ||\n file.endsWith(\"App.tsx\") ||\n file.endsWith(\"App.js\")\n )\n return false;\n if (\n !file.includes(path.sep) &&\n !file.endsWith(\".ts\") &&\n !file.endsWith(\".tsx\") &&\n !file.endsWith(\".js\") &&\n !file.endsWith(\".jsx\")\n )\n return false;\n return count === 0;\n })\n .map(([file]) => file);\n\n // --- 6. Unused exports\n const unusedExports: Record<string, string[]> = {};\n if (analyzeExports) {\n for (const sourceFile of project.getSourceFiles()) {\n const filePath = sourceFile.getFilePath();\n const relativePath = path\n .relative(rootPath, filePath)\n .replace(/\\\\/g, \"/\");\n\n if (\n filePath.includes(\"pages/\") ||\n filePath.includes(\"app/\") ||\n filePath.endsWith(\"main.tsx\") ||\n filePath.endsWith(\"index.tsx\") ||\n filePath.endsWith(\"index.js\") ||\n filePath.endsWith(\"App.tsx\") ||\n filePath.endsWith(\"App.js\") ||\n unused.includes(filePath)\n )\n continue;\n\n const exportedDeclarations = sourceFile.getExportedDeclarations();\n const fileUnusedExports: string[] = [];\n\n for (const [name, declarations] of exportedDeclarations) {\n let isUsed = false;\n\n for (const decl of declarations) {\n // --- Type guard: only check certain declaration types\n if (\n Node.isFunctionDeclaration(decl) ||\n Node.isClassDeclaration(decl) ||\n Node.isVariableDeclaration(decl) ||\n Node.isEnumDeclaration(decl) ||\n Node.isInterfaceDeclaration(decl) ||\n Node.isTypeAliasDeclaration(decl)\n ) {\n try {\n const refs = decl.findReferences();\n for (const ref of refs) {\n for (const entry of ref.getReferences()) {\n if (entry.getSourceFile().getFilePath() !== filePath) {\n isUsed = true;\n break;\n }\n }\n if (isUsed) break;\n }\n } catch {\n isUsed = true;\n }\n } else {\n // Other export types: assume used\n isUsed = true;\n }\n\n if (isUsed) break;\n }\n\n if (!isUsed) fileUnusedExports.push(name);\n }\n\n if (fileUnusedExports.length)\n unusedExports[relativePath] = fileUnusedExports;\n }\n }\n\n return {\n packages: reportPackages,\n components: localUsage,\n unusedFiles: unused,\n unusedExports\n };\n}\n","#!/usr/bin/env node\nimport { Command } from \"commander\";\nimport pc from \"picocolors\";\nimport path from \"path\";\nimport Table from \"cli-table3\";\nimport boxen from \"boxen\";\nimport { analyzeProject } from \"./analyzer\";\n\nconst program = new Command();\n\nprogram\n .name(\"react-prune\")\n .description(\n \"Analyze React/Next/Vite/React Native codebases for unused files, exports, and packages\"\n )\n .version(require(\"../package.json\").version)\n .option(\"--no-size\", \"Skip calculating package sizes\")\n .option(\"--no-exports\", \"Skip analyzing unused exports\")\n .option(\"--limit <n>\", \"Limit output rows\", \"50\");\n\nprogram.action(async (opts) => {\n const rootPath = process.cwd();\n const limit = Number(opts.limit);\n\n const report = await analyzeProject({\n rootPath,\n includeSizes: opts.size,\n analyzeExports: opts.exports\n });\n\n // ---------- Packages ----------\n console.log(\n boxen(pc.bold(\"๐ฆ Package Usage\"), {\n padding: 1,\n borderColor: \"green\",\n borderStyle: \"round\"\n })\n );\n\n const packageTable = new Table({ head: [\"Package\", \"Count\", \"Size\"] });\n Object.entries(report.packages)\n .sort((a, b) => b[1].count - a[1].count)\n .slice(0, limit)\n .forEach(([pkg, data]) => packageTable.push([pkg, data.count, data.size]));\n\n console.log(packageTable.toString());\n\n // ---------- Unused Files ----------\n if (report.unusedFiles.length) {\n console.log(\n boxen(pc.bold(`โ ๏ธ Unused Files (${report.unusedFiles.length})`), {\n padding: 1,\n borderColor: \"yellow\"\n })\n );\n report.unusedFiles.forEach((f) => console.log(pc.yellow(f)));\n } else {\n console.log(\n boxen(pc.green(\"โ
No unused files detected!\"), {\n padding: 1,\n borderColor: \"green\"\n })\n );\n }\n\n // ---------- Unused Exports ----------\n const unusedExportsEntries = Object.entries(report.unusedExports);\n if (unusedExportsEntries.length) {\n console.log(\n boxen(\n pc.bold(`โ ๏ธ Potential Unused Exports (${unusedExportsEntries.length})`),\n {\n padding: 1,\n borderColor: \"yellow\"\n }\n )\n );\n const exportsTable = new Table({\n head: [\"File\", \"Unused Exports\"],\n wordWrap: true\n });\n unusedExportsEntries.slice(0, limit).forEach(([file, exports]) => {\n exportsTable.push([file, exports.join(\", \")]);\n });\n console.log(exportsTable.toString());\n }\n});\n\nprogram.parse();\n"]}
|
package/package.json
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-prune",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"bin": {
|
|
6
6
|
"react-prune": "./dist/cli.js"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
|
-
"build": "tsup",
|
|
10
|
-
"dev": "tsup --watch",
|
|
9
|
+
"build": "tsup src/cli.ts --format cjs --dts",
|
|
10
|
+
"dev": "tsup src/cli.ts --watch",
|
|
11
11
|
"lint": "eslint src/**",
|
|
12
12
|
"format": "prettier --write .",
|
|
13
13
|
"prepublishOnly": "npm run build",
|
|
14
|
-
"release": "changeset publish"
|
|
14
|
+
"release": "changeset publish",
|
|
15
|
+
"test": "vitest"
|
|
15
16
|
},
|
|
16
17
|
"publishConfig": {
|
|
17
18
|
"access": "public"
|
|
@@ -62,6 +63,7 @@
|
|
|
62
63
|
"eslint": "^9.39.2",
|
|
63
64
|
"prettier": "^3.8.1",
|
|
64
65
|
"tsup": "^8.5.1",
|
|
65
|
-
"typescript": "^5.9.3"
|
|
66
|
+
"typescript": "^5.9.3",
|
|
67
|
+
"vitest": "^4.0.18"
|
|
66
68
|
}
|
|
67
69
|
}
|
package/dist/cli.d.mts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
package/dist/cli.mjs
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {Command}from'commander';import u from'picocolors';import p from'path';import {Project,SyntaxKind}from'ts-morph';import R from'fast-glob';import b from'fs';import P from'cli-table3';import F from'boxen';function w(s){let n=0;try{let r=b.readdirSync(s);for(let g of r){let d=p.join(s,g),o=b.statSync(d);o.isDirectory()?n+=w(d):n+=o.size;}}catch{return 0}return n}function $(s,n=2){if(s===0)return "0 Bytes";let r=1024,g=n<0?0:n,d=["Bytes","KB","MB","GB","TB"],o=Math.floor(Math.log(s)/Math.log(r));return parseFloat((s/Math.pow(r,o)).toFixed(g))+" "+d[o]}function T(s,n){let r=p.join(s,"node_modules",n);if(b.existsSync(r)){let g=w(r);return $(g)}return "N/A"}async function W(s){console.log(u.green(`Analyzing project at ${s}`));let n=await R("**/*.{js,jsx,ts,tsx}",{cwd:s,ignore:["**/node_modules/**","**/dist/**","**/build/**","**/.next/**","**/coverage/**","**/*.config.{js,ts,cjs,mjs}","**/.d.ts"],absolute:true});console.log(u.blue(`Found ${n.length} files to analyze.`));let r=p.join(s,"tsconfig.json"),g={skipAddingFilesFromTsConfig:true};b.existsSync(r)&&(g.tsConfigFilePath=r);let d=new Project(g);n.forEach(e=>{try{d.addSourceFileAtPath(e);}catch(f){console.warn(u.yellow(`Skipping file ${e} due to load error:`),f);}});let o={},i={};n.forEach(e=>{let f=p.relative(s,e);i[f]=0;});for(let e of d.getSourceFiles()){let f=e.getImportDeclarations();for(let h of f){let c=h.getModuleSpecifierValue();if(c.startsWith(".")||c.startsWith("/"))try{let a=e.getFilePath(),l=p.dirname(a),t=p.resolve(l,c),m=["",".ts",".tsx",".js",".jsx","/index.ts","/index.tsx","/index.js","/index.jsx"];for(let y of m){let C=t+y,v=p.relative(s,C);if(i.hasOwnProperty(v)){i[v]++;break}}}catch{}else {let a;try{let t=h.getModuleSpecifierSourceFile();t&&(a=t.getFilePath());}catch{}if(!a){let t=n.filter(m=>m.includes(c));if(t.length>0)for(let m of t){let y=m.replace(/\\/g,"/");if(y.endsWith(`${c}.tsx`)||y.endsWith(`${c}.ts`)||y.endsWith(`${c}/index.tsx`)){a=m;break}}}if(a){let t=p.relative(s,a);if(i.hasOwnProperty(t)){i[t]++;continue}}let l=c;if(c.startsWith("@")){let t=c.split("/");t.length>=2&&(l=`${t[0]}/${t[1]}`);}else {let t=c.split("/");t.length>=1&&(l=t[0]);}o[l]=(o[l]||0)+1;}}let x=e.getDescendantsOfKind(SyntaxKind.CallExpression);for(let h of x)if(h.getExpression().getText()==="require"){let a=h.getArguments();if(a.length>0&&a[0].getKind()===SyntaxKind.StringLiteral){let l=a[0].getText().replace(/['"`]/g,"");if(!l.startsWith(".")&&!l.startsWith("/")){let t=l.startsWith("@")?l.split("/").slice(0,2).join("/"):l.split("/")[0];o[t]=(o[t]||0)+1;}}}}let k={};for(let[e,f]of Object.entries(o)){let x=T(s,e);k[e]={count:f,size:x};}let A=Object.entries(i).filter(([e,f])=>e.includes("pages/")||e.includes("app/")||e.endsWith("main.tsx")||e.endsWith("index.tsx")||e.endsWith("index.js")||e.endsWith("App.tsx")||e.endsWith("App.js")||!p.relative(s,e).includes(p.sep)?false:f===0).map(([e])=>e);return {packages:k,components:i,unusedFiles:A}}var S=new Command;S.name("react-prune").description("Monitor usage of packages and component imports across your React/Next.js/React Native app").version("1.0.0");S.command("analyze [directory]").description("Analyze the current project for package and component usage").action(async s=>{console.log(u.blue("Starting analysis..."));try{let n=s?p.resolve(s):process.cwd(),r=await W(n),g=new P({head:[u.cyan("Package Name"),u.cyan("Usage Count"),u.cyan("Est. Size")],colWidths:[40,15,15]}),d=Object.entries(r.packages).sort((o,i)=>i[1].count-o[1].count);if(d.slice(0,50).forEach(([o,i])=>{g.push([o,i.count,i.size]);}),console.log(F(u.bold("\u{1F4E6} Package Usage Report"),{padding:1,margin:1,borderStyle:"round",borderColor:"green"})),console.log(g.toString()),d.length>50&&console.log(u.gray(`...and ${d.length-50} more packages.`)),r.unusedFiles.length>0){let o=new P({head:[u.yellow("File Path")],colWidths:[80]});console.log(F(u.bold(`\u26A0\uFE0F Potential Unused Files (${r.unusedFiles.length})`),{padding:1,margin:1,borderStyle:"round",borderColor:"yellow"})),r.unusedFiles.forEach(i=>o.push([i])),console.log(o.toString());}else console.log(F(u.bold("\u2705 No unused files detected!"),{padding:1,margin:1,borderStyle:"round",borderColor:"green"}));}catch(n){console.error(u.red("Analysis failed:"),n),process.exit(1);}});S.parse(process.argv);//# sourceMappingURL=cli.mjs.map
|
|
3
|
-
//# sourceMappingURL=cli.mjs.map
|
package/dist/cli.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/analyzer.ts","../src/cli.ts"],"names":["getFolderSize","dirPath","size","files","fs","file","filePath","path","stats","formatBytes","bytes","decimals","k","dm","sizes","i","getPackageSize","rootPath","packageName","pkgPath","analyzeProject","pc","glob","tsConfigPath","projectConfig","project","Project","e","packageUsage","localUsage","f","relative","sourceFile","imports","imp","moduleSpecifier","sourceFilePath","sourceDir","resolvedPath","extensions","ext","tryPath","relativeTry","resolvedLocalFile","resolvedSourceFile","possibleMatches","match","normalizedMatch","parts","callExpressions","SyntaxKind","call","args","rawArg","pkg","reportPackages","count","unused","program","Command","directory","targetDir","report","packageTable","Table","sortedPackages","a","b","data","boxen","unusedTable","error"],"mappings":";kNAaA,SAASA,EAAcC,CAAAA,CAAyB,CAC9C,IAAIC,CAAAA,CAAO,CAAA,CACX,GAAI,CACF,IAAMC,CAAAA,CAAQC,CAAAA,CAAG,WAAA,CAAYH,CAAO,CAAA,CACpC,IAAA,IAAWI,CAAAA,IAAQF,CAAAA,CAAO,CACxB,IAAMG,CAAAA,CAAWC,CAAAA,CAAK,IAAA,CAAKN,CAAAA,CAASI,CAAI,CAAA,CAClCG,CAAAA,CAAQJ,CAAAA,CAAG,QAAA,CAASE,CAAQ,CAAA,CAC9BE,CAAAA,CAAM,aAAY,CACpBN,CAAAA,EAAQF,CAAAA,CAAcM,CAAQ,CAAA,CAE9BJ,CAAAA,EAAQM,CAAAA,CAAM,KAElB,CACF,CAAA,KAAY,CACV,OAAO,CACT,CACA,OAAON,CACT,CAEA,SAASO,CAAAA,CAAYC,CAAAA,CAAeC,CAAAA,CAAW,CAAA,CAAG,CAChD,GAAID,CAAAA,GAAU,CAAA,CAAG,OAAO,SAAA,CACxB,IAAME,CAAAA,CAAI,KACJC,CAAAA,CAAKF,CAAAA,CAAW,CAAA,CAAI,CAAA,CAAIA,CAAAA,CACxBG,CAAAA,CAAQ,CAAC,OAAA,CAAS,IAAA,CAAM,IAAA,CAAM,IAAA,CAAM,IAAI,CAAA,CACxCC,CAAAA,CAAI,IAAA,CAAK,KAAA,CAAM,IAAA,CAAK,GAAA,CAAIL,CAAK,CAAA,CAAI,IAAA,CAAK,GAAA,CAAIE,CAAC,CAAC,CAAA,CAClD,OAAO,UAAA,CAAA,CAAYF,CAAAA,CAAQ,IAAA,CAAK,GAAA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C7D,OAAA,CAAQ,GAAA,CAAIiE,CAAAA,CAAY,QAAA,EAAU,EACpC,CAAA,KACE,OAAA,CAAQ,GAAA,CACND,CAAAA,CAAMhD,CAAAA,CAAG,IAAA,CAAK,kCAA6B,CAAA,CAAG,CAC5C,OAAA,CAAS,CAAA,CACT,MAAA,CAAQ,EACR,WAAA,CAAa,OAAA,CACb,WAAA,CAAa,OACf,CAAC,CACH,EAEJ,CAAA,MAASkD,CAAAA,CAAO,CACd,OAAA,CAAQ,KAAA,CAAMlD,CAAAA,CAAG,GAAA,CAAI,kBAAkB,CAAA,CAAGkD,CAAK,CAAA,CAC/C,OAAA,CAAQ,IAAA,CAAK,CAAC,EAChB,CACF,CAAC,CAAA,CAEHb,CAAAA,CAAQ,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA","file":"cli.mjs","sourcesContent":["import { Project, SyntaxKind } from \"ts-morph\";\nimport pc from \"picocolors\";\nimport glob from \"fast-glob\";\nimport path from \"path\";\nimport fs from \"fs\";\n\nexport interface UsageReport {\n packages: Record<string, { count: number; size: string }>;\n components: Record<string, number>;\n unusedFiles: string[];\n}\n\n// Helper to calculate folder size recursively\nfunction getFolderSize(dirPath: string): number {\n let size = 0;\n try {\n const files = fs.readdirSync(dirPath);\n for (const file of files) {\n const filePath = path.join(dirPath, file);\n const stats = fs.statSync(filePath);\n if (stats.isDirectory()) {\n size += getFolderSize(filePath);\n } else {\n size += stats.size;\n }\n }\n } catch (e) {\n return 0;\n }\n return size;\n}\n\nfunction formatBytes(bytes: number, decimals = 2) {\n if (bytes === 0) return \"0 Bytes\";\n const k = 1024;\n const dm = decimals < 0 ? 0 : decimals;\n const sizes = [\"Bytes\", \"KB\", \"MB\", \"GB\", \"TB\"];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + \" \" + sizes[i];\n}\n\nfunction getPackageSize(rootPath: string, packageName: string): string {\n // Try to find module in node_modules\n // Search in local node_modules first, then maybe recursive?\n // For now simple check in root/node_modules\n const pkgPath = path.join(rootPath, \"node_modules\", packageName);\n if (fs.existsSync(pkgPath)) {\n const size = getFolderSize(pkgPath);\n return formatBytes(size);\n }\n return \"N/A\";\n}\n\nexport async function analyzeProject(rootPath: string): Promise<UsageReport> {\n console.log(pc.green(`Analyzing project at ${rootPath}`));\n\n // 1. Find all files\n const files = await glob(\"**/*.{js,jsx,ts,tsx}\", {\n cwd: rootPath,\n ignore: [\n \"**/node_modules/**\",\n \"**/dist/**\",\n \"**/build/**\",\n \"**/.next/**\",\n \"**/coverage/**\",\n \"**/*.config.{js,ts,cjs,mjs}\", // Ignore config files\n \"**/.d.ts\" // Ignore definition files\n ],\n absolute: true\n });\n\n console.log(pc.blue(`Found ${files.length} files to analyze.`));\n\n // 2. Initialize ts-morph project\n const tsConfigPath = path.join(rootPath, \"tsconfig.json\");\n const projectConfig: any = {\n skipAddingFilesFromTsConfig: true\n };\n\n if (fs.existsSync(tsConfigPath)) {\n projectConfig.tsConfigFilePath = tsConfigPath;\n }\n\n const project = new Project(projectConfig);\n\n // Add files to project\n files.forEach((file) => {\n try {\n project.addSourceFileAtPath(file);\n } catch (e) {\n console.warn(pc.yellow(`Skipping file ${file} due to load error:`), e);\n }\n });\n\n const packageUsage: Record<string, number> = {};\n const localUsage: Record<string, number> = {};\n\n // Initialize local usage with 0 for all files to track unused ones\n files.forEach((f) => {\n // Normalize path to be relative and standard for comparison\n const relative = path.relative(rootPath, f);\n localUsage[relative] = 0;\n });\n\n // 3. Analyze Imports\n for (const sourceFile of project.getSourceFiles()) {\n const imports = sourceFile.getImportDeclarations();\n\n for (const imp of imports) {\n const moduleSpecifier = imp.getModuleSpecifierValue();\n\n if (moduleSpecifier.startsWith(\".\") || moduleSpecifier.startsWith(\"/\")) {\n // Local Import\n try {\n // Resolve the import to a file on disk\n const sourceFilePath = sourceFile.getFilePath();\n const sourceDir = path.dirname(sourceFilePath);\n\n const resolvedPath = path.resolve(sourceDir, moduleSpecifier);\n // We need to try extensions\n const extensions = [\n \"\",\n \".ts\",\n \".tsx\",\n \".js\",\n \".jsx\",\n \"/index.ts\",\n \"/index.tsx\",\n \"/index.js\",\n \"/index.jsx\"\n ];\n\n for (const ext of extensions) {\n const tryPath = resolvedPath + ext;\n const relativeTry = path.relative(rootPath, tryPath);\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n break;\n }\n }\n } catch (e) {\n // ignore resolution errors\n }\n } else {\n // Check if it's a path alias or non-relative local import\n let resolvedLocalFile: string | undefined;\n\n // Try ts-morph resolution first (if tsconfig loaded)\n try {\n const resolvedSourceFile = imp.getModuleSpecifierSourceFile();\n if (resolvedSourceFile) {\n resolvedLocalFile = resolvedSourceFile.getFilePath();\n }\n } catch (e) {}\n\n // Fallback: Check if the module specifier matches a known local file\n // relative to baseUrl (src) or just fuzzy match\n if (!resolvedLocalFile) {\n const possibleMatches = files.filter((f) =>\n f.includes(moduleSpecifier)\n );\n if (possibleMatches.length > 0) {\n for (const match of possibleMatches) {\n const normalizedMatch = match.replace(/\\\\/g, \"/\");\n if (\n normalizedMatch.endsWith(`${moduleSpecifier}.tsx`) ||\n normalizedMatch.endsWith(`${moduleSpecifier}.ts`) ||\n normalizedMatch.endsWith(`${moduleSpecifier}/index.tsx`)\n ) {\n resolvedLocalFile = match;\n break;\n }\n }\n }\n }\n\n if (resolvedLocalFile) {\n const relativeTry = path.relative(rootPath, resolvedLocalFile);\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n continue; // Skip package counting\n }\n }\n\n // Package Import\n let packageName = moduleSpecifier;\n if (moduleSpecifier.startsWith(\"@\")) {\n const parts = moduleSpecifier.split(\"/\");\n if (parts.length >= 2) {\n packageName = `${parts[0]}/${parts[1]}`;\n }\n } else {\n const parts = moduleSpecifier.split(\"/\");\n if (parts.length >= 1) {\n packageName = parts[0];\n }\n }\n\n packageUsage[packageName] = (packageUsage[packageName] || 0) + 1;\n }\n }\n\n // Check for require() calls (CommonJS)\n const callExpressions = sourceFile.getDescendantsOfKind(\n SyntaxKind.CallExpression\n );\n for (const call of callExpressions) {\n const expression = call.getExpression();\n if (expression.getText() === \"require\") {\n const args = call.getArguments();\n if (args.length > 0 && args[0].getKind() === SyntaxKind.StringLiteral) {\n const rawArg = args[0].getText().replace(/['\"`]/g, \"\");\n // Simple duplicate logic for MVP (should refactor)\n if (!rawArg.startsWith(\".\") && !rawArg.startsWith(\"/\")) {\n let pkg = rawArg.startsWith(\"@\")\n ? rawArg.split(\"/\").slice(0, 2).join(\"/\")\n : rawArg.split(\"/\")[0];\n packageUsage[pkg] = (packageUsage[pkg] || 0) + 1;\n }\n }\n }\n }\n }\n\n // 4. Construct Report Data\n const reportPackages: Record<string, { count: number; size: string }> = {};\n\n for (const [pkg, count] of Object.entries(packageUsage)) {\n const size = getPackageSize(rootPath, pkg);\n reportPackages[pkg] = { count, size };\n }\n\n const unused = Object.entries(localUsage)\n .filter(([file, count]) => {\n // Known Entry Points & Framework specifics\n if (\n file.includes(\"pages/\") ||\n file.includes(\"app/\") ||\n file.endsWith(\"main.tsx\") ||\n file.endsWith(\"index.tsx\") ||\n file.endsWith(\"index.js\") ||\n file.endsWith(\"App.tsx\") ||\n file.endsWith(\"App.js\")\n )\n return false;\n\n // Ignore files in the project root (usually configs, scripts, etc.)\n // We check if the relative path contains a separator. If not, it's in the root.\n const relative = path.relative(rootPath, file);\n if (!relative.includes(path.sep)) {\n return false;\n }\n return count === 0;\n })\n .map(([file]) => file);\n\n return {\n packages: reportPackages,\n components: localUsage,\n unusedFiles: unused\n };\n}\n","#!/usr/bin/env node\nimport { Command } from \"commander\";\nimport pc from \"picocolors\";\nimport path from \"path\";\nimport { analyzeProject } from \"./analyzer\";\n// @ts-ignore\nimport Table from \"cli-table3\";\n// @ts-ignore\nimport boxen from \"boxen\";\n\nconst program = new Command();\n\nprogram\n .name(\"react-prune\")\n .description(\n \"Monitor usage of packages and component imports across your React/Next.js/React Native app\"\n )\n .version(\"1.0.0\");\n\nprogram\n .command(\"analyze [directory]\")\n .description(\"Analyze the current project for package and component usage\")\n .action(async (directory) => {\n console.log(pc.blue(\"Starting analysis...\"));\n try {\n const targetDir = directory ? path.resolve(directory) : process.cwd();\n const report = await analyzeProject(targetDir);\n\n // Package Usage Table\n const packageTable = new Table({\n head: [\n pc.cyan(\"Package Name\"),\n pc.cyan(\"Usage Count\"),\n pc.cyan(\"Est. Size\")\n ],\n colWidths: [40, 15, 15]\n });\n\n const sortedPackages = Object.entries(report.packages).sort(\n (a, b) => b[1].count - a[1].count\n );\n\n sortedPackages.slice(0, 50).forEach(([pkg, data]) => {\n packageTable.push([pkg, data.count, data.size]);\n });\n\n console.log(\n boxen(pc.bold(\"๐ฆ Package Usage Report\"), {\n padding: 1,\n margin: 1,\n borderStyle: \"round\",\n borderColor: \"green\"\n })\n );\n console.log(packageTable.toString());\n if (sortedPackages.length > 50) {\n console.log(\n pc.gray(`...and ${sortedPackages.length - 50} more packages.`)\n );\n }\n\n // Unused Files\n if (report.unusedFiles.length > 0) {\n const unusedTable = new Table({\n head: [pc.yellow(\"File Path\")],\n colWidths: [80]\n });\n\n console.log(\n boxen(\n pc.bold(\n `โ ๏ธ Potential Unused Files (${report.unusedFiles.length})`\n ),\n {\n padding: 1,\n margin: 1,\n borderStyle: \"round\",\n borderColor: \"yellow\"\n }\n )\n );\n\n report.unusedFiles.forEach((file) => unusedTable.push([file]));\n console.log(unusedTable.toString());\n } else {\n console.log(\n boxen(pc.bold(\"โ
No unused files detected!\"), {\n padding: 1,\n margin: 1,\n borderStyle: \"round\",\n borderColor: \"green\"\n })\n );\n }\n } catch (error) {\n console.error(pc.red(\"Analysis failed:\"), error);\n process.exit(1);\n }\n });\n\nprogram.parse(process.argv);\n"]}
|
package/dist/index.d.mts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
interface UsageReport {
|
|
2
|
-
packages: Record<string, {
|
|
3
|
-
count: number;
|
|
4
|
-
size: string;
|
|
5
|
-
}>;
|
|
6
|
-
components: Record<string, number>;
|
|
7
|
-
unusedFiles: string[];
|
|
8
|
-
}
|
|
9
|
-
declare function analyzeProject(rootPath: string): Promise<UsageReport>;
|
|
10
|
-
|
|
11
|
-
export { type UsageReport, analyzeProject };
|
package/dist/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
interface UsageReport {
|
|
2
|
-
packages: Record<string, {
|
|
3
|
-
count: number;
|
|
4
|
-
size: string;
|
|
5
|
-
}>;
|
|
6
|
-
components: Record<string, number>;
|
|
7
|
-
unusedFiles: string[];
|
|
8
|
-
}
|
|
9
|
-
declare function analyzeProject(rootPath: string): Promise<UsageReport>;
|
|
10
|
-
|
|
11
|
-
export { type UsageReport, analyzeProject };
|
package/dist/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
'use strict';var tsMorph=require('ts-morph'),y=require('picocolors'),w=require('fast-glob'),l=require('path'),j=require('fs');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var y__default=/*#__PURE__*/_interopDefault(y);var w__default=/*#__PURE__*/_interopDefault(w);var l__default=/*#__PURE__*/_interopDefault(l);var j__default=/*#__PURE__*/_interopDefault(j);function k(s){let n=0;try{let c=j__default.default.readdirSync(s);for(let f of c){let p=l__default.default.join(s,f),a=j__default.default.statSync(p);a.isDirectory()?n+=k(p):n+=a.size;}}catch{return 0}return n}function M(s,n=2){if(s===0)return "0 Bytes";let c=1024,f=n<0?0:n,p=["Bytes","KB","MB","GB","TB"],a=Math.floor(Math.log(s)/Math.log(c));return parseFloat((s/Math.pow(c,a)).toFixed(f))+" "+p[a]}function P(s,n){let c=l__default.default.join(s,"node_modules",n);if(j__default.default.existsSync(c)){let f=k(c);return M(f)}return "N/A"}async function C(s){console.log(y__default.default.green(`Analyzing project at ${s}`));let n=await w__default.default("**/*.{js,jsx,ts,tsx}",{cwd:s,ignore:["**/node_modules/**","**/dist/**","**/build/**","**/.next/**","**/coverage/**","**/*.config.{js,ts,cjs,mjs}","**/.d.ts"],absolute:true});console.log(y__default.default.blue(`Found ${n.length} files to analyze.`));let c=l__default.default.join(s,"tsconfig.json"),f={skipAddingFilesFromTsConfig:true};j__default.default.existsSync(c)&&(f.tsConfigFilePath=c);let p=new tsMorph.Project(f);n.forEach(e=>{try{p.addSourceFileAtPath(e);}catch(g){console.warn(y__default.default.yellow(`Skipping file ${e} due to load error:`),g);}});let a={},d={};n.forEach(e=>{let g=l__default.default.relative(s,e);d[g]=0;});for(let e of p.getSourceFiles()){let g=e.getImportDeclarations();for(let h of g){let i=h.getModuleSpecifierValue();if(i.startsWith(".")||i.startsWith("/"))try{let o=e.getFilePath(),r=l__default.default.dirname(o),t=l__default.default.resolve(r,i),u=["",".ts",".tsx",".js",".jsx","/index.ts","/index.tsx","/index.js","/index.jsx"];for(let m of u){let W=t+m,S=l__default.default.relative(s,W);if(d.hasOwnProperty(S)){d[S]++;break}}}catch{}else {let o;try{let t=h.getModuleSpecifierSourceFile();t&&(o=t.getFilePath());}catch{}if(!o){let t=n.filter(u=>u.includes(i));if(t.length>0)for(let u of t){let m=u.replace(/\\/g,"/");if(m.endsWith(`${i}.tsx`)||m.endsWith(`${i}.ts`)||m.endsWith(`${i}/index.tsx`)){o=u;break}}}if(o){let t=l__default.default.relative(s,o);if(d.hasOwnProperty(t)){d[t]++;continue}}let r=i;if(i.startsWith("@")){let t=i.split("/");t.length>=2&&(r=`${t[0]}/${t[1]}`);}else {let t=i.split("/");t.length>=1&&(r=t[0]);}a[r]=(a[r]||0)+1;}}let x=e.getDescendantsOfKind(tsMorph.SyntaxKind.CallExpression);for(let h of x)if(h.getExpression().getText()==="require"){let o=h.getArguments();if(o.length>0&&o[0].getKind()===tsMorph.SyntaxKind.StringLiteral){let r=o[0].getText().replace(/['"`]/g,"");if(!r.startsWith(".")&&!r.startsWith("/")){let t=r.startsWith("@")?r.split("/").slice(0,2).join("/"):r.split("/")[0];a[t]=(a[t]||0)+1;}}}}let F={};for(let[e,g]of Object.entries(a)){let x=P(s,e);F[e]={count:g,size:x};}let v=Object.entries(d).filter(([e,g])=>e.includes("pages/")||e.includes("app/")||e.endsWith("main.tsx")||e.endsWith("index.tsx")||e.endsWith("index.js")||e.endsWith("App.tsx")||e.endsWith("App.js")||!l__default.default.relative(s,e).includes(l__default.default.sep)?false:g===0).map(([e])=>e);return {packages:F,components:d,unusedFiles:v}}exports.analyzeProject=C;//# sourceMappingURL=index.js.map
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/analyzer.ts"],"names":["getFolderSize","dirPath","size","files","fs","file","filePath","path","stats","formatBytes","bytes","decimals","k","dm","sizes","i","getPackageSize","rootPath","packageName","pkgPath","analyzeProject","pc","glob","tsConfigPath","projectConfig","project","Project","e","packageUsage","localUsage","f","relative","sourceFile","imports","imp","moduleSpecifier","sourceFilePath","sourceDir","resolvedPath","extensions","ext","tryPath","relativeTry","resolvedLocalFile","resolvedSourceFile","possibleMatches","match","normalizedMatch","parts","callExpressions","SyntaxKind","call","args","rawArg","pkg","reportPackages","count","unused"],"mappings":"2XAaA,SAASA,CAAAA,CAAcC,CAAAA,CAAyB,CAC9C,IAAIC,EAAO,CAAA,CACX,GAAI,CACF,IAAMC,CAAAA,CAAQC,mBAAG,WAAA,CAAYH,CAAO,CAAA,CACpC,IAAA,IAAWI,KAAQF,CAAAA,CAAO,CACxB,IAAMG,CAAAA,CAAWC,kBAAAA,CAAK,KAAKN,CAAAA,CAASI,CAAI,CAAA,CAClCG,CAAAA,CAAQJ,mBAAG,QAAA,CAASE,CAAQ,EAC9BE,CAAAA,CAAM,WAAA,GACRN,CAAAA,EAAQF,CAAAA,CAAcM,CAAQ,CAAA,CAE9BJ,GAAQM,CAAAA,CAAM,KAElB,CACF,CAAA,KAAY,CACV,OAAO,CACT,CACA,OAAON,CACT,CAEA,SAASO,CAAAA,CAAYC,EAAeC,CAAAA,CAAW,CAAA,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A,CAAM,EAAG,CAAC,CAAA,CAAE,IAAA,CAAK,GAAG,EACtCA,CAAAA,CAAO,KAAA,CAAM,GAAG,CAAA,CAAE,CAAC,EACvBzB,CAAAA,CAAa0B,CAAG,CAAA,CAAA,CAAK1B,CAAAA,CAAa0B,CAAG,CAAA,EAAK,CAAA,EAAK,EACjD,CACF,CACF,CAEJ,CAGA,IAAMC,CAAAA,CAAkE,GAExE,IAAA,GAAW,CAACD,EAAKE,CAAK,CAAA,GAAK,OAAO,OAAA,CAAQ5B,CAAY,CAAA,CAAG,CACvD,IAAM1B,CAAAA,CAAOc,CAAAA,CAAeC,EAAUqC,CAAG,CAAA,CACzCC,EAAeD,CAAG,CAAA,CAAI,CAAE,KAAA,CAAAE,EAAO,IAAA,CAAAtD,CAAK,EACtC,CAEA,IAAMuD,EAAS,MAAA,CAAO,OAAA,CAAQ5B,CAAU,CAAA,CACrC,OAAO,CAAC,CAACxB,EAAMmD,CAAK,CAAA,GAGjBnD,EAAK,QAAA,CAAS,QAAQ,CAAA,EACtBA,CAAAA,CAAK,SAAS,MAAM,CAAA,EACpBA,EAAK,QAAA,CAAS,UAAU,GACxBA,CAAAA,CAAK,QAAA,CAAS,WAAW,CAAA,EACzBA,EAAK,QAAA,CAAS,UAAU,GACxBA,CAAAA,CAAK,QAAA,CAAS,SAAS,CAAA,EACvBA,CAAAA,CAAK,QAAA,CAAS,QAAQ,GAOpB,CADaE,kBAAAA,CAAK,SAASU,CAAAA,CAAUZ,CAAI,EAC/B,QAAA,CAASE,kBAAAA,CAAK,GAAG,CAAA,CACtB,MAEFiD,CAAAA,GAAU,CAClB,EACA,GAAA,CAAI,CAAC,CAACnD,CAAI,CAAA,GAAMA,CAAI,CAAA,CAEvB,OAAO,CACL,QAAA,CAAUkD,CAAAA,CACV,WAAY1B,CAAAA,CACZ,WAAA,CAAa4B,CACf,CACF","file":"index.js","sourcesContent":["import { Project, SyntaxKind } from \"ts-morph\";\nimport pc from \"picocolors\";\nimport glob from \"fast-glob\";\nimport path from \"path\";\nimport fs from \"fs\";\n\nexport interface UsageReport {\n packages: Record<string, { count: number; size: string }>;\n components: Record<string, number>;\n unusedFiles: string[];\n}\n\n// Helper to calculate folder size recursively\nfunction getFolderSize(dirPath: string): number {\n let size = 0;\n try {\n const files = fs.readdirSync(dirPath);\n for (const file of files) {\n const filePath = path.join(dirPath, file);\n const stats = fs.statSync(filePath);\n if (stats.isDirectory()) {\n size += getFolderSize(filePath);\n } else {\n size += stats.size;\n }\n }\n } catch (e) {\n return 0;\n }\n return size;\n}\n\nfunction formatBytes(bytes: number, decimals = 2) {\n if (bytes === 0) return \"0 Bytes\";\n const k = 1024;\n const dm = decimals < 0 ? 0 : decimals;\n const sizes = [\"Bytes\", \"KB\", \"MB\", \"GB\", \"TB\"];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + \" \" + sizes[i];\n}\n\nfunction getPackageSize(rootPath: string, packageName: string): string {\n // Try to find module in node_modules\n // Search in local node_modules first, then maybe recursive?\n // For now simple check in root/node_modules\n const pkgPath = path.join(rootPath, \"node_modules\", packageName);\n if (fs.existsSync(pkgPath)) {\n const size = getFolderSize(pkgPath);\n return formatBytes(size);\n }\n return \"N/A\";\n}\n\nexport async function analyzeProject(rootPath: string): Promise<UsageReport> {\n console.log(pc.green(`Analyzing project at ${rootPath}`));\n\n // 1. Find all files\n const files = await glob(\"**/*.{js,jsx,ts,tsx}\", {\n cwd: rootPath,\n ignore: [\n \"**/node_modules/**\",\n \"**/dist/**\",\n \"**/build/**\",\n \"**/.next/**\",\n \"**/coverage/**\",\n \"**/*.config.{js,ts,cjs,mjs}\", // Ignore config files\n \"**/.d.ts\" // Ignore definition files\n ],\n absolute: true\n });\n\n console.log(pc.blue(`Found ${files.length} files to analyze.`));\n\n // 2. Initialize ts-morph project\n const tsConfigPath = path.join(rootPath, \"tsconfig.json\");\n const projectConfig: any = {\n skipAddingFilesFromTsConfig: true\n };\n\n if (fs.existsSync(tsConfigPath)) {\n projectConfig.tsConfigFilePath = tsConfigPath;\n }\n\n const project = new Project(projectConfig);\n\n // Add files to project\n files.forEach((file) => {\n try {\n project.addSourceFileAtPath(file);\n } catch (e) {\n console.warn(pc.yellow(`Skipping file ${file} due to load error:`), e);\n }\n });\n\n const packageUsage: Record<string, number> = {};\n const localUsage: Record<string, number> = {};\n\n // Initialize local usage with 0 for all files to track unused ones\n files.forEach((f) => {\n // Normalize path to be relative and standard for comparison\n const relative = path.relative(rootPath, f);\n localUsage[relative] = 0;\n });\n\n // 3. Analyze Imports\n for (const sourceFile of project.getSourceFiles()) {\n const imports = sourceFile.getImportDeclarations();\n\n for (const imp of imports) {\n const moduleSpecifier = imp.getModuleSpecifierValue();\n\n if (moduleSpecifier.startsWith(\".\") || moduleSpecifier.startsWith(\"/\")) {\n // Local Import\n try {\n // Resolve the import to a file on disk\n const sourceFilePath = sourceFile.getFilePath();\n const sourceDir = path.dirname(sourceFilePath);\n\n const resolvedPath = path.resolve(sourceDir, moduleSpecifier);\n // We need to try extensions\n const extensions = [\n \"\",\n \".ts\",\n \".tsx\",\n \".js\",\n \".jsx\",\n \"/index.ts\",\n \"/index.tsx\",\n \"/index.js\",\n \"/index.jsx\"\n ];\n\n for (const ext of extensions) {\n const tryPath = resolvedPath + ext;\n const relativeTry = path.relative(rootPath, tryPath);\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n break;\n }\n }\n } catch (e) {\n // ignore resolution errors\n }\n } else {\n // Check if it's a path alias or non-relative local import\n let resolvedLocalFile: string | undefined;\n\n // Try ts-morph resolution first (if tsconfig loaded)\n try {\n const resolvedSourceFile = imp.getModuleSpecifierSourceFile();\n if (resolvedSourceFile) {\n resolvedLocalFile = resolvedSourceFile.getFilePath();\n }\n } catch (e) {}\n\n // Fallback: Check if the module specifier matches a known local file\n // relative to baseUrl (src) or just fuzzy match\n if (!resolvedLocalFile) {\n const possibleMatches = files.filter((f) =>\n f.includes(moduleSpecifier)\n );\n if (possibleMatches.length > 0) {\n for (const match of possibleMatches) {\n const normalizedMatch = match.replace(/\\\\/g, \"/\");\n if (\n normalizedMatch.endsWith(`${moduleSpecifier}.tsx`) ||\n normalizedMatch.endsWith(`${moduleSpecifier}.ts`) ||\n normalizedMatch.endsWith(`${moduleSpecifier}/index.tsx`)\n ) {\n resolvedLocalFile = match;\n break;\n }\n }\n }\n }\n\n if (resolvedLocalFile) {\n const relativeTry = path.relative(rootPath, resolvedLocalFile);\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n continue; // Skip package counting\n }\n }\n\n // Package Import\n let packageName = moduleSpecifier;\n if (moduleSpecifier.startsWith(\"@\")) {\n const parts = moduleSpecifier.split(\"/\");\n if (parts.length >= 2) {\n packageName = `${parts[0]}/${parts[1]}`;\n }\n } else {\n const parts = moduleSpecifier.split(\"/\");\n if (parts.length >= 1) {\n packageName = parts[0];\n }\n }\n\n packageUsage[packageName] = (packageUsage[packageName] || 0) + 1;\n }\n }\n\n // Check for require() calls (CommonJS)\n const callExpressions = sourceFile.getDescendantsOfKind(\n SyntaxKind.CallExpression\n );\n for (const call of callExpressions) {\n const expression = call.getExpression();\n if (expression.getText() === \"require\") {\n const args = call.getArguments();\n if (args.length > 0 && args[0].getKind() === SyntaxKind.StringLiteral) {\n const rawArg = args[0].getText().replace(/['\"`]/g, \"\");\n // Simple duplicate logic for MVP (should refactor)\n if (!rawArg.startsWith(\".\") && !rawArg.startsWith(\"/\")) {\n let pkg = rawArg.startsWith(\"@\")\n ? rawArg.split(\"/\").slice(0, 2).join(\"/\")\n : rawArg.split(\"/\")[0];\n packageUsage[pkg] = (packageUsage[pkg] || 0) + 1;\n }\n }\n }\n }\n }\n\n // 4. Construct Report Data\n const reportPackages: Record<string, { count: number; size: string }> = {};\n\n for (const [pkg, count] of Object.entries(packageUsage)) {\n const size = getPackageSize(rootPath, pkg);\n reportPackages[pkg] = { count, size };\n }\n\n const unused = Object.entries(localUsage)\n .filter(([file, count]) => {\n // Known Entry Points & Framework specifics\n if (\n file.includes(\"pages/\") ||\n file.includes(\"app/\") ||\n file.endsWith(\"main.tsx\") ||\n file.endsWith(\"index.tsx\") ||\n file.endsWith(\"index.js\") ||\n file.endsWith(\"App.tsx\") ||\n file.endsWith(\"App.js\")\n )\n return false;\n\n // Ignore files in the project root (usually configs, scripts, etc.)\n // We check if the relative path contains a separator. If not, it's in the root.\n const relative = path.relative(rootPath, file);\n if (!relative.includes(path.sep)) {\n return false;\n }\n return count === 0;\n })\n .map(([file]) => file);\n\n return {\n packages: reportPackages,\n components: localUsage,\n unusedFiles: unused\n };\n}\n"]}
|
package/dist/index.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import {Project,SyntaxKind}from'ts-morph';import y from'picocolors';import w from'fast-glob';import l from'path';import j from'fs';function k(s){let n=0;try{let c=j.readdirSync(s);for(let f of c){let p=l.join(s,f),a=j.statSync(p);a.isDirectory()?n+=k(p):n+=a.size;}}catch{return 0}return n}function M(s,n=2){if(s===0)return "0 Bytes";let c=1024,f=n<0?0:n,p=["Bytes","KB","MB","GB","TB"],a=Math.floor(Math.log(s)/Math.log(c));return parseFloat((s/Math.pow(c,a)).toFixed(f))+" "+p[a]}function P(s,n){let c=l.join(s,"node_modules",n);if(j.existsSync(c)){let f=k(c);return M(f)}return "N/A"}async function C(s){console.log(y.green(`Analyzing project at ${s}`));let n=await w("**/*.{js,jsx,ts,tsx}",{cwd:s,ignore:["**/node_modules/**","**/dist/**","**/build/**","**/.next/**","**/coverage/**","**/*.config.{js,ts,cjs,mjs}","**/.d.ts"],absolute:true});console.log(y.blue(`Found ${n.length} files to analyze.`));let c=l.join(s,"tsconfig.json"),f={skipAddingFilesFromTsConfig:true};j.existsSync(c)&&(f.tsConfigFilePath=c);let p=new Project(f);n.forEach(e=>{try{p.addSourceFileAtPath(e);}catch(g){console.warn(y.yellow(`Skipping file ${e} due to load error:`),g);}});let a={},d={};n.forEach(e=>{let g=l.relative(s,e);d[g]=0;});for(let e of p.getSourceFiles()){let g=e.getImportDeclarations();for(let h of g){let i=h.getModuleSpecifierValue();if(i.startsWith(".")||i.startsWith("/"))try{let o=e.getFilePath(),r=l.dirname(o),t=l.resolve(r,i),u=["",".ts",".tsx",".js",".jsx","/index.ts","/index.tsx","/index.js","/index.jsx"];for(let m of u){let W=t+m,S=l.relative(s,W);if(d.hasOwnProperty(S)){d[S]++;break}}}catch{}else {let o;try{let t=h.getModuleSpecifierSourceFile();t&&(o=t.getFilePath());}catch{}if(!o){let t=n.filter(u=>u.includes(i));if(t.length>0)for(let u of t){let m=u.replace(/\\/g,"/");if(m.endsWith(`${i}.tsx`)||m.endsWith(`${i}.ts`)||m.endsWith(`${i}/index.tsx`)){o=u;break}}}if(o){let t=l.relative(s,o);if(d.hasOwnProperty(t)){d[t]++;continue}}let r=i;if(i.startsWith("@")){let t=i.split("/");t.length>=2&&(r=`${t[0]}/${t[1]}`);}else {let t=i.split("/");t.length>=1&&(r=t[0]);}a[r]=(a[r]||0)+1;}}let x=e.getDescendantsOfKind(SyntaxKind.CallExpression);for(let h of x)if(h.getExpression().getText()==="require"){let o=h.getArguments();if(o.length>0&&o[0].getKind()===SyntaxKind.StringLiteral){let r=o[0].getText().replace(/['"`]/g,"");if(!r.startsWith(".")&&!r.startsWith("/")){let t=r.startsWith("@")?r.split("/").slice(0,2).join("/"):r.split("/")[0];a[t]=(a[t]||0)+1;}}}}let F={};for(let[e,g]of Object.entries(a)){let x=P(s,e);F[e]={count:g,size:x};}let v=Object.entries(d).filter(([e,g])=>e.includes("pages/")||e.includes("app/")||e.endsWith("main.tsx")||e.endsWith("index.tsx")||e.endsWith("index.js")||e.endsWith("App.tsx")||e.endsWith("App.js")||!l.relative(s,e).includes(l.sep)?false:g===0).map(([e])=>e);return {packages:F,components:d,unusedFiles:v}}export{C as analyzeProject};//# sourceMappingURL=index.mjs.map
|
|
2
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/analyzer.ts"],"names":["getFolderSize","dirPath","size","files","fs","file","filePath","path","stats","formatBytes","bytes","decimals","k","dm","sizes","i","getPackageSize","rootPath","packageName","pkgPath","analyzeProject","pc","glob","tsConfigPath","projectConfig","project","Project","e","packageUsage","localUsage","f","relative","sourceFile","imports","imp","moduleSpecifier","sourceFilePath","sourceDir","resolvedPath","extensions","ext","tryPath","relativeTry","resolvedLocalFile","resolvedSourceFile","possibleMatches","match","normalizedMatch","parts","callExpressions","SyntaxKind","call","args","rawArg","pkg","reportPackages","count","unused"],"mappings":"mIAaA,SAASA,CAAAA,CAAcC,CAAAA,CAAyB,CAC9C,IAAIC,EAAO,CAAA,CACX,GAAI,CACF,IAAMC,CAAAA,CAAQC,EAAG,WAAA,CAAYH,CAAO,CAAA,CACpC,IAAA,IAAWI,KAAQF,CAAAA,CAAO,CACxB,IAAMG,CAAAA,CAAWC,CAAAA,CAAK,KAAKN,CAAAA,CAASI,CAAI,CAAA,CAClCG,CAAAA,CAAQJ,EAAG,QAAA,CAASE,CAAQ,EAC9BE,CAAAA,CAAM,WAAA,GACRN,CAAAA,EAAQF,CAAAA,CAAcM,CAAQ,CAAA,CAE9BJ,GAAQM,CAAAA,CAAM,KAElB,CACF,CAAA,KAAY,CACV,OAAO,CACT,CACA,OAAON,CACT,CAEA,SAASO,CAAAA,CAAYC,EAAeC,CAAAA,CAAW,CAAA,CAAG,CAChD,GAAID,CAAAA,GAAU,CAAA,CAAG,OAAO,UACxB,IAAME,CAAAA,CAAI,KACJC,CAAAA,CAAKF,CAAAA,CAAW,EAAI,CAAA,CAAIA,CAAAA,CACxBG,CAAAA,CAAQ,CAAC,QAAS,IAAA,CAAM,IAAA,CAAM,KAAM,IAAI,CAAA,CACxCC,EAAI,IAAA,CAAK,KAAA,CAAM,IAAA,CAAK,GAAA,CAAIL,CAAK,CAAA,CAAI,IAAA,CAAK,IAAIE,CAAC,CA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CAAG,CACvD,IAAM1B,CAAAA,CAAOc,CAAAA,CAAeC,EAAUqC,CAAG,CAAA,CACzCC,EAAeD,CAAG,CAAA,CAAI,CAAE,KAAA,CAAAE,EAAO,IAAA,CAAAtD,CAAK,EACtC,CAEA,IAAMuD,EAAS,MAAA,CAAO,OAAA,CAAQ5B,CAAU,CAAA,CACrC,OAAO,CAAC,CAACxB,EAAMmD,CAAK,CAAA,GAGjBnD,EAAK,QAAA,CAAS,QAAQ,CAAA,EACtBA,CAAAA,CAAK,SAAS,MAAM,CAAA,EACpBA,EAAK,QAAA,CAAS,UAAU,GACxBA,CAAAA,CAAK,QAAA,CAAS,WAAW,CAAA,EACzBA,EAAK,QAAA,CAAS,UAAU,GACxBA,CAAAA,CAAK,QAAA,CAAS,SAAS,CAAA,EACvBA,CAAAA,CAAK,QAAA,CAAS,QAAQ,GAOpB,CADaE,CAAAA,CAAK,SAASU,CAAAA,CAAUZ,CAAI,EAC/B,QAAA,CAASE,CAAAA,CAAK,GAAG,CAAA,CACtB,MAEFiD,CAAAA,GAAU,CAClB,EACA,GAAA,CAAI,CAAC,CAACnD,CAAI,CAAA,GAAMA,CAAI,CAAA,CAEvB,OAAO,CACL,QAAA,CAAUkD,CAAAA,CACV,WAAY1B,CAAAA,CACZ,WAAA,CAAa4B,CACf,CACF","file":"index.mjs","sourcesContent":["import { Project, SyntaxKind } from \"ts-morph\";\nimport pc from \"picocolors\";\nimport glob from \"fast-glob\";\nimport path from \"path\";\nimport fs from \"fs\";\n\nexport interface UsageReport {\n packages: Record<string, { count: number; size: string }>;\n components: Record<string, number>;\n unusedFiles: string[];\n}\n\n// Helper to calculate folder size recursively\nfunction getFolderSize(dirPath: string): number {\n let size = 0;\n try {\n const files = fs.readdirSync(dirPath);\n for (const file of files) {\n const filePath = path.join(dirPath, file);\n const stats = fs.statSync(filePath);\n if (stats.isDirectory()) {\n size += getFolderSize(filePath);\n } else {\n size += stats.size;\n }\n }\n } catch (e) {\n return 0;\n }\n return size;\n}\n\nfunction formatBytes(bytes: number, decimals = 2) {\n if (bytes === 0) return \"0 Bytes\";\n const k = 1024;\n const dm = decimals < 0 ? 0 : decimals;\n const sizes = [\"Bytes\", \"KB\", \"MB\", \"GB\", \"TB\"];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + \" \" + sizes[i];\n}\n\nfunction getPackageSize(rootPath: string, packageName: string): string {\n // Try to find module in node_modules\n // Search in local node_modules first, then maybe recursive?\n // For now simple check in root/node_modules\n const pkgPath = path.join(rootPath, \"node_modules\", packageName);\n if (fs.existsSync(pkgPath)) {\n const size = getFolderSize(pkgPath);\n return formatBytes(size);\n }\n return \"N/A\";\n}\n\nexport async function analyzeProject(rootPath: string): Promise<UsageReport> {\n console.log(pc.green(`Analyzing project at ${rootPath}`));\n\n // 1. Find all files\n const files = await glob(\"**/*.{js,jsx,ts,tsx}\", {\n cwd: rootPath,\n ignore: [\n \"**/node_modules/**\",\n \"**/dist/**\",\n \"**/build/**\",\n \"**/.next/**\",\n \"**/coverage/**\",\n \"**/*.config.{js,ts,cjs,mjs}\", // Ignore config files\n \"**/.d.ts\" // Ignore definition files\n ],\n absolute: true\n });\n\n console.log(pc.blue(`Found ${files.length} files to analyze.`));\n\n // 2. Initialize ts-morph project\n const tsConfigPath = path.join(rootPath, \"tsconfig.json\");\n const projectConfig: any = {\n skipAddingFilesFromTsConfig: true\n };\n\n if (fs.existsSync(tsConfigPath)) {\n projectConfig.tsConfigFilePath = tsConfigPath;\n }\n\n const project = new Project(projectConfig);\n\n // Add files to project\n files.forEach((file) => {\n try {\n project.addSourceFileAtPath(file);\n } catch (e) {\n console.warn(pc.yellow(`Skipping file ${file} due to load error:`), e);\n }\n });\n\n const packageUsage: Record<string, number> = {};\n const localUsage: Record<string, number> = {};\n\n // Initialize local usage with 0 for all files to track unused ones\n files.forEach((f) => {\n // Normalize path to be relative and standard for comparison\n const relative = path.relative(rootPath, f);\n localUsage[relative] = 0;\n });\n\n // 3. Analyze Imports\n for (const sourceFile of project.getSourceFiles()) {\n const imports = sourceFile.getImportDeclarations();\n\n for (const imp of imports) {\n const moduleSpecifier = imp.getModuleSpecifierValue();\n\n if (moduleSpecifier.startsWith(\".\") || moduleSpecifier.startsWith(\"/\")) {\n // Local Import\n try {\n // Resolve the import to a file on disk\n const sourceFilePath = sourceFile.getFilePath();\n const sourceDir = path.dirname(sourceFilePath);\n\n const resolvedPath = path.resolve(sourceDir, moduleSpecifier);\n // We need to try extensions\n const extensions = [\n \"\",\n \".ts\",\n \".tsx\",\n \".js\",\n \".jsx\",\n \"/index.ts\",\n \"/index.tsx\",\n \"/index.js\",\n \"/index.jsx\"\n ];\n\n for (const ext of extensions) {\n const tryPath = resolvedPath + ext;\n const relativeTry = path.relative(rootPath, tryPath);\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n break;\n }\n }\n } catch (e) {\n // ignore resolution errors\n }\n } else {\n // Check if it's a path alias or non-relative local import\n let resolvedLocalFile: string | undefined;\n\n // Try ts-morph resolution first (if tsconfig loaded)\n try {\n const resolvedSourceFile = imp.getModuleSpecifierSourceFile();\n if (resolvedSourceFile) {\n resolvedLocalFile = resolvedSourceFile.getFilePath();\n }\n } catch (e) {}\n\n // Fallback: Check if the module specifier matches a known local file\n // relative to baseUrl (src) or just fuzzy match\n if (!resolvedLocalFile) {\n const possibleMatches = files.filter((f) =>\n f.includes(moduleSpecifier)\n );\n if (possibleMatches.length > 0) {\n for (const match of possibleMatches) {\n const normalizedMatch = match.replace(/\\\\/g, \"/\");\n if (\n normalizedMatch.endsWith(`${moduleSpecifier}.tsx`) ||\n normalizedMatch.endsWith(`${moduleSpecifier}.ts`) ||\n normalizedMatch.endsWith(`${moduleSpecifier}/index.tsx`)\n ) {\n resolvedLocalFile = match;\n break;\n }\n }\n }\n }\n\n if (resolvedLocalFile) {\n const relativeTry = path.relative(rootPath, resolvedLocalFile);\n if (localUsage.hasOwnProperty(relativeTry)) {\n localUsage[relativeTry]++;\n continue; // Skip package counting\n }\n }\n\n // Package Import\n let packageName = moduleSpecifier;\n if (moduleSpecifier.startsWith(\"@\")) {\n const parts = moduleSpecifier.split(\"/\");\n if (parts.length >= 2) {\n packageName = `${parts[0]}/${parts[1]}`;\n }\n } else {\n const parts = moduleSpecifier.split(\"/\");\n if (parts.length >= 1) {\n packageName = parts[0];\n }\n }\n\n packageUsage[packageName] = (packageUsage[packageName] || 0) + 1;\n }\n }\n\n // Check for require() calls (CommonJS)\n const callExpressions = sourceFile.getDescendantsOfKind(\n SyntaxKind.CallExpression\n );\n for (const call of callExpressions) {\n const expression = call.getExpression();\n if (expression.getText() === \"require\") {\n const args = call.getArguments();\n if (args.length > 0 && args[0].getKind() === SyntaxKind.StringLiteral) {\n const rawArg = args[0].getText().replace(/['\"`]/g, \"\");\n // Simple duplicate logic for MVP (should refactor)\n if (!rawArg.startsWith(\".\") && !rawArg.startsWith(\"/\")) {\n let pkg = rawArg.startsWith(\"@\")\n ? rawArg.split(\"/\").slice(0, 2).join(\"/\")\n : rawArg.split(\"/\")[0];\n packageUsage[pkg] = (packageUsage[pkg] || 0) + 1;\n }\n }\n }\n }\n }\n\n // 4. Construct Report Data\n const reportPackages: Record<string, { count: number; size: string }> = {};\n\n for (const [pkg, count] of Object.entries(packageUsage)) {\n const size = getPackageSize(rootPath, pkg);\n reportPackages[pkg] = { count, size };\n }\n\n const unused = Object.entries(localUsage)\n .filter(([file, count]) => {\n // Known Entry Points & Framework specifics\n if (\n file.includes(\"pages/\") ||\n file.includes(\"app/\") ||\n file.endsWith(\"main.tsx\") ||\n file.endsWith(\"index.tsx\") ||\n file.endsWith(\"index.js\") ||\n file.endsWith(\"App.tsx\") ||\n file.endsWith(\"App.js\")\n )\n return false;\n\n // Ignore files in the project root (usually configs, scripts, etc.)\n // We check if the relative path contains a separator. If not, it's in the root.\n const relative = path.relative(rootPath, file);\n if (!relative.includes(path.sep)) {\n return false;\n }\n return count === 0;\n })\n .map(([file]) => file);\n\n return {\n packages: reportPackages,\n components: localUsage,\n unusedFiles: unused\n };\n}\n"]}
|