最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • lerna项目中集成husky、lint-staged、commitlint和cz-customizable

    正文概述 掘金(kebai)   2021-04-03   1651

    Monorepo 是针对单仓库、多 package 的流行解决方案, lerna 是它的一种实现。

    说明

    重要package版本说明:

    • "husky": "^6.0.0"
    • "lint-staged": "^10.5.4"
    • "@commitlint/cli": "^12.0.1"
    • "@commitlint/config-conventional": "^12.0.1"
    • "cz-customizable": "^6.3.0"

    配置husky

    在lerna项目根目录中安装husky:

    yarn add husky -D
    
    1. package.jsonscripts中添加"prepare": "husky install",并运行这条命令:
    npm set-script prepare "husky install" && npm run prepare
    
    1. 添加一个hook:
    npx husky add .husky/pre-commit "npm test"
    

    上面这个命令会在.husky目录下新建一个pre-commit文件,其内容如下:

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    npm test
    
    

    以上都是手动安装husky的过程,当然官方也提供了一键安装和配置脚本,推荐使用:

    npx husky-init && npm install       # npm
    npx husky-init && yarn              # Yarn 1
    yarn dlx husky-init --yarn2 && yarn # Yarn 2
    

    如果使用的是v4版本的husky,想升级到v6,可以使用以下命名,一键迁移:

    // npm
    npm install husky@6 --save-dev \
      && npx husky-init \
      && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config
      
    // yarn 1
    yarn add husky@6 --dev \
      && npx husky-init \
      && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config
    
    // yarn 2
    yarn add husky@6 --dev \
      && yarn dlx husky-init --yarn2 \
      && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config
    

    配置lint-staged

    lerna项目中,由于所有子项目公用一个 repo 源代码仓库,因此它的 husky 钩子只能建立在最顶层目录;

    而每次 commit 都很有可能是多个子项目都有改动,这个时候使用 lint-staged 时,就不但要区分文件类型,还要区分改动文件所在的子项目(因为不同的子项目可能会有不同的校验处理)。

    这时,我们可以使用 lerna 命令来实现对“哪个子项目有修改”的判断;而 lint-staged 就需要安装在任何一个需要做校验的子项目中。

    1. 添加或修改.husky目录下的pre-commit钩子如下:
    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    lerna run --concurrency 1 --stream precommit --since HEAD --exclude-dependents
    
    

    其中,precommit 是在pre-commit钩子中触发的子项目的命令

    1. 在子项目中安装和配置lint-staged,并添加precommit命令
    • 安装lint-staged
    lerna add lint-staged --scope=xxxx -D
    
    • 在添加precommit命令:
    "precommit": "lint-staged"
    
    • 配置lint-staged
    "lint-staged": {
      "*.{ts,tsx,js,jsx}": [
        "eslint"
      ]
    },
    

    配置commitlint和cz-customizable

    每个团队对提交的commit message格式有约定俗称的要求,但是没有一个统一的规范,导致大家提交的commit message或多或少不太一样。因此,需要一个工具来帮助大家统一commit message的格式,也方便后续的分析和拓展。

    cz-customizable是一个帮助书写commit message的工具,而commitlint是一个校验commit message的工具。

    1. 安装commitlintcz-customizable:
    yarn add @commitlint/cli @commitlint/config-conventional cz-customizable -D
    
    1. package.json中添加以下配置:
    {
      ...
      "config": {
        "commitizen": {
          "path": "./node_modules/cz-customizable"
        },
        "cz-customizable": {
          "config": "./.cz-config.js"
        }
      },
      ...
    }
    
    1. 在项目根目录中新建.cz-config.js文件,内容如下:
    module.exports = {
      types: [
        { value: 'feat', name: 'feat:     A new feature' },
        { value: 'fix', name: 'fix:      A bug fix' },
        {
          value: 'style',
          name:
            'style:    Changes that do not affect the meaning of the code\n            (white-space, formatting, missing semi-colons, etc)',
        },
        {
          value: 'refactor',
          name:
            'refactor: A code change that neither fixes a bug nor adds a feature',
        },
        { value: 'revert', name: 'revert:   Revert to a commit' },
        {
          value: 'chore',
          name:
            'chore:    Changes to the build process or auxiliary tools\n            and libraries such as documentation generation',
        },
        { value: 'docs', name: 'docs:     Documentation only changes' },
        {
          value: 'perf',
          name: 'perf:     A code change that improves performance',
        },
        { value: 'test', name: 'test:     Adding missing tests' },
      ],
    
      scopes: [
        { name: 'frontend' },
        { name: 'backend' },
        { name: 'service' },
      ],
    
      messages: {
        type: "Select the type of change that you're committing:",
        scope: "\n Select the scope of change that you're committing:",
        // used if allowCustomScopes is true
        customScope: 'Denote the custom scope:',
        subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
        body:
          'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
        breaking: 'List any BREAKING CHANGES (optional):\n',
        footer:
          'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
        confirmCommit: 'Are you sure you want to proceed with the commit above?',
      },
    
      allowCustomScopes: true,
    }
    
    
    1. 在项目根目录中新建.commitlintrc.js文件,内容如下:
    const typeEnum = require('./.cz-config');
    
    module.exports = {
      extends: ['@commitlint/config-conventional'],
      rules: {
        'type-enum': [2, 'always', typeEnum.types.map((i) => i.value)],
        'scope-enum': [2, 'always', typeEnum.scopes.map((i) => i.name)],
        'scope-empty': [2, 'never'],
      },
    };
    
    

    配置完成后,每次提交commit时,可以使用git cz替换git commit命令,从而辅助我们更加规范的书写commit message。

    总结

    以上就是我对如何在lerna项目中配置husky、lint-staged和Cz工具的一些粗略认知,当然不仅仅是lerna项目,也适用于任何前端项目。

    链接

    • husky官文文档

    • lint-staged官方文档

    • Cz工具集使用介绍


    起源地下载网 » lerna项目中集成husky、lint-staged、commitlint和cz-customizable

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元