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('node:fs')
const path = require('node:path')

const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: [...packages]
  }
}
// .commitlintrc.js
const fs = require('node:fs')
const path = require('node: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('node:fs')
const path = require('node:path')

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

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

demo-gif

Scopes for business system

js
// .commitlintrc.js
module.exports = {
  prompt: {
    scopes: ['app', 'home', 'account', 'comment']
  }
}
// .commitlintrc.js
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
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
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

Checkbox mode

  • 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

TIP

In checkbox mode, if defaultScope is passed as a string[], it will default-select and pin top the options whose values match those within the scopes range list.

Input mode

If you don't want to use selection mode, you want to use input mode.
Can use the custom scope input instead

js
module.exports = {
  messages: { customScope: 'What is the scope of this change:' },
  skipQuestions: ['scope'],
  defaultScope: '___CUSTOM___:'
}
module.exports = {
  messages: { customScope: 'What is the scope of this change:' },
  skipQuestions: ['scope'],
  defaultScope: '___CUSTOM___:'
}

TIP

If you want to get the completion effect in input mode, Same you can add ___CUSTOM___: to the prefix of content

js
module.exports = {
  messages: { customScope: 'What is the scope of this change:' },
  skipQuestions: ['scope'],
  defaultScope: '___CUSTOM___:Hello World'
}
module.exports = {
  messages: { customScope: 'What is the scope of this change:' },
  skipQuestions: ['scope'],
  defaultScope: '___CUSTOM___:Hello World'
}

[Advanced] cache your custom scope




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"



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 ⭐