Skip to content
On this page

scopes

scopes, usually to define the scope of this commit, there are generally two types: according to the project code distinction such as monorepo , the other is project business distinction

scopes for project code

If you need to manage multiple packages for a better experience, for example use: pnpm | lerna.js to manage monorepo you can Use the path and fs modules to dynamically define the scopes (scopes) display in the commit message.

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]
  }
}

If you define a scope-enum using the commitlint rule, it will be imported automatically.

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

support for multiple scopes

  • use or Space to choice
  • use Enter to submit
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 for business system

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']
  }
}

Of course, if you want to add description information to the module-wide customization to display on the command line, you can use name and value to define.

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:       System business' },
      { value: 'home', name: 'home:      Homepage' },
      { value: 'account', name: 'account:   Account related' },
      { value: 'comment', name: 'comment:   Comment related' },
    ]
  }
}
// .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:       System business' },
      { value: 'home', name: 'home:      Homepage' },
      { value: 'account', name: 'account:   Account related' },
      { value: 'comment', name: 'comment:   Comment related' },
    ]
  }
}

demo-gif

TIP

If cz-git detects that allowEmptyScopes and allowCustomScopes have very strict rules (both set to false) and the scopes selection list has only one item, it will automatically skip question and output

TIP

The following code can get the HOME directory at runtime,
you can use it with fs and path for default global custom configuration.

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"

[Advanced] cache your custom scope




Using highly customizable cz-git makes committing more convenient and more customary. Welcome to share.

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