npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

snipdy

v1.0.7

Published

Snippet Tidy: A tool to automatically remove unused methods, classes, and code elements from TypeScript projects.

Downloads

171

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 and require 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 entire OrdersController class since they are not called or related to getVisits.
  • 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.