/
open-report.js
65 lines (52 loc) · 1.54 KB
/
open-report.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
'use strict';
const path = require('path');
const spawn = require('child_process').spawn;
const lineColumn = message => `${message.line}:${message.column}`;
const openReport = (report, opts) => {
opts = Object.assign({
editor: process.env.EDITOR,
spawn
}, opts);
if (report.errorCount === 0) {
return;
}
if (!opts.editor) {
console.log(`
\`open\` option was used, but your $EDITOR environment variable is empty.
Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc:
export EDITOR=atom
`);
return;
}
const executableName = opts.editor.split(path.sep).pop();
const args = [];
report.results
.filter(result => result.errorCount > 0)
.forEach(result => {
// VS Code requires --goto option for path:line:column
if (executableName === 'code') {
args.push('--goto');
}
// Sublime Text, Atom, and VS Code support opening file at exact position
if (['subl', 'atom', 'code'].indexOf(executableName) >= 0) {
args.push(result.filePath + ':' + lineColumn(result.messages[0]));
return;
}
// WebStorm supports opening file on a specific line (no column support)
if (executableName === 'wstorm') {
args.push(result.filePath + ':' + result.messages[0].line);
return;
}
// TextMate requires a `--line` option
if (executableName === 'mate') {
args.push('--line', lineColumn(result.messages[0]), result.filePath);
return;
}
args.push(result.filePath);
});
opts.spawn(opts.editor, args, {
detached: true,
stdio: 'ignore'
}).unref();
};
module.exports = openReport;