{"id":6424,"date":"2023-01-03T16:48:19","date_gmt":"2023-01-03T08:48:19","guid":{"rendered":"https:\/\/i007.vip:15443\/wordpress\/?p=6424"},"modified":"2023-01-03T16:48:19","modified_gmt":"2023-01-03T08:48:19","slug":"%e6%96%b0%e7%89%88-husky-%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/6424","title":{"rendered":"\u65b0\u7248 husky \u4f7f\u7528"},"content":{"rendered":"<div class=\"art-header\" data-v-15ffc2bc=\"\">\n<p><a href=\"https:\/\/ainyi.com\/129\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>\u00a0<\/p>\n<div class=\"header-info\" data-v-15ffc2bc=\"\"><i class=\"iconfont icon-tags-fill\" data-v-15ffc2bc=\"\"><\/i><a class=\"\" href=\"https:\/\/ainyi.com\/category\/1\" data-v-15ffc2bc=\"\">\u77e5\u8bc6\u603b\u7ed3<\/a><i class=\"iconfont icon-PC-date\" data-v-15ffc2bc=\"\"><\/i><span data-v-15ffc2bc=\"\">2022-04-13<\/span><i class=\"iconfont icon-eye-fill\" data-v-15ffc2bc=\"\"><\/i><span data-v-15ffc2bc=\"\">564<\/span><i class=\"iconfont icon-comments\" data-v-15ffc2bc=\"\"><\/i><span data-v-15ffc2bc=\"\">1<\/span><\/div>\n<div class=\"header-tag\" data-v-15ffc2bc=\"\"><a class=\"\" href=\"https:\/\/ainyi.com\/tag\/git\" data-v-15ffc2bc=\"\">git<\/a><\/div>\n<div class=\"tag-time\" data-v-15ffc2bc=\"\"><i class=\"iconfont icon-time\" data-v-15ffc2bc=\"\"><\/i>\u00a016:19:01<\/div>\n<\/div>\n<div id=\"blog\" class=\"content markdown-body\" data-v-15ffc2bc=\"\">\n<p>husky \u5728\u524d\u7aef\u5de5\u7a0b\u5316\u662f\u4e00\u4e2a\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u9879\u76ee\u91cc\u6dfb\u52a0 git hooks\uff0c\u5728 commit \u524d\u6821\u9a8c\u4ee3\u7801\u89c4\u8303\u3001commit \u4fe1\u606f\u89c4\u8303\u7b49\u3002<\/p>\n<h2 id=\"heading-0\"><a id=\"v6__2\"><\/a>v6 \u7248\u672c\u4e4b\u524d<\/h2>\n<p>\u5b89\u88c5\u76f8\u5173\u4f9d\u8d56<\/p>\n<div class=\"hljs\"><code class=\"lang-powershell\">yarn add -D husky lint-staged<br \/>\n<\/code><\/div>\n<p>\u7136\u540e\u5728\u00a0<code>package.json<\/code>\u00a0\u76f4\u63a5\u914d\u7f6e<\/p>\n<div class=\"hljs\"><code class=\"lang-json\">{<br \/>\n  <span class=\"hljs-attr\">\"husky\"<\/span>: {<br \/>\n    <span class=\"hljs-attr\">\"hooks\"<\/span>: {<br \/>\n      <span class=\"hljs-attr\">\"pre-commit\"<\/span>: <span class=\"hljs-string\">\"lint-staged\"<\/span>,<br \/>\n      <span class=\"hljs-attr\">\"commit-msg\"<\/span>: <span class=\"hljs-string\">\"node scripts\/verify-commit-msg.js\"<\/span><br \/>\n    }<br \/>\n  },<br \/>\n  <span class=\"hljs-attr\">\"lint-staged\"<\/span>: {<br \/>\n    <span class=\"hljs-attr\">\"*.{js,vue}\"<\/span>: [<br \/>\n      <span class=\"hljs-string\">\"npm run lint\"<\/span>,<br \/>\n      <span class=\"hljs-string\">\"git add\"<\/span><br \/>\n    ]<br \/>\n  }<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\u5728 scripts \u914d\u7f6e\u6821\u9a8c commit-msg \u7684\u811a\u672c\u00a0<code>scripts\/verify-commit-msg.js<\/code><\/p>\n<p>v6 \u7248\u672c\u4ee5\u524d\u662f\u8fd9\u4e48\u914d\u7f6e\u7684\u3002<\/p>\n<h2 id=\"heading-1\"><a id=\"_28\"><\/a>\u65b0\u7248\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>v6 \u7248\u672c\u4e4b\u540e\u7684 husky \u7684\u914d\u7f6e\u65b9\u5f0f\u5168\u90e8\u90fd\u53d8\u4e86<\/p>\n<p><strong>\u65b0\u7248\u5de5\u4f5c\u539f\u7406\uff1a<\/strong><br \/>\n\u4f7f\u7528\u4e86\u4ece git 2.9 \u5f00\u59cb\u5f15\u5165\u7684\u4e00\u4e2a\u65b0\u529f\u80fd\u00a0<code>core.hooksPath<\/code>\u3002core.hooksPath \u53ef\u4ee5\u8ba9\u4f60\u6307\u5b9a git hooks \u6240\u5728\u7684\u76ee\u5f55\u800c\u4e0d\u662f\u4f7f\u7528\u9ed8\u8ba4\u7684 .git\/hooks\/\u3002\u8fd9\u6837 husky \u53ef\u4ee5\u4f7f\u7528 husky install \u5c06 git hooks \u7684\u76ee\u5f55\u6307\u5b9a\u4e3a .husky\/\uff0c\u7136\u540e\u4f7f\u7528\u00a0<code>husky add<\/code>\u00a0\u547d\u4ee4\u5411 .husky\/ \u4e2d\u6dfb\u52a0 hook\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u6211\u4eec\u5c31\u53ef\u4ee5\u53ea\u6dfb\u52a0\u6211\u4eec\u9700\u8981\u7684\u00a0<code>git hook<\/code>\uff0c\u800c\u4e14\u6240\u6709\u7684\u811a\u672c\u90fd\u4fdd\u5b58\u5728\u00a0<code>.husky\/<\/code>\u00a0\u76ee\u5f55\u4e0b\uff0c\u56e0\u6b64\u4e5f\u5c31\u4e0d\u5b58\u5728\u540c\u6b65\u6587\u4ef6\u7684\u95ee\u9898\u4e86\u3002<\/p>\n<h2 id=\"heading-2\"><a id=\"_husky__35\"><\/a>\u65b0\u7248 husky \u5b9e\u8df5<\/h2>\n<p>1\u3001 precommit \u4f1a\u7528\u5230\u00a0<code>lint-staged<\/code>\uff0c\u6240\u4ee5\u8fd8\u8981\u5b89\u88c5<\/p>\n<div class=\"hljs\"><code class=\"lang-powershell\">yarn add -D husky lint-staged<br \/>\n<\/code><\/div>\n<p>2\u3001\u5728 package.json \u6dfb\u52a0\u4e00\u4e2a\u00a0<code>prepare script<\/code><\/p>\n<div class=\"hljs\"><code class=\"lang-json\">{<br \/>\n  <span class=\"hljs-attr\">\"scripts\"<\/span>: {<br \/>\n    <span class=\"hljs-attr\">\"prepare\"<\/span>: <span class=\"hljs-string\">\"husky install\"<\/span><br \/>\n  }<br \/>\n}<br \/>\n<\/code><\/div>\n<p>3\u3001\u7136\u540e\u6267\u884c prepare<\/p>\n<div class=\"hljs\"><code class=\"lang-powershell\">yarn prepare<br \/>\n<\/code><\/div>\n<blockquote><p>husky &#8211; Git hooks installed<\/p><\/blockquote>\n<p>\u5c31\u4f1a\u53d1\u73b0\u9879\u76ee\u6839\u76ee\u5f55\u589e\u52a0\u4e86\u4e00\u4e2a\u6587\u4ef6\u5939\u00a0<code>.husky\/<\/code>\u00a0\u5e76\u6307\u5b9a\u8be5\u76ee\u5f55\u4e3a git hooks \u6240\u5728\u7684\u76ee\u5f55\u3002<br \/>\n\u800c prepare \u811a\u672c\u4f1a\u5728 npm install \/ yarn\uff08\u4e0d\u5e26\u53c2\u6570\uff09\u4e4b\u540e\u81ea\u52a8\u6267\u884c\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u6211\u4eec\u514b\u9686\u9879\u76ee\u5b89\u88c5\u5b8c\u4f9d\u8d56\u540e\u4f1a\u81ea\u52a8\u6267\u884c\u00a0<code>husky install<\/code>\u00a0\u547d\u4ee4\u3002<\/p>\n<p>4\u3001\u6dfb\u52a0\u00a0<code>pre-commit<\/code>\u00a0git hook<\/p>\n<div class=\"hljs\"><code class=\"lang-powershell\">npx husky add .husky\/pre-commit <span class=\"hljs-string\">\"npx --no-install lint-staged\"<\/span><br \/>\n<\/code><\/div>\n<p>\u53ef\u4ee5\u53d1\u73b0 .husky\/ \u76ee\u5f55\u4e0b\u65b0\u589e\u4e86 pre-commit \u811a\u672c\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<div class=\"hljs\"><code class=\"lang-bash\"><span class=\"hljs-meta\">#!\/bin\/sh<\/span><br \/>\n. <span class=\"hljs-string\">\"<span class=\"hljs-variable\">$(dirname \"$0\")<\/span>\/_\/husky.sh\"<\/span><\/p>\n<p>npx --no-install lint-staged<br \/>\n<\/code><\/div>\n<p>\u4e5f\u5c31\u662f\u5728\u6267\u884c git commit \u524d\u4f1a\u5148\u6267\u884c\u00a0<code>pre-commit<\/code>\u00a0\u811a\u672c<\/p>\n<p>5\u3001\u4e0a\u9762\u5728 git commit \u524d\u4f1a\u6267\u884c\u00a0<code>npx --no-install lint-staged<\/code>\uff0c\u8fd9\u4e00\u6b65\u9700\u8981\u914d\u7f6e lint-staged\u3002<br \/>\n\u5728\u9879\u76ee\u6839\u76ee\u5f55\u65b0\u5efa\u00a0<code>.lintstagedrc.json<\/code>\uff0c\u914d\u7f6e\u5185\u5bb9\u8ddf\u4ee5\u524d\u5728 package.json \u4e00\u6837<\/p>\n<div class=\"hljs\"><code class=\"lang-json\">{<br \/>\n  <span class=\"hljs-attr\">\"*.{js,jsx,vue,ts,tsx}\"<\/span>: [<br \/>\n    <span class=\"hljs-string\">\"eslint --fix\"<\/span><br \/>\n  ],<br \/>\n  <span class=\"hljs-attr\">\"*.{vue,css,scss}\"<\/span>: [<br \/>\n    <span class=\"hljs-string\">\"stylelint --fix\"<\/span><br \/>\n  ]<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\u8fd9\u6837\u5c31\u4f1a\u6267\u884c\u4ee3\u7801\u6821\u9a8c\u4e86<\/p>\n<p>6\u3001\u6dfb\u52a0\u00a0<code>commit-msg<\/code>\u00a0git hook\uff0c\u6821\u9a8c\u6bcf\u6b21\u63d0\u4ea4\u7684\u4fe1\u606f\u89c4\u8303<br \/>\n\u4ee5\u524d\u5728 package.json \u914d\u7f6e\u7684 commit-msg \u662f\u8fd9\u6837<\/p>\n<div class=\"hljs\"><code class=\"lang-json\">{<br \/>\n  <span class=\"hljs-attr\">\"husky\"<\/span>: {<br \/>\n    <span class=\"hljs-attr\">\"hooks\"<\/span>: {<br \/>\n      <span class=\"hljs-attr\">\"commit-msg\"<\/span>: <span class=\"hljs-string\">\"node scripts\/verify-commit-msg.js --name=$HUSKY_GIT_PARAMS\"<\/span><br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\u73b0\u5728\u65b0\u7248\u7684\u00a0<code>$HUSKY_GIT_PARAMS<\/code>\u00a0\u53d8\u91cf\u5df2\u4e0d\u518d\u4f7f\u7528\uff0c\u53d6\u800c\u4ee3\u4e4b\u662f\u00a0<code>$1<\/code><br \/>\n\u6240\u4ee5\u914d\u7f6e\u00a0<code>commit-msg<\/code>\u00a0\u811a\u672c\u5982\u4e0b<\/p>\n<div class=\"hljs\"><code class=\"lang-powershell\"><span class=\"hljs-comment\">#!\/bin\/sh<\/span><br \/>\n. <span class=\"hljs-string\">\"$(dirname \"<\/span><span class=\"hljs-variable\">$0<\/span><span class=\"hljs-string\">\")\/_\/husky.sh\"<\/span><\/p>\n<p><span class=\"hljs-comment\"># Previous HUSKY_GIT_PARAMS environment variable is replaced by native params $1, $2, etc.<\/span><br \/>\nnode scripts\/verify-commit-msg.js --name=<span class=\"hljs-variable\">$1<\/span><br \/>\n<\/code><\/div>\n<p>\u6b64\u65f6\u6dfb\u52a0\u4e86\u4e24\u4e2a\u811a\u672c<br \/>\n<img decoding=\"async\" src=\"https:\/\/ainyi.com\/upload\/content\/129\/WechatIMG383.png\" alt=\"WechatIMG383.png\" \/><\/p>\n<p>\u518d\u914d\u7f6e\u6821\u9a8c\u811a\u672c\u00a0<code>scripts\/verify-commit-msg.js<\/code><\/p>\n<div class=\"hljs\"><code class=\"lang-js\"><span class=\"hljs-keyword\">const<\/span> chalk = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'chalk'<\/span>)<br \/>\n<span class=\"hljs-keyword\">const<\/span> args = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'minimist'<\/span>)(process.argv.slice(<span class=\"hljs-number\">2<\/span>))<br \/>\n<span class=\"hljs-keyword\">const<\/span> msgPath = args[<span class=\"hljs-string\">'name'<\/span>]<br \/>\n<span class=\"hljs-keyword\">const<\/span> msg = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'fs'<\/span>)<br \/>\n  .readFileSync(msgPath, <span class=\"hljs-string\">'utf-8'<\/span>)<br \/>\n  .trim()<\/p>\n<p><span class=\"hljs-keyword\">const<\/span> commitRE = <span class=\"hljs-regexp\">\/^(revert: )?(feat|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build)((.+))?: .{1,50}\/<\/span><\/p>\n<p><span class=\"hljs-keyword\">if<\/span> (!commitRE.test(msg)) {<br \/>\n  <span class=\"hljs-built_in\">console<\/span>.log()<br \/>\n  <span class=\"hljs-built_in\">console<\/span>.error(<br \/>\n    <span class=\"hljs-string\">`  <span class=\"hljs-subst\">${chalk.bgRed.white(' ERROR ')}<\/span> <span class=\"hljs-subst\">${chalk.red(<br \/>\n      `invalid commit message format.`<br \/>\n    )}<\/span>nn`<\/span> +<br \/>\n      chalk.red(<br \/>\n        <span class=\"hljs-string\">`  Proper commit message format is required for automated changelog generation. Examples:nn`<\/span><br \/>\n      ) +<br \/>\n      <span class=\"hljs-string\">`    <span class=\"hljs-subst\">${chalk.green(`feat(compiler): add 'comments' option`)}<\/span>n`<\/span> +<br \/>\n      <span class=\"hljs-string\">`    <span class=\"hljs-subst\">${chalk.green(<br \/>\n        `fix(v-model): handle events on blur (close #28)`<br \/>\n      )}<\/span>nn`<\/span> +<br \/>\n      chalk.red(<br \/>\n        <span class=\"hljs-string\">`  You can also use <span class=\"hljs-subst\">${chalk.cyan(<br \/>\n          `git cz`<br \/>\n        )}<\/span> to interactively generate a commit message.n`<\/span><br \/>\n      )<br \/>\n  )<br \/>\n  process.exit(<span class=\"hljs-number\">1<\/span>)<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\u5927\u529f\u544a\u6210\uff0c\u6267\u884c\u4e00\u6b21 commit\uff0c\u53ef\u4ee5\u770b\u5230\u94a9\u5b50\u6267\u884c\u8fc7\u7a0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ainyi.com\/upload\/content\/129\/WechatIMG381.png\" alt=\"WechatIMG381.png\" \/><\/p>\n<\/div>\n<div class=\"content-footer\" data-v-15ffc2bc=\"\">\n<div class=\"footer-left\" data-v-15ffc2bc=\"\">\n<p data-v-15ffc2bc=\"\">\u672c\u6587\u7531\u00a0<a class=\"\" href=\"https:\/\/ainyi.com\/\" data-v-15ffc2bc=\"\">Krry<\/a>\u00a0\u521b\u4f5c\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u6e90<\/p>\n<p data-v-15ffc2bc=\"\">\u6700\u540e\u7f16\u8f91\u65f6\u95f4\uff1a2022-05-24 17:22:12<\/p>\n<\/div>\n<div class=\"footer-right\" data-v-15ffc2bc=\"\"><span class=\"\" tabindex=\"0\" aria-describedby=\"el-popper-1821\" data-v-15ffc2bc=\"\">\u4e0a\u4e00\u7bc7<\/span><span class=\"split\" data-v-15ffc2bc=\"\">|<\/span><span class=\"\" tabindex=\"0\" aria-describedby=\"el-popper-8872\" data-v-15ffc2bc=\"\">\u4e0b\u4e00\u7bc7<\/span><\/div>\n<div class=\"clear\" data-v-15ffc2bc=\"\"><\/div>\n<\/div>\n<div class=\"pay\" data-v-c56613ca=\"\" data-v-15ffc2bc=\"\">\n<div class=\"p-box\" data-v-c56613ca=\"\"><i class=\"alipay iconfont icon-alipay-fill\" data-v-c56613ca=\"\"><\/i><img decoding=\"async\" class=\"ali-img\" src=\"https:\/\/ainyi.com\/assets\/alipay.6fd4f164.jpg\" width=\"130\" data-v-c56613ca=\"\" \/><\/div>\n<div class=\"p-box\" data-v-c56613ca=\"\"><i class=\"wechat iconfont icon-wechat\" data-v-c56613ca=\"\"><\/i><img decoding=\"async\" class=\"we-img\" src=\"https:\/\/ainyi.com\/assets\/wechat.22db6a03.jpg\" width=\"130\" data-v-c56613ca=\"\" \/><\/div>\n<\/div>\n<aside id=\"directory\" data-v-15ffc2bc=\"\">\n<div class=\"cl-wrapper\"><\/div>\n<\/aside>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u00a0 \u77e5\u8bc6\u603b\u7ed32022-04-1<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-6424","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/6424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=6424"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/6424\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=6424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=6424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=6424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}