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

line-commenter-tool

v1.1.1

Published

A tool to comment or uncomment lines in a file based on regex patterns and specific strings.

Downloads

40

Readme

Line Commenter Tool

A Node.js package to comment or uncomment lines in a file based on regex patterns and specific strings, while preserving existing inline comments.

Features

  • 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.

Installation

You can install this package globally or locally in your project.

Global Installation

npm install -g line-commenter-tool

Local Installation

npm install line-commenter-tool

Usage

Once installed, you can use the tool via command line:

line-commenter-tool <action> <filename> <regexPattern> [string1,string2,...]

Description

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.

Actions

  • 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.

Arguments

  • <action>: The action to perform: comment or uncomment.
  • <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.

Options

  • --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.

Examples

  1. Comment all lines containing the string console.log in a JavaScript file:

    line-commenter-tool comment app.js "console\\.log"
  2. Uncomment all lines that match the regex pattern TODO in a Python file:

    line-commenter-tool uncomment script.py "TODO"
  3. Comment specific lines in a file using multiple strings:

    line-commenter-tool comment config.yml "DEBUG" "error,warning"
  4. Uncomment a full multiline block comment in a CSS file:

    line-commenter-tool uncomment styles.css "/*" --multiline

Notes

  • ⚠️ 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 --multiline option is particularly useful for handling languages that use block comments for larger sections of code, such as CSS, HTML, or JavaScript.

Integration with Husky

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.

Setup Husky

First, install Husky in your project:

npm install husky --save-dev

Enable Git hooks:

npx husky install

Add a Hook

Create 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.

Recommended Husky Configuration (Husky v9+)

Here's an example of configuring Husky to run line-commenter-tool as part of your pre-commit hook:

  1. Create .husky/pre-commit

    npx husky add .husky/pre-commit "npx line-commenter-tool comment path/to/file.js 'console\\.log\\(\\)' 'TODO'"
  2. Make sure your hooks are executable

    chmod +x .husky/pre-commit
  3. 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'

License

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

Contribution

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

Support

If you encounter any issues or have questions, please open an issue on the GitHub repository.