snipdy
v1.0.7
Published
Snippet Tidy: A tool to automatically remove unused methods, classes, and code elements from TypeScript projects.
Downloads
171
Maintainers
Readme
Snipdy
Snipdy (short for “Snippet Tidy”) is a powerful and efficient command-line tool designed to automatically analyze and remove unused methods, classes, and code elements from your TypeScript projects. With a focus on a specified entry point or method, Snipdy intelligently prunes away unnecessary code, streamlining your codebase while ensuring that only relevant pieces remain.
Features
- Focus on a specific method: Start from a target method and remove all unrelated and unused code.
- Clean up imports: Automatically remove unused
import
andrequire
statements. - Remove unused elements: Classes, variables, types, interfaces, and other unused code elements are automatically detected and removed.
- Debug mode: Get detailed logging to track the cleanup process.
Installation
To install Snipdy via npm, run the following command:
npm install -g snipdy
This will install Snipdy globally on your system, making it accessible as a CLI tool.
Usage
Once installed, Snipdy can be used via the command line by invoking the snipdy
command. Here’s the basic syntax:
snipdy [command] [options]
Commands
- focus: Analyze a TypeScript file and remove unused methods, classes, or elements based on the specified target method.
snipdy focus <file> <method-name> [options]
Options
- --override: (Optional) Override the original file with the cleaned-up version.
- --debug: (Optional) Enable debug mode for detailed logging during the analysis.
Example Use Cases
Use Case 1: Basic Method Cleanup
Suppose you have a file example.ts
with the following content:
import fs from 'fs';
import { createHmac } from 'crypto';
class VisitsController {
getVisits() {
this.fetchCoupons();
console.log('crypto', createHmac);
}
fetchCoupons() {
this.showReceipt();
}
showReceipt() {
console.log("Showing receipt...");
}
createVisit() {
console.log("Creating visit...");
}
}
class OrdersController {
placeOrder() {
console.log("Placing order...");
}
}
You want to clean up the file, keeping only the methods related to getVisits
. You can run:
snipdy focus path/to/example.ts getVisits --override --debug
This will:
- Analyze the
example.ts
file. - Focus on the
getVisits
method. - Remove the unrelated
createVisit
method and the entireOrdersController
class since they are not called or related togetVisits
. - Clean up unused imports, such as
fs
if not used anywhere else.
Use Case 2: Import Cleanup Only
If you only want to clean up unused imports and require statements, Snipdy can help as well. For example, in this file:
import { readFileSync } from 'fs';
import { spawn } from 'child_process';
const path = require('path');
const { randomBytes } = require('crypto');
console.log(randomBytes(16));
You want to clean up unused imports (readFileSync
and spawn
are not used). Run the following command:
snipdy focus path/to/example.ts randomBytes --override
The result will remove the unused imports and keep only the necessary code:
const { randomBytes } = require('crypto');
console.log(randomBytes(16));
Use Case 3: Debugging Unused Code Removal
If you want to see detailed logs of which methods, classes, or imports are being removed, enable the --debug
flag:
snipdy focus path/to/example.ts getVisits --debug
This will print out which methods or imports are removed, making it easier to understand the decisions Snipdy is making during the cleanup.
Use Case 4: Working Without Overwriting
If you want to analyze a file and see the result without overwriting the original file, simply omit the --override
option. The cleaned code will be printed to the console instead:
snipdy focus path/to/example.ts getVisits
This will allow you to inspect the cleaned version of the file without making any permanent changes.
Future Enhancements
- Multi-file analysis: Extend support for analyzing multiple files and projects.
- More granular removal: Introduce fine-grained control over what elements (e.g., methods, imports) can be removed or retained.
Contributing
Contributions are welcome! If you'd like to contribute to Snipdy, feel free to fork the repository, create a feature branch, and submit a pull request with a detailed description of your changes.
License
This project is licensed under the MIT License.
This version of the README.md
includes more descriptive examples of how users can use Snipdy and should make it easier for them to get started and explore different use cases.