typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
31.99k stars 1k forks source link

prevent overwriting custom commands #1439

Closed adiletbaimyrza closed 3 days ago

adiletbaimyrza commented 2 months ago

Hello, my team is working on a project and at some point we decided to integrate husky. The thing is that our project is not located in the same directory as .git file. I managed to change prepare script from "scripts": { ..., "prepare": "husky" }, to "scripts": { ..., "prepare": "cd .. && husky frontend/.husky && cd frontend && npx husky init" },

and I created .husky/pre-commit file with cd frontend npx lint-staged

when running npm install it overwrites package.json and .husky/pre-commit files to default values. I created a fork of the husky repository and added a few lines to prevent overwriting those files. Like so:

// create a prepare script only if it doesn't exist
if (!o.scripts.prepare) {
    ;(o.scripts ||= {}).prepare = 'husky'
    w(n, JSON.stringify(o, 0, /\t/.test(s) ? '\t' : 2) + '\n')
}

p.stdout.write(i())
try { f.mkdirSync('.husky') } catch {}

// create a pre-commit file with test command only if it doesn't exist
if (!e('.husky/pre-commit')) {
    w('.husky/pre-commit', process.env.npm_config_user_agent.split('/')[0] + ' test\n');
}

it works well in our project. What do you think about adding those lines to the bin.mjs?

typicode commented 2 months ago

Hi, husky init needs to be called only once in a project and shouldn't be added to scripts

adiletbaimyrza commented 1 month ago

thank you