forked from nodejs/core-validate-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-pretty.js
86 lines (73 loc) · 1.77 KB
/
format-pretty.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
'use strict'
const chalk = require('chalk')
const utils = require('./utils')
const MAX_LINE_COL_LEN = 6
module.exports = function formatPretty(context, msgs, validator, opts) {
opts = Object.assign({
detailed: false
}, opts)
if (!msgs.length) {
console.log(' ', utils.header(context.sha, 'pass'))
return
}
let level = 'pass'
for (const msg of msgs) {
if (msg.level === 'fail') level = 'fail'
}
msgs.sort((a, b) => {
if (a.line === b.line) {
return a.column < b.column
? -1
: a.column > b.column
? 1
: 0
}
return a.line < b.line
? -1
: a.line > b.line
? 1
: 0
})
console.log(' ', utils.header(context.sha, level))
for (const msg of msgs) {
const ruleId = msg.id
const rule = validator.rules.get(ruleId)
if (!rule) {
throw new Error(`Invalid rule: "${ruleId}"`)
}
switch (ruleId) {
case 'title-length':
case 'line-length':
console.log(formatLength(msg, opts))
break
default:
console.log(formatMessage(msg))
break
}
}
}
function formatLength(msg, opts) {
const out = formatMessage(msg)
const str = msg.string
const l = str.length
if (!opts.detailed) return out
var col = msg.column || 0
const diff = str.slice(0, col) + chalk.red(str.slice(col, l))
return `${out}
${diff}`
}
function formatMessage(msg) {
const l = msg.line || 0
const col = msg.column || 0
const pad = utils.rightPad(`${l}:${col}`, MAX_LINE_COL_LEN)
const line = chalk.grey(pad)
const id = formatId(msg.id)
const m = msg.message
const icon = msg.level === 'fail'
? utils.X
: utils.CHECK
return ` ${icon} ${line} ${utils.rightPad(m, 40)} ${id}`
}
function formatId(id) {
return chalk.red(id)
}