Skip to content
On this page

scopes

scopes,通常来定义本次 commit 涉及范围,一般有两种:根据项目代码层面区分比如 monorepo ,另外一种就是项目业务区分

针对 项目代码 的 scopes

如果你需要管理多软件包在commit时获得更好的体验,比如使用: pnpm | lerna.js 管理 monorepo 可以利用 pathfs 模块动态定义 commit message 中的scopes(范围)显示

js
// .commitlintrc.js
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: [...packages]
  }
}
// .commitlintrc.js
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: [...packages]
  }
}

当然如果你使用 commitlint 规则定义了 scope-enum,会自动引入

js
// .commitlintrc.js
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  rules: {
    'scope-enum': [2, 'always', [...packages]]
  }
}
// .commitlintrc.js
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  rules: {
    'scope-enum': [2, 'always', [...packages]]
  }
}

demo-gif

支持多选 scopes

  • 使用 空格 选中
  • 使用 回车 确定
js
// .commitlintrc.js 
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: { 
    scopes: [...packages],
    enableMultipleScopes: true,
    scopeEnumSeparator: "," 
  }
}
// .commitlintrc.js 
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: { 
    scopes: [...packages],
    enableMultipleScopes: true,
    scopeEnumSeparator: "," 
  }
}

demo-gif



针对 项目业务 的 scopes

js
// .commitlintrc.js
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: ['app', 'home', 'account', 'comment']
  }
}
// .commitlintrc.js
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: ['app', 'home', 'account', 'comment']
  }
}

当然如果你想给模块范围自定义添加 描述信息 显示在命令行中可以使用 namevalue属性来定义

js
// .commitlintrc.js
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: [
      { value: 'app', name: 'app:       系统业务' },
      { value: 'home', name: 'home:      首页相关' },
      { value: 'account', name: 'account:   账户相关' },
      { value: 'comment', name: 'comment:   评论相关' },
    ]
  }
}
// .commitlintrc.js
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: [
      { value: 'app', name: 'app:       系统业务' },
      { value: 'home', name: 'home:      首页相关' },
      { value: 'account', name: 'account:   账户相关' },
      { value: 'comment', name: 'comment:   评论相关' },
    ]
  }
}

demo-gif

TIP

如果 cz-git 检测到如果 allowEmptyScopesallowCustomScopes 具有非常严格规则(都设置为false)并且 scopes 选择列表仅有一项时,会自动跳过问题并输出

TIP

下面代码可以运行时获取到 HOME 目录,
你可以使用它配合 fspath 进行默认的全局自定义配置

js
const USER_HOME = process.env.HOME || process.env.USERPROFILE
// console.log(USER_HOME) === echo "$HOME"
const USER_HOME = process.env.HOME || process.env.USERPROFILE
// console.log(USER_HOME) === echo "$HOME"



利用可高度可定制的 cz-git 让 commit 更方便,更契合习惯,欢迎分享。

I just try my best to make thing well, Could you give a star ⭐