Remove Comments CLI
A powerful command-line tool to remove comments from your code files while preserving important documentation comments.
Features
- Removes single-line and multi-line comments from code files
- Preserves important documentation comments (marked with
//!or/*!) - Supports multiple programming languages
- Handles nested comments and edge cases
- Preserves code formatting and structure
Installation
You can install the package globally using npm:
npm install -g remove-comments-cli
Or use it directly with npx:
npx remove-comments-cli [options] <file>
Quick Start
-
Install the package globally:
npm install -g remove-comments-cli -
Run it on a file:
npx remove-comments file.js -o cleaned/ -
Run it on a file (Output on Terminal):
remove-comments file.js -
Or use it with multiple files:
remove-comments src/**/*.ts
For more detailed usage instructions and options, check out the Usage guide.
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}" --outDir dist
Preserving Important 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 to preserve different types of comments:
# Preserve comments starting with # or *
npx remove-comments "src/**/*.ts" -k "#*" --outDir dist
Command Line 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
Process all TypeScript files in the src directory and preserve comments starting with ! or #:
npx remove-comments "src/**/*.ts" -k "!#" --outDir dist
Process JSX/TSX Files
Process React component files and preserve comments starting with *:
npx remove-comments "src/**/*.{jsx,tsx}" -k "*" --outDir dist
Process Single File
Process a single file and output to stdout:
npx remove-comments input.js > output.js
Verbose Output
Process files with detailed logging:
npx remove-comments "src/**/*.{js,ts}" -k "!" -o dist -v
Exit Codes
The CLI uses the following exit codes:
0: Success - all files processed successfully1: Fatal error - no files found or initialization failed2: Partial success - some files had errors but others were processed