Skip to content

Siddhantbg/remove-comments-cli

Repository files navigation

remove-comments-cli

npm version CI codecov License: MIT PRs Welcome

A fast and flexible CLI tool to remove comments from JavaScript/TypeScript files while preserving important ones.

Features

  • 🚀 Fast and memory-efficient comment removal
  • 💡 Preserve important comments using markers
  • 🌟 Support for JavaScript, TypeScript, and JSX
  • 🔍 Smart handling of strings, regex, and JSX syntax
  • 📁 Process multiple files using glob patterns
  • 🎯 Configurable output directory

Installation

npm install remove-comments-cli --save-dev

Usage

Basic Usage

Remove all comments from a file:

npx remove-comments input.ts > output.ts

Process multiple files using glob patterns:

npx remove-comments "src/**/*.{js,ts,jsx,tsx}" --outDir dist

Preserving Comments

By default, comments starting with ! are preserved:

// This comment will be removed
//! This important comment will be preserved
/* This block comment will be removed */
/*! This important block comment will be preserved */

You can specify custom markers:

# Preserve comments starting with # or *
npx remove-comments input.ts -k "#*" > output.ts

CLI Options

npx remove-comments --help

Options:
  -k, --keep-markers <chars>  Characters that mark comments to preserve (default: "!")
  -o, --outDir <dir>         Output directory for processed files
  -v, --verbose             Print detailed processing information
  -h, --help                Display help information

Examples

Process TypeScript files and preserve comments starting with ! or #:

npx remove-comments "src/**/*.ts" -k "!#" --outDir dist

Process JSX/TSX files and preserve comments starting with *:

npx remove-comments "src/**/*.{jsx,tsx}" -k "*" --outDir dist

Process a single file and output to stdout:

npx remove-comments input.js > output.js

Performance

The tool is optimized for performance and memory efficiency:

  • ⚡ Processes ~1000 files/second on modern hardware
  • 📉 Low memory footprint (~10MB for 1000 files)
  • 🔄 Streaming file processing for large files
  • 💻 Multi-threaded processing for large directories

Compatibility

Node.js Version Support Status
20.x ✅ Full
18.x (LTS) ✅ Full
16.x ❌ Not supported

Tested on:

  • Windows 10/11
  • macOS 10.15+
  • Ubuntu 20.04+

Troubleshooting

Common Issues

  1. "Error: ENOENT: no such file or directory"

    • Check if the file paths are correct
    • Ensure glob patterns are quoted in the shell
    • Use forward slashes (/) even on Windows
  2. "SyntaxError: Unexpected token"

    • Verify the input file is valid JavaScript/TypeScript
    • Check for malformed comments or JSX syntax
    • Try running with --verbose for more details
  3. "Error: Cannot find module"

    • Run npm install remove-comments-cli again
    • Check Node.js version compatibility
    • Verify package.json dependencies

Debug Mode

Run with debug logging enabled:

DEBUG=remove-comments* npx remove-comments input.ts

Security

  • No eval or dynamic code execution
  • No external network calls
  • Input validation for all file paths
  • Safe handling of symbolic links

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

MIT

About

A lightweight CLI tool to strip all comments from JavaScript/TypeScript files—preserving only those explicitly marked with ! or JSDoc—via a simple remove-comments command.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors