Skip to content

DX Utility package to comment out and uncomment pattern specific lines

License

Notifications You must be signed in to change notification settings

darkmastermindz/line-commenter-tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

216 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Line Commenter Tool

NPM Node.js Package

A developer experience utility to comment or uncomment specific lines in files (by regex or explicit strings), while preserving existing inline comments. Built with Node.js for easy integration into developer workflows (for example via Husky Git hooks).

  • Preferred usage: pair it with Husky to automatically comment/uncomment lines during post-checkout, pre-commit, or other Git hooks to streamline local development.
  • Have a success story or a use case to share? Email: oss@hanselwei.dev
  • If this tool helped your team, please consider sponsoring the project.

Features

  • Comment and uncomment individual lines based on regex patterns or exact string matches.
  • Support for many file types and comment styles (single-line and block comments).
  • Preserves existing inline comments when adding or removing comment wrappers.
  • Handles nested comments by adding or removing one layer of commenting at a time.
  • Command-line interface for easy automation and integration.

Supported comment styles

The tool works with common single-line and block comment styles including, but not limited to:

  • Single-line: // (JS/TS), # (sh, py), -- (SQL)
  • Block comments: /* ... */ (CSS/JS), <!-- ... --> (HTML) Note: behavior varies with language; use --multiline to operate on block comments.

Installation

Install globally:

npm install -g line-commenter-tool

Or use without installing via npx:

npx line-commenter-tool --help

Basic Usage

CLI pattern:

line-commenter-tool <action> <filename> <regexPattern> [string1,string2,...] [options]
  • <action>: comment or uncomment
  • <filename>: path to the file to process (relative or absolute)
  • <regexPattern>: regex pattern (as a quoted string) used to match lines
  • [string1,string2,...] (optional): comma-separated exact strings to match

Options:

  • --help Show help and exit
  • --version Show version and exit
  • --silent Suppress output messages
  • --multiline Enable processing of multiline block comments (e.g., /* ... */, <!-- ... -->)

Behavior notes:

  • The tool is case-sensitive by default. Ensure your regex or string matches the target casing.
  • The tool modifies files in place. Use version control or back up files before running on important files.

Examples

  1. Comment all lines containing console.log in a JS file:
line-commenter-tool comment app.js "console\\.log"
  1. Uncomment all lines that contain TODO in a Python file:
line-commenter-tool uncomment script.py "TODO"
  1. Comment lines matching multiple explicit strings (comma-separated):
line-commenter-tool comment config.yml "DEBUG" "error,warning"
  1. Uncomment a full multiline block in a CSS file:
line-commenter-tool uncomment styles.css "/\\*" --multiline
  1. Use with npx (no install):
npx line-commenter-tool comment src/index.js "debug\\(" --silent

Tips:

  • When writing shell commands in examples, escape regex metacharacters appropriately (see examples above).
  • If a pattern contains spaces or shell-special characters, wrap it in single quotes.

Integration with Husky

Husky can run this tool as part of Git hooks to automatically apply comment/uncomment transformations.

Install Husky:

npm install husky --save-dev
npx husky install

Add a pre-commit hook that runs the tool:

npx husky add .husky/pre-commit "npx line-commenter-tool comment path/to/file.js 'console\\.log\\(\\)' 'TODO'"
chmod +x .husky/pre-commit

Example .husky/pre-commit:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# Comment out console.log and TODO lines before commit
npx line-commenter-tool comment path/to/file.js 'console\.log\(' 'TODO'

Safety & Best Practices

  • The tool edits files in place. Keep files under version control so changes can be reviewed and reverted.
  • Test your regex patterns on small files or with controlled inputs before running across a codebase.
  • Consider running the command locally with --silent disabled to confirm behavior before automating in hooks.

Contribution

Contributions and improvements are welcome. Please open an issue or submit a pull request.

If you'd like me to prepare a branch and PR with this README update, tell me the target branch name and I can create it for you.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Support

If you encounter issues or have questions, please open an issue on the GitHub repository: https://github.com/darkmastermindz/line-commenter-tool/issues

About

DX Utility package to comment out and uncomment pattern specific lines

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors 3

  •  
  •  
  •