jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.2k stars 505 forks source link

Husky hooks don't run straight out of the box #1127

Open ackvf opened 2 years ago

ackvf commented 2 years ago

Current Behavior

Husky >4 does not automatically create hooks with new tsdx installs.

Expected behavior

When I create my first commit, it should run pre-commit hooks.

Suggested solution(s)

Husky lists steps to register a hook on their site, maybe it could be automated using a script. https://github.com/typicode/husky#usage

What works for me

{
  "version": "0.1.0",
  "license": "MIT",
  "main": "dist/index.js",
  "typings": "dist/index.d.ts",
  "files": [
    "dist",
    "src"
  ],
  "engines": {
    "node": ">=10.4",
    "npm": ">=8"
  },
  "scripts": {
    "start": "tsdx watch",
    "build": "tsdx build",
    "test": "tsdx test",
    "lint": "tsdx lint",
    "lint:fix": "tsdx lint --fix",
    "prepare": "init:husky && tsdx build",
    "size": "size-limit",
    "analyze": "size-limit --why",
    "init:husky": "husky install && npx husky add .husky/pre-push \"tsdx lint\""
  },
  "module": "dist/bign.esm.js",
  "size-limit": [
    {
      "path": "dist/bign.cjs.production.min.js",
      "limit": "10 KB"
    },
    {
      "path": "dist/bign.esm.js",
      "limit": "10 KB"
    }
  ],
  "peerDependencies": {},
  "devDependencies": {
    "@size-limit/preset-small-lib": "^7.0.8",
    "husky": "^7.0.4",
    "size-limit": "^7.0.8",
    "tsdx": "^0.14.1",
    "tslib": "^2.3.1",
    "typescript": "^4.6.2"
  }
}

note

Additional context

There have been some changes in Husky since version 4 and it does not register the hooks automatically any more. Husky 7 is used with tsdx and thus it is also affected.

azigler commented 1 year ago

@ackvf Great suggestion! I took this for a spin on my end, however, and the prepare script pushes a new line with tsdx lint to the ./husky/pre-push hook every time the package is installed.

I'm using Yarn, and the Husky guide is a little different for Yarn. I also removed husky: {} from package.json but I made these script changes instead:

"postinstall": "husky install",
"prepack": "pinst --disable",
"postpack": "pinst --enable",
"husky:init": "npx husky add .husky/pre-commit 'tsdx lint'"

I just run husky:init once to bootstrap the command I want, instead of attaching it to prepare.

lumentut commented 1 year ago

I'm using "tsdx": "^0.14.1" & yarn v1.22.19. What I do only following this:

npx husky-init && yarn

and the command will:

  1. auto update the package.json by adding && husky install at prepare section and install husky with a new version that I think it will be adjusted according to the peer dependencies.
    {
    "scripts": {
    ...
    "prepare": "tsdx build && husky install",   // <--- previous "prepare": "tsdx build"
    ...
    },
    "devDependencies": {
    ...
    "husky": "^8.0.0",
    ...
    }
  2. auto add .husky/pre-commit at my root directory
    
    #!/usr/bin/env sh
    . "$(dirname -- "$0")/_/husky.sh"

npm test

Since I using yarn, then I change npm test into:

!/usr/bin/env sh

. "$(dirname -- "$0")/_/husky.sh"

yarn lint && yarn test

Next, remove **husky** section below at **package.json**

"husky": { "hooks": { "pre-commit": "tsdx lint" } },


and that's it. When I do a commit the husky will run as expected