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

stylish-commit

v0.0.8

Published

Check staged changes with style scripts before commiting with git.

Downloads

287

Readme

stylish-commit

Build Status npm

stylish-commit is a command-line tool and git pre-commit hook that checks your changes using simple lint scripts written in javascript.

Use it for simple tasks like:

Operation

When you commit some code, the style scripts will be run against all staged changes. If a script has modified lines you will be prompted to change them, like: prompt with changes

Here, you have the option to:

  • continue (ignores suggestions) - proceeds with the commit without making any changes.
  • apply the suggestions - updates files with the suggested changes. This option takes you to another menu with the options to apply suggestions and commit, apply suggestions and cancel commit, ignore suggestions and cancel, or cancel (abort) the commit. This option will only be available if the changes can be applied cleanly (i.e. there are no unstaged changes to the file).
  • select suggestions to apply - lets you pick which suggestions are applied when you select the option above.
  • cancel (abort) the commit - so you can manually make changes.

Installation

For node/iojs projects, The hook can be installed automatically by running npm install stylish-commit-auto-hook-install --save-dev in your project's root folder. This will add stylish-commit-auto-hook-install to your project's dev-dependencies and automatically install the hook whenever someone installs dependencies with npm install.

You can also install the hook manually by running npm install stylish-commit -g then stylish-commit --install-hook. If you take this route, you'll need to manually install the hook for each clone of the project.

Creating style scripts

Style scripts sit in your project's .style directory1, and look like:

module.exports = {
  name: 'no-trailing-spaces',
  appliesTo: '**/*.+(js|txt)',
  validate: function (lines) {
    return lines.map(function (line) { return line.replace(/\s+$/, ''); });
  }
};

Each style script must have a name field and a validate function. It can also provide an appliesTo minimatch glob that restricts which modified files it checks.

The validate function receives an array of strings, each representing a changed line and is expected to return an array of the same length with either the unaltered line or a line containing modifications. e.g. the above script takes ['twas brillig and', 'the slithy    ', 'toves'] and returns ['twas brillig and', 'the slithy', 'toves']`.

There are some scripts to get you started at https://github.com/electronifie/style-guide.

Testing style scripts

You can test your scripts with the contents of a test file:

  1. create a test file e.g. ~/foo.txt
  2. ensure stylish-commit is globally installed with npm install -g stylish-commit
  3. run stylish-commit -t ~/foo.txt from within the repo configured to use your style scripts

Alternative script formats

Regex search + replace

Runs a regex replace on each line. The with property can use dollar-notation (e.g. $1) to reference matching groups. The example below is functionally equivalent to the one above, just a bit easier to read.

module.exports = {
  name: 'no-trailing-spaces',
  appliesTo: '**/*.+(js|txt)',
  validate: { replace: /\s+$/, with: '' }
};

Known issues

  • only works when committing from a terminal. The scripts will not run when committing from non-tty interfaces like IDEs or git GUIs.
  • does not smartly handle multiple changes to the same line of the same file. In this case, only the last script to run's changes will be applied.