Giter VIP home page Giter VIP logo

npm-husky-hooks's Introduction

@jeliasson/husky-hooks

NPM npm bundle size npm Libraries.io dependency status for latest release, scoped npm package GitHub issues

This npm package aims to increase the developer experience and consistency by providing a set of hooks that can be opted-in the development lifecycle. It depends on husky for pre-commit and pre-push hooks, and a few other zero/low dependency packages.

⚠️ Note: This package is in development and comes with breaking changes, so move with caution.

Setup

First, make sure you have husky installed. See installation here or refer to below quick setup.

# Install husky dependency
yarn add --dev husky

# Install husky
yarn husky install

Once you have husky installed, let's proceed with setting up @jeliasson/husky-hooks and connect it with husky's pre-commit and pre-push hooks.

# Install dependency @jeliasson/husky-hooks
yarn add --dev @jeliasson/husky-hooks

# Add package pre-commit hook
npx husky add .husky/pre-commit "npx @jeliasson/husky-hooks pre-commit"

# Add package pre-push hook
npx husky add .husky/pre-push "npx @jeliasson/husky-hooks pre-push"

Now to create a config file. husky-hooks.config.js will be placed in the root folder of the project.

# Create config
npx @jeliasson/husky-hooks create-config

Let's test it out and see if we get some magic ✨

# Make a new branch, create a test file, git add and commit
git checkout -b testing/jeliasson-husky-hooks
touch test.tmp && git add test.tmp
git commit -m "test(repo): keep calm and commit"

This should yield the following output...

Running hook test-sleep... ✅
Running hook check-branch... ✅
Running hook check-lock-files... ✅
Running hook run with argument 'echo Test'... ✅

...unless you have anything other than yarn.lock in your repo 😅

Running hook test-sleep... ✅
Running hook check-branch... ✅
Running hook check-lock-files... ❌

Invalid occurence of "package-lock.json" file. Remove it and only use "yarn.lock"

Hooks

Hooks to run is defined in the configuration file husky-hooks.config.js that was created as part of the setup. Below is a table of available built-in hooks, followed by it's respective specific configuration. Future hooks may be created that require additional dev dependencies, not included in this package, but those will be marked.

Name Description
check-branch Check which git branch we're currently on, and abort if it's a protected branch.
check-lock-files Check for package manager lock files, and abort if any are present lock file that we don't want.
run-cmd Run a ad-hoc command, and abort if the commands fails.

check-branch

Check which git branch we're currently on, and abort if it's a protected branch. This can be useful to make sure that commits stay away from branches that only being used for Pull Requests or CI/CD.

Setup

Add check-branch hook to pre-commit and/or pre-push.

{
  hooks: {
    'pre-commit': [
      'check-branch', /* This line */
      ...
    ],

    'pre-push': [
      'check-branch', /* This line */
      ...
    ],
  },
}

Settings

{
  settings: {
    'check-branch': {
      // Git branches that should be protected from accidental commit or push
      protectedBranches: ['main', 'dev'],
    },
  }
}

check-lock-files

Check for package manager lock files, and abort if any are present lock file that we don't want. This is useful to ensure that e.g. onlyyarn.lock is present in the repository.

Setup

Add check-lock-files hook to pre-commit and/or pre-push.

{
  hooks: {
    'pre-commit': [
      'check-lock-files', /* This line */
      ...
    ],

    'pre-push': [
      'check-lock-files', /* This line */
      ...
    ],
  },
}

Settings

{
  settings: {
    'check-lock-files': {
      // Package manager lock file that should be present in the repository
      allowLockFile: 'yarn.lock',

      // Package manager lock files that should yield a abort
      denyLockFiles: ['package-lock.json', 'pnpm-lock.yaml'],
    },
  }
}

run-cmd

Run a ad-hoc command, for example yarn lint, and abort if the commands fails. This can be useful if you have other commands, for exmaple in your husky hooks, that you want to run as part of this package.

⚠️ Note: Still figuring out the best approach to capture stdout/stderr and present them where appropriate. It works fine for now.

Setup

Add run-cmd hooks to pre-commit and/or pre-push. It should be shaped as an array where the first argument is run-cmd and the second argument would be the command to run, e.g. yarn lint. In below example we have two ad-hoc commands for each hook.

{
  hooks: {
    'pre-commit': [
      ['run-cmd', 'echo This is a pre-commit hook via run-cmd'],
      ['run-cmd', 'yarn lint'],
      ...
    ],

    'pre-push': [
      ['run-cmd', 'echo This is a pre-push hook via run-cmd'],
      ['run-cmd', 'yarn lint'],
      ...
    ],
  },
}

Other

Print output

Sometimes you may want to print the actual output of a hook, and passing --stdout will print the stdout of all hooks.

npx @jeliasson/husky-hooks pre-commit --stdout

Development

Prerequisites

  • NodeJS >= 18.18.0
  • yarn

From the package directory, run

yarn link

Start tsc watch

yarn dev

From the test project directory, run

yarn link @jeliasson/husky-hooks

Todo

See Issues

Contributing

See CONTRIBUTING ❤️

npm-husky-hooks's People

Contributors

github-actions[bot] avatar jeliasson avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

npm-husky-hooks's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Pending Status Checks

These updates await pending status checks. To force their creation now, click the checkbox below.

  • chore(deps): update dependency eslint to v8.57.1
  • fix(deps): update dependency husky to v9.1.6

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/draft-release.yml
  • actions/checkout v4
  • actions/setup-node v4
  • peter-evans/create-pull-request v6
.github/workflows/main.yml
  • actions/checkout v4
  • actions/setup-node v4
  • ubuntu 22.04
.github/workflows/publish.yml
  • actions/checkout v4
  • actions/setup-node v4
  • JS-DevTools/npm-publish v3
.github/workflows/renovate.yml
  • actions/checkout v4
  • actions/setup-node v4
  • ubuntu 22.04
.github/workflows/research.yml
  • actions/checkout v4
  • actions/setup-node v4
  • ubuntu 22.04
npm
package.json
  • commander ^11.0.0
  • husky ^9.0.0
  • node-fetch ^2.6.7
  • zod ^3.17.3
  • @types/jest 28.1.8
  • @types/node 20.16.5
  • @types/node-fetch 2.6.11
  • @typescript-eslint/eslint-plugin 7.18.0
  • @typescript-eslint/parser 7.18.0
  • eslint 8.57.0
  • eslint-config-prettier 9.1.0
  • eslint-plugin-import 2.30.0
  • eslint-plugin-import-helpers 1.3.1
  • jest 28.1.3
  • prettier 3.3.3
  • ts-jest 28.0.8
  • tsc-watch 6.2.0
  • typescript 5.6.2
  • node >=18.18.0
  • yarn >=1.15

  • Check this box to trigger a request for Renovate to run again on this repository

Replace yargs

Alternative

  • commander
  • clipanion
  • node 18's parser

With yargs: 70.1 kB (0.2.1)

CI/CD

  • issues
  • dev
  • main
  • publish

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.