/
index.js
131 lines (106 loc) · 3.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { h, render, Component, Color } = require('ink');
const loadMdFiles = require('../helper/file');
const lint = require('./lint');
const string = require('../helper/string');
const { getDescription } = require('../../lib');
/**
* Lint 组件
* @type {module.Lint}
*/
module.exports = class Lint extends Component {
constructor(props) {
super(props);
this.state = {
// 错误,格式为:{ path, file, errors: { start: { line, column }, end: { line, column } level, text, type } }
errorFiles: [],
// 当前检查文件数量
fileCount: 0,
};
}
componentDidMount() {
this.start();
}
async start() {
const { files, config, onExit } = this.props;
const mdFiles = await loadMdFiles(files, config);
for (const file of mdFiles) {
const errorFile = await lint(file, config);
let { errorFiles, fileCount } = this.state;
fileCount += 1;
if (errorFile && errorFile.length > 0) {
errorFiles = errorFiles.concat(errorFile);
}
await this.setStateAsync({
errorFiles,
fileCount,
});
}
const { error, warning } = this.errorCount();
// 是否出错
onExit(error === 0 ? 0 : 1);
}
setStateAsync(state) {
return new Promise((resolve, reject) => {
this.setState(state, () => {
resolve();
});
});
};
renderError(error) {
const { start, end, level, text, type } = error;
const props = level === 'error' ? { red: true } : { yellow: true };
const pos = `${start.line}:${start.column}-${end.line}:${end.column}`;
return h('div', {}, [
' ',
h('span', {}, h(Color, { grey: true }, string.rightPad(pos, 16))),
' ',
h('span', {}, h(Color, { grey: true }, string.rightPad(`${type}`, 24))),
' ',
h(Color, props, `${getDescription(type).message} ${text}`),
]);
}
renderFile(errorFile) {
const { path, file, errors } = errorFile;
return h('div', {}, [
h('div', {}, `${path}/${file}`), // 文件
...errors.map(error => this.renderError(error)), // 错误
]);
}
renderErrorFiles() {
const { errorFiles } = this.state;
return h('span', {}, errorFiles.map(errorFile => this.renderFile(errorFile)));
}
errorCount() {
const { errorFiles } = this.state;
const warningCnt = errorFiles.reduce((r, current) => {
return r + current.errors.filter(error => error.level === 'warning').length;
}, 0);
const errorCnt = errorFiles.reduce((r, current) => {
return r + current.errors.filter(error => error.level === 'error').length;
}, 0);
return {
error: errorCnt,
warning: warningCnt,
};
};
renderOverview() {
const { fileCount } = this.state;
const { error, warning } = this.errorCount();
return h('span', {}, [
h('span', {}, h(Color, { green: true }, `Lint total ${fileCount} files`)),
h('span', {}, h(Color, { grey: true }, ',')),
' ',
h('span', {}, h(Color, { yellow: true }, `${warning} warnings`)),
' ',
h('span', {}, h(Color, { red: true }, `${error} errors`)),
]);
}
render() {
// 渲染错误
return h(
'div',
{},
[ this.renderErrorFiles(), this.renderOverview() ]
);
}
};