git commit 自动修复 eslint 和 prettier

npm install lint-staged eslint prettier --save-dev


修改 package.json

"lint-staged": {
  "**/*.{js,jsx,ts,tsx,vue}": [
    "prettier --write",
    "eslint --fix"
  ],
  "**/*.json": [
    "prettier --write"
  ],
  "**/*.{less,md}": [
    "prettier --write"
  ]
},

npm install --save-dev husky
npx husky-init
npx husky add .husky/pre-commit "npx lint-staged"


package.json(参考)

{
  "name": "integrated-platform",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "rimraf dist && vite build",
    "preview": "vite preview",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
    "format": "prettier --write src/",
    "commit": "git-cz",
    "commitlint": "commitlint --config commitlint.config.cjs -e -V",
    "prepare": "husky install"
  },
  "dependencies": {
    "@element-plus/icons-vue": "^2.3.1",
    "@vueuse/core": "^11.0.3",
    "axios": "^1.7.7",
    "crypto-js": "^4.2.0",
    "element-plus": "^2.8.1",
    "js-cookie": "^3.0.5",
    "pinia": "^2.1.7",
    "pre-commit": "^1.2.2",
    "vue": "^3.4.29",
    "vue-router": "^4.3.3"
  },
  "devDependencies": {
    "@commitlint/cli": "^19.4.1",
    "@commitlint/config-conventional": "^19.4.1",
    "@iconify-json/ep": "^1.2.0",
    "@iconify-json/ic": "^1.2.0",
    "@rushstack/eslint-patch": "^1.8.0",
    "@vitejs/plugin-vue": "^5.0.5",
    "@vitejs/plugin-vue-jsx": "^4.0.0",
    "@vue/eslint-config-prettier": "^9.0.0",
    "autoprefixer": "^10.4.20",
    "commitizen": "^4.3.0",
    "cz-conventional-changelog": "^3.3.0",
    "cz-customizable": "^7.2.1",
    "eslint": "^8.57.0",
    "eslint-plugin-vue": "^9.23.0",
    "git-cz": "^4.9.0",
    "husky": "^8.0.0",
    "less": "^4.2.0",
    "lint-staged": "^15.2.10",
    "postcss": "^8.4.45",
    "prettier": "^3.2.5",
    "rimraf": "^6.0.1",
    "tailwindcss": "^3.4.10",
    "unplugin-auto-import": "^0.18.2",
    "unplugin-icons": "^0.19.3",
    "unplugin-vue-components": "^0.27.4",
    "vite": "^5.3.1"
  },
  "lint-staged": {
    "**/*.{js,jsx,ts,tsx,vue}": [
      "prettier --write",
      "eslint --fix"
    ],
    "**/*.json": [
      "prettier --write"
    ],
    "**/*.{less,md}": [
      "prettier --write"
    ]
  },
  "engines": {
    "node": "18.3.0"
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    },
    "cz-customizable": {
      "config": ".cz-config.cjs"
    }
  }
}

# .eslintrc.cjs(参考)
/* eslint-env node */
require('@rushstack/eslint-patch/modern-module-resolution')

module.exports = {
  root: true,

  // 解决 'process' is not defined | 'require' is not defined | 'module' is not defined
  env: {
    browser: true,
    es6: true,
    node: true // 添加 Node.js 环境
  },

  extends: [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/eslint-config-prettier/skip-formatting'
  ],

  parserOptions: {
    ecmaVersion: 'latest'
  },

  rules: {
    // 生产环境禁用 console 和 debugger,并给出提示
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',

    // 解决 setup 组件不能直接设置 name 属性导致的报错 Component name "xxx" should always be multi-word
    'vue/multi-word-component-names': 'off'
  }
}

mac 系统 husky 钩子不执行

git commit -m '1532'

hint: The '.husky/pre-commit' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.
hint: The '.husky/commit-msg' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.

[main af74cc4] 1532
 1 file changed, 1 insertion(+), 1 deletion(-)

husky 没有执行权限,项目终端输入,添加执行权限
chmod +x .husky/*


查看钩子的权限

 ls -l .husky/pre-commit

 // 只有读权限
 -rw-r--r--@ 1 user admin  74 Sep  7 20:24 .husky/pre-commit

 chmod +x .husky/pre-commit

 // 加了执行权限
 -rwxr-xr-x 1 user group 1234 May 15 14:00 .husky/pre-commit

你可能感兴趣的:(前端,前端)