A developer experience tool to comment or uncomment lines in a file based on regex patterns and specific strings, while preserving existing inline comments built in NodeJS.
- Recommend usage is to pair it with husky to automate your post-checkout and pre-commit actions to comment / uncomment code for local development to speed up your team's workflow.
- Have a sucess story or another use case? Would love to hear about it, email: oss@hanselwei.dev.
- If this has helped your team, please consider sponsoring this repo.
- Comment and Uncomment Lines: Add or remove comments from lines matching specified regex patterns or containing specific strings.
- Support for Multiple File Types: Handles various comment styles, including single-line comments (
//,#) and block comments (/* */,<!-- -->). - Preserve Inline Comments: Ensures existing inline comments remain unchanged when commenting or uncommenting lines.
- Nested Comment Handling: Supports commenting and uncommenting nested comments by adding or removing one layer at a time.
- Command-Line Interface: Easily use the tool via CLI for integration with various workflows.
You can install this package globally or locally in your project.
npm install -g line-commenter-tool
npm line-commenter-tool --helpnpx line-commenter-tool --helpOnce installed, you can use the tool via command line:
line-commenter-tool <action> <filename> <regexPattern> [string1,string2,...]The line-commenter-tool is a command-line utility designed to comment or uncomment specific lines in a file based on a regex pattern or specific strings. It supports various file formats and comment styles, making it versatile for multiple programming languages.
This tool works by searching for lines in the specified file that match the given regex pattern or strings. Depending on the specified action (comment or uncomment), the tool will either add or remove comment markers on those lines.
comment: Adds comment markers to lines that match the regex pattern or strings.uncomment: Removes comment markers from lines that match the regex pattern or strings.
<action>: The action to perform:commentoruncomment.<filename>: The file to process.<regexPattern>: A regex pattern to identify lines to be commented or uncommented.[string1,string2,...](Optional): A comma-separated list of strings to be matched exactly.
--help: Show this help message and exit.--version: Show the tool's version and exit.--silent: Suppress output messages. When this flag is used, the tool will run without logging any success or error messages.--multiline: Enable processing of multiline comments. When this flag is used, the tool will comment or uncomment entire multiline block comments (e.g.,/* ... */,<!-- ... -->) based on a full match of the regex pattern.
-
Comment all lines containing the string
console.login a JavaScript file:line-commenter-tool comment app.js "console\\.log" -
Uncomment all lines that match the regex pattern
TODOin a Python file:line-commenter-tool uncomment script.py "TODO" -
Comment specific lines in a file using multiple strings:
line-commenter-tool comment config.yml "DEBUG" "error,warning"
-
Uncomment a full multiline block comment in a CSS file:
line-commenter-tool uncomment styles.css "/*" --multiline
⚠️ The tool is case-sensitive by default. Ensure your regex patterns and strings match the case of the content you want to comment or uncomment.- 💡 The
--multilineoption is particularly useful for handling languages that use block comments for larger sections of code, such as CSS, HTML, or JavaScript.
Husky is a tool for managing Git hooks. Here's how you can integrate line-commenter-tool with Husky to automatically comment or uncomment lines during certain Git actions.
First, install Husky in your project:
npm install husky --save-devEnable Git hooks:
npx husky installCreate a new hook, for example, a pre-commit hook to comment specific lines:
npx husky add .husky/pre-commit "npx line-commenter-tool comment path/to/file.js 'console\\.log\\(\\)' 'TODO'"This will ensure that every time you commit, the specified lines in file.js are commented according to your defined patterns.
Here's an example of configuring Husky to run line-commenter-tool as part of your pre-commit hook:
-
Create
.husky/pre-commitnpx husky add .husky/pre-commit "npx line-commenter-tool comment path/to/file.js 'console\\.log\\(\\)' 'TODO'" -
Make sure your hooks are executable
chmod +x .husky/pre-commit
-
Example pre-commit hook file content
#!/bin/sh . "$(dirname "$0")/_/husky.sh" # Run line-commenter-tool before committing npx line-commenter-tool comment path/to/file.js 'console\\.log\\(\\)' 'TODO'
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for improvements.
If you encounter any issues or have questions, please open an issue on the GitHub repository.