qleaner 1.2.0 โ†’ 1.3.0

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 CHANGED
@@ -1,21 +1,23 @@
1
1
  # Qleaner
2
2
 
3
- **Qleaner** (v1.2.0) is a powerful CLI tool for analyzing and cleaning up React, TypeScript, and JavaScript projects. It helps you identify unused files, images, dependencies, and dead code to optimize your bundle size and maintain a clean codebase.
3
+ **Qleaner** (v1.3.0) is a powerful CLI tool for analyzing and cleaning up React, TypeScript, and JavaScript projects. It helps you identify unused files, images, dependencies, and dead code to optimize your bundle size and maintain a clean codebase.
4
4
 
5
- ## โœจ Features
5
+ ## Features
6
6
 
7
- - ๐Ÿ” **Find Unused Code Files** - Identify files that aren't imported or used anywhere
8
- - ๐Ÿ–ผ๏ธ **Find Unused Images** - Detect image assets that are never referenced in your code
9
- - ๐Ÿ“ฆ **Unused Dependencies** - Discover npm/yarn packages that are installed but not used
10
- - ๐Ÿ”— **Dead Image Links** - Find image references in code that point to non-existent files
11
- - ๐Ÿ“Š **Project Summary** - Get comprehensive statistics about your codebase
12
- - ๐Ÿ“ˆ **File Size Analysis** - Identify the largest files and potential optimization targets
13
- - ๐ŸŽฏ **Dependency Analysis** - See which files have heavy dependencies and hotspots
14
- - ๐Ÿ’พ **Smart Caching** - Fast incremental scans with intelligent cache invalidation
15
- - ๐Ÿงช **Dry-Run Mode** - Preview what would be deleted without actually deleting anything
16
- - ๐Ÿ—‘๏ธ **Interactive Deletion** - Choose to delete files permanently or move them to `.trash`
7
+ - **Find Unused Code Files** - Identify files that aren't imported or used anywhere
8
+ - **Find Unused Images** - Detect image assets that are never referenced in your code
9
+ - **Unused Dependencies** - Discover npm/yarn packages that are installed but not used
10
+ - **Dead Image Links** - Find image references in code that point to non-existent files
11
+ - **Project Summary** - Get comprehensive statistics about your codebase
12
+ - **File Size Analysis** - Identify the largest files and potential optimization targets
13
+ - **Dependency Analysis** - See which files have heavy dependencies and hotspots
14
+ - **Smart Caching** - Fast incremental scans with intelligent cache invalidation
15
+ - **Dry-Run Mode** - Preview what would be deleted without actually deleting anything
16
+ - **Interactive Deletion** - Choose to delete files permanently or move them to `.trash`
17
+ - **Enhanced File Finding** - Advanced path resolution using jsconfig/tsconfig for accurate dependency tracking in large codebases with complex import structures
18
+ - **Advanced Image Detection** - Sophisticated AST parsing extracts image references from imports, requires, JSX, CSS, styled-components, template literals, arrays, and more
17
19
 
18
- ## ๐Ÿš€ Installation
20
+ ## Installation
19
21
 
20
22
  ### Global Installation
21
23
 
@@ -37,20 +39,32 @@ yarn add qleaner --dev
37
39
  npm install qleaner --save-dev
38
40
  ```
39
41
 
40
- ## ๐Ÿ“– Quick Start
42
+ ## Quick Start
41
43
 
42
44
  ### 1. Initialize Configuration
43
45
 
46
+ **IMPORTANT:** Proper configuration is critical for Qleaner to accurately find and resolve files in your project, especially for large codebases. Take time to get this right!
47
+
44
48
  First, set up Qleaner for your project:
45
49
 
46
50
  ```bash
47
51
  qleaner init
