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

git-precommit-checks

v3.1.0

Published

Customizable checks on pre-commit (staged) contents

Downloads

23,021

Readme

Check staged contents before commiting

version travis build node version dependencies coverage vulnerabilities MIT license semantic-release

Documentation also available in French: 🇫🇷 Traduction française 🇫🇷

Because we love git hooks and npm, we want to share and automate code/content quality.

git-precommit-checks has to be loaded manually or using any wrapper around git hooks.

As you can read below we highly recommend Husky.

How does it look like?

asciicast

Install

npm install --save-dev git-precommit-checks

How to setup my checking rules?

Configuration is loaded from the project root/top level directory using git-precommit-checks.config.js or git-precommit-checks.json, or from package.json so you can customize it according to your needs.

Here is an example using git-precommit-checks.json configuration file:

{
  "rules": [
    {
      "filter": "\\.js$",
      "nonBlocking": "true",
      "message": "You’ve got leftover `console.log`",
      "regex": "console\\.log"
    },
    {
      "message": "You’ve got leftover conflict markers",
      "regex": "/^[<>|=]{4,}/m"
    },
    {
      "message": "You have unfinished devs",
      "nonBlocking": "true",
      "regex": "(?:FIXME|TODO)"
    }
  ]
}

Same thing using JS configuration file:

module.exports = {
  display: {
    notifications: true,
    offendingContent: true,
    rulesSummary: false,
    shortStats: true,
    verbose: false,
  },
  rules: [
    {
      message: 'You’ve got leftover conflict markers',
      regex: /^[<>|=]{4,}/m,
    },
    {
      filter: /^(?!README(_fr)?\.md)$/,
      message: 'You have unfinished devs',
      nonBlocking: true,
      regex: /(?:FIXME|TODO)/,
    },
  ],
}

When using package.json file, you must add a dedicated git-precommit-checks key:

"git-precommit-checks": {
  "rules": [
    {
      "filter": "\\.js$",
      "nonBlocking": "true",
      "message": "You’ve got leftover `console.log`",
      "regex": "console\\.log"
    },
    {
      "message": "You’ve got leftover conflict markers",
      "regex": "/^[<>|=]{4,}/m"
    },
    {
      "message": "You have unfinished devs",
      "nonBlocking": "true",
      "regex": "(?:FIXME|TODO)"
    }
  ]
}

Each "pre-commit" entry is a checking rule: the pattern describes a regular expression that will be searched upon staged content. The associated message is displayed when the pattern is found.

Each rule will stop the commit when the associated pattern is found unless you set the nonBlocking key to true. Non blocking rules will print warning messages.

Only message and regex keys are mandatory.

You can also filter on files patterns using the filter key.

For instance, you'll get a warning about your package.json the first time you set the FIXME/TODO rule and every time you update that line. If you want to prevent such a warning you can extend that rule like this:

  {
    "filter": "^package\\.json$",
    "message": "You have unfinished devs",
    "nonBlocking": "true",
    "regex": "(?:FIXME|TODO)"
  }

⚠️ There is no default checks configured after install, so please be aware that nothing will happend without adding your own rules!

Display options

You can add an optional display entry in your config to enable some options:

"git-precommit-checks": {
  "display": {
    "notifications": true,
    "offendingContent": true,
    "rulesSummary": true,
    "shortStats": true,
    "verbose": true
  },
  …
  • notifications: print error/warning summary using system notification.
  • offendingContent: print offending contents right after associated file path and line number.
  • rulesSummary: print rules as a table before parsing staged files.
  • shortStats: print short stats (ie. 1 error, 1 warning.).
  • verbose: print every performed action, files parsed, short summary/stats (errors and warnings number).

Usage

Triggering it straight with git hooks

After installing locally or globally your module, add the following code (or equivalent) to your project pre-commit hook .git/hooks/pre-commit:

#!/bin/sh
scriptName="git-precommit-checks"
scriptPath="$(npm bin)/$scriptName"

if [ -f $scriptPath ]; then
  $scriptPath
else
  echo "Can't find $scriptName module"
  echo "You can reinstall it using 'npm install $scriptName --save-dev' or delete this hook"
fi

Running git-precommit-checks with Husky

Husky is a great tool to manage git hooks from your package.json.

You can use it and call git-precommit-checks on pre-commit:

  "husky": {
    "hooks": {
      "pre-commit": "git-precommit-checks"
    }
  }

Contributing

Any contribution is welcomed. Here is our contribution guideline