开发规范

eslint

本项目配置有eslint规范,具体规则位于.eslintrc.cjs文件中,

  • 校验代码规范
    npm run eslint
  • 校验并修复
    npm run eslint:fix

prettier

本项目配置有prettier代码格式化规则,具体配置位于.prettierrc.cjs中。

  • 校验代码格式规范
    npm run prettier
  • 校验并修复格式规范
    npm run prettier:fix

git提交规范conventionalcommitsopen in new window

"types": [
    {
    "type": "feat",
    "section": "新功能[feat]"
    },
    {
    "type": "fix",
    "section": "Bug 修复[fix]"
    },
    {
    "type": "chore",
    "section": "其他[chore]"
    },
    {
    "type": "docs",
    "section": "文档更改[docs]"
    },
    {
    "type": "style",
    "section": "样式更改[style]"
    },
    {
    "type": "refactor",
    "section": "重构[refactor]"
    },
    {
    "type": "perf",
    "section": "性能改进[perf]"
    },
    {
    "type": "test",
    "section": "测试添加/更正[test]"
    },
    {
    "type": "revert",
    "section": "还原提交[revert]"
    },
    {
    "type": "ignore",
    "section": "需要忽略[ignore]",
    "hidden": true
    },
    {
    "type": "ci",
    "section": "CI发版[ci]"
    }
]

husky自动化

  1. 安装时不使用npm install直接使用npm run i ,或者初始化后在当前目录下运行npx husky install
  2. 或者在 package.json加入以下scripts再install (husky对应文档open in new window)
    //npm/pnpm
    {
         "scripts": {
             "prepare": "husky install"
         }
     }
    
    //yarn
     {
     "private": true, // ← your package is private, you only need postinstall
     "scripts": {
         "postinstall": "husky install"
     }
     }
    

会自动生成生成husky对应的git hookhusky会在git提交时进行以下操作:

  • 自动运行prettier --write进行代码格式化
  • 自动运行eslint --cache --fix进行eslint校验和修复,如果无法修复会报错并停止提交
  • 自动校验提交message是否符合conventionalcommits规范,如果不符合会报错并停止提交

husky 在linux/mac下不生效

husky目前有一个bug, windows下创建的hooks 当在linux/macos下拉取后即使执行了husky install也不能成功调用,需要单独执行下chmod ug+x .husky/* 赋予脚本执行权限。对应的issueopen in new window