How to run husky pre-commit in child directory only

余生颓废 提交于 2021-01-22 06:37:08

问题


We have an enterprise application with a folder structure like the following:

/project
  .git
  /sub1
    ...Java project
  /sub2
    package.json
    ...Javascript Backbone project
  /sub3
    ...Java project
  /sub4
    ...Java project
  /sub5
    package.json
    ...Javascript React project

I currently have Husky set up both in sub2 and sub5 projects, which causes conflicts (requires an npm install whenever I switch projects). Also, the Java developers are reporting that when they commit code in projects sub1, sub3 and sub4, the Husky git hooks are being executed.

Is it possible to only have the hooks run if you are IN a certain folder when you commit?


回答1:


Is it possible to only have the hooks run if you are IN a certain folder when you commit?

No, but the hook itself can determine which directory it was in when it was run. This is not properly documented, but GIT_PREFIX is set in the environment before Git chdir-s to the $GIT_WORK_TREE or $GIT_DIR directory. (This has been the case since Git version 1.7.8.)




回答2:


I faced the same issue. I fixed it by installing husky in the main directory.
my setup:

/project
    .git
    package.json
    /frontend
        package.json
    /frontend-admin
        package.json

In /project/package.json:

{
  "license": "MIT",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "frontend": "cd frontend && yarn lint"
    "frontend:admin": "cd frontend-admin && yarn lint",
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm-run-all frontend frontend:admin"
    }
  },
  // In the main folder you should install husky 
  // and npm-run-all to execute multiple commands
  "dependencies": {
    "husky": "^4.3.0",
    "npm-run-all": "^4.1.5"
  }
}

Your subdirectory should have a lint script to execute.
When one of the scripts crashes husky will give a report and abort immediately.



来源:https://stackoverflow.com/questions/53869155/how-to-run-husky-pre-commit-in-child-directory-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!