.git 位于不同文件夹时如何配置 husky?

问题描述 投票:0回答:4

我正在尝试为我公司的项目设置提交时的自动 linting,我见过的最好方法是

husky
NPM 包。我正在按照有关使用
Husky
eslint
prettier
设置
lint-staged
的说明进行操作,但我一直遇到同样的问题,但我不知道如何解决。

我的问题是我的项目是这样设置的:

- Parent Directory
  - .git
  - Working Project Directory
    - node_modules
    - package.json

当我尝试安装 husky 时,出现错误,提示我缺少

.git
文件,但它就在那里,只是文件结构中的上一级!!

我在其他帖子中看到了一些关于使用以下技术来修复它的内容:

npx mrm lint-staged
这会失败,这是预料之中的

然后修复 npm 准备脚本

 "prepare": "husky install" -> "prepare": "cd .. && husky install some-folder/.husky"

然后运行

npm i

然而它对我不起作用。

我也尝试过在

parent directory
中安装husky,但它不太有效,这使得我们项目的安装过程变得更加困难

有人有什么建议吗?

编辑:这是我收到的错误消息:

% npx husky-ini
t && npm install
Need to install the following packages:
  husky-init
Ok to proceed? (y) y
husky-init updating package.json
  setting prepare script to command "husky install"
/Users/me/.npm/_npx/1ab9c0f68ac2536e/node_modules/husky/lib/index.js:22
        throw new Error(`.git can't be found (see ${url})`);
        ^

Error: .git can't be found (see https://typicode.github.io/husky/#/?id=custom-directory)
    at install (/Users/me/.npm/_npx/1ab9c0f68ac2536e/node_modules/husky/lib/index.js:22:15)
    at Object.<anonymous> (/Users/me/.npm/_npx/1ab9c0f68ac2536e/node_modules/husky-init/lib/bin.js:16:21)
javascript npm eslint package.json husky
4个回答
15
投票

默认情况下,husky 假设你项目的 package.json 文件与 .git 文件处于同一级别。 就你的情况而言,正如你所说,它们处于不同的级别。

要解决此问题,您应该修改 package.json 文件。

即使你有这个错误

   Error(`.git can't be found (see ${url})`);) 

你应该在脚本中包含这个

"scripts": {
 ...
 "prepare": "husky install"
 ...
}

您应该将其替换为:

"scripts": {
   ...
   "prepare": "cd .. && husky install [child-folder]/.husky"
   ...
}

示例:

  • 父文件夹
    • .git
    • 项目文件夹
      • 节点模块
      • package.json

解决方案

"scripts": {
   ...
   "prepare": "cd .. && husky install project-folder/.husky"
   ...
}

6
投票

默认情况下,husky 支持 monorepos,这意味着您的所有代码都应该位于 1 个父文件夹中。但是,如果您有 2 个不同的文件夹,例如,如果您正在构建带有前端和后端文件夹的全栈应用程序,则需要自定义目录。

  1. 首先,安装这个:
npm i husky -D
  1. 转到子文件夹
    package.json
    文件并添加此行
{
  "scripts": {
    "prepare": "cd .. && husky install [childFolderName]/.husky"
  }
}
  1. 奔跑
    npm install
  2. 在子文件夹中执行步骤 3 后,您应该能够看到一个
    .husky
    文件夹。
  3. pre-commit
    文件夹内创建一个名为
    .husky
    且不带扩展名的文件,并添加这些基本行和其他内容
# ...
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
cd ./[childFolderName] && npm test

完成!希望能帮助到你。如需更多参考,请将官方文档放在这里:https://typicode.github.io/husky/guide.html


0
投票

我陷入了同样的问题&&这是我想出的解决方案:

*** 如果您在 Parent Directory 中没有 package.json,您应该使用

npm init -y
***

创建一个
  • 在家长目录
    npm install husky --save-dev
  • 在家长目录
    npx husky install Working Project Directory/.husky
  • 在家长目录
    npx husky add Working Project Directory/.husky/pre-commit "cd Working Project Directory && npm run test"

这对我有用


0
投票

用这种方式解决了同样的问题:

第 1 步: 您已找到 .git 目录

像这样的示例文件结构(git 位于父文件夹 [ADMIN] 内),

 .ADMIN
 ├── .git/
 ├── folder_1/  [ No package.json ]
 └── folder_2/  [ Contains Package.json with .husky]

Step-2: 在folder_2的Package.json中添加准备脚本

 "scripts": {
    "prepare": "cd .. && husky folder_1/.husky" //put this line and put your directory name
  },

步骤 3: 运行

npm run prepare
生成 .husky 目录及其内容。 然后你会在folder_2中看到目录.husky,如下所示

第 4 步: 然后您所要做的就是在 .husky/pre-commit 中创建新文件

第 5 步: 在 .husky/pre-commit 中,添加以下内容:

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

cd folder_2
npm run compile
npx pretty-quick --staged
npm run lint

确保将示例命令(npm runcompile、npx Pretty-quick --staged、npm run lint)替换为用于编译、格式化和 linting 的实际命令。

© www.soinside.com 2019 - 2024. All rights reserved.