.cz-config.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. const chalk = require('chalk');
  6. const spawn = require('cross-spawn');
  7. const defaultConfig = require('cz-customizable');
  8. const { getChangedPackages } = require('./utils');
  9. const typesConfig = [
  10. { value: 'feat', name: 'A new feature' },
  11. { value: 'fix', name: 'A bug fix' },
  12. { value: 'docs', name: 'Documentation only changes' },
  13. {
  14. value: 'style',
  15. name: 'Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)',
  16. },
  17. {
  18. value: 'refactor',
  19. name: 'A code change that neither fixes a bug nor adds a feature',
  20. },
  21. {
  22. value: 'perf',
  23. name: 'A code change that improves performance',
  24. },
  25. { value: 'test', name: 'Adding missing tests' },
  26. {
  27. value: 'chore',
  28. name: 'Changes to the build process or auxiliary tools',
  29. },
  30. {
  31. value: 'build',
  32. name: 'Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)',
  33. },
  34. {
  35. value: 'ci',
  36. name: 'Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)',
  37. },
  38. {
  39. value: 'revert',
  40. name: 'Reverts a previous commit',
  41. },
  42. ];
  43. const { stdout = '' } = spawn.sync(`git diff --staged --name-only`, {
  44. shell: true,
  45. encoding: 'utf8',
  46. stdio: 'pipe',
  47. });
  48. const changedFiles = stdout.split('\n').filter(Boolean);
  49. const changeSet = getChangedPackages(changedFiles);
  50. if (changeSet.size > 1) {
  51. process.stderr.write(
  52. `${[
  53. chalk.yellow(
  54. `Multiple packages detected in current commit, please consider splitting into smaller commits`,
  55. ),
  56. ].join('\n')}\n`,
  57. );
  58. changeSet.clear();
  59. changeSet.add('multiple');
  60. }
  61. const changedScopes = [...changeSet];
  62. module.exports = {
  63. ...defaultConfig,
  64. types: typesConfig.map(({ value, name }) => {
  65. return {
  66. name: `${value}:${new Array(10 - value.length)
  67. .fill(' ')
  68. .join('')}${name}`,
  69. value,
  70. };
  71. }),
  72. messages: {
  73. ...defaultConfig.messages,
  74. type: "Select the type of change that you're committing",
  75. scope: 'Ensure the scope of this change',
  76. subject: 'Write a short, imperative tense description of the change',
  77. body: 'Provide a longer description of the change. Use "|" to break new line:\n',
  78. breaking: 'List any BREAKING CHANGES (optional):\n',
  79. confirmCommit: 'Are you sure you want to proceed with the commit above?',
  80. },
  81. scopes: changedScopes.join(','),
  82. allowCustomScopes: false,
  83. skipQuestions: ['customScope', 'footer', 'body'],
  84. allowBreakingChanges: ['feat', 'fix'],
  85. };