1 | 'use strict'
|
2 |
|
3 | const debug = require('debug')('elint:main')
|
4 | const co = require('co')
|
5 | const _ = require('lodash')
|
6 | const walker = require('./walker')
|
7 | const report = require('./utils/report')
|
8 | const isGitHooks = require('./utils/is-git-hooks')
|
9 | const eslint = require('./workers/eslint')
|
10 | const stylelint = require('./workers/stylelint')
|
11 | const notifier = require('./notifier')
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function elint (files, options) {
|
28 | co(function * () {
|
29 | const fileList = yield walker(files, options)
|
30 |
|
31 |
|
32 | if (!fileList.es.length && !fileList.style.length) {
|
33 | process.exit()
|
34 | }
|
35 |
|
36 |
|
37 | const linters = {
|
38 | es: eslint,
|
39 | style: stylelint
|
40 | }
|
41 |
|
42 |
|
43 | const isGit = yield isGitHooks()
|
44 |
|
45 | if (isGit) {
|
46 | options.fix = false
|
47 | }
|
48 | if (options.forceFix) {
|
49 | options.fix = true
|
50 | }
|
51 |
|
52 | debug('parsed options: %o', options)
|
53 |
|
54 | const { type } = options
|
55 | const argus = JSON.stringify(options)
|
56 | let workers = []
|
57 |
|
58 | if (type) {
|
59 |
|
60 | workers.push(linters[type](argus, ...fileList[type]))
|
61 | } else {
|
62 | |
63 |
|
64 |
|
65 |
|
66 | _.toPairs(linters).forEach(([linterType, linter]) => {
|
67 | if (!fileList[linterType].length) {
|
68 | return
|
69 | }
|
70 |
|
71 | workers.push(linter(argus, ...fileList[linterType]))
|
72 | })
|
73 | }
|
74 |
|
75 |
|
76 | workers.push(notifier.notify())
|
77 |
|
78 | Promise.all(workers).then(results => {
|
79 | const notifierResult = results.pop()
|
80 | const lintResults = results
|
81 |
|
82 | const outputs = []
|
83 | let success = true
|
84 |
|
85 | lintResults.forEach(result => {
|
86 | const output = JSON.parse(result.stdout)
|
87 | outputs.push(output)
|
88 | success = success && output.success
|
89 | })
|
90 |
|
91 | console.log(report(outputs))
|
92 |
|
93 | if (notifierResult) {
|
94 | console.log(notifierResult)
|
95 | }
|
96 |
|
97 | process.exit(success ? 0 : 1)
|
98 | })
|
99 | })
|
100 | }
|
101 |
|
102 | module.exports = elint
|