UNPKG

1.43 kBJavaScriptView Raw
1'use strict'
2
3const debug = require('debug')('elint:workers:commitlint')
4const co = require('co')
5const fs = require('fs-extra')
6const path = require('path')
7const { format, load, lint, read } = require('@commitlint/core')
8const log = require('../utils/log')
9const { getBaseDir } = require('../env')
10
11/**
12 * run commitlint
13 *
14 * @returns {void}
15 */
16function 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
58module.exports = commitlint