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

@thtliife/eslint-staged-ignored-filter

v1.0.1

Published

A small command line utility and api to output staged git files without eslint excluded files

Downloads

6

Readme

eslint-staged-ignored-filter

A small helper utility for filtering out the files ignored by eslint when performing a linting test.

It is mainly useful when linting as a pre-commit hook for git. (vanilla hook, lint-staged, husky, etc...)

Install

npm install --save-dev @thtliife/eslint-staged-ignored-filter

or

yarn add --dev @thtliife/eslint-staged-ignored-filter

Use

There are two ways this util can be used.

I prefer to use vanilla git hooks, rather than installing a package like husky or lint-staged.

CLI

For this use case, the cli tool is the easiest way, as native githooks are standard shell scripts. The cli is usable as follows:

eslint-staged-ignored-filter file1.js file2.js file3.ts ... lastfile.tsx

it is also aliased to the shorter command esif so can be used as follows:

esif file1.js file2.js file3.ts ... lastfile.tsx

An example script for using this as a vanilla githook is:

#!/usr/bin/env bash

# Get the currently staged files
declare -a STAGED_FILES=($(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g'))

# Quit if nothing is staged
[ -z "$STAGED_FILES" ] && exit 0

# Determine the package runner via lock files, falling back to "npx"
# yarn outputs extra text on running packages, so need to silence that with --silent option
[ -z "$PACKAGE_RUNNER" ] && [ -f "./yarn.lock" ] && PACKAGE_RUNNER="yarn --silent"
[ -z "$PACKAGE_RUNNER" ] && [ -f "./pnpm-lock.yaml" ] && PACKAGE_RUNNER="pnpm"
[ -z "$PACKAGE_RUNNER" ] && [ -f "./package-lock.json" ] && PACKAGE_RUNNER="npx"
[ -z "$PACKAGE_RUNNER" ] && PACKAGE_RUNNER="npx"

ESLINT_INSTALLED=$(npm --silent list eslint >/dev/null && echo true || echo false)

# Lint staged files with eslint if it is installed in the project
if [ "$ESLINT_INSTALLED" = "true" ]; then
  # Filter the staged files by file extension that we want to lint
  STAGED_FILES_TO_LINT=$((IFS=$'\n' && echo "${STAGED_FILES[*]}") | grep -E '.*\.(js|jsx|tsx|ts)$')

  # Get files that eslint can process, which will not throw "Ignored" warnings
  LINTABLE_FILES=$($PACKAGE_RUNNER esif ${STAGED_FILES_TO_LINT})

  # Exit if no staged files are lintable
  [ -z "$LINTABLE_FILES" ] && exit 0

  echo ""
  echo Running eslint on staged files...
  # Run eslint using the detected package runner
  $PACKAGE_RUNNER eslint $LINTABLE_FILES --max-warnings 0 --report-unused-disable-directives --fix

  # Get the eslint exit code
  ESLINT_EXIT="$?"

  # Re-add files since they may have been fixed
  # Remove the next line if you are not running eslint with the --fix option
  git add "${LINTABLE_FILES}"

  # Test the eslint exit code for non-zero status
  if [[ "${ESLINT_EXIT}" == 0 ]]; then
    echo "✔ Ok"
  else
    echo "✘ COMMIT FAILED: Fix eslint errors and try again"
    exit $ESLINT_EXIT
  fi
fi

API

If you are using a tool like lint-staged, you should use the exported filterIgnored function in your .lintstagedrc.js configuration file as below.

The filterIgnored function receives a list of paths either as a space delimited string, or as an array, and outputs an array of files which eslint will not ignore.

An example .lintstagedrc.js file might look like:

import { filterIgnored } from '@thtliife/eslint-staged-ignored-filter';

export default {
  '**/*.{ts,tsx,js,jsx}': async (files) => {
    const filesToLint = (await filterIgnored(files)).join(' ');
    return [`eslint --max-warnings=0 ${filesToLint}`];
  }
};

Building

Run yarn nx build eslint-staged-ignored-filter to build the library.

Running unit tests

Run yarn nx test eslint-staged-ignored-filter to execute the unit tests via Jest.