husky icon indicating copy to clipboard operation
husky copied to clipboard

Husky replaces my "prepare" script

Open ezeamin opened this issue 1 year ago • 5 comments

Context

  • Terminal: Powershell - Windows 11
  • Node: 20.9.0
  • pnpm: 8.14.0

Hi! Husky is working great so far, thanks for your time! But, when I run pnpm exec husky init, following the docs, it installs the hooks but also replaces my prepare script with prepare: husky. I understand the purpose of this behaviour, but if I have a prepare script already, it should't replace it with its own script.

It also replaces my repo pre-commit file inside of /husky with a default one that does pnpm test. This is undesired behavior, because I already had a configuration written for the pre-commit hook.

ezeamin avatar Feb 06 '24 10:02 ezeamin

Hi!

For existing hooks, it's better to follow the 'How to migrate' section from https://github.com/typicode/husky/releases/tag/v9.0.1

I'll update docs to make that clearer.

typicode avatar Feb 06 '24 11:02 typicode

Hello @typicode and @ezeamin,

we use the script prepare for more than just husky. I think it is more comfortable to leave it as it is.

deleonio avatar Feb 20 '24 06:02 deleonio

@typicode I created a pull request #1407 to fix this issue, please test and check.

zayne-wang avatar Mar 12 '24 05:03 zayne-wang

At least there should be a condition to check if prepare script has any commands in it or not If there is a command then this husky command should be appended.

Like this

- 14 ;(o.scripts ||= {}).prepare = 'husky'
+ 14 ;(o.scripts ||= {}).prepare = '' === (o.scripts ||= {}).prepare ? 'husky' : (o.scripts ||= {}).prepare + ' && husky'

Raised the PR for the mentioned solution please check it out.

SH4LIN avatar Mar 24 '24 10:03 SH4LIN

At least there should be a condition to check if prepare script has any commands in it or not If there is a command then this husky command should be appended.

Like this

- 14 ;(o.scripts ||= {}).prepare = 'husky'
+ 14 ;(o.scripts ||= {}).prepare = '' === (o.scripts ||= {}).prepare ? 'husky' : (o.scripts ||= {}).prepare + ' && husky'

Raised the PR for the mentioned solution please check it out.

Hi @SH4LIN,

Maybe we experienced the same mistake. According to typicode's reply in PR #1407, the custom scripts should be placed in .husky/, but not in .husky/_/. The .husky/_/ directory is managed by husky itself.

Hope this could make sense for you.

zayne-wang avatar Mar 25 '24 01:03 zayne-wang

At least there should be a condition to check if prepare script has any commands in it or not If there is a command then this husky command should be appended. Like this

- 14 ;(o.scripts ||= {}).prepare = 'husky'
+ 14 ;(o.scripts ||= {}).prepare = '' === (o.scripts ||= {}).prepare ? 'husky' : (o.scripts ||= {}).prepare + ' && husky'

Raised the PR for the mentioned solution please check it out.

Hi @SH4LIN,

Maybe we experienced the same mistake. According to typicode's reply in PR #1407, the custom scripts should be placed in .husky/, but not in .husky/_/. The .husky/_/ directory is managed by husky itself.

Hope this could make sense for you.

Hi, The problem here is that you can't have a custom "prepare" script inside your package, Husky always replace it with "prepare: "husky"". For automation purpose, it is a huge cons of using v9.

benwck avatar Jul 30 '24 12:07 benwck

Why is this not prioritized? This issue has blocked me to upgrade Husky. 😔

ernilambar avatar Jul 31 '24 01:07 ernilambar

husky init is a one-time setup command.

It does only three things that can be done manually:

  1. set "prepare": "husky" in package.json
  2. run husky command (you can do it with npx husky or npm run prepare)
  3. create an example .husky/pre-commit file (you can create it manually)

https://typicode.github.io/husky/how-to.html#manual-setup

Once these three steps are done, there's no need to run husky init or redo these steps ever again.

typicode avatar Jul 31 '24 09:07 typicode

@typicode Thanks for the reply. I will go manual route then. I can live with running one extra command npx husky rather than not being able to use "prepare". Thanks for the awesome tool. 😊

ernilambar avatar Jul 31 '24 10:07 ernilambar

You're welcome, glad it's solved :)

typicode avatar Aug 02 '24 23:08 typicode