-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpectral.js
74 lines (66 loc) · 1.96 KB
/
Spectral.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
'use strict';
const PreCommitBase = require('./Base');
/**
* @class Spectral
* @extends PreCommitBase
* @classdesc Run spectral on changed files
*/
class Spectral extends PreCommitBase {
/**
* Run `spectral` against files that apply.
* Uses spawnPromiseOnApplicableFiles to parallelize
*
* @returns {Promise}
* @resolves {HookMessage[]} An array of hook messages produced by the hook
* @rejects {Error} An Error thrown or emitted while running the hook
*/
run() {
return new Promise((resolve, reject) => {
this.spawnPromiseOnApplicableFiles().then((result) => {
let output = result.stdout.trim();
if (result.status === 0 && !output) { return resolve('pass'); }
resolve(this.extractMessages(
this.parseSpectralOutput(output),
Spectral.MESSAGE_REGEX,
Spectral.MESSAGE_CAPTURE_MAP,
Spectral.MESSAGE_TYPE_CATEGORIZER
));
}, reject);
});
}
// Parses the output stream of Spectral to produce an array of
// output messages. Ensures we only send valid lines to the
// standard `extractMessages` function.
parseSpectralOutput(output) {
let outputLines = output.split('\n');
return outputLines.filter((outputLine) => {
return / error | warning /.test(outputLine);
});
}
static MESSAGE_TYPE_CATEGORIZER(capturedType) {
return capturedType.toLowerCase();
}
}
/**
* Regex to capture various parts of Spectral text output.
* The output lines look like this:
* `path/to/file.js:1:1 error rule-name "Error message"`
* @type {RegExp}
*/
Spectral.MESSAGE_REGEX = new RegExp(
'^((?:\w:)?[^:]+):' + // 1: File name
'(\\d+).*?' + // 2: Line number
'(error|warning)' // 3: Type
);
/**
* Maps the types of captured data to the index in the
* matches array produced when executing `MESSAGE_REGEX`
*
* @type {{file: number, line: number, type: number}}
*/
Spectral.MESSAGE_CAPTURE_MAP = {
'file': 1,
'line': 2,
'type': 3
};
module.exports = Spectral;