1 | 'use strict'
|
2 |
|
3 | const debug = require('debug')('elint:workers:commitlint')
|
4 | const co = require('co')
|
5 | const fs = require('fs-extra')
|
6 | const path = require('path')
|
7 | const { format, load, lint, read } = require('@commitlint/core')
|
8 | const log = require('../utils/log')
|
9 | const { getBaseDir } = require('../env')
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function commitlint () {
|
17 | const baseDir = getBaseDir()
|
18 |
|
19 | co(function * () {
|
20 | const readOptions = {
|
21 | cwd: baseDir,
|
22 | edit: '.git/COMMIT_EDITMSG'
|
23 | }
|
24 |
|
25 | debug('commitlint.read options: %o', readOptions)
|
26 |
|
27 | const gitMsgFilePath = path.join(readOptions.cwd, readOptions.edit)
|
28 |
|
29 | if (!fs.existsSync(gitMsgFilePath)) {
|
30 | debug(`can not found "${gitMsgFilePath}"`)
|
31 | throw new Error('无法读取 git commit 信息')
|
32 | }
|
33 |
|
34 | const [message, config] = yield Promise.all([
|
35 | read(readOptions),
|
36 | load()
|
37 | ])
|
38 |
|
39 | debug('git commit message: %o', message)
|
40 | debug('commitlint config: %o', config)
|
41 |
|
42 | const rules = config.rules
|
43 | const options = config.parserPreset ? { parserOpts: config.parserPreset.parserOpts } : {}
|
44 | const report = yield lint(message[0], rules, options)
|
45 | const formatted = format(report)
|
46 |
|
47 | console.log()
|
48 | console.log(formatted.join('\n'))
|
49 | console.log()
|
50 |
|
51 | process.exit(report.errors.length ? 1 : 0)
|
52 | }).catch(error => {
|
53 | log.error(error.message)
|
54 | process.exit(1)
|
55 | })
|
56 | }
|
57 |
|
58 | module.exports = commitlint
|