{"id":1373,"date":"2024-11-10T11:49:54","date_gmt":"2024-11-10T03:49:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1373\/"},"modified":"2024-11-10T11:49:54","modified_gmt":"2024-11-10T03:49:54","slug":"%e4%bd%bf%e7%94%a8-husky%e3%80%81commitlint%e3%80%81prettier-%e5%92%8c-lint-staging-%e5%a2%9e%e5%bc%ba%e6%82%a8%e7%9a%84%e5%bc%80%e5%8f%91%e5%b7%a5%e4%bd%9c%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1373\/","title":{"rendered":"\u4f7f\u7528 Husky\u3001Commitlint\u3001Prettier \u548c Lint-Staging \u589e\u5f3a\u60a8\u7684\u5f00\u53d1\u5de5\u4f5c\u6d41\u7a0b"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173038274817756.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 Husky\u3001Commitlint\u3001Prettier \u548c Lint-Staging \u589e\u5f3a\u60a8\u7684\u5f00\u53d1\u5de5\u4f5c\u6d41\u7a0b\u63d2\u56fe\" alt=\"\u4f7f\u7528 Husky\u3001Commitlint\u3001Prettier \u548c Lint-Staging \u589e\u5f3a\u60a8\u7684\u5f00\u53d1\u5de5\u4f5c\u6d41\u7a0b\u63d2\u56fe\" \/><\/p>\n<p>\u8bbe\u7f6e\u81ea\u52a8\u5316\u5de5\u4f5c\u6d41\u7a0b\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u9879\u76ee\u4e2d\u7684\u4ee3\u7801\u8d28\u91cf\u548c\u4e00\u81f4\u6027\u3002\u5728\u672c\u6307\u5357\u4e2d\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u8bbe\u7f6e husky\u3001commitlint\u3001prettier \u548c lint-staging\uff0c\u4ee5\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u5e93\u683c\u5f0f\u4e00\u81f4\u3001\u9075\u5faa\u63d0\u4ea4\u6d88\u606f\u7ea6\u5b9a\uff0c\u5e76\u5728\u6bcf\u6b21\u5408\u5e76\u540e\u5177\u6709\u6700\u65b0\u7684\u4f9d\u8d56\u9879\u3002<\/p>\n<h2> \u8bbe\u7f6e\u54c8\u58eb\u5947 <\/h2>\n<p>husky \u5e2e\u52a9\u60a8\u8f7b\u677e\u7ba1\u7406  \u6302\u94a9\uff0c\u5141\u8bb8\u5728\u6bcf\u6b21\u63d0\u4ea4\u4e4b\u524d\u8fd0\u884c\u4ee3\u7801\u8d28\u91cf\u68c0\u67e5\u7b49\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/p>\n<h3> \u5b89\u88c5 <\/h3>\n<p>\u4f7f\u7528 npm \u5c06 husky \u5b89\u88c5\u4e3a\u5f00\u53d1\u4f9d\u8d56\u9879\uff08\u6211\u4eec\u5c06\u5728\u672c\u6587\u4e2d\u4f7f\u7528 npm\uff09\uff1a<\/p>\n<pre>npm install --save-dev husky\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u521d\u59cb\u5316 <\/h3>\n<p>\u8981\u521b\u5efa\u7528\u4e8e\u5b58\u50a8 git \u6302\u94a9\u7684 .husky \u76ee\u5f55\uff0c\u8bf7\u8fd0\u884c\uff1a<\/p>\n<pre>npx husky init\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728 package.json \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u811a\u672c\u4ee5\u5728\u5b89\u88c5\u4f9d\u8d56\u9879\u65f6\u8bbe\u7f6e husky\uff1a<\/p>\n<pre>\"scripts\": {\n  \"prepare\": \"husky install\"\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u914d\u7f6e commitlint <\/h2>\n<p>commitlint \u786e\u4fdd\u6240\u6709\u63d0\u4ea4\u6d88\u606f\u9075\u5faa\u4e00\u81f4\u7684\u683c\u5f0f\uff0c\u7ef4\u62a4\u5e72\u51c0\u7684\u63d0\u4ea4\u5386\u53f2\u8bb0\u5f55\u3002<\/p>\n<h3> \u5b89\u88c5 <\/h3>\n<p>\u5b89\u88c5 commitlint \u4ee5\u53ca\u5e38\u89c4\u914d\u7f6e\uff1a<\/p>\n<pre>npm install --save-dev @commitlint\/config-conventional @commitlint\/cli\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u8bbe\u7f6e <\/h3>\n<ol>\n<li>\u5728 .husky \u4e2d\u521b\u5efa\u4e00\u4e2a commit-msg \u94a9\u5b50\uff1a \u73b0\u5728\u5728 .husky \u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a commit-msg \u7684\u65b0\u6587\u4ef6\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a <\/li>\n<\/ol>\n<pre>npx husky add .husky\/commit-msg \"npx --no-install commitlint --edit \"$1\"\"\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u5c06 commitlint.config.js \u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u7684\u6839\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a <\/li>\n<\/ol>\n<pre>module.exports = {\n  extends: ['@commitlint\/config-conventional'],\n  rules: {\n    \/\/   todo add scope enum here\n    \/\/ 'scope-enum': [2, 'always', ['yourscope', 'yourscope']],\n    'type-enum': [\n      2,\n      'always',\n      [\n        'feat',\n        'fix',\n        'docs',\n        'chore',\n        'style',\n        'refactor',\n        'ci',\n        'test',\n        'revert',\n        'perf',\n        'vercel',\n      ],\n    ],\n  },\n};\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u6dfb\u52a0 lint-staging \u548c prettier <\/h2>\n<p>lint-staging \u5141\u8bb8\u60a8\u5728\u6682\u5b58\u6587\u4ef6\u4e0a\u8fd0\u884c\u811a\u672c\uff0c\u800c prettier \u5728\u60a8\u7684\u4ee3\u7801\u5e93\u4e2d\u5f3a\u5236\u6267\u884c\u4e00\u81f4\u7684\u6837\u5f0f\u3002<\/p>\n<h3> \u5b89\u88c5 <\/h3>\n<p>\u5c06\u4e24\u8005\u5b89\u88c5\u4e3a\u5f00\u53d1\u4f9d\u8d56\u9879\uff1a<\/p>\n<pre>npm install --save-dev lint-staged prettier\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u66f4\u6f02\u4eae\u7684\u914d\u7f6e <\/h3>\n<p>\u4f7f\u7528\u60a8\u9996\u9009\u7684\u914d\u7f6e\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a .prettierrc.json \u6587\u4ef6\u3002\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>{\n  \"plugins\": [\"prettier-plugin-tailwindcss\"],\n  \"printwidth\": 120,\n  \"usetabs\": false,\n  \"tabwidth\": 2,\n  \"trailingcomma\": \"es5\", \n  \"semi\": true, \n  \"singlequote\": true,\n  \"bracketspacing\": true, \n  \"arrowparens\": \"always\",\n  \"jsxsinglequote\": false, \n  \"bracketsameline\": false,\n  \"endofline\": \"lf\"\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> lint \u9636\u6bb5\u914d\u7f6e <\/h3>\n<p>\u5c06\u4ee5\u4e0b\u914d\u7f6e\u6dfb\u52a0\u5230 lint-staged \u4e0b\u7684 package.json \u4e2d\uff1a<\/p>\n<pre>\"lint-staged\": {\n    \"**\/*.{js,jsx,ts,tsx}\": [\n      \"eslint --max-warnings=0\",\n      \"prettier --write\"\n    ],\n    \"**\/*.{html,json,css,scss,md,mdx}\": [\n      \"prettier -w\"\n    ]\n  }\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6dfb\u52a0\u9884\u63d0\u4ea4\u6302\u94a9\u6765\u8fd0\u884c lint-staging\uff1a<\/p>\n<pre>npx husky add .husky\/pre-commit \"npx lint-staged\"\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u4e3a\u4f9d\u8d56\u9879\u6dfb\u52a0\u5408\u5e76\u540e\u6302\u94a9 <\/h2>\n<p>\u5408\u5e76\u540e\u6302\u94a9\u53ef\u786e\u4fdd\u6bcf\u6b21\u5408\u5e76\u540e\u901a\u8fc7\u8fd0\u884c npm install \u6216\u4efb\u4f55\u5305\u7ba1\u7406\u5668\u66f4\u65b0\u60a8\u7684\u4f9d\u8d56\u9879\u3002<\/p>\n<p>\u521b\u5efa\u5408\u5e76\u540e\u94a9\u5b50\uff1a<\/p>\n<pre>npx husky add .husky\/post-merge \"npm install\"\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u901a\u8fc7\u6b64\u8bbe\u7f6e\uff0c\u60a8\u7684\u9879\u76ee\u5c06\u7ef4\u62a4\u6807\u51c6\u5316\u7684\u63d0\u4ea4\u6d88\u606f\u683c\u5f0f\uff0c\u81ea\u52a8\u683c\u5f0f\u5316\u4ee3\u7801\uff0c\u5e76\u5728\u5408\u5e76\u540e\u4fdd\u6301\u4f9d\u8d56\u9879\u6700\u65b0\u3002\u8fd9\u79cd\u5f3a\u5927\u7684\u5de5\u4f5c\u6d41\u7a0b\u5c06\u7b80\u5316\u534f\u4f5c\u5e76\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\uff0c\u5e2e\u52a9\u60a8\u4e13\u6ce8\u4e8e\u6784\u5efa\u51fa\u8272\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 Husky\u3001Commitlint\u3001Prettier \u548c Lint-Staging \u589e\u5f3a\u60a8\u7684\u5f00\u53d1\u5de5\u4f5c\u6d41\u7a0b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bbe\u7f6e\u81ea\u52a8\u5316\u5de5\u4f5c\u6d41\u7a0b\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u9879\u76ee\u4e2d\u7684\u4ee3\u7801\u8d28\u91cf\u548c\u4e00\u81f4\u6027\u3002\u5728\u672c\u6307\u5357\u4e2d\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u8bbe\u7f6e husky\u3001commitlint\u3001prettier \u548c lint-staging\uff0c\u4ee5\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u5e93\u683c\u5f0f\u4e00\u81f4\u3001\u9075\u5faa\u63d0\u4ea4\u6d88\u606f\u7ea6\u5b9a\uff0c\u5e76\u5728\u6bcf\u6b21\u5408\u5e76\u540e\u5177\u6709\u6700\u65b0\u7684\u4f9d\u8d56\u9879\u3002 \u8bbe\u7f6e\u54c8\u58eb\u5947 husky \u5e2e\u52a9\u60a8\u8f7b\u677e\u7ba1\u7406 \u6302\u94a9\uff0c\u5141\u8bb8\u5728\u6bcf\u6b21\u63d0\u4ea4\u4e4b\u524d\u8fd0\u884c\u4ee3\u7801\u8d28\u91cf\u68c0\u67e5\u7b49\u81ea\u52a8\u5316\u4efb\u52a1\u3002 \u5b89\u88c5 \u4f7f\u7528 npm \u5c06 husky \u5b89\u88c5\u4e3a\u5f00\u53d1\u4f9d\u8d56\u9879\uff08\u6211\u4eec\u5c06\u5728\u672c\u6587\u4e2d\u4f7f\u7528 npm\uff09\uff1a npm install &#8211;save-dev husky \u767b\u5f55\u540e\u590d\u5236 \u521d\u59cb\u5316 \u8981\u521b\u5efa\u7528\u4e8e\u5b58\u50a8 git \u6302\u94a9\u7684 .husky \u76ee\u5f55\uff0c\u8bf7\u8fd0\u884c\uff1a npx husky init \u767b\u5f55\u540e\u590d\u5236 \u63a5\u4e0b\u6765\uff0c\u5728 package.json \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u811a\u672c\u4ee5\u5728\u5b89\u88c5\u4f9d\u8d56\u9879\u65f6\u8bbe\u7f6e husky\uff1a &#8220;scripts&#8221;: { &#8220;prepare&#8221;: &#8220;husky install&#8221; } \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6e commitlint commitlint \u786e\u4fdd\u6240\u6709\u63d0\u4ea4\u6d88\u606f\u9075\u5faa\u4e00\u81f4\u7684\u683c\u5f0f\uff0c\u7ef4\u62a4\u5e72\u51c0\u7684\u63d0\u4ea4\u5386\u53f2\u8bb0\u5f55\u3002 \u5b89\u88c5 \u5b89\u88c5 commitlint \u4ee5\u53ca\u5e38\u89c4\u914d\u7f6e\uff1a npm install &#8211;save-dev @commitlint\/config-conventional @commitlint\/cli \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1373","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1373"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1373\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}