【问题标题】:How to run husky pre-commit in child directory only如何仅在子目录中运行 husky 预提交
【发布时间】:2019-05-21 00:04:58
【问题描述】:

我们有一个企业应用程序,其文件夹结构如下:

/项目 .git /sub1 ...Java 项目 /sub2 包.json ...Javascript 骨干项目 /sub3 ...Java 项目 /sub4 ...Java 项目 /sub5 包.json ...Javascript React 项目

我目前在 sub2 和 sub5 项目中都设置了 Husky,这会导致冲突(每次切换项目时都需要安装 npm)。此外,Java 开发人员报告说,当他们在项目 sub1、sub3 和 sub4 中提交代码时,正在执行 Husky git 挂钩。

是否可以只在提交时在某个文件夹中运行挂钩?

【问题讨论】:

    标签: git githooks git-husky husky


    【解决方案1】:

    我遇到了同样的问题。我通过在主目录中安装 husky 来修复它。
    我的设置:

    /项目 .git 包.json /前端 包.json /前端管理员 包.json

    /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"
      }
    }
    

    您的子目录应该有一个要执行的 lint 脚本。
    当其中一个脚本崩溃时,husky 会立即报告并中止。

    【讨论】:

      【解决方案2】:

      是否可以只在提交时在某个文件夹中运行挂钩?

      不,但是钩子本身可以确定它在运行时在哪个目录。这没有正确记录,但是在 Git chdir-s 到 $GIT_WORK_TREE$GIT_DIR 目录之前的环境中设置了 GIT_PREFIX。 (从 Git 版本 1.7.8 开始就是这种情况。)

      【讨论】:

      • 我们可以举个例子说明如何使用它吗?
      • @MauroColella:在 shell 代码中,使用:loc=${GIT_PREFIX:-.},然后您所在的目录位于 $loc。例如,在cd-ing 到工作树的顶层之后,$loc 只是.,但在随后的cd dir1/dir2 之后,$locdir1/dir2
      【解决方案3】:

      我通过在 /root 目录中安装和运行 husky 解决了同样的问题。 (这是必需的,因为 husky 需要与 .git 文件夹位于同一目录中。

      npm install husky
      npx husky install #use this command to create a .husky folder
      

      搜索“预提交”文件

      cd ./frontend && npm run lint && npm run build
      cd ..
      cd ./backend && npm run lint && npm run test
      

      注意:输入相同的命令以执行每个子目录/项目 谢谢!

      【讨论】:

        猜你喜欢
        • 2022-01-07
        • 2021-07-13
        • 2017-09-19
        • 2023-01-17
        • 2021-10-21
        • 1970-01-01
        • 1970-01-01
        • 2021-07-07
        • 1970-01-01
        相关资源
        最近更新 更多