网站搜索

typescript - 将 pre-commit 钩子链接到 VSC 源代码控制


我的目标是每次我执行 git commit 时,eslint 都应该先运行。目前,我正在使用 husky 钩子。

当我通过终端使用 git commit -m "some message" 提交时,它按预期工作。

问题是在我使用 Visual Studio Code(或 WebStorm)中的源代码管理时出现的。

源代码管理提交

当我这样提交时,eslint 不会运行。

这是 husky pre-commit 文件:husky pre-commit 文件。

这是 package.json 的脚本部分:package.json 脚本部分

答案#1

按照推荐步骤安装 husky 对我来说似乎有效:

 From github.com:rachOS/BonApp-FRONT
 * branch            feature/create-user-account -> FETCH_HEAD
> git status -z -u
> git symbolic-ref --short HEAD
> git for-each-ref --format=%(refname)%00%(upstream:short)%00%(objectname)%00%(upstream:track) refs/heads/feature/create-user-account refs/remotes/feature/create-user-account
> git for-each-ref --sort -committerdate --format %(refname) %(objectname) %(*objectname)
> git remote --verbose
> git config --get commit.template
> git ls-files --stage -- /home/rach/Documents/Code/BonApp/front-bon_app.ts/src/entities/Recipe.ts
> git cat-file -s 96fa250abd168e2175cffe887b14db1f5787e7dc
> git show --textconv :src/entities/Recipe.ts
> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
> git ls-files --stage -- /home/rach/Documents/Code/BonApp/front-bon_app.ts/src/entities/Recipe.ts
> git cat-file -s 96fa250abd168e2175cffe887b14db1f5787e7dc
> git show --textconv :src/entities/Recipe.ts

> [email  validate
> npm-run-all --parallel  check-format lint build


> [email  lint
> eslint --ignore-path .gitignore -c .eslintrc --ext .ts --fix .


> [email  build
> react-scripts build


> [email  check-format
> npm run format -- --list-different


> [email  format
> npm run prettier -- --write  "--list-different"


> [email  prettier
> prettier --ignore-path .gitignore "**/*.+(js|ts|json|jsx|tsx|css|html)" "--write" "--list-different"

Creating an optimized production build...
Compiled with warnings.

src/adapters/primary/components/Form/Form.jsx
  Line 103:3:   'readOnly' is defined but never used  no-unused-vars
  Line 137:45:  'type' is defined but never used      no-unused-vars

src/adapters/primary/components/Signup/manager/SignupManager.jsx
  Line 4:10:  'signup' is defined but never used  no-unused-vars

Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.

File sizes after gzip:

  59.19 KB  build/static/js/2.58d43881.chunk.js
  1.63 KB   build/static/js/3.90fad592.chunk.js
  1.55 KB   build/static/js/main.5cbbd48f.chunk.js
  1.17 KB   build/static/js/runtime-main.63262bbd.js
  1.14 KB   build/static/css/main.8636b64b.chunk.css

The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.
You may serve it with a static server:

  npm install -g serve
  serve -s build

Find out more about deployment here:

  https://cra.link/deployment
> git status -z -u
> git symbolic-ref --short HEAD
> git for-each-ref --format=%(refname)%00%(upstream:short)%00%(objectname)%00%(upstream:track) refs/heads/feature/create-user-account refs/remotes/feature/create-user-account
> git for-each-ref --sort -committerdate --format %(refname) %(objectname) %(*objectname)
> git remote --verbose
> git config --get commit.template
> git config --get commit.template
> git ls-files --stage -- /home/rach/Documents/Code/BonApp/front-bon_app.ts/src/entities/Recipe.ts
> git cat-file -s 96fa250abd168e2175cffe887b14db1f5787e7dc
> git show --textconv :src/entities/Recipe.ts

答案#2

我最近为我的 React 应用安装了 husky。我只是按照官方指南《Getting started | 🐶 husky》中的说明进行操作。由于我使用的是 yarn,我特别按照 yarn 部分《Yarn 2 install》中的步骤进行操作。

  1. 安装 husky
yarn add husky --dev
  1. 启用 git 钩子
yarn husky install
  1. 安装您想要的钩子——在我的例子中,我希望 husky 运行 npx pretty-quick --staged
yarn husky set .husky/pre-commit \"npx pretty-quick --staged\"
  1. 要在安装后启用 Git 钩子,请编辑 package.json。如果你打算克隆仓库并运行 yarn install,你将需要这样做。
{
  "scripts": {
    "postinstall": "husky install"
  }
}

之后,我使用 VS Code 的源代码管理提交了我的更改。为了验证 husky 预钩子是否工作,我检查了 Git 日志。你可以在 调试控制台 中找到它们(使用 Shift+Escape 打开),然后切换到 OUTPUT 标签,并从下拉菜单中选择 Git 选项。