UNPKG

@fe-fast/code-sweeper

Version:

A lightweight JavaScript/TypeScript code cleaning tool

307 lines (231 loc) โ€ข 6.97 kB
# ๐Ÿงน Code Sweeper > Lightweight JavaScript/TypeScript code cleanup tool [![npm version](https://badge.fury.io/js/code-sweeper.svg)](https://www.npmjs.com/package/code-sweeper) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/) Code Sweeper is a tool focused on automatically cleaning redundant code in projects, filling the code cleanup gaps that ESLint and Prettier cannot cover. ## โœจ Features - ๐Ÿ” **Smart Analysis**: AST-based static analysis for precise identification of unused code - ๐Ÿงน **One-click Cleanup**: Remove unused imports, variables, and functions - ๐Ÿ› **Debug Cleanup**: Automatically remove console.log and debugger statements - โš™๏ธ **Flexible Configuration**: Support custom cleanup rules and file filtering - ๐Ÿš€ **Multi-framework Support**: Compatible with Vue, React, and TypeScript projects - ๐Ÿ“Š **Detailed Reports**: Provide before-and-after cleanup analysis - ๐Ÿ”Œ **Build Tool Integration**: Support for Webpack, Vite, and Rollup plugins ## ๐Ÿš€ Quick Start ### Installation ```bash # Global installation npm install -g code-sweeper # Or install in project npm install --save-dev code-sweeper ``` ### Basic Usage (CLI) ```bash # Analyze code issues code-sweeper analyze # Clean code (preview mode) code-sweeper clean --dry-run # Execute cleanup code-sweeper clean # Initialize configuration file code-sweeper config --init ``` ## ๐Ÿ”Œ Build Tool Integration For seamless integration into your build process, Code Sweeper provides official plugins for major build tools. ### Installation ```bash npm install @fe-fast/code-sweeper --save-dev # or pnpm add -D @fe-fast/code-sweeper # or yarn add -D @fe-fast/code-sweeper ``` ### Webpack ```javascript // webpack.config.js const { CodeSweeperWebpackPlugin } = require('@fe-fast/code-sweeper/webpack'); module.exports = { // ... other configs plugins: [ new CodeSweeperWebpackPlugin({ // options rules: { removeConsoleLog: process.env.NODE_ENV === 'production', }, }), ], }; ``` ### Vite ```typescript // vite.config.ts import { defineConfig } from 'vite'; import codeSweeperPlugin from '@fe-fast/code-sweeper/vite'; export default defineConfig({ plugins: [ codeSweeperPlugin({ rules: { removeConsoleLog: process.env.NODE_ENV === 'production', }, }), ], }); ``` ### Rollup ```javascript // rollup.config.js import codeSweeperPlugin from '@fe-fast/code-sweeper/rollup'; export default { // ... other configs plugins: [ codeSweeperPlugin({ rules: { removeConsoleLog: process.env.NODE_ENV === 'production', }, }), ], }; ``` ### Plugin Options All plugins support the following options: ```typescript interface PluginOptions { include?: string[]; exclude?: string[]; dryRun?: boolean; skipConfirmation?: boolean; rules?: { removeUnusedImports?: boolean; removeUnusedVariables?: boolean; removeConsoleLog?: boolean; removeDebugger?: boolean; formatCode?: boolean; renameToCamelCase?: boolean; }; } ``` For more examples, check out the [examples](./examples) directory. ## ๐Ÿ“‹ Command Reference ### `analyze` - Code Analysis Analyze code issues in the project without making any modifications. ```bash code-sweeper analyze [options] Options: -p, --path <path> Target directory path (default: current directory) -c, --config <file> Configuration file path --json Output results in JSON format ``` ### `clean` - Code Cleanup Execute code cleanup operations. ```bash code-sweeper clean [options] Options: -p, --path <path> Target directory path (default: current directory) -c, --config <file> Configuration file path --dry-run Preview mode, don't actually modify files --imports Only clean unused imports --variables Only clean unused variables --console Only remove console statements --debugger Only remove debugger statements -y, --yes Skip confirmation prompts ``` ### `config` - Configuration Management Manage cleanup rule configurations. ```bash code-sweeper config [options] Options: --init Initialize configuration file --show Show current configuration ``` ## โš™๏ธ Configuration File Create a `.code-sweeper.json` file in your project root: ```json { "rules": { "removeUnusedImports": true, "removeUnusedVariables": true, "removeConsoleLog": true, "removeDebugger": true, "formatCode": false, "renameToCamelCase": false }, "include": [ "src/**/*.{js,ts,jsx,tsx}", "components/**/*.{js,ts,jsx,tsx}" ], "exclude": [ "node_modules/**", "dist/**", "build/**", "*.min.js" ], "parser": { "typescript": true, "jsx": true, "decorators": true, "classProperties": true } } ``` ## ๐Ÿ“Š Usage Examples ### Analysis Result Example ```bash $ code-sweeper analyze ๐Ÿ” Code Sweeper - Analyzing your code... ๐Ÿ“ Target path: /Users/project/src ๐Ÿ“Š Code Analysis Report โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• ๐Ÿ“ˆ Summary: โ€ข Total files scanned: 45 โ€ข Files with issues: 12 โ€ข Total issues found: 28 ๐Ÿ” Issue Breakdown: โ€ข Unused imports: 15 โ€ข Unused variables: 8 โ€ข Console statements: 3 โ€ข Debugger statements: 2 ๐Ÿ’ก Recommendations: โ€ข Run code-sweeper clean to fix these issues โ€ข Use --dry-run flag to preview changes first ``` ### Cleanup Result Example ```bash $ code-sweeper clean ๐Ÿงน Code Sweeper - Cleaning your code... โœ… Cleaning completed! ๐Ÿ“Š Summary: โ€ข Files modified: 8 โ€ข Issues fixed: 23 โ€ข Lines removed: 45 โ€ข Estimated size reduction: ~2.1KB ``` ## ๐Ÿ› ๏ธ Development ```bash # Clone the project git clone https://github.com/william-xue/code-sweeper.git cd code-sweeper # Install dependencies npm install # Build project npm run build # Run tests npm test # Local development npm run dev ``` ## ๐Ÿค Contributing Welcome to submit Issues and Pull Requests! 1. Fork this project 2. Create a feature branch (`git checkout -b feature/AmazingFeature`) 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) 4. Push to the branch (`git push origin feature/AmazingFeature`) 5. Open a Pull Request ## ๐Ÿ“„ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## ๐Ÿ™ Acknowledgments - [Babel](https://babeljs.io/) - AST parsing and transformation - [TypeScript](https://www.typescriptlang.org/) - Type support - [Commander.js](https://github.com/tj/commander.js/) - CLI framework --- **Make code cleaner, make development more efficient!** ๐Ÿš€