Skip to content

Commit

Permalink
chore: move commitizen config to commitlint; remove cz-lerna-changelo…
Browse files Browse the repository at this point in the history
…g pkg; skip commit hook on rebase and amend
  • Loading branch information
balzss committed Dec 19, 2024
1 parent 47f6eb8 commit 890bb9e
Show file tree
Hide file tree
Showing 21 changed files with 419 additions and 1,817 deletions.
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npm run lint:commit
npx --no -- commitlint --edit $1
1 change: 0 additions & 1 deletion .husky/pre-push

This file was deleted.

17 changes: 16 additions & 1 deletion .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
exec < /dev/tty && npx cz --hook || true
# Don't run this hook on --amend or rebase
# From https://github.com/commitizen/cz-cli/issues/672#issuecomment-1328123802
#
# Explanation:
# Per the [prepare-commit-msg hook docs](https://git-scm.com/docs/githooks#_prepare_commit_msg),
# the second argument can have these values:
# - Empty: No message supplied
# - "message": Message was supplied via -m or -F
# - "template": Template was supplied via -t or via commit.template git config
# - "merge": Commit is a merge commit or .git/MERGE_MSG file exists
# - "squash": .git/SQUASH_MSG file exists
# - "commit": -c, -C, or --amend was supplied. In this case, the script gets a third argument representing the commit object.

if [ -z "$2" ]; then
exec < /dev/tty && npx cz --hook
fi
78 changes: 59 additions & 19 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,71 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const { execSync } = require('child_process')
const fs = require('fs')
const path = require('path')

const ignoredPackages = ['__docs__', '__examples__']

function getChangedPackages() {
try {
const output = execSync('git diff --cached --name-only', {
encoding: 'utf-8'
})
return Array.from(
new Set(
output
.split('\n')
.filter((line) => line.startsWith('packages/'))
.map((line) => line.split('/')[1])
.filter((pkg) => !ignoredPackages.includes(pkg))
)
)
} catch (error) {
console.error(error.message)
return []
}
}

function getAllPackages() {
try {
const packagesDir = path.resolve('packages')
return fs
.readdirSync(packagesDir)
.filter(
(pkg) =>
fs.statSync(path.join(packagesDir, pkg)).isDirectory() &&
!ignoredPackages.includes(pkg)
)
} catch (error) {
console.error(error.message)
return []
}
}

module.exports = {
extends: ['@commitlint/config-conventional'],
parserOpts: {
headerPattern: /^(\w*)\((\w*)\)-(\w*)\s(.*)$/,
headerCorrespondence: ['type', 'scope', 'subject']
},
// see https://commitlint.js.org/#/reference-rules
// https://commitlint.js.org/reference/rules.html
rules: {
'type-enum': [
2,
'always',
[
'WIP',
'feat',
'fix',
'docs',
'chore',
'style',
'refactor',
'test',
'perf',
'revert'
]
],
'type-case': [0],
'header-max-length': [0, 'always', 150] // commit message first field (subject) length
'header-max-length': [2, 'always', 150]
},

// https://cz-git.qbb.sh/config/
prompt: {
enableMultipleScopes: true,
scopeEnumSeparator: ',',
scopes: getAllPackages(),
defaultScope: getChangedPackages(),
skipQuestions: ['footerPrefix', 'confirmCommit'],
// If more than 3 packages are selected display 'many', e.g. `refactor(many): some message`
formatMessageCB: ({ scope, defaultMessage }) =>
scope.split(',').length > 3
? defaultMessage.replace(scope, 'many')
: defaultMessage
}
}
Loading

0 comments on commit 890bb9e

Please sign in to comment.