|
21 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | 22 | * SOFTWARE. |
23 | 23 | */ |
| 24 | + |
| 25 | +const { execSync } = require('child_process') |
| 26 | +const fs = require('fs') |
| 27 | +const path = require('path') |
| 28 | + |
| 29 | +const ignoredPackages = ['__docs__', '__examples__'] |
| 30 | + |
| 31 | +function getChangedPackages() { |
| 32 | + try { |
| 33 | + const output = execSync('git diff --cached --name-only', { |
| 34 | + encoding: 'utf-8' |
| 35 | + }) |
| 36 | + return Array.from( |
| 37 | + new Set( |
| 38 | + output |
| 39 | + .split('\n') |
| 40 | + .filter((line) => line.startsWith('packages/')) |
| 41 | + .map((line) => line.split('/')[1]) |
| 42 | + .filter((pkg) => !ignoredPackages.includes(pkg)) |
| 43 | + ) |
| 44 | + ) |
| 45 | + } catch (error) { |
| 46 | + console.error(error.message) |
| 47 | + return [] |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function getAllPackages() { |
| 52 | + try { |
| 53 | + const packagesDir = path.resolve('packages') |
| 54 | + return fs |
| 55 | + .readdirSync(packagesDir) |
| 56 | + .filter( |
| 57 | + (pkg) => |
| 58 | + fs.statSync(path.join(packagesDir, pkg)).isDirectory() && |
| 59 | + !ignoredPackages.includes(pkg) |
| 60 | + ) |
| 61 | + } catch (error) { |
| 62 | + console.error(error.message) |
| 63 | + return [] |
| 64 | + } |
| 65 | +} |
| 66 | + |
24 | 67 | module.exports = { |
25 | 68 | extends: ['@commitlint/config-conventional'], |
26 | 69 | parserOpts: { |
27 | 70 | headerPattern: /^(\w*)\((\w*)\)-(\w*)\s(.*)$/, |
28 | 71 | headerCorrespondence: ['type', 'scope', 'subject'] |
29 | 72 | }, |
30 | | - // see https://commitlint.js.org/#/reference-rules |
| 73 | + // https://commitlint.js.org/reference/rules.html |
31 | 74 | rules: { |
32 | | - 'type-enum': [ |
33 | | - 2, |
34 | | - 'always', |
35 | | - [ |
36 | | - 'WIP', |
37 | | - 'feat', |
38 | | - 'fix', |
39 | | - 'docs', |
40 | | - 'chore', |
41 | | - 'style', |
42 | | - 'refactor', |
43 | | - 'test', |
44 | | - 'perf', |
45 | | - 'revert' |
46 | | - ] |
47 | | - ], |
48 | | - 'type-case': [0], |
49 | | - 'header-max-length': [0, 'always', 150] // commit message first field (subject) length |
| 75 | + 'header-max-length': [2, 'always', 150] |
| 76 | + }, |
| 77 | + |
| 78 | + // https://cz-git.qbb.sh/config/ |
| 79 | + prompt: { |
| 80 | + enableMultipleScopes: true, |
| 81 | + scopeEnumSeparator: ',', |
| 82 | + scopes: getAllPackages(), |
| 83 | + defaultScope: getChangedPackages(), |
| 84 | + skipQuestions: ['footerPrefix', 'confirmCommit'], |
| 85 | + // If more than 3 packages are selected display 'many', e.g. `refactor(many): some message` |
| 86 | + formatMessageCB: ({ scope, defaultMessage }) => |
| 87 | + scope.split(',').length > 3 |
| 88 | + ? defaultMessage.replace(scope, 'many') |
| 89 | + : defaultMessage |
50 | 90 | } |
51 | 91 | } |
0 commit comments