forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (136 loc) Β· 3.61 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import util from 'util';
import isIgnored from '@commitlint/is-ignored';
import parse from '@commitlint/parse';
import implementations from '@commitlint/rules';
import {toPairs, values} from 'lodash';
const buildCommitMesage = ({header, body, footer}) => {
let message = header;
message = body ? `${message}\n\n${body}` : message;
message = footer ? `${message}\n\n${footer}` : message;
return message;
};
export default async (message, rules = {}, opts = {}) => {
// Found a wildcard match, skip
if (
isIgnored(message, {defaults: opts.defaultIgnores, ignores: opts.ignores})
) {
return {
valid: true,
errors: [],
warnings: [],
input: message
};
}
// Parse the commit message
const parsed = await parse(message, undefined, opts.parserOpts);
const mergedImplementations = Object.assign({}, implementations);
if (opts.plugins) {
values(opts.plugins).forEach(plugin => {
if (plugin.rules) {
Object.keys(plugin.rules).forEach(ruleKey => {
mergedImplementations[ruleKey] = plugin.rules[ruleKey];
});
}
});
}
// Find invalid rules configs
const missing = Object.keys(rules).filter(
name => typeof mergedImplementations[name] !== 'function'
);
if (missing.length > 0) {
const names = Object.keys(mergedImplementations);
throw new RangeError(
`Found invalid rule names: ${missing.join(
', '
)}. Supported rule names are: ${names.join(', ')}`
);
}
const invalid = toPairs(rules)
.map(([name, config]) => {
if (!Array.isArray(config)) {
return new Error(
`config for rule ${name} must be array, received ${util.inspect(
config
)} of type ${typeof config}`
);
}
const [level, when] = config;
if (typeof level !== 'number' || isNaN(level)) {
return new Error(
`level for rule ${name} must be number, received ${util.inspect(
level
)} of type ${typeof level}`
);
}
if (level === 0 && config.length === 1) {
return null;
}
if (config.length !== 2 && config.length !== 3) {
return new Error(
`config for rule ${name} must be 2 or 3 items long, received ${util.inspect(
config
)} of length ${config.length}`
);
}
if (level < 0 || level > 2) {
return new RangeError(
`level for rule ${name} must be between 0 and 2, received ${util.inspect(
level
)}`
);
}
if (typeof when !== 'string') {
return new Error(
`condition for rule ${name} must be string, received ${util.inspect(
when
)} of type ${typeof when}`
);
}
if (when !== 'never' && when !== 'always') {
return new Error(
`condition for rule ${name} must be "always" or "never", received ${util.inspect(
when
)}`
);
}
return null;
})
.filter(item => item instanceof Error);
if (invalid.length > 0) {
throw new Error(invalid.map(i => i.message).join('\n'));
}
// Validate against all rules
const results = toPairs(rules)
.filter(entry => {
const [, [level]] = toPairs(entry);
return level > 0;
})
.map(entry => {
const [name, config] = entry;
const [level, when, value] = config;
// Level 0 rules are ignored
if (level === 0) {
return null;
}
const rule = mergedImplementations[name];
const [valid, message] = rule(parsed, when, value);
return {
level,
valid,
name,
message
};
})
.filter(Boolean);
const errors = results.filter(result => result.level === 2 && !result.valid);
const warnings = results.filter(
result => result.level === 1 && !result.valid
);
const valid = errors.length === 0;
return {
valid,
errors,
warnings,
input: buildCommitMesage(parsed)
};
};