48
52
  ```
49
53
 
50
- This interactive command will:
51
- - Ask about your package manager (npm/yarn/pnpm)
52
- - Configure image path resolution (aliases or root-referenced paths)
53
- - Detect your framework (React/Next.js) to set appropriate entry points
54
+ This interactive command will ask you:
55
+ 1. **Package manager** (npm/yarn/pnpm)
56
+ 2. **Image path resolution** - How you reference images (relative paths vs absolute paths from public/)
57
+ 3. **Framework type** (React or Next.js) - Sets appropriate entry points
58
+ 4. **Configuration file for path aliases** - **This is crucial!** Select which config file Qleaner should use to resolve import aliases:
59
+ - `tsconfig.json` (most common for TypeScript projects)
60
+ - `jsconfig.json` (for JavaScript projects)
61
+ - `tsconfig.app.json` (for monorepos or specific app configs)
62
+ - `tsconfig.base.json` (for monorepo base configs)
63
+ - `none` (if you don't use path aliases or want to configure manually)
64
+
65
+ **Why this matters:** Qleaner uses your project's `jsconfig.json` or `tsconfig.json` file to understand how import paths are resolved. This enables accurate file finding even in large codebases with complex path aliases (e.g., `@/components`, `~/utils`, etc.). Selecting the correct config file ensures Qleaner can properly trace dependencies and find all file references.
66
+
67
+ **If you don't use jsconfig/tsconfig:** Select "none" during initialization, then manually add your path aliases to the `paths` field in `qleaner.config.json` (see Configuration section below).
54
68
 
55
69
  This creates a `qleaner.config.json` file in your project root with sensible defaults.
56
70
 
@@ -81,24 +95,47 @@ qleaner image public/images src -a --dry-run
81
95
  qleaner image public/images src -r --dry-run
82
96
  ```
83
97
 
84
- ## ๐Ÿ“‹ Commands
98
+ ## Commands
85
99
 
86
100
  ### `qleaner init`
87
101
 
88
102
  Initialize Qleaner with an interactive configuration wizard. This command asks you a series of questions to set up Qleaner for your project and creates a `qleaner.config.json` file in your project root with sensible defaults.
89
103
 
104
+ **Configuration is Critical:** Getting the configuration right, especially the jsconfig/tsconfig selection, is essential for Qleaner to accurately find files in large codebases. Incorrect configuration may result in false positives (files reported as unused when they're actually used) or missed dependencies.
105
+
90
106
  **Questions asked:**
91
- 1. Package manager (npm, yarn, or pnpm)
92
- 2. Whether you use path aliases for images (e.g., `@/assets/...`)
93
- 3. Framework type (React or Next.js)
107
+ 1. **Package manager** (npm, yarn, or pnpm)
108
+ 2. **Image path resolution** - How you reference images:
109
+ - Relative paths (e.g., `./assets/logo.png`)
110
+ - Absolute paths from public/ (e.g., `/images/logo.png`)
111
+ 3. **Framework type** (React or Next.js) - Determines entry point files
112
+ 4. **Configuration file for path aliases** - **Most important question!** Select which config file Qleaner should use:
113
+ - `tsconfig.json` - Standard TypeScript configuration
114
+ - `jsconfig.json` - Standard JavaScript configuration
115
+ - `tsconfig.app.json` - App-specific config (common in monorepos)
116
+ - `tsconfig.base.json` - Base config (common in monorepos)
117
+ - `none` - No config file (use manual path configuration)
118
+
119
+ **Why the config file matters:**
120
+ - Qleaner reads path aliases from your `jsconfig.json` or `tsconfig.json` to resolve imports like `@/components/Button` or `~/utils/helpers`
121
+ - This enables accurate dependency tracking even in large projects with complex import structures
122
+ - Without the correct config file, Qleaner may not properly resolve aliased imports, leading to incorrect results
123
+
124
+ **If you don't have a jsconfig/tsconfig file:**
125
+ - Select "none" during initialization
126
+ - Manually configure path aliases in the `paths` field of `qleaner.config.json` (see Configuration section)
127
+ - Example: `"paths": { "@/*": ["./src/*"], "~/*": ["./src/*"] }`
94
128
 
95
129
  **Examples:**
96
130
  ```bash
97
131
  # Initialize with interactive prompts
98
132
  qleaner init
99
133
 
100
- # After initialization, you can run scans
101
- qleaner scan src
134
+ # After initialization, verify your config
135
+ cat qleaner.config.json
136
+
137
+ # Then run your first scan
138
+ qleaner scan src --dry-run
102
139
  ```
103
140
 
104
141
  **Note:** If you already have a `qleaner.config.json` file, this command will prompt you before overwriting it. You can manually edit the config file instead if needed.
@@ -309,7 +346,7 @@ qleaner summary --dependencies
309
346
 
310
347
  **Note:** Requires a cache file. Run `qleaner scan <path>` to generate code cache and/or `qleaner image <dir> <root>` to generate image cache first.
311
348
 
312
- ## โš™๏ธ Configuration
349
+ ## Configuration
313
350
 
314
351
  Qleaner can be configured via `qleaner.config.json` or CLI flags. CLI flags always override config file values.
315
352
 
@@ -317,6 +354,8 @@ Qleaner can be configured via `qleaner.config.json` or CLI flags. CLI flags alwa
317
354
 
318
355
  ```json
319
356
  {
357
+ "codeAlias": "tsconfig.json",
358
+ "paths": {},
320
359
  "packageManager": "yarn",
321
360
  "excludeDir": ["node_modules", "dist", "build", ".next"],
322
361
  "excludeFile": ["payload-types.ts"],
@@ -335,6 +374,8 @@ Qleaner can be configured via `qleaner.config.json` or CLI flags. CLI flags alwa
335
374
 
336
375
  | Option | Type | Description |
337
376
  |--------|------|-------------|
377
+ | `codeAlias` | string \| null | **Critical!** Config file to use for path alias resolution. Options: `"tsconfig.json"`, `"jsconfig.json"`, `"tsconfig.app.json"`, `"tsconfig.base.json"`, or `null` for manual configuration. Qleaner reads path aliases from this file to resolve imports (e.g., `@/components`, `~/utils`). |
378
+ | `paths` | object | **Manual path aliases** (only used if `codeAlias` is `null`). Object mapping alias patterns to file paths. Example: `{ "@/*": ["./src/*"], "~/*": ["./src/*"] }`. If `codeAlias` is set, this field is ignored and paths are read from the config file instead. |
338
379
  | `packageManager` | string | Package manager used: `npm`, `yarn`, or `pnpm` |
339
380
  | `excludeDir` | string[] | Directories to exclude from code scans |
340
381
  | `excludeFile` | string[] | Files to exclude by name from code scans |
@@ -347,7 +388,36 @@ Qleaner can be configured via `qleaner.config.json` or CLI flags. CLI flags alwa
347
388
  | `isRootFolderReferenced` | boolean | `true` if image paths are root-referenced (e.g., `/images/logo.png`) |
348
389
  | `alias` | boolean | `true` if image paths use aliases (e.g., `@/assets/images/logo.png`) |
349
390
 
350
- **Important:** `isRootFolderReferenced` and `alias` should not both be `true` or both be `false`. Set one to `true` and the other to `false`.
391
+ **Important Configuration Notes:**
392
+
393
+ 1. **`codeAlias` and `paths`:**
394
+ - If `codeAlias` is set (e.g., `"tsconfig.json"`), Qleaner will read path aliases from that config file. The `paths` field will be ignored.
395
+ - If `codeAlias` is `null`, Qleaner will use the `paths` field for manual path alias configuration.
396
+ - **Getting this right is crucial** for accurate file resolution in large codebases!
397
+
398
+ 2. **`isRootFolderReferenced` and `alias`:** These should not both be `true` or both be `false`. Set one to `true` and the other to `false`.
399
+
400
+ ### Manual Path Configuration
401
+
402
+ If you don't use `jsconfig.json` or `tsconfig.json`, or need custom path resolution, you can manually configure paths in `qleaner.config.json`:
403
+
404
+ ```json
405
+ {
406
+ "codeAlias": null,
407
+ "paths": {
408
+ "@/*": ["./src/*"],
409
+ "~/*": ["./src/*"],
410
+ "components/*": ["./src/components/*"],
411
+ "utils/*": ["./src/utils/*"]
412
+ }
413
+ }
414
+ ```
415
+
416
+ The `paths` object follows the same format as TypeScript's path mapping:
417
+ - **Key:** The alias pattern (e.g., `"@/*"` matches `@/components`, `@/utils`, etc.)
418
+ - **Value:** Array of actual file paths to resolve to (relative to project root)
419
+
420
+ **Example:** If you have `import Button from '@/components/Button'` in your code, and your config has `"@/*": ["./src/*"]`, Qleaner will resolve it to `./src/components/Button.tsx` (or `.ts`, `.jsx`, `.js`).
351
421
 
352
422
  ### Default Exclusions
353
423
 
@@ -373,7 +443,7 @@ Qleaner automatically excludes common build and generated directories:
373
443
  - `_app.tsx`, `_app.jsx`, `_document.tsx`, `_document.jsx`
374
444
  - `_error.tsx`, `_error.jsx`
375
445
 
376
- ## ๐Ÿ’พ Caching
446
+ ## Caching
377
447
 
378
448
  Qleaner uses intelligent caching to speed up subsequent scans:
379
449
 
@@ -391,7 +461,7 @@ Qleaner uses intelligent caching to speed up subsequent scans:
391
461
  qleaner scan src --clear-cache
392
462
  ```
393
463
 
394
- ## ๐Ÿ—‘๏ธ File Deletion
464
+ ## File Deletion
395
465
 
396
466
  When you run a scan without `--dry-run`, Qleaner will prompt you to:
397
467
 
@@ -408,59 +478,77 @@ When you run a scan without `--dry-run`, Qleaner will prompt you to:
408
478
  5. Test your application
409
479
  6. Empty `.trash` when confident
410
480
 
411
- ## ๐Ÿ”ง How It Works
481
+ ## How It Works
412
482
 
413
483
  ### Code Analysis
414
- 1. **File Discovery** - Uses `fast-glob` to find all code files matching patterns
415
- 2. **AST Parsing** - Parses JavaScript/TypeScript with Babel to extract imports
416
- 3. **Module Resolution** - Uses `enhanced-resolve` to resolve import paths (supports aliases)
417
- 4. **Dependency Graph** - Builds a graph of file dependencies
418
- 5. **Unused Detection** - Identifies files with no incoming imports (except entry points)
484
+ 1. **File Discovery** - Uses `fast-glob` to find all code files matching patterns with intelligent exclusion handling
485
+ 2. **Path Alias Resolution** - Reads path aliases from your `jsconfig.json` or `tsconfig.json` (or manual `paths` config) to understand how imports are resolved. This enables accurate file finding even in large codebases with complex import structures.
486
+ 3. **AST Parsing** - Parses JavaScript/TypeScript with Babel to extract imports and exports from code files
487
+ 4. **Module Resolution** - Uses `enhanced-resolve` with your project's path aliases to resolve import paths accurately. Supports:
488
+ - Path aliases (e.g., `@/components`, `~/utils`)
489
+ - Relative imports (`./component`, `../utils`)
490
+ - Node modules (`react`, `lodash`)
491
+ - Directory index files (automatically resolves `./components` to `./components/index.ts`)
492
+ 5. **Dependency Graph** - Builds a comprehensive graph of file dependencies, tracking both imports and exports
493
+ 6. **Unused Detection** - Identifies files with no incoming imports (except entry points defined in `excludeFilePrint`)
419
494
 
420
495
  ### Image Analysis
421
- 1. **Image Discovery** - Finds all image files (png, jpg, jpeg, svg, gif, webp)
422
- 2. **Code Scanning** - Extracts image references from:
423
- - Import statements
424
- - Require calls
425
- - JSX attributes (`<img src>`)
426
- - CSS files (`url()`)
427
- - Styled-components
428
- - Template literals
429
- - Arrays
430
- - String literals
431
- 3. **Path Normalization** - Normalizes paths based on alias/root configuration
432
- 4. **Unused Detection** - Compares image files against references
496
+ 1. **Image Discovery** - Finds all image files (png, jpg, jpeg, svg, gif, webp) using glob patterns with exclusion support
497
+ 2. **Advanced Code Scanning** - Uses sophisticated AST traversal to extract image references from:
498
+ - ES6 import statements (`import logo from './logo.png'`)
499
+ - CommonJS require (`require('./logo.png')`)
500
+ - Dynamic imports (`import('./logo.png')`)
501
+ - JSX attributes (`<img src="./logo.png" />`)
502
+ - React style props (`style={{ backgroundImage: "url('./logo.png')" }}`)
503
+ - CSS files (`url('./logo.png')` or `url('/images/logo.png')`)
504
+ - Styled-components (`` css`background-image: url('./logo.png')` ``)
505
+ - Arrays (`const images = ['./logo.png', './icon.png']`)
506
+ - Template literals (`` `./logo-${name}.png` ``)
507
+ - String literals (any string containing an image path pattern)
508
+ - Spread elements in arrays
509
+ 3. **Path Normalization** - Normalizes paths based on alias/root configuration, handling both relative and absolute paths
510
+ 4. **Unused Detection** - Compares image files against all discovered references to identify truly unused images
433
511
 
434
512
  ## ๐Ÿ“Š Example Workflow
435
513
 
436
514
  ```bash
437
- # 1. Initialize configuration
515
+ # 1. Initialize configuration (CRITICAL STEP - get this right!)
438
516
  qleaner init
517
+ # Make sure to select the correct jsconfig.json or tsconfig.json file
518
+ # Verify the generated qleaner.config.json has the correct codeAlias
519
+
520
+ # 2. Verify your configuration
521
+ cat qleaner.config.json
522
+ # Check that codeAlias matches your project's config file
439
523
 
440
- # 2. Scan for unused code files (dry run)
524
+ # 3. Scan for unused code files (dry run first!)
441
525
  qleaner scan src --dry-run --table
442
526
 
443
- # 3. Scan for unused images (dry run)
527
+ # 4. Review results - if you see false positives, check your config
528
+ # Fix codeAlias or paths if needed, then clear cache and rescan:
529
+ qleaner scan src --clear-cache --dry-run --table
530
+
531
+ # 5. Scan for unused images (dry run)
444
532
  qleaner image public/images src -r --dry-run --table
445
533
 
446
- # 4. Check for unused dependencies
534
+ # 6. Check for unused dependencies
447
535
  qleaner dep --table
448
536
 
449
- # 5. Get project summary
537
+ # 7. Get project summary
450
538
  qleaner summary
451
539
 
452
- # 6. Get largest files report
540
+ # 8. Get largest files report
453
541
  qleaner summary --largest-files
454
542
 
455
- # 7. Get dependency analysis
543
+ # 9. Get dependency analysis
456
544
  qleaner summary --dependencies
457
545
 
458
- # 8. Once confident, run actual scans and delete files
546
+ # 10. Once confident with results, run actual scans and delete files
459
547
  qleaner scan src
460
548
  qleaner image public/images src -r
461
549
  ```
462
550
 
463
- ## ๐ŸŽฏ Use Cases
551
+ ## Use Cases
464
552
 
465
553
  - **Before Deployment** - Clean up unused files to reduce bundle size
466
554
  - **Code Review** - Verify no unused files are being added
@@ -468,12 +556,12 @@ qleaner image public/images src -r
468
556
  - **Optimization** - Identify large files and dependency hotspots
469
557
  - **Maintenance** - Regular cleanup to keep codebase healthy
470
558
 
471
- ## ๐Ÿ“ Requirements
559
+ ## Requirements
472
560
 
473
561
  - Node.js 14+ (LTS recommended)
474
562
  - npm or yarn
475
563
 
476
- ## ๐Ÿค Contributing
564
+ ## Contributing
477
565
 
478
566
  Contributions are welcome! Please feel free to submit a Pull Request.
479
567
 
@@ -487,7 +575,7 @@ Contributions are welcome! Please feel free to submit a Pull Request.
487
575
 
488
576
  MIT
489
577
 
490
- ## ๐Ÿ”— Links
578
+ ## Links
491
579
 
492
580
  - **Repository:** [https://github.com/trevismurithi/react-cleaner](https://github.com/trevismurithi/react-cleaner)
493
581
  - **Issues:** [https://github.com/trevismurithi/react-cleaner/issues](https://github.com/trevismurithi/react-cleaner/issues)
@@ -497,13 +585,16 @@ MIT
497
585
 
498
586
  ### Tips for Best Results
499
587
 
500
- 1. **Run scans regularly** - Set up a cron job or CI check to catch unused files early
501
- 2. **Use dry-run first** - Always preview with `--dry-run` before deleting anything
502
- 3. **Review entry points** - Make sure `excludeFilePrint` in config includes all entry point files (index.tsx, main.js, etc.)
503
- 4. **Clear cache when needed** - After major refactoring or dependency changes, use `--clear-cache` for accurate results
504
- 5. **Test after deletion** - Always test your application thoroughly after removing files
505
- 6. **Use table format for large outputs** - The `-t` flag makes results easier to read
506
- 7. **Check summary regularly** - Use `qleaner summary` to monitor project health
588
+ 1. **Get configuration right first** - **Most important!** Ensure `codeAlias` points to the correct `jsconfig.json` or `tsconfig.json` file, or manually configure `paths` if you don't use these config files. Incorrect configuration leads to false positives and missed dependencies.
589
+ 2. **Verify your config file** - After running `qleaner init`, check that `codeAlias` matches your project's actual config file. For monorepos, you may need `tsconfig.app.json` or `tsconfig.base.json` instead of `tsconfig.json`.
590
+ 3. **Run scans regularly** - Set up a cron job or CI check to catch unused files early
591
+ 4. **Use dry-run first** - Always preview with `--dry-run` before deleting anything
592
+ 5. **Review entry points** - Make sure `excludeFilePrint` in config includes all entry point files (index.tsx, main.js, etc.)
593
+ 6. **Clear cache when needed** - After major refactoring, dependency changes, or config file updates, use `--clear-cache` for accurate results
594
+ 7. **Test after deletion** - Always test your application thoroughly after removing files
595
+ 8. **Use table format for large outputs** - The `-t` flag makes results easier to read
596
+ 9. **Check summary regularly** - Use `qleaner summary` to monitor project health
597
+ 10. **For large codebases** - The enhanced file finding mechanism with proper config ensures accurate results even in projects with thousands of files and complex import structures
507
598
 
508
599
  ### Command Dependencies
509
600
 
@@ -520,7 +611,14 @@ Some commands require cache files from other commands:
520
611
  ### Common Issues
521
612
 
522
613
  **Q: Qleaner reports files as unused that are actually used.**
523
- A: These are likely entry points. Add them to `excludeFilePrint` in your config or use `-F` flag. Also verify the file is actually imported somewhere - check with `qleaner list <file-path>`.
614
+ A:
615
+ 1. **Check your configuration** - Most commonly, this happens when `codeAlias` is incorrect or `paths` is not configured properly. Verify that Qleaner can resolve your import aliases:
616
+ - Ensure `codeAlias` points to the correct config file (check if it exists and has the right path mappings)
617
+ - If using manual `paths`, verify the alias patterns match your imports
618
+ - Try running with `--clear-cache` after fixing config
619
+ 2. These might be entry points - Add them to `excludeFilePrint` in your config or use `-F` flag
620
+ 3. Verify the file is actually imported - Check with `qleaner list <file-path>` to see if it's referenced
621
+ 4. Check for dynamic imports - Some dynamic imports may not be detected
524
622
 
525
623
  **Q: Images aren't being detected.**
526
624
  A:
@@ -533,8 +631,20 @@ A:
533
631
  A:
534
632
  - First scan is always slower - it builds the complete dependency graph
535
633
  - Subsequent scans use caching and are much faster (only changed files are re-analyzed)
634
+ - The enhanced file finding mechanism with proper config ensures accurate and efficient resolution even in large codebases
536
635
  - Exclude more directories with `-e` flag to speed up scans
537
636
  - Consider scanning smaller subdirectories separately
637
+ - Ensure your `codeAlias` config is correct - incorrect config can cause unnecessary resolution attempts
638
+
639
+ **Q: How do I know if my configuration is correct?**
640
+ A:
641
+ - After `qleaner init`, verify `codeAlias` in `qleaner.config.json` matches your project's config file
642
+ - Run a test scan: `qleaner scan src --dry-run` and check if files you know are used are incorrectly reported as unused
643
+ - If you see false positives, check:
644
+ 1. Does your `jsconfig.json`/`tsconfig.json` exist and have correct `paths` configuration?
645
+ 2. Is `codeAlias` pointing to the right file?
646
+ 3. If using manual `paths`, do the patterns match your import statements?
647
+ - For monorepos, you may need `tsconfig.app.json` or `tsconfig.base.json` instead of `tsconfig.json`
538
648
 
539
649
  **Q: How do I undo deletions?**
540
650
  A:
@@ -556,4 +666,4 @@ A:
556
666
 
557
667
  ---
558
668
 
559
- **Made with โค๏ธ for clean codebases**
669
+ **Made with love for clean codebases